Skip to content

Commit 613bb16

Browse files
committed
Improved I2C handling
1 parent 7b32589 commit 613bb16

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed
File renamed without changes.

OpenledRace/MPU6050IMUData.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ bool MPU6050IMUData::initMPU6050(uint8_t aSampleRateDivider, mpu6050_bandwidth_t
163163
return false;
164164
}
165165
i2c_stop();
166+
#elif defined(USE_SOFT_WIRE)
167+
#warning SoftWire does not support dynamically check of connection because it has no setWireTimeout() function. You should use "#define USE_SOFT_I2C_MASTER" instead.
168+
#else
169+
Wire.setWireTimeout(); // Sets default timeout of 25 ms.
170+
Wire.beginTransmission(I2CAddress);
171+
if (Wire.endTransmission(true) != 0) {
172+
return false;
173+
}
166174
#endif
167175
MPU6050WriteByte(MPU6050_RA_PWR_MGMT_1, MPU6050_CLOCK_PLL_ZGYRO); // use recommended gyro reference: PLL with Z axis gyroscope reference
168176
MPU6050WriteByte(MPU6050_RA_SMPLRT_DIV, aSampleRateDivider - 1); // parameter 0 => divider 1, 19 -> divider 20

OpenledRace/OpenLedRace.ino

+19-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
#include "LongUnion.h"
117117

118118
#define LCD_I2C_ADDRESS 0x27 // Default LCD address is 0x27 for a 20 chars and 4 line / 2004 display
119-
#include "LiquidCrystal_I2C.h" // Use an up to date library version which has the init method
119+
#include "LiquidCrystal_I2C.hpp" // Include source! Use only the modified version delivered with this program.
120120
LiquidCrystal_I2C myLCD(LCD_I2C_ADDRESS, 20, 4);
121121
void printBigNumber4(byte digit, byte leftAdjust);
122122
void initBigNumbers();
@@ -662,11 +662,14 @@ public:
662662
AcceleratorInput.setI2CAddress(MPU6050_ADDRESS_AD0_HIGH);
663663
}
664664
// use maximum filtering. It prefers slow and huge movements :-)
665-
666665
if (!AcceleratorInput.initMPU6050AndCalculateAllOffsetsAndWait(20, MPU6050_BAND_5_HZ)) {
667666
AcceleratorInputConnected = false;
668667
Serial.print(F("No MPU6050 IMU connected at address 0x"));
668+
#if defined(USE_SOFT_I2C_MASTER)
669669
Serial.print(AcceleratorInput.I2CAddress >> 1, HEX);
670+
#else
671+
Serial.print(AcceleratorInput.I2CAddress, HEX);
672+
#endif
670673
Serial.print(F(" for car "));
671674
Serial.print(aNumberOfThisCar);
672675
Serial.println(F(". You may want to disable \"#define ENABLE_ACCELERATOR_INPUT\""));
@@ -1541,6 +1544,20 @@ void checkForLCDConnected() {
15411544
sSerialLCDAvailable = true;
15421545
}
15431546
i2c_stop();
1547+
#elif defined(USE_SOFT_WIRE)
1548+
#warning SoftWire does not support dynamically check of connection because it has no setWireTimeout() function. You should use "#define USE_SOFT_I2C_MASTER" instead.
1549+
#else
1550+
Wire.setWireTimeout(); // Sets default timeout of 25 ms.
1551+
Wire.beginTransmission(LCD_I2C_ADDRESS);
1552+
if (Wire.endTransmission(true) != 0) {
1553+
if (!sOnlyPlotterOutput) {
1554+
Serial.println(F("No I2C LCD connected at address " STR(LCD_I2C_ADDRESS)));
1555+
}
1556+
playError();
1557+
sSerialLCDAvailable = false;
1558+
} else {
1559+
sSerialLCDAvailable = true;
1560+
}
15441561
#endif
15451562
}
15461563

0 commit comments

Comments
 (0)