Skip to content

Commit

Permalink
refactor #78, redo #76
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Jul 3, 2024
1 parent 3675881 commit 90039d1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
47 changes: 28 additions & 19 deletions ADS1X15.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ uint8_t ADS1X15::getGain()
case ADS1X15_PGA_0_512V: return 8;
case ADS1X15_PGA_0_256V: return 16;
}
_err = ADS1X15_INVALID_GAIN;
return _err;
_error = ADS1X15_INVALID_GAIN;
return _error;
}


Expand Down Expand Up @@ -219,8 +219,8 @@ float ADS1X15::getMaxVoltage()
case ADS1X15_PGA_0_512V: return 0.512;
case ADS1X15_PGA_0_256V: return 0.256;
}
_err = ADS1X15_INVALID_VOLTAGE;
return _err;
_error = ADS1X15_INVALID_VOLTAGE;
return _error;
}


Expand All @@ -242,8 +242,8 @@ uint8_t ADS1X15::getMode(void)
case ADS1X15_MODE_CONTINUE: return 0;
case ADS1X15_MODE_SINGLE: return 1;
}
_err = ADS1X15_INVALID_MODE;
return _err;
_error = ADS1X15_INVALID_MODE;
return _error;
}


Expand Down Expand Up @@ -402,8 +402,8 @@ int16_t ADS1X15::getComparatorThresholdHigh()

int8_t ADS1X15::getError()
{
int8_t rv = _err;
_err = ADS1X15_OK;
int8_t rv = _error;
_error = ADS1X15_OK;
return rv;
}

Expand Down Expand Up @@ -455,10 +455,10 @@ int16_t ADS1X15::_readADC(uint16_t readmode)
// timeout == { 129, 65, 33, 17, 9, 5, 3, 2 }
// a few ms more than max conversion time.
uint8_t timeOut = (128 >> (_datarate >> 5)) + 1;
while (isBusy())
while (isBusy())
{
yield(); // wait for conversion; yield for ESP.
if ( (millis() - start) > timeOut)
if ( (millis() - start) > timeOut)
{
return ADS1X15_ERROR_TIMEOUT;
}
Expand Down Expand Up @@ -491,7 +491,7 @@ void ADS1X15::_requestADC(uint16_t readmode)
_writeRegister(_address, ADS1X15_REG_CONFIG, config);

// remember last request type.
_lastRequest = readmode;
_lastRequest = readmode;
}


Expand All @@ -501,23 +501,32 @@ bool ADS1X15::_writeRegister(uint8_t address, uint8_t reg, uint16_t value)
_wire->write((uint8_t)reg);
_wire->write((uint8_t)(value >> 8));
_wire->write((uint8_t)(value & 0xFF));
return (_wire->endTransmission() == 0);
int rv = _wire->endTransmission();
if (rv != 0)
{
_error = ADS1X15_ERROR_I2C;
return false;
}
return true;
}


uint16_t ADS1X15::_readRegister(uint8_t address, uint8_t reg)
{
_wire->beginTransmission(address);
_wire->write(reg);
_wire->endTransmission();

int rv = _wire->requestFrom((int) address, (int) 2);
if (rv == 2)
int rv = _wire->endTransmission();
if (rv == 0)
{
uint16_t value = _wire->read() << 8;
value += _wire->read();
return value;
rv = _wire->requestFrom((int) address, (int) 2);
if (rv == 2)
{
uint16_t value = _wire->read() << 8;
value += _wire->read();
return value;
}
}
_error = ADS1X15_ERROR_I2C;
return 0x0000;
}

Expand Down
3 changes: 2 additions & 1 deletion ADS1X15.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define ADS1X15_OK 0
#define ADS1X15_INVALID_VOLTAGE -100
#define ADS1X15_ERROR_TIMEOUT -101
#define ADS1X15_ERROR_I2C -102
#define ADS1X15_INVALID_GAIN 0xFF
#define ADS1X15_INVALID_MODE 0xFE

Expand Down Expand Up @@ -195,7 +196,7 @@ class ADS1X15
void _requestADC(uint16_t readmode);
bool _writeRegister(uint8_t address, uint8_t reg, uint16_t value);
uint16_t _readRegister(uint8_t address, uint8_t reg);
int8_t _err = ADS1X15_OK;
int8_t _error = ADS1X15_OK;

TwoWire* _wire;
uint32_t _clockSpeed = 0;
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.4.5] - 2024-07-03
- Fix #78, prevent infinite loop. (Thanks to devmirek).
- Fix #76 (again), update readme.md Comparator Polarity.
- add ADS1X15_ERROR_I2C, communication error.
- add minimal section in readme.md about error codes.
- minor edits.

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,8 @@ This is read and reset by **getError()**
|:-------:|:-------------------------:|:-------------:|
| 0 | ADS1X15_OK | idem.
| -100 | ADS1X15_INVALID_VOLTAGE | getMaxVoltage()
| -101 | ADS1X15_ERROR_TIMEOUT | readADC() device failure.
| -101 | ADS1X15_ERROR_TIMEOUT | readADC() device did not respond in time.
| -102 | ADS1X15_ERROR_I2C | I2C communication failure.
| 0xFF | ADS1X15_INVALID_GAIN | getGain()
| 0xFE | ADS1X15_INVALID_MODE | getMode()
Expand All @@ -614,13 +615,13 @@ This is read and reset by **getError()**
- Remove the experimental **getWireClock()** as this is not really a library function
but a responsibility of the I2C library.
- Investigate ADS1118 library which should be a similar SPI based ADC.
- improve error handling
- refactor values to be more logic.
#### Could
- More examples
- SMB alert command (00011001) on I2C bus?
- Sync code order .h / .cpp
- more error handling?
#### Wont (unless requested)
Expand Down

0 comments on commit 90039d1

Please sign in to comment.