Skip to content

Commit

Permalink
fix: add timeout handling to potentially infinite loop (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmirek authored Jul 3, 2024
1 parent 6c86d80 commit bff9430
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions ADS1X15.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,12 @@ int16_t ADS1X15::_readADC(uint16_t readmode)
_requestADC(readmode);
if (_mode == ADS1X15_MODE_SINGLE)
{
while ( isBusy() ) yield(); // wait for conversion; yield for ESP.
unsigned long start = millis();
while (isBusy()) {
yield(); // wait for conversion; yield for ESP.
if ((start + ADS1X15_READ_TIMEOUT_MS) < millis())
return ADS1X15_ERROR_TIMEOUT;
}
}
else
{
Expand Down Expand Up @@ -737,4 +742,3 @@ void ADS1115::requestADC_Differential_2_3()


// -- END OF FILE --

5 changes: 4 additions & 1 deletion ADS1X15.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
#define ADS1115_ADDRESS 0x48
#endif

#ifndef ADS1X15_READ_TIMEOUT_MS
#define ADS1X15_READ_TIMEOUT_MS 200 //Longest acquisition time is 125ms (8SPS)
#endif

#define ADS1X15_OK 0
#define ADS1X15_INVALID_VOLTAGE -100
#define ADS1X15_ERROR_TIMEOUT -101
#define ADS1X15_INVALID_GAIN 0xFF
#define ADS1X15_INVALID_MODE 0xFE

Expand Down Expand Up @@ -268,4 +272,3 @@ class ADS1115 : public ADS1X15


// -- END OF FILE --

0 comments on commit bff9430

Please sign in to comment.