From 45a8900c8c77b720cf2d923962884b736717df1a Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 5 Jul 2021 12:03:48 +0200 Subject: [PATCH] Ensure readPressure function is executed only if begin returns 1 --- src/BARO.cpp | 44 +++++++++++++++++++++++++------------------- src/BARO.h | 1 + 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/BARO.cpp b/src/BARO.cpp index cdb4ad5..8695030 100644 --- a/src/BARO.cpp +++ b/src/BARO.cpp @@ -31,7 +31,8 @@ #define LPS22HB_PRESS_OUT_H_REG 0x2a LPS22HBClass::LPS22HBClass(TwoWire& wire) : - _wire(&wire) + _wire(&wire), + _initialized(false) { } @@ -44,35 +45,40 @@ int LPS22HBClass::begin() return 0; } + _initialized = true; return 1; } void LPS22HBClass::end() { _wire->end(); + _initialized = false; } float LPS22HBClass::readPressure(int units) { - // trigger one shot - i2cWrite(LPS22HB_CTRL2_REG, 0x01); - - // wait for ONE_SHOT bit to be cleared by the hardware - while ((i2cRead(LPS22HB_CTRL2_REG) & 0x01) != 0) { - yield(); - } - - float reading = (i2cRead(LPS22HB_PRESS_OUT_XL_REG) | - (i2cRead(LPS22HB_PRESS_OUT_L_REG) << 8) | - (i2cRead(LPS22HB_PRESS_OUT_H_REG) << 16)) / 40960.0; - - if (units == MILLIBAR) { // 1 kPa = 10 millibar - return reading * 10; - } else if (units == PSI) { // 1 kPa = 0.145038 PSI - return reading * 0.145038; - } else { - return reading; + if (_initialized == true) { + // trigger one shot + i2cWrite(LPS22HB_CTRL2_REG, 0x01); + + // wait for ONE_SHOT bit to be cleared by the hardware + while ((i2cRead(LPS22HB_CTRL2_REG) & 0x01) != 0) { + yield(); + } + + float reading = (i2cRead(LPS22HB_PRESS_OUT_XL_REG) | + (i2cRead(LPS22HB_PRESS_OUT_L_REG) << 8) | + (i2cRead(LPS22HB_PRESS_OUT_H_REG) << 16)) / 40960.0; + + if (units == MILLIBAR) { // 1 kPa = 10 millibar + return reading * 10; + } else if (units == PSI) { // 1 kPa = 0.145038 PSI + return reading * 0.145038; + } else { + return reading; + } } + return 0; } int LPS22HBClass::i2cRead(uint8_t reg) diff --git a/src/BARO.h b/src/BARO.h index 0744073..9fb37af 100644 --- a/src/BARO.h +++ b/src/BARO.h @@ -44,6 +44,7 @@ class LPS22HBClass { private: TwoWire* _wire; + bool _initialized; }; extern LPS22HBClass BARO;