Skip to content

Commit 206faf8

Browse files
committed
Fix: MCP3421 correct Begin check + retry logic
1 parent 8bbc6ec commit 206faf8

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/components/i2c/drivers/WipperSnapper_I2C_Driver_MCP3421.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class WipperSnapper_I2C_Driver_MCP3421 : public WipperSnapper_I2C_Driver {
5656
/*******************************************************************************/
5757
bool begin() {
5858
_mcp3421 = new Adafruit_MCP3421();
59-
if (_mcp3421->begin((uint8_t)_sensorAddress, _i2c)) {
59+
if (!_mcp3421->begin((uint8_t)_sensorAddress, _i2c)) {
6060
WS_DEBUG_PRINTLN("Failed to find MCP3421 chip");
6161
return false;
6262
}
@@ -79,32 +79,35 @@ class WipperSnapper_I2C_Driver_MCP3421 : public WipperSnapper_I2C_Driver {
7979

8080
/*******************************************************************************/
8181
/*!
82-
@brief Reads a voltage sensor and converts the
83-
reading into the expected SI unit.
84-
@param voltageEvent
85-
voltage sensor reading, in volts.
82+
@brief Reads the ADC sensor with short wait for data.
83+
@param rawEvent
84+
ADC sensor reading
8685
@returns True if the sensor event was obtained successfully, False
8786
otherwise.
8887
*/
8988
/*******************************************************************************/
90-
bool getEventVoltage(sensors_event_t *voltageEvent) {
89+
bool getEventRaw(sensors_event_t *rawEvent) {
9190
ulong start = millis();
9291
if (!_mcp3421->startOneShotConversion()) {
9392
WS_DEBUG_PRINTLN("Failed to start one-shot conversion");
9493
return false;
9594
}
9695
while (!_mcp3421->isReady()) {
97-
if (millis() - start > 1000) {
96+
ulong newMillis = millis();
97+
if (newMillis - start > 500) {
9898
WS_DEBUG_PRINTLN("Timeout waiting for conversion to complete");
9999
return false;
100+
} else if (start > newMillis) {
101+
start = millis(); // rollover
100102
}
103+
delay(50);
101104
}
102-
voltageEvent->voltage = (float)_mcp3421->readADC();
105+
rawEvent->data[0] = (float)_mcp3421->readADC();
103106
return true;
104107
}
105108

106109
protected:
107-
Adafruit_MCP3421 *_mcp3421; ///< Pointer to MCP3421 temperature sensor object
110+
Adafruit_MCP3421 *_mcp3421; ///< Pointer to MCP3421 sensor object
108111
};
109112

110113
#endif // WipperSnapper_I2C_Driver_MCP3421

0 commit comments

Comments
 (0)