-
Notifications
You must be signed in to change notification settings - Fork 24
/
basic.ino
198 lines (176 loc) · 6.15 KB
/
basic.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Copyright (C) 2021 Bosch Sensortec GmbH
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/
/* The new sensor needs to be conditioned before the example can work reliably. You may run this
* example for 24hrs to let the sensor stabilize.
*/
/**
* basic.ino sketch :
* This is an example for illustrating the BSEC virtual outputs using BME688 Development Kit,
* which has been designed to work with Adafruit ESP32 Feather Board
* For more information visit :
* https://www.bosch-sensortec.com/software-tools/software/bme688-software/
*/
#include <bsec2.h>
#include "commMux.h"
/* Macros used */
/* Number of sensors to operate*/
#define NUM_OF_SENS 8
#define PANIC_LED LED_BUILTIN
#define ERROR_DUR 1000
/* Helper functions declarations */
/**
* @brief : This function toggles the led when a fault was detected
*/
void errLeds(void);
/**
* @brief : This function checks the BSEC status, prints the respective error code. Halts in case of error
* @param[in] bsec : Bsec2 class object
*/
void checkBsecStatus(Bsec2 bsec);
/**
* @brief : This function is called by the BSEC library when a new output is available
* @param[in] input : BME68X sensor data before processing
* @param[in] outputs : Processed BSEC BSEC output data
* @param[in] bsec : Instance of BSEC2 calling the callback
*/
void newDataCallback(const bme68xData data, const bsecOutputs outputs, Bsec2 bsec);
/* Create an array of objects of the class Bsec2 */
Bsec2 envSensor[NUM_OF_SENS];
commMux communicationSetup[NUM_OF_SENS];
uint8_t bsecMemBlock[NUM_OF_SENS][BSEC_INSTANCE_SIZE];
uint8_t sensor = 0;
/* Entry point for the example */
void setup(void)
{
/* Desired subscription list of BSEC2 outputs */
bsecSensor sensorList[] = {
BSEC_OUTPUT_IAQ,
BSEC_OUTPUT_RAW_TEMPERATURE,
BSEC_OUTPUT_RAW_PRESSURE,
BSEC_OUTPUT_RAW_HUMIDITY,
BSEC_OUTPUT_RAW_GAS,
BSEC_OUTPUT_STABILIZATION_STATUS,
BSEC_OUTPUT_RUN_IN_STATUS
};
/* Initialize the communication interfaces */
Serial.begin(115200);
commMuxBegin(Wire, SPI);
pinMode(PANIC_LED, OUTPUT);
delay(100);
/* Valid for boards with USB-COM. Wait until the port is open */
while(!Serial) delay(10);
for (uint8_t i = 0; i < NUM_OF_SENS; i++)
{
/* Sets the Communication interface for the sensors */
communicationSetup[i] = commMuxSetConfig(Wire, SPI, i, communicationSetup[i]);
/* Assigning a chunk of memory block to the bsecInstance */
envSensor[i].allocateMemory(bsecMemBlock[i]);
/* Initialize the library and interfaces */
if (!envSensor[i].begin(BME68X_SPI_INTF, commMuxRead, commMuxWrite, commMuxDelay, &communicationSetup[i]))
{
checkBsecStatus (envSensor[i]);
}
/* Subscribe to the desired BSEC2 outputs */
if (!envSensor[i].updateSubscription(sensorList, ARRAY_LEN(sensorList), BSEC_SAMPLE_RATE_LP))
{
checkBsecStatus (envSensor[i]);
}
/* Whenever new data is available call the newDataCallback function */
envSensor[i].attachCallback(newDataCallback);
}
Serial.println("BSEC library version " + \
String(envSensor[0].version.major) + "." \
+ String(envSensor[0].version.minor) + "." \
+ String(envSensor[0].version.major_bugfix) + "." \
+ String(envSensor[0].version.minor_bugfix));
}
/* Function that is looped forever */
void loop(void)
{
/* Call the run function often so that the library can
* check if it is time to read new data from the sensor
* and process it.
*/
for (sensor = 0; sensor < NUM_OF_SENS; sensor++)
{
if (!envSensor[sensor].run())
{
checkBsecStatus(envSensor[sensor]);
}
}
}
void errLeds(void)
{
while(1)
{
digitalWrite(PANIC_LED, HIGH);
delay(ERROR_DUR);
digitalWrite(PANIC_LED, LOW);
delay(ERROR_DUR);
}
}
void newDataCallback(const bme68xData data, const bsecOutputs outputs, Bsec2 bsec)
{
if (!outputs.nOutputs)
{
return;
}
Serial.println("BSEC outputs:\n\tsensor num = " + String(sensor));
Serial.println("\ttimestamp = " + String((int) (outputs.output[0].time_stamp / INT64_C(1000000))));
for (uint8_t i = 0; i < outputs.nOutputs; i++)
{
const bsecData output = outputs.output[i];
switch (output.sensor_id)
{
case BSEC_OUTPUT_IAQ:
Serial.println("\tiaq = " + String(output.signal));
Serial.println("\tiaq accuracy = " + String((int) output.accuracy));
break;
case BSEC_OUTPUT_RAW_TEMPERATURE:
Serial.println("\ttemperature = " + String(output.signal));
break;
case BSEC_OUTPUT_RAW_PRESSURE:
Serial.println("\tpressure = " + String(output.signal));
break;
case BSEC_OUTPUT_RAW_HUMIDITY:
Serial.println("\thumidity = " + String(output.signal));
break;
case BSEC_OUTPUT_RAW_GAS:
Serial.println("\tgas resistance = " + String(output.signal));
break;
case BSEC_OUTPUT_STABILIZATION_STATUS:
Serial.println("\tstabilization status = " + String(output.signal));
break;
case BSEC_OUTPUT_RUN_IN_STATUS:
Serial.println("\trun in status = " + String(output.signal));
break;
default:
break;
}
}
}
void checkBsecStatus(Bsec2 bsec)
{
if (bsec.status < BSEC_OK)
{
Serial.println("BSEC error code : " + String(bsec.status));
errLeds(); /* Halt in case of failure */
}
else if (bsec.status > BSEC_OK)
{
Serial.println("BSEC warning code : " + String(bsec.status));
}
if (bsec.sensor.status < BME68X_OK)
{
Serial.println("BME68X error code : " + String(bsec.sensor.status));
errLeds(); /* Halt in case of failure */
}
else if (bsec.sensor.status > BME68X_OK)
{
Serial.println("BME68X warning code : " + String(bsec.sensor.status));
}
}