From 0a1e31421e5c4f007e56c9bf8a912b1cb0bb9a44 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Mon, 7 Jun 2021 11:49:28 +0200 Subject: [PATCH] 0.3.0 I2C_ASDX --- libraries/I2C_ASDX/I2C_ASDX.cpp | 58 +++++++++++++----- libraries/I2C_ASDX/I2C_ASDX.h | 14 +++-- libraries/I2C_ASDX/README.md | 41 ++++++++----- libraries/I2C_ASDX/library.json | 2 +- libraries/I2C_ASDX/library.properties | 2 +- libraries/I2C_ASDX/test/unit_test_001.cpp | 74 ++++++++++++++++------- 6 files changed, 132 insertions(+), 59 deletions(-) diff --git a/libraries/I2C_ASDX/I2C_ASDX.cpp b/libraries/I2C_ASDX/I2C_ASDX.cpp index 3930d6af..300fb52c 100644 --- a/libraries/I2C_ASDX/I2C_ASDX.cpp +++ b/libraries/I2C_ASDX/I2C_ASDX.cpp @@ -11,72 +11,100 @@ // 0.2.1 2020-07-04 add getBar(), getMilliBar(), getPSI() // 0.2.2 2020-12-29 add arduiino-ci + unit test (minimal), keywords update. // 0.2.3 2021-06-06 add different units to read pressure +// 0.3.0 2021-06-07 add multiWire interface + #include "I2C_ASDX.h" -I2C_ASDX::I2C_ASDX(uint8_t address, uint8_t psi) + +I2C_ASDX::I2C_ASDX(uint8_t address, uint8_t psi, TwoWire *wire) { - _address = address; + reset(); + _address = address; + _wire = wire; _maxPressure = 0; + if ((psi == 100) || (psi == 60) || (psi == 30) || (psi == 15) || (psi == 05) || (psi == 01)) { _maxPressure = psi * PSI2MILLIBAR; } - reset(); + _state = I2C_ASDX_INIT; } + #if defined (ESP8266) || defined(ESP32) -void I2C_ASDX::begin(uint8_t sda, uint8_t scl) +bool I2C_ASDX::begin(uint8_t sda, uint8_t scl) { - Wire.begin(sda, scl); reset(); + _wire = &Wire; + _wire->begin(sda, scl); + if (! isConnected()) + { + _state = I2C_ASDX_CONNECT_ERROR; + return false; + } + _state = I2C_ASDX_OK; + return true; } #endif -void I2C_ASDX::begin() + +bool I2C_ASDX::begin() { - Wire.begin(); reset(); + _wire->begin(); + if (! isConnected()) + { + _state = I2C_ASDX_CONNECT_ERROR; + return false; + } + _state = I2C_ASDX_OK; + return true; } + void I2C_ASDX::reset() { - _state = I2C_ASDX_INIT; _errorCount = 0; _lastRead = 0; _pressure = 0; } + bool I2C_ASDX::isConnected() { - Wire.beginTransmission(_address); - return (Wire.endTransmission() == 0); + _wire->beginTransmission(_address); + return (_wire->endTransmission() == 0); } + int I2C_ASDX::read() { - Wire.requestFrom(_address, (uint8_t)2); - if (Wire.available() != 2) + _wire->requestFrom(_address, (uint8_t)2); + if (_wire->available() != 2) { _errorCount++; _state = I2C_ASDX_READ_ERROR; return _state; } - int count = Wire.read() * 256; // hi byte - count += Wire.read(); // lo byte + int count = _wire->read() * 256; // hi byte + count += _wire->read(); // lo byte if (count & 0xC000) { _errorCount++; _state = I2C_ASDX_C000_ERROR; // no documentation, bits may not be set? return _state; } + // _pressure = map(count, 1638, 14746, 0, _maxPressure); // _pressure = (count - 1638) * (_maxPressure - 0) / ( 14746 - 1638); _pressure = (count - 1638) * _maxPressure * 7.62892889838E-5; - _state = I2C_ASDX_OK; _lastRead = millis(); + + _state = I2C_ASDX_OK; return _state; } + // -- END OF FILE -- diff --git a/libraries/I2C_ASDX/I2C_ASDX.h b/libraries/I2C_ASDX/I2C_ASDX.h index d4f6b34e..91374e05 100644 --- a/libraries/I2C_ASDX/I2C_ASDX.h +++ b/libraries/I2C_ASDX/I2C_ASDX.h @@ -2,7 +2,7 @@ // // FILE: I2C_ASDX.h // AUTHOR: Rob Tillaart -// VERSION: 0.2.3 +// VERSION: 0.3.0 // PURPOSE: Arduino library for I2C ASDX pressure sensor // URL: https://github.com/RobTillaart/I2C_ASDX // @@ -45,25 +45,26 @@ -#define I2C_ASDX_VERSION (F("0.2.3")) +#define I2C_ASDX_VERSION (F("0.3.0")) #define I2C_ASDX_OK 1 #define I2C_ASDX_INIT 0 #define I2C_ASDX_READ_ERROR -1 #define I2C_ASDX_C000_ERROR -2 +#define I2C_ASDX_CONNECT_ERROR -3 class I2C_ASDX { public: - // psi: 100, 60, 30, 15 - I2C_ASDX(uint8_t address, uint8_t psi); + // psi: 100, 60, 30, 15, 5 or 1 + I2C_ASDX(uint8_t address, uint8_t psi, TwoWire *wire = &Wire); #if defined (ESP8266) || defined(ESP32) - void begin(uint8_t sda, uint8_t scl); + bool begin(uint8_t sda, uint8_t scl); #endif - void begin(); + bool begin(); void reset(); bool isConnected(); bool available() { return isConnected(); }; // obsolete in future @@ -101,6 +102,7 @@ public: private: uint8_t _address; + TwoWire* _wire; float _maxPressure; float _pressure; diff --git a/libraries/I2C_ASDX/README.md b/libraries/I2C_ASDX/README.md index 97a2d3e4..dc625bf9 100644 --- a/libraries/I2C_ASDX/README.md +++ b/libraries/I2C_ASDX/README.md @@ -8,6 +8,7 @@ Arduino library for I2C ASDX pressure sensor + ## Description The ASDX sensor of Honeywell exist in many variations. @@ -17,27 +18,29 @@ The I2C_ASDX library can read the sensor and give the pressure in millibar, bar The interface: + #### Constructor -- **I2C_ASDX(address, psi)** Constructor, I2C address and maximum pressure. -- **void begin(sda, scl)** I2C parameters for ESP32 a.o. -- **void begin()** for UNO and other boards supporting Wire. -- **void reset()** resets internal variables, incl pressure. +- **I2C_ASDX(uint8_taddress, uint8_t psi, TwoWire \*wire = &Wire)** Constructor, I2C address and maximum pressure. Optional the wire interface can be defined. +- **bool begin(uint8_t sda, uint8_t scl)** I2C parameters for ESP32 a.o. Returns true if address can be found on I2C bus. +- **bool begin()** for UNO and other boards supporting Wire. Returns true if address can be found on I2C bus. +- **void reset()** resets internal variables, including pressure. - **bool isConnected()** tests if address can be found on I2C bus. - **bool available()** wrapper around isConnected. Obsolete in the future. + #### Read Before any call to **getPressure()** one need to call **read()** unless one wants the last value read. -- **int read()** actually reads the sensor, checks for errors, calculates the pressure and set lastRead, Returns **I2C_ASDX_OK** or error code. +- **int read()** actually reads the sensor, checks for errors, calculates the pressure and set the lastRead timestamp. Returns **I2C_ASDX_OK** or an error code. #### Units -- **int getPressure()** retuns pressure (integer format) in milliBar, will return 0 after reset() and no read done. +- **int getPressure()** returns pressure (integer format) in milliBar, will return 0 after reset() and no read done. - **float getMilliBar()** returns pressure in milliBar. -- **float getBar()** returns presure in bar. +- **float getBar()** returns pressure in bar. - **float getPSI()** returns pressure in PSI = Pounds per Square Inch. - **float getATM()** returns pressure in Atmosphere. - **float getDynes()** returns pressure in Dynes. @@ -45,16 +48,25 @@ Before any call to **getPressure()** one need to call **read()** unless one want - **float getInchH2O()** returns pressure in inches water. - **float getPascal()** returns pressure in Pascal. Note this is the SI unit. - **float getTORR()** returns pressure in TORR. -- **float getCmHg()** returns pressure in centimeter mercury. -- **float getCmH2O()** returns pressure in centimeter water. +- **float getCmHg()** returns pressure in centimetre mercury. +- **float getCmH2O()** returns pressure in centimetre water. - **float getMSW()** returns pressure in Meters of Sea Water. (under water pressure unit). + #### State -- **uint16_t errorCount()** total counter for the number of errors occured. -- **uint32_t lastRead()** time in millis of last succesful read of the sensor. +- **uint16_t errorCount()** total counter for the number of errors occurred. +- **uint32_t lastRead()** time in milliseconds of last successful read of the sensor. - **int state()** last known state of read, also returned by **read()** +| state | meaning | +|:------------------------|:-------------------| +| I2C_ASDX_OK | no error | +| I2C_ASDX_INIT | begin() not called | +| I2C_ASDX_READ_ERROR | I2C error | +| I2C_ASDX_C000_ERROR | sensor error | +| I2C_ASDX_CONNECT_ERROR | I2C error | + ## Testing @@ -64,7 +76,7 @@ Code is prepared but not tested for 15, 5 and 1 PSI too. ``` ID UNIT TYPE DESCRIPTION - output is porportional to difference + output is proportional to difference PG PSI Gage * between applied pressure and atmospheric pressure MG mBar Gage * idem BG Bar Gage * idem @@ -84,7 +96,7 @@ Code is prepared but not tested for 15, 5 and 1 PSI too. V = voltage (3 volt also supported, not tested) ``` -That saidm it is expected that the library is modifyable to support many +That said it is expected that the library is modifiable to support many more as long as they have the following raw read values. ``` @@ -92,6 +104,7 @@ more as long as they have the following raw read values. 14746 = max PSI ``` + ## Testing TESTED TYPES - type A 10% - 90% only @@ -101,8 +114,6 @@ TESTED TYPES - type A 10% - 90% only #### Must -- multiple Wire interface (breaks interface) -- test isCOnnected in bool begin(). (breaks interface?) - find a good reference for conversion formula constants. - diff --git a/libraries/I2C_ASDX/library.json b/libraries/I2C_ASDX/library.json index 5ab29696..bcc0e51f 100644 --- a/libraries/I2C_ASDX/library.json +++ b/libraries/I2C_ASDX/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/I2C_ASDX.git" }, - "version": "0.2.3", + "version": "0.3.0", "license": "MIT", "frameworks": "arduino", "platforms": "*" diff --git a/libraries/I2C_ASDX/library.properties b/libraries/I2C_ASDX/library.properties index 63fbe9ae..26506cbb 100644 --- a/libraries/I2C_ASDX/library.properties +++ b/libraries/I2C_ASDX/library.properties @@ -1,5 +1,5 @@ name=I2C_ASDX -version=0.2.3 +version=0.3.0 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for I2C ASDX pressure sensor diff --git a/libraries/I2C_ASDX/test/unit_test_001.cpp b/libraries/I2C_ASDX/test/unit_test_001.cpp index 9d162654..181536c7 100644 --- a/libraries/I2C_ASDX/test/unit_test_001.cpp +++ b/libraries/I2C_ASDX/test/unit_test_001.cpp @@ -63,36 +63,58 @@ unittest(test_constructor) fprintf(stderr, "VERSION: %s\n", I2C_ASDX_VERSION); I2C_ASDX sensor(0x58, 100); - sensor.begin(); - assertTrue(sensor.available()); - - fprintf(stderr, "Test default pressure\n"); - - assertEqual(0, sensor.getPressure()); - assertEqual(0, sensor.getMilliBar()); - assertEqual(0, sensor.getBar()); - assertEqual(0, sensor.getPSI()); - - fprintf(stderr, "tes state\n"); - assertEqual(0, sensor.errorCount()); - assertEqual(0, sensor.lastRead()); assertEqual(I2C_ASDX_INIT, sensor.state()); - fprintf(stderr, "Test conversion constants\n"); - assertEqualFloat(68.9475729, PSI2MILLIBAR, 0.0001); - assertEqualFloat(0.01450377377, MILLIBAR2PSI, 0.0001); - assertEqualFloat(0.0689475729, PSI2BAR, 0.0001); - assertEqualFloat(14.503773773, BAR2PSI, 0.0001); + assertTrue(sensor.begin()); + assertTrue(sensor.isConnected()); // incorrect, keep build happy + assertTrue(sensor.available()); // obsolete in the future + + fprintf(stderr, "test state\n"); + assertEqual(0, sensor.errorCount()); + assertEqual(0, sensor.lastRead()); + assertEqual(I2C_ASDX_OK, sensor.state()); } -unittest(test_read) +unittest(test_constants) +{ + fprintf(stderr, "VERSION: %s\n", I2C_ASDX_VERSION); + + fprintf(stderr, "Test conversion constants\n"); + assertEqualFloat(68.9475729, PSI2MILLIBAR, 1e-4); + assertEqualFloat(0.01450377377, MILLIBAR2PSI, 1e-4); + assertEqualFloat(0.0689475729, PSI2BAR, 1e-4); + assertEqualFloat(14.503773773, BAR2PSI, 1e-4); + + assertEqualFloat(0.001, MILLIBAR2BAR, 1e-7); + assertEqualFloat(9.872e-4, MILLIBAR2ATM, 1e-7); + assertEqualFloat(1000, MILLIBAR2DYNES, 1); + assertEqualFloat(2.9539e-2, MILLIBAR2INHG, 1e-5); + + assertEqualFloat(0.4018, MILLIBAR2INH2O, 1e-4); + assertEqualFloat(100, MILLIBAR2PASCAL, 1e-3); + assertEqualFloat(0.75028, MILLIBAR2TORR, 1e-5); + assertEqualFloat(0.075028, MILLIBAR2CMHG, 1e-6); + + assertEqualFloat(1.02056, MILLIBAR2CMH2O, 1e-5); + assertEqualFloat(100, MILLIBAR2MSW, 1); + + fprintf(stderr, "Test state constants\n"); + assertEqual(1, I2C_ASDX_OK); + assertEqual(0, I2C_ASDX_INIT); + assertEqual(-1, I2C_ASDX_READ_ERROR); + assertEqual(-2, I2C_ASDX_C000_ERROR); + assertEqual(-3, I2C_ASDX_CONNECT_ERROR); +} + + +unittest(test_read_zero) { fprintf(stderr, "VERSION: %s\n", I2C_ASDX_VERSION); I2C_ASDX sensor(0x58, 100); - sensor.begin(); - assertTrue(sensor.available()); + assertTrue(sensor.begin()); + assertTrue(sensor.isConnected()); // incorrect, keep build happy fprintf(stderr, "Test default pressure\n"); assertEqual(0, sensor.getPressure()); @@ -100,6 +122,16 @@ unittest(test_read) assertEqual(0, sensor.getBar()); assertEqual(0, sensor.getPSI()); + assertEqual(0, sensor.getATM()); + assertEqual(0, sensor.getDynes()); + assertEqual(0, sensor.getInchHg()); + assertEqual(0, sensor.getInchH2O()); + assertEqual(0, sensor.getPascal()); + assertEqual(0, sensor.getTORR()); + assertEqual(0, sensor.getCmHg()); + assertEqual(0, sensor.getCmH2O()); + assertEqual(0, sensor.getMSW()); + // assertEqual(I2C_ASDX_READ_ERROR, sensor.read()); // fprintf(stderr, "Test after read\n");