Skip to content

Commit aeb9f6b

Browse files
committed
bsec: configure sensors for low power+caching
1 parent 11cdd73 commit aeb9f6b

File tree

6 files changed

+98
-52
lines changed

6 files changed

+98
-52
lines changed

src/AirQualityClass.cpp

+44-24
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ AirQualityClass::~AirQualityClass()
2929
{
3030
}
3131

32+
/* Configure the BSEC library with information about the sensor
33+
18v/33v = Voltage at Vdd. 1.8V or 3.3V
34+
3s/300s = BSEC operating mode, BSEC_SAMPLE_RATE_LP or BSEC_SAMPLE_RATE_ULP
35+
4d/28d = Operating age of the sensor in days
36+
generic_18v_3s_4d
37+
generic_18v_3s_28d
38+
generic_18v_300s_4d
39+
generic_18v_300s_28d
40+
generic_33v_3s_4d
41+
generic_33v_3s_28d
42+
generic_33v_300s_4d
43+
generic_33v_300s_28d
44+
*/
45+
46+
extern "C" const uint8_t bsec_config_iaq[] = {
47+
#include "config/generic_33v_3s_4d/bsec_iaq.txt"
48+
};
49+
3250
int AirQualityClass::begin()
3351
{
3452
_revision = board_revision();
@@ -39,6 +57,7 @@ int AirQualityClass::begin()
3957
if (checkIaqSensorStatus() == STATUS_ERROR){
4058
return 0;
4159
}
60+
iaqSensor->setConfig(bsec_config_iaq);
4261

4362
bsec_virtual_sensor_t sensorList[13] = {
4463
BSEC_OUTPUT_IAQ,
@@ -55,7 +74,7 @@ int AirQualityClass::begin()
5574
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
5675
BSEC_OUTPUT_GAS_PERCENTAGE
5776
};
58-
iaqSensor->updateSubscription(sensorList, 13, BSEC_SAMPLE_RATE_CONT);
77+
iaqSensor->updateSubscription(sensorList, 13, BSEC_SAMPLE_RATE_LP);
5978
if (checkIaqSensorStatus() == STATUS_ERROR){
6079
return 0;
6180
}
@@ -94,62 +113,63 @@ void AirQualityClass::end()
94113

95114
float AirQualityClass::readVOC()
96115
{
97-
float reading = 0.0;
98116
if (_revision == BOARD_REVISION_2) {
99-
while(!iaqSensor->run()){ }
100-
reading = iaqSensor->breathVocEquivalent;
117+
if(iaqSensor->run()){
118+
breathVocEquivalent = iaqSensor->breathVocEquivalent;
119+
}
101120
}
102-
return reading;
121+
return breathVocEquivalent;
103122
}
104123

105124
float AirQualityClass::readGasResistor()
106125
{
107-
float reading = 0.0;
108126
if (_revision == BOARD_REVISION_2) {
109-
while(!iaqSensor->run()){ }
110-
reading = iaqSensor->gasResistance;
127+
if(iaqSensor->run()){
128+
gasResistance = iaqSensor->gasResistance;
129+
}
111130
}
112-
return reading;
131+
return gasResistance;
113132
}
114133

115134
float AirQualityClass::readIAQ()
116135
{
117-
float reading = 0.0;
118136
if (_revision == BOARD_REVISION_2) {
119-
while(!iaqSensor->run()){ }
120-
reading = iaqSensor->iaq;
137+
if(iaqSensor->run()){
138+
iaq = iaqSensor->iaq;
139+
}
121140
}
122-
return reading;
141+
return iaq;
123142
}
124143

125144
float AirQualityClass::readIAQAccuracy()
126145
{
127-
float reading = 0.0;
128146
if (_revision == BOARD_REVISION_2) {
129-
while(!iaqSensor->run()){ }
130-
reading = iaqSensor->iaqAccuracy;
147+
if(iaqSensor->run()){
148+
iaqAccuracy = iaqSensor->iaqAccuracy;
149+
}
131150
}
132-
return reading;
151+
return iaqAccuracy;
133152
}
134153

135154
float AirQualityClass::readStaticIAQ()
136155
{
137-
float reading = 0.0;
138156
if (_revision == BOARD_REVISION_2) {
139-
while(!iaqSensor->run()){ }
140-
reading = iaqSensor->staticIaq;
157+
if(iaqSensor->run()){
158+
staticIaq = iaqSensor->staticIaq;
159+
}
141160
}
142-
return reading;
161+
return staticIaq;
143162
}
144163

145164

146165
float AirQualityClass::readCO2()
147166
{
148167
float reading = 0.0;
149168
if (_revision == BOARD_REVISION_2) {
150-
while(!iaqSensor->run()){ }
151-
reading = iaqSensor->co2Equivalent;
169+
if(iaqSensor->run()){
170+
co2Equivalent = iaqSensor->co2Equivalent;
171+
}
152172
}
153-
return reading;
173+
return co2Equivalent;
154174
}
155175

src/AirQualityClass.h

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ class AirQualityClass {
2525
private:
2626
// Helper functions declarations
2727
int checkIaqSensorStatus(void);
28+
float breathVocEquivalent = 0.0f;
29+
float gasResistance = 0.0f;
30+
float iaq = 0.0f;
31+
float iaqAccuracy = 0.0f;
32+
float staticIaq = 0.0f;
33+
float co2Equivalent = 0.0f;
2834

2935
private:
3036

src/EnvClass.cpp

+21-13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ EnvClass::~EnvClass()
3030
{
3131
}
3232

33+
extern const uint8_t bsec_config_iaq[];
34+
3335
int EnvClass::begin()
3436
{
3537
_revision = board_revision();
@@ -40,21 +42,25 @@ int EnvClass::begin()
4042
if (checkIaqSensorStatus() == STATUS_ERROR){
4143
return 0;
4244
}
45+
iaqSensor->setConfig(bsec_config_iaq);
4346

44-
bsec_virtual_sensor_t sensorList[10] = {
45-
BSEC_OUTPUT_RAW_TEMPERATURE,
46-
BSEC_OUTPUT_RAW_PRESSURE,
47-
BSEC_OUTPUT_RAW_HUMIDITY,
48-
BSEC_OUTPUT_RAW_GAS,
47+
bsec_virtual_sensor_t sensorList[13] = {
4948
BSEC_OUTPUT_IAQ,
5049
BSEC_OUTPUT_STATIC_IAQ,
5150
BSEC_OUTPUT_CO2_EQUIVALENT,
5251
BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
52+
BSEC_OUTPUT_RAW_TEMPERATURE,
53+
BSEC_OUTPUT_RAW_PRESSURE,
54+
BSEC_OUTPUT_RAW_HUMIDITY,
55+
BSEC_OUTPUT_RAW_GAS,
56+
BSEC_OUTPUT_STABILIZATION_STATUS,
57+
BSEC_OUTPUT_RUN_IN_STATUS,
5358
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
5459
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
60+
BSEC_OUTPUT_GAS_PERCENTAGE
5561
};
5662

57-
iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONT);
63+
iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP);
5864
if (checkIaqSensorStatus() == STATUS_ERROR){
5965
return 0;
6066
}
@@ -102,12 +108,13 @@ void EnvClass::end()
102108
float EnvClass::readTemperature(int units /*= CELSIUS*/)
103109
{
104110
if (_revision == BOARD_REVISION_2) {
105-
while(!iaqSensor->run()){ }
106-
float reading = iaqSensor->temperature;
111+
if(iaqSensor->run()){
112+
temperature = iaqSensor->temperature;
113+
}
107114
if (units == FAHRENHEIT){
108-
return (reading * 9.0 / 5.0) + 32.0;
115+
return (temperature * 9.0 / 5.0) + 32.0;
109116
} else {
110-
return reading;
117+
return temperature;
111118
}
112119
}
113120
return HTS221->readTemperature(units);
@@ -116,9 +123,10 @@ float EnvClass::readTemperature(int units /*= CELSIUS*/)
116123
float EnvClass::readHumidity()
117124
{
118125
if (_revision == BOARD_REVISION_2) {
119-
while(!iaqSensor->run()){ }
120-
return iaqSensor->humidity;
121-
126+
if(iaqSensor->run()){
127+
humidity = iaqSensor->humidity;
128+
}
129+
return humidity;
122130
}
123131
return HTS221->readHumidity();
124132
}

src/EnvClass.h

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class EnvClass {
2828

2929
int (*board_revision)(void);
3030
int _revision;
31+
float temperature = 0.0f;
32+
float humidity = 0.0f;
3133
};
3234

3335
#endif //_ENVCLASS_H_INCLUDED

src/PressureClass.cpp

+23-15
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ PressureClass::~PressureClass()
2929
{
3030
}
3131

32+
extern const uint8_t bsec_config_iaq[];
33+
3234
int PressureClass::begin()
3335
{
3436
_revision = board_revision();
@@ -39,21 +41,25 @@ int PressureClass::begin()
3941
if (checkIaqSensorStatus() == STATUS_ERROR){
4042
return 0;
4143
}
44+
iaqSensor->setConfig(bsec_config_iaq);
4245

43-
bsec_virtual_sensor_t sensorList[10] = {
44-
BSEC_OUTPUT_RAW_TEMPERATURE,
45-
BSEC_OUTPUT_RAW_PRESSURE,
46-
BSEC_OUTPUT_RAW_HUMIDITY,
47-
BSEC_OUTPUT_RAW_GAS,
46+
bsec_virtual_sensor_t sensorList[13] = {
4847
BSEC_OUTPUT_IAQ,
4948
BSEC_OUTPUT_STATIC_IAQ,
5049
BSEC_OUTPUT_CO2_EQUIVALENT,
5150
BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
51+
BSEC_OUTPUT_RAW_TEMPERATURE,
52+
BSEC_OUTPUT_RAW_PRESSURE,
53+
BSEC_OUTPUT_RAW_HUMIDITY,
54+
BSEC_OUTPUT_RAW_GAS,
55+
BSEC_OUTPUT_STABILIZATION_STATUS,
56+
BSEC_OUTPUT_RUN_IN_STATUS,
5257
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
5358
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
59+
BSEC_OUTPUT_GAS_PERCENTAGE
5460
};
5561

56-
iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_CONT);
62+
iaqSensor->updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP);
5763
if (checkIaqSensorStatus() == STATUS_ERROR){
5864
return 0;
5965
}
@@ -102,14 +108,15 @@ void PressureClass::end()
102108
float PressureClass::readPressure(int units)
103109
{
104110
if (_revision == BOARD_REVISION_2) {
105-
while(!iaqSensor->run()){ }
106-
float reading = iaqSensor->pressure/1000;
111+
if(iaqSensor->run()){
112+
pressure = iaqSensor->pressure/1000;
113+
}
107114
if (units == MILLIBAR) { // 1 kPa = 10 millibar
108-
return reading * 10;
115+
return pressure * 10;
109116
} else if (units == PSI) { // 1 kPa = 0.145038 PSI
110-
return reading * 0.145038;
117+
return pressure * 0.145038;
111118
} else {
112-
return reading;
119+
return pressure;
113120
}
114121
}
115122
return LPS22HB->readPressure(units);
@@ -118,12 +125,13 @@ float PressureClass::readPressure(int units)
118125
float PressureClass::readTemperature(int units /*= CELSIUS*/)
119126
{
120127
if (_revision == BOARD_REVISION_2) {
121-
while(!iaqSensor->run()){}
122-
float reading = iaqSensor->temperature;
128+
if(iaqSensor->run()){
129+
temperature = iaqSensor->temperature;
130+
}
123131
if (units == FAHRENHEIT){
124-
return (reading * 9.0 / 5.0) + 32.0;
132+
return (temperature * 9.0 / 5.0) + 32.0;
125133
} else {
126-
return reading;
134+
return temperature;
127135
}
128136
}
129137
return LPS22HB->readTemperature();

src/PressureClass.h

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class PressureClass {
2424

2525
LPS22HBClass* LPS22HB;
2626

27+
float pressure = 0.0f;
28+
float temperature = 0.0f;
2729

2830
private:
2931

0 commit comments

Comments
 (0)