Skip to content

Commit

Permalink
esp32c3: workaround to i2c issue with secondary sensor disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
l0ud committed Jul 26, 2023
1 parent ced549d commit 5096fd1
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/i2cscan/i2cscan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ namespace I2CSCAN
{

uint8_t pickDevice(uint8_t addr1, uint8_t addr2, bool scanIfNotFound) {
if(I2CSCAN::isI2CExist(addr1))
if(I2CSCAN::isI2CExist(addr1)) {
return addr1;
if(!I2CSCAN::isI2CExist(addr2)) {
if(scanIfNotFound) {
Serial.println("[ERR] I2C: Can't find I2C device on provided addresses, scanning for all I2C devices and returning");
I2CSCAN::scani2cports();
} else {
Serial.println("[ERR] I2C: Can't find I2C device on provided addresses");
}
return 0;
}
return addr2;
if(I2CSCAN::isI2CExist(addr2)) {
return addr2;
}
if (scanIfNotFound) {
Serial.println("[ERR] I2C: Can't find I2C device on provided addresses, scanning for all I2C devices and returning");
I2CSCAN::scani2cports();
} else {
Serial.println("[ERR] I2C: Can't find I2C device on provided addresses");
}
return 0;
}

void scani2cports()
Expand Down Expand Up @@ -111,8 +112,16 @@ namespace I2CSCAN
}

bool isI2CExist(uint8_t addr) {
Wire.beginTransmission(addr);
byte error = Wire.endTransmission();
byte error;
#if ESP32C3
do {
#endif
Wire.beginTransmission(addr);
error = Wire.endTransmission();
#if ESP32C3
}
while (error == 5);
#endif
if(error == 0)
return true;
return false;
Expand Down

0 comments on commit 5096fd1

Please sign in to comment.