Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/BARO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
#define LPS22HB_PRESS_OUT_H_REG 0x2a

LPS22HBClass::LPS22HBClass(TwoWire& wire) :
_wire(&wire)
_wire(&wire),
_initialized(false)
{
}

Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/BARO.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class LPS22HBClass {

private:
TwoWire* _wire;
bool _initialized;
};

extern LPS22HBClass BARO;
Expand Down