Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Status function. #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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.

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