Skip to content

Commit

Permalink
Adding ReadStatus function. Adding logic to wait until sensor is fini…
Browse files Browse the repository at this point in the history
…shed reading before returing a measurement.
  • Loading branch information
finitespace authored and finitespace committed Dec 27, 2017
1 parent 335a562 commit 2000dc7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
40 changes: 35 additions & 5 deletions src/BME280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ bool BME280::Initialize()
{
InitializeFilter();
}

WriteSettings();
}

Expand Down Expand Up @@ -151,6 +151,25 @@ bool BME280::begin
return success;
}


/****************************************************************/
bool BME280::ReadStatus
(
Status& status
)
{
bool success(true);
uint8_t statusBits(0);

success &= ReadRegister(STATUS_ADDR, &statusBits, STATUS_LENGTH);

status.im_update = statusBits & 0x01; // Bit 0.
status.measuring = statusBits & 0x04; // Bit 3.

This comment has been minimized.

Copy link
@jirkaptr

jirkaptr Mar 3, 2018

Contributor

status.measuring must be equal to statusBits & 0x08; // because Bit 3, das heisst (which means) 1<<3


return success;
}


/****************************************************************/
void BME280::CalculateRegisters
(
Expand Down Expand Up @@ -210,17 +229,28 @@ bool BME280::ReadData
int32_t data[SENSOR_DATA_LENGTH]
)
{
bool success;
bool success(true);
uint8_t buffer[SENSOR_DATA_LENGTH];

// For forced mode we need to write the mode to BME280 register before reading
// For forced mode we need to write the mode to BME280 register
// before reading.
if (m_settings.mode == Mode_Forced)
{
WriteSettings();
}

// Registers are in order. So we can start at the pressure register and read 8 bytes.
success = ReadRegister(PRESS_ADDR, buffer, SENSOR_DATA_LENGTH);
Status status;

// Wait until measurements are finished.
do
{
success &= ReadStatus(status);
}
while(success && status.im_update && status.measuring);

// Registers are in order. So we can start at the pressure register
// and read 8 bytes.
success &= ReadRegister(PRESS_ADDR, buffer, SENSOR_DATA_LENGTH);

for(int i = 0; i < SENSOR_DATA_LENGTH; ++i)
{
Expand Down
17 changes: 16 additions & 1 deletion src/BME280.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ class BME280
SpiEnable spiEnable;
};

struct Status
{
Status():
measuring(false),
im_update(false) {}

bool measuring;
bool im_update;
};

/*****************************************************************/
/* INIT FUNCTIONS */
/*****************************************************************/
Expand Down Expand Up @@ -245,13 +255,15 @@ class BME280
static const uint8_t HUM_DIG_ADDR1 = 0xA1;
static const uint8_t HUM_DIG_ADDR2 = 0xE1;
static const uint8_t ID_ADDR = 0xD0;
static const uint8_t STATUS_ADDR = 0xF3;

static const uint8_t TEMP_DIG_LENGTH = 6;
static const uint8_t PRESS_DIG_LENGTH = 18;
static const uint8_t HUM_DIG_ADDR1_LENGTH = 1;
static const uint8_t HUM_DIG_ADDR2_LENGTH = 7;
static const uint8_t DIG_LENGTH = 32;
static const uint8_t SENSOR_DATA_LENGTH = 8;
static const uint8_t STATUS_LENGTH = 1;


/*****************************************************************/
Expand Down Expand Up @@ -287,6 +299,10 @@ class BME280
/* WORKER FUNCTIONS */
/*****************************************************************/

/////////////////////////////////////////////////////////////////
/// Write the settings to the chip.
bool ReadStatus(Status& status);

/////////////////////////////////////////////////////////////////
/// Calculates registers based on settings.
void CalculateRegisters(
Expand All @@ -298,7 +314,6 @@ class BME280
/// Write the settings to the chip.
bool WriteSettings();


/////////////////////////////////////////////////////////////////
/// Read the the chip id data from the BME280, return true if
/// successful and the id matches a known value.
Expand Down

0 comments on commit 2000dc7

Please sign in to comment.