Skip to content

Commit

Permalink
bsec: cache everything on successfull run()
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Mar 28, 2024
1 parent aeb9f6b commit 5d8947b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 35 deletions.
24 changes: 12 additions & 12 deletions src/AirQualityClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,50 +115,50 @@ float AirQualityClass::readVOC()
{
if (_revision == BOARD_REVISION_2) {
if(iaqSensor->run()){
breathVocEquivalent = iaqSensor->breathVocEquivalent;
mkr_iot_carrier_rev2::cache();
}
}
return breathVocEquivalent;
return mkr_iot_carrier_rev2::breathVocEquivalent;
}

float AirQualityClass::readGasResistor()
{
if (_revision == BOARD_REVISION_2) {
if(iaqSensor->run()){
gasResistance = iaqSensor->gasResistance;
mkr_iot_carrier_rev2::cache();
}
}
return gasResistance;
return mkr_iot_carrier_rev2::gasResistance;
}

float AirQualityClass::readIAQ()
{
if (_revision == BOARD_REVISION_2) {
if(iaqSensor->run()){
iaq = iaqSensor->iaq;
mkr_iot_carrier_rev2::cache();
}
}
return iaq;
return mkr_iot_carrier_rev2::iaq;
}

float AirQualityClass::readIAQAccuracy()
{
if (_revision == BOARD_REVISION_2) {
if(iaqSensor->run()){
iaqAccuracy = iaqSensor->iaqAccuracy;
mkr_iot_carrier_rev2::cache();
}
}
return iaqAccuracy;
return mkr_iot_carrier_rev2::iaqAccuracy;
}

float AirQualityClass::readStaticIAQ()
{
if (_revision == BOARD_REVISION_2) {
if(iaqSensor->run()){
staticIaq = iaqSensor->staticIaq;
mkr_iot_carrier_rev2::cache();
}
}
return staticIaq;
return mkr_iot_carrier_rev2::staticIaq;
}


Expand All @@ -167,9 +167,9 @@ float AirQualityClass::readCO2()
float reading = 0.0;
if (_revision == BOARD_REVISION_2) {
if(iaqSensor->run()){
co2Equivalent = iaqSensor->co2Equivalent;
mkr_iot_carrier_rev2::cache();
}
}
return co2Equivalent;
return mkr_iot_carrier_rev2::co2Equivalent;
}

6 changes: 0 additions & 6 deletions src/AirQualityClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ class AirQualityClass {
private:
// Helper functions declarations
int checkIaqSensorStatus(void);
float breathVocEquivalent = 0.0f;
float gasResistance = 0.0f;
float iaq = 0.0f;
float iaqAccuracy = 0.0f;
float staticIaq = 0.0f;
float co2Equivalent = 0.0f;

private:

Expand Down
9 changes: 9 additions & 0 deletions src/Arduino_MKRIoTCarrier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ bool CARRIER_CASE = false;

mkr_iot_carrier_rev2 mkr_iot_carrier_rev2_instance;
Bsec* mkr_iot_carrier_rev2::iaqSensor;
float mkr_iot_carrier_rev2::breathVocEquivalent;
float mkr_iot_carrier_rev2::gasResistance;
float mkr_iot_carrier_rev2::iaq;
float mkr_iot_carrier_rev2::iaqAccuracy;
float mkr_iot_carrier_rev2::staticIaq;
float mkr_iot_carrier_rev2::co2Equivalent;
float mkr_iot_carrier_rev2::temperature;
float mkr_iot_carrier_rev2::pressure;
float mkr_iot_carrier_rev2::humidity;

MKRIoTCarrier::MKRIoTCarrier() {
}
Expand Down
10 changes: 5 additions & 5 deletions src/EnvClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ float EnvClass::readTemperature(int units /*= CELSIUS*/)
{
if (_revision == BOARD_REVISION_2) {
if(iaqSensor->run()){
temperature = iaqSensor->temperature;
mkr_iot_carrier_rev2::cache();
}
if (units == FAHRENHEIT){
return (temperature * 9.0 / 5.0) + 32.0;
return (mkr_iot_carrier_rev2::temperature * 9.0 / 5.0) + 32.0;
} else {
return temperature;
return mkr_iot_carrier_rev2::temperature;
}
}
return HTS221->readTemperature(units);
Expand All @@ -124,9 +124,9 @@ float EnvClass::readHumidity()
{
if (_revision == BOARD_REVISION_2) {
if(iaqSensor->run()){
humidity = iaqSensor->humidity;
mkr_iot_carrier_rev2::cache();
}
return humidity;
return mkr_iot_carrier_rev2::humidity;
}
return HTS221->readHumidity();
}
2 changes: 0 additions & 2 deletions src/EnvClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class EnvClass {

int (*board_revision)(void);
int _revision;
float temperature = 0.0f;
float humidity = 0.0f;
};

#endif //_ENVCLASS_H_INCLUDED
20 changes: 20 additions & 0 deletions src/MKRIoTCarrierDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ class mkr_iot_carrier_rev2 {
GROVE_AN1 = A0,
GROVE_AN2 = A6,
};
static void cache() {
breathVocEquivalent = iaqSensor->breathVocEquivalent;
gasResistance = iaqSensor->gasResistance;
iaq = iaqSensor->iaq;
iaqAccuracy = iaqSensor->iaqAccuracy;
staticIaq = iaqSensor->staticIaq;
co2Equivalent = iaqSensor->co2Equivalent;
temperature = iaqSensor->temperature;
pressure = iaqSensor->pressure;
humidity = iaqSensor->humidity;
};
static float breathVocEquivalent;
static float gasResistance;
static float iaq;
static float iaqAccuracy;
static float staticIaq;
static float co2Equivalent;
static float temperature;
static float pressure;
static float humidity;
};

extern mkr_iot_carrier_rev2 mkr_iot_carrier_rev2_instance;
Expand Down
15 changes: 8 additions & 7 deletions src/PressureClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ float PressureClass::readPressure(int units)
{
if (_revision == BOARD_REVISION_2) {
if(iaqSensor->run()){
pressure = iaqSensor->pressure/1000;
mkr_iot_carrier_rev2::cache();
}
auto _pressure = mkr_iot_carrier_rev2::pressure / 1000;
if (units == MILLIBAR) { // 1 kPa = 10 millibar
return pressure * 10;
return _pressure * 10;
} else if (units == PSI) { // 1 kPa = 0.145038 PSI
return pressure * 0.145038;
return _pressure * 0.145038;
} else {
return pressure;
return _pressure;
}
}
return LPS22HB->readPressure(units);
Expand All @@ -126,12 +127,12 @@ float PressureClass::readTemperature(int units /*= CELSIUS*/)
{
if (_revision == BOARD_REVISION_2) {
if(iaqSensor->run()){
temperature = iaqSensor->temperature;
mkr_iot_carrier_rev2::cache();
}
if (units == FAHRENHEIT){
return (temperature * 9.0 / 5.0) + 32.0;
return (mkr_iot_carrier_rev2::temperature * 9.0 / 5.0) + 32.0;
} else {
return temperature;
return mkr_iot_carrier_rev2::temperature;
}
}
return LPS22HB->readTemperature();
Expand Down
3 changes: 0 additions & 3 deletions src/PressureClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ class PressureClass {

LPS22HBClass* LPS22HB;

float pressure = 0.0f;
float temperature = 0.0f;

private:

int (*board_revision)(void);
Expand Down

0 comments on commit 5d8947b

Please sign in to comment.