From 8cfe8a486ce1399bff27a96537de4d078418f712 Mon Sep 17 00:00:00 2001 From: Michal Fapso Date: Tue, 20 Mar 2018 08:12:12 +0100 Subject: [PATCH 1/4] Using multiple TSYS01 sensors I2C address is set in constructor, not hardcoded in define --- TSYS01.cpp | 30 +++++++++++++++++++----------- TSYS01.h | 5 ++++- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/TSYS01.cpp b/TSYS01.cpp index bdb6555..fda9970 100644 --- a/TSYS01.cpp +++ b/TSYS01.cpp @@ -1,19 +1,28 @@ #include "TSYS01.h" #include -#define TSYS01_ADDR 0x77 #define TSYS01_RESET 0x1E #define TSYS01_ADC_READ 0x00 #define TSYS01_ADC_TEMP_CONV 0x48 #define TSYS01_PROM_READ 0XA0 -TSYS01::TSYS01() { +TSYS01::TSYS01(uint8_t i2cAddress) + : mAddress(i2cAddress) +{} +bool TSYS01::isValid() { + return mAddress != 0; } void TSYS01::init() { + Wire.beginTransmission(mAddress); + if (Wire.endTransmission() != 0) { + mAddress = 0; + return; + } + // Reset the TSYS01, per datasheet - Wire.beginTransmission(TSYS01_ADDR); + Wire.beginTransmission(mAddress); Wire.write(TSYS01_RESET); Wire.endTransmission(); @@ -21,34 +30,33 @@ void TSYS01::init() { // Read calibration values for ( uint8_t i = 0 ; i < 8 ; i++ ) { - Wire.beginTransmission(TSYS01_ADDR); + Wire.beginTransmission(mAddress); Wire.write(TSYS01_PROM_READ+i*2); Wire.endTransmission(); - Wire.requestFrom(TSYS01_ADDR,2); + Wire.requestFrom(mAddress, uint8_t(2)); C[i] = (Wire.read() << 8) | Wire.read(); } } void TSYS01::read() { - - Wire.beginTransmission(TSYS01_ADDR); + Wire.beginTransmission(mAddress); Wire.write(TSYS01_ADC_TEMP_CONV); Wire.endTransmission(); delay(10); // Max conversion time per datasheet - Wire.beginTransmission(TSYS01_ADDR); + Wire.beginTransmission(mAddress); Wire.write(TSYS01_ADC_READ); Wire.endTransmission(); - - Wire.requestFrom(TSYS01_ADDR,3); + + Wire.requestFrom(mAddress, uint8_t(3)); D1 = 0; D1 = Wire.read(); D1 = (D1 << 8) | Wire.read(); D1 = (D1 << 8) | Wire.read(); - + calculate(); } diff --git a/TSYS01.h b/TSYS01.h index 06047ee..e4445d3 100644 --- a/TSYS01.h +++ b/TSYS01.h @@ -36,8 +36,10 @@ THE SOFTWARE. class TSYS01 { public: - TSYS01(); + TSYS01(uint8_t i2cAddress); + + bool isValid(); void init(); /** The read from I2C takes up for 40 ms, so use sparingly is possible. @@ -59,6 +61,7 @@ class TSYS01 { uint32_t D1; float TEMP; uint32_t adc; + uint8_t mAddress; /** Performs calculations per the sensor data sheet for conversion and * second order compensation. From d0fa9541c349550ae84888d677d77be294cd0fd9 Mon Sep 17 00:00:00 2001 From: Michal Fapso Date: Tue, 20 Mar 2018 08:13:01 +0100 Subject: [PATCH 2/4] Use LowPower library for deep sleep to save power instead of using delay() --- TSYS01.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/TSYS01.cpp b/TSYS01.cpp index fda9970..88273d8 100644 --- a/TSYS01.cpp +++ b/TSYS01.cpp @@ -1,6 +1,11 @@ #include "TSYS01.h" #include +#ifdef TSYS01_USE_LOW_POWER + #warning "TSYS01_USE_LOW_POWER: enabled" + #include +#endif + #define TSYS01_RESET 0x1E #define TSYS01_ADC_READ 0x00 #define TSYS01_ADC_TEMP_CONV 0x48 @@ -26,7 +31,11 @@ void TSYS01::init() { Wire.write(TSYS01_RESET); Wire.endTransmission(); + #ifdef TSYS01_USE_LOW_POWER + LowPower.powerDown(SLEEP_15MS, ADC_OFF, BOD_OFF); + #else delay(10); + #endif // Read calibration values for ( uint8_t i = 0 ; i < 8 ; i++ ) { @@ -44,9 +53,13 @@ void TSYS01::read() { Wire.beginTransmission(mAddress); Wire.write(TSYS01_ADC_TEMP_CONV); Wire.endTransmission(); - - delay(10); // Max conversion time per datasheet + #ifdef TSYS01_USE_LOW_POWER + LowPower.powerDown(SLEEP_15MS, ADC_OFF, BOD_OFF); + #else + delay(10); // Max conversion time per datasheet + #endif + Wire.beginTransmission(mAddress); Wire.write(TSYS01_ADC_READ); Wire.endTransmission(); From ca4f90b26f3e5edb0688c7aba1efd04f8cd98fb2 Mon Sep 17 00:00:00 2001 From: Michal Fapso Date: Tue, 20 Mar 2018 08:45:29 +0100 Subject: [PATCH 3/4] Added link to the LowPower library --- TSYS01.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/TSYS01.cpp b/TSYS01.cpp index 88273d8..14e62eb 100644 --- a/TSYS01.cpp +++ b/TSYS01.cpp @@ -2,6 +2,7 @@ #include #ifdef TSYS01_USE_LOW_POWER + // Use deep sleep instead of delay() to save power. Get the library from https://github.com/LowPowerLab/LowPower #warning "TSYS01_USE_LOW_POWER: enabled" #include #endif From 476fe15176ac977b0408c7165ffe170ddb485fdf Mon Sep 17 00:00:00 2001 From: Michal Fapso Date: Tue, 20 Mar 2018 08:55:40 +0100 Subject: [PATCH 4/4] Added default I2C address for compatibility with old code --- TSYS01.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TSYS01.h b/TSYS01.h index e4445d3..ccd94b4 100644 --- a/TSYS01.h +++ b/TSYS01.h @@ -37,7 +37,7 @@ class TSYS01 { public: - TSYS01(uint8_t i2cAddress); + TSYS01(uint8_t i2cAddress = 0x77); bool isValid(); void init();