From 325564fbc7da74211b70b166f27bcea9d832a022 Mon Sep 17 00:00:00 2001 From: Robert Jaakke Date: Wed, 10 Jun 2020 10:03:02 +0200 Subject: [PATCH] Added option to set i2c address in measure...Once in lib --- lib/LOLIN_HP303B/src/LOLIN_HP303B.cpp | 46 ++++++++++++++++++++++++--- lib/LOLIN_HP303B/src/LOLIN_HP303B.h | 6 ++-- tasmota/xsns_73_hp303b.ino | 4 +-- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/lib/LOLIN_HP303B/src/LOLIN_HP303B.cpp b/lib/LOLIN_HP303B/src/LOLIN_HP303B.cpp index 7f7f60a3f..b73e12b83 100644 --- a/lib/LOLIN_HP303B/src/LOLIN_HP303B.cpp +++ b/lib/LOLIN_HP303B/src/LOLIN_HP303B.cpp @@ -183,7 +183,23 @@ int16_t LOLIN_HP303B::standby(void) */ int16_t LOLIN_HP303B::measureTempOnce(float &result) { - return measureTempOnce(result, m_tempOsr); + return measureTempOnce(result, m_slaveAddress, m_tempOsr); +} + +/** + * performs one temperature measurement and writes result to the given address + * + * &result: reference to a 32-Bit signed Integer value where the result will be written + * It will not be written if result==NULL + * returns: 0 on success + * -4 if the HP303B is could not finish its measurement in time + * -3 if the HP303B is already busy + * -2 if the object initialization failed + * -1 on other fail + */ +int16_t LOLIN_HP303B::measureTempOnce(float &result, uint8_t slaveAddress) +{ + return measureTempOnce(result, slaveAddress, m_tempOsr); } /** @@ -202,8 +218,11 @@ int16_t LOLIN_HP303B::measureTempOnce(float &result) * -2 if the object initialization failed * -1 on other fail */ -int16_t LOLIN_HP303B::measureTempOnce(float &result, uint8_t oversamplingRate) +int16_t LOLIN_HP303B::measureTempOnce(float &result, uint8_t slaveAddress, uint8_t oversamplingRate) { + //Set I2C bus connection + m_slaveAddress = slaveAddress; + //Start measurement int16_t ret = startMeasureTempOnce(oversamplingRate); if(ret!=HP303B__SUCCEEDED) @@ -288,7 +307,23 @@ int16_t LOLIN_HP303B::startMeasureTempOnce(uint8_t oversamplingRate) */ int16_t LOLIN_HP303B::measurePressureOnce(float &result) { - return measurePressureOnce(result, m_prsOsr); + return measurePressureOnce(result, m_slaveAddress, m_prsOsr); +} + +/** + * performs one pressure measurement and writes result to the given address + * + * &result: reference to a 32-Bit signed Integer value where the result will be written + * It will not be written if result==NULL + * returns: 0 on success + * -4 if the HP303B is could not finish its measurement in time + * -3 if the HP303B is already busy + * -2 if the object initialization failed + * -1 on other fail + */ +int16_t LOLIN_HP303B::measurePressureOnce(float &result, uint8_t slaveAddress) +{ + return measurePressureOnce(result, slaveAddress, m_prsOsr); } /** @@ -307,8 +342,11 @@ int16_t LOLIN_HP303B::measurePressureOnce(float &result) * -2 if the object initialization failed * -1 on other fail */ -int16_t LOLIN_HP303B::measurePressureOnce(float &result, uint8_t oversamplingRate) +int16_t LOLIN_HP303B::measurePressureOnce(float &result, uint8_t slaveAddress, uint8_t oversamplingRate) { + //Set I2C bus connection + m_slaveAddress = slaveAddress; + //start the measurement int16_t ret = startMeasurePressureOnce(oversamplingRate); if(ret != HP303B__SUCCEEDED) diff --git a/lib/LOLIN_HP303B/src/LOLIN_HP303B.h b/lib/LOLIN_HP303B/src/LOLIN_HP303B.h index 651b80380..4d04ff6da 100644 --- a/lib/LOLIN_HP303B/src/LOLIN_HP303B.h +++ b/lib/LOLIN_HP303B/src/LOLIN_HP303B.h @@ -35,11 +35,13 @@ public: //Command Mode int16_t measureTempOnce(float &result); - int16_t measureTempOnce(float &result, uint8_t oversamplingRate); + int16_t measureTempOnce(float &result, uint8_t slaveAddress); + int16_t measureTempOnce(float &result, uint8_t slaveAddress, uint8_t oversamplingRate); int16_t startMeasureTempOnce(void); int16_t startMeasureTempOnce(uint8_t oversamplingRate); int16_t measurePressureOnce(float &result); - int16_t measurePressureOnce(float &result, uint8_t oversamplingRate); + int16_t measurePressureOnce(float &result, uint8_t slaveAddress); + int16_t measurePressureOnce(float &result, uint8_t slaveAddress, uint8_t oversamplingRate); int16_t startMeasurePressureOnce(void); int16_t startMeasurePressureOnce(uint8_t oversamplingRate); int16_t getSingleResult(float &result); diff --git a/tasmota/xsns_73_hp303b.ino b/tasmota/xsns_73_hp303b.ino index 01145e0d1..1be0a1606 100644 --- a/tasmota/xsns_73_hp303b.ino +++ b/tasmota/xsns_73_hp303b.ino @@ -58,11 +58,11 @@ bool HP303B_Read(uint8_t hp303b_idx) float p; int16_t ret; - ret = HP303BSensor.measureTempOnce(t, hp303b_cfg.oversampling); + ret = HP303BSensor.measureTempOnce(t, hp303b_sensor[hp303b_idx].address, hp303b_cfg.oversampling); if (ret != 0) return false; - ret = HP303BSensor.measurePressureOnce(p, hp303b_cfg.oversampling); + ret = HP303BSensor.measurePressureOnce(p, hp303b_sensor[hp303b_idx].address, hp303b_cfg.oversampling); if (ret != 0) return false;