From 90039d14d65584ba0a53906c031ed3cd8ea8e5f2 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Wed, 3 Jul 2024 10:42:51 +0200 Subject: [PATCH] refactor #78, redo #76 --- ADS1X15.cpp | 47 ++++++++++++++++++++++++++++------------------- ADS1X15.h | 3 ++- CHANGELOG.md | 1 + README.md | 7 ++++--- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/ADS1X15.cpp b/ADS1X15.cpp index 87e0d96..c65d710 100644 --- a/ADS1X15.cpp +++ b/ADS1X15.cpp @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -491,7 +491,7 @@ void ADS1X15::_requestADC(uint16_t readmode) _writeRegister(_address, ADS1X15_REG_CONFIG, config); // remember last request type. - _lastRequest = readmode; + _lastRequest = readmode; } @@ -501,7 +501,13 @@ 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; } @@ -509,15 +515,18 @@ 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; } diff --git a/ADS1X15.h b/ADS1X15.h index eea1244..601dacb 100644 --- a/ADS1X15.h +++ b/ADS1X15.h @@ -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 @@ -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; diff --git a/CHANGELOG.md b/CHANGELOG.md index a517ac4..98afd75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 4be6192..9f4f020 100644 --- a/README.md +++ b/README.md @@ -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() @@ -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)