Skip to content

Commit

Permalink
[ADS1115] Retry on error reads within timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-er committed Aug 20, 2023
1 parent d9a93ac commit d2ead99
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/_P025_ADS1115.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ boolean Plugin_025(uint8_t function, struct EventStruct *event, String& string)
case PLUGIN_I2C_HAS_ADDRESS:
case PLUGIN_WEBFORM_SHOW_I2C_PARAMS:
{
# define ADS1115_I2C_OPTION 4
const uint8_t i2cAddressValues[] = { 0x48, 0x49, 0x4A, 0x4B };
constexpr int nrAddressOptions = sizeof(i2cAddressValues) / sizeof(i2cAddressValues[0]);

if (function == PLUGIN_WEBFORM_SHOW_I2C_PARAMS) {
addFormSelectorI2C(F("i2c_addr"), ADS1115_I2C_OPTION, i2cAddressValues, P025_I2C_ADDR);
addFormSelectorI2C(F("i2c_addr"), nrAddressOptions, i2cAddressValues, P025_I2C_ADDR);
} else {
success = intArrayContains(ADS1115_I2C_OPTION, i2cAddressValues, event->Par1);
success = intArrayContains(nrAddressOptions, i2cAddressValues, event->Par1);
}
break;
}
Expand Down
34 changes: 23 additions & 11 deletions src/src/PluginStructs/P025_data_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,44 @@ P025_data_struct::P025_data_struct(struct EventStruct *event) {
_fullScaleFactor = 1.0f;

if (P025_VOLT_OUT_GET) {
if (P025_GAIN == 0) {
_fullScaleFactor = 6.144 / 32768.0f;
if (P025_GAIN == 0) {
_fullScaleFactor = 6144;
} else {
const uint8_t shift = 13 - P025_GAIN;
_fullScaleFactor = (1 << shift) / 32768000.0f;
_fullScaleFactor = (1 << shift);
}
_fullScaleFactor /= 32768000.0f;
}
}

bool P025_data_struct::read(float& value) const {
if (!waitReady025(5)) { return false; }
if (!waitReady025(5)) {
// addLog(LOG_LEVEL_INFO, F("ADS1115: Not Ready at start read"));
return false;
}

if (!I2C_write16_reg(_i2cAddress, P025_CONFIG_REGISTER, _configRegisterValue)) {
// addLog(LOG_LEVEL_INFO, F("ADS1115: Start measurement failed"));
return false;
}

// See https://github.com/letscontrolit/ESPEasy/issues/3159#issuecomment-660546091
if (!waitReady025(10)) { return false; }
if (!waitReady025(10)) {
// addLog(LOG_LEVEL_INFO, F("ADS1115: Not Ready after start measurement"));

return false;
}

int16_t raw = 0;
if (!readConversionRegister025(raw)) return false;

if (!readConversionRegister025(raw)) {
addLog(LOG_LEVEL_INFO, F("ADS1115: Cannot read from conversion register"));
return false;
}

value = _fullScaleFactor * raw;

// addLog(LOG_LEVEL_INFO, strformat(F("ADS1115: RAW value: %d, output value: %f"), raw, value));
return true;
}

Expand All @@ -65,14 +80,11 @@ bool P025_data_struct::waitReady025(unsigned long timeout_ms) const {
const unsigned long timeout = millis() + timeout_ms;

while (!timeOutReached(timeout)) {
bool is_ok = false;

// bit15=0 performing a conversion =1 not performing a conversion
bool is_ok = false;
const bool ready = (I2C_read16_reg(_i2cAddress, P025_CONFIG_REGISTER, &is_ok) & 0x8000) != 0;

if (!is_ok) { return false; }

if (ready) { return true; }
if (ready && is_ok) { return true; }

This comment has been minimized.

Copy link
@uzi18

uzi18 Aug 20, 2023

Contributor

@TD-er Nice catch

delay(1);
}
return false;
Expand Down

0 comments on commit d2ead99

Please sign in to comment.