forked from Akcelerometry_drgania_WMT/PI_mikrokontroler
49 lines
1.1 KiB
C++
49 lines
1.1 KiB
C++
#include <Tool.h>
|
|
|
|
static const char *TOOL = "tool";
|
|
|
|
bool isI2CDevPresent(uint8_t address) {
|
|
Wire.beginTransmission(address);
|
|
uint8_t error = Wire.endTransmission();
|
|
|
|
if (error == 0) {
|
|
ESP_LOGI(TOOL, "I2C response from 0x%02X", address);
|
|
return true;
|
|
} else {
|
|
if (error == 4) {
|
|
ESP_LOGW(TOOL, "I2C unknown error at 0x%02X", address);
|
|
} else {
|
|
ESP_LOGI(TOOL, "No I2C device at 0x%02X", address);
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void scanI2C() {
|
|
byte error, address;
|
|
int nDevices = 0;
|
|
ESP_LOGI(TOOL, "I2C start scan");
|
|
for (address = 1; address < 127; address++) {
|
|
Wire.beginTransmission(address);
|
|
error = Wire.endTransmission();
|
|
|
|
if (error == 0) {
|
|
char buf[32];
|
|
snprintf(buf, sizeof(buf), "I2C device: 0x%02X", address);
|
|
ESP_LOGI(TOOL, "%s", buf);
|
|
nDevices++;
|
|
Watchdog::feed();
|
|
}
|
|
else if (error == 4) {
|
|
ESP_LOGW(TOOL, "I2C error at address: 0x%02X", address);
|
|
}
|
|
}
|
|
|
|
if (nDevices == 0) {
|
|
ESP_LOGE(TOOL, "I2C no devices found");
|
|
} else {
|
|
ESP_LOGI(TOOL, "I2C scan finished. Found %d device(s)", nDevices);
|
|
}
|
|
Watchdog::feed();
|
|
}
|