diff --git a/src/BME280.cpp b/src/BME280.cpp index bc57c38..aedb76f 100644 --- a/src/BME280.cpp +++ b/src/BME280.cpp @@ -58,7 +58,7 @@ bool BME280::Initialize() { InitializeFilter(); } - + WriteSettings(); } @@ -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. + + return success; +} + + /****************************************************************/ void BME280::CalculateRegisters ( @@ -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) { diff --git a/src/BME280.h b/src/BME280.h index a0c3df0..52c52ab 100644 --- a/src/BME280.h +++ b/src/BME280.h @@ -147,6 +147,16 @@ class BME280 SpiEnable spiEnable; }; + struct Status + { + Status(): + measuring(false), + im_update(false) {} + + bool measuring; + bool im_update; + }; + /*****************************************************************/ /* INIT FUNCTIONS */ /*****************************************************************/ @@ -245,6 +255,7 @@ 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; @@ -252,6 +263,7 @@ class BME280 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; /*****************************************************************/ @@ -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( @@ -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.