SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_aux_bmm150_bme280_gpio.ino
Go to the documentation of this file.
1
30#include <Wire.h>
31#include <SPI.h>
32#include <Arduino.h>
33#include <ImuDrv.hpp>
34
35#include <Commander.h> //Deplib https://github.com/CreativeRobotics/Commander
36Commander cmd;
38
39// #define USE_I2C_INTERFACE true
40// #define USE_SPI_INTERFACE true
41
42#if !defined(USE_I2C_INTERFACE) && !defined(USE_SPI_INTERFACE)
43#define USE_I2C_INTERFACE
44#warning "No interface type is selected, use I2C interface"
45#endif
46
47#if defined(USE_SPI_INTERFACE)
48#ifndef SPI_MOSI
49#define SPI_MOSI 33
50#endif
51
52#ifndef SPI_MISO
53#define SPI_MISO 34
54#endif
55
56#ifndef SPI_SCK
57#define SPI_SCK 35
58#endif
59
60// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
61#ifndef BHI260_IRQ
62#define BHI260_IRQ 37
63#endif
64
65#ifndef BHI260_CS
66#define BHI260_CS 36
67#endif
68
69#else //* I2C */
70
71#ifndef BHI260_SDA
72#define BHI260_SDA 2
73#endif
74
75#ifndef BHI260_SCL
76#define BHI260_SCL 3
77#endif
78
79// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
80#ifndef BHI260_IRQ
81#define BHI260_IRQ 8
82#endif
83#endif /*USE_SPI_INTERFACE*/
84
85#ifndef BHI260_RST
86#define BHI260_RST -1
87#endif
88
90
91// The firmware runs in RAM and will be lost if the power is off. The firmware will be loaded from RAM each time it is run.
92#define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO
93// #define BOSCH_BHI260_AUX_BMM150_GPIO
94// #define BOSCH_BHI260_GPIO
95
96// Firmware is stored in flash and booted from flash,Depends on BHI260 hardware connected to SPI Flash
97// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO_FLASH
98// #define BOSCH_BHI260_AUX_BMM150_GPIO_FLASH
99// #define BOSCH_BHI260_GPIO_FLASH
100
101#include <BoschFirmware.h>
102
103// Force update of current firmware, whether it exists or not.
104// Only works when external SPI Flash is connected to BHI260.
105// After uploading firmware once, you can change this to false to speed up boot time.
107
108#if BHI260_IRQ > 0
109#define USING_SENSOR_IRQ_METHOD
110#endif
111
112#ifdef USING_SENSOR_IRQ_METHOD
113volatile bool isInterruptTriggered = false;
114
116{
118}
119#endif /*USING_SENSOR_IRQ_METHOD*/
120
121void parse_bme280_sensor_data(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
122{
123 float humidity = 0;
124 float temperature = 0;
125 float pressure = 0;
126 switch (sensor_id) {
127 case BoschSensorID::HUMIDITY:
128 bhy2_parse_humidity(data_ptr, &humidity);
129 Serial.print("humidity:"); Serial.print(humidity); Serial.println("%");
130 break;
131 case BoschSensorID::TEMPERATURE:
132 bhy2_parse_temperature_celsius(data_ptr, &temperature);
133 Serial.print("temperature:"); Serial.print(temperature); Serial.println("*C");
134 break;
135 case BoschSensorID::BAROMETER:
136 bhy2_parse_pressure(data_ptr, &pressure);
137 Serial.print("pressure:"); Serial.print(pressure); Serial.println("hPa");
138 break;
139 default:
140 Serial.println("Unkown.");
141 break;
142 }
143}
144
145// Firmware update progress callback
146void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
147{
148 float progress = (float)transferred / total * 100;
149 Serial.print("Upload progress: ");
150 Serial.print(progress);
151 Serial.println("%");
152}
153
154void setup()
155{
156 Serial.begin(115200);
157 while (!Serial);
158
159 // Set the reset pin
160 bhy.setPins(BHI260_RST);
161
162 Serial.println("Initializing Sensors...");
163
164 // Set the firmware array address and firmware size
165 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size, bosch_firmware_type, force_update_flash_firmware);
166
167 // Set the firmware update processing progress callback function
168 // bhy.setUpdateProcessCallback(progress_callback, NULL);
169
170 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
171 // bhy.setMaxiTransferSize(256);
172
173 // Set the processing fifo data buffer size,The default size is 512 bytes.
174 // bhy.setProcessBufferSize(1024);
175
176 // Set to load firmware from flash
177 bhy.setBootFromFlash(bosch_firmware_type);
178
179 Serial.println("Initializing Sensors...");
180
181#ifdef USE_I2C_INTERFACE
182 // Using I2C interface
183 // BHI260AP_SLAVE_ADDRESS_L = 0x28
184 // BHI260AP_SLAVE_ADDRESS_H = 0x29
185 if (!bhy.begin(Wire, BHI260AP_SLAVE_ADDRESS_L, BHI260_SDA, BHI260_SCL)) {
186 Serial.print("Failed to initialize sensor - error code:");
187 Serial.println(bhy.getError());
188 while (1) {
189 delay(1000);
190 }
191 }
192#endif
193
194#ifdef USE_SPI_INTERFACE
195 // Using SPI interface
196 if (!bhy.begin(SPI, BHI260_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
197 Serial.print("Failed to initialize sensor - error code:");
198 Serial.println(bhy.getError());
199 while (1) {
200 delay(1000);
201 }
202 }
203#endif
204
205 Serial.println("Initializing the sensor successfully!");
206
207 // Register BME280 data parse callback function
208 bhy.onResultEvent(BoschSensorID::TEMPERATURE, parse_bme280_sensor_data);
209 bhy.onResultEvent(BoschSensorID::HUMIDITY, parse_bme280_sensor_data);
210 bhy.onResultEvent(BoschSensorID::BAROMETER, parse_bme280_sensor_data);
211
212 // Output all sensors info to Serial
213 BoschSensorInfo info = bhy.getSensorInfo();
214
215#ifdef ARDUINO
216 ArduinoStreamPrinter printer(Serial);
217 info.printInfo(printer);
218#else
219 info.printInfo([](const char *format, ...) -> int {
220 va_list args;
221 va_start(args, format);
222 int result = vprintf(format, args);
223 va_end(args);
224 return result;
225 });
226#endif
227
229
230 Serial.println("Hello: Type 'help' to get help");
231
232 cmd.printCommandPrompt();
233
234#ifdef USING_SENSOR_IRQ_METHOD
235 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
236 // This makes the pin ready to receive external signals.
237 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
238 pinMode(BHI260_IRQ, INPUT);
239
240 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
241 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
242 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
243#endif
244}
245
246void loop()
247{
248 //Call the update functions using the activeCommander pointer
249 cmd.update();
250
251#ifdef USING_SENSOR_IRQ_METHOD
253 isInterruptTriggered = false;
254#endif /*USING_SENSOR_IRQ_METHOD*/
255
256 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
257 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
258 */
259 bhy.update();
260
261#ifdef USING_SENSOR_IRQ_METHOD
262 }
263#endif /*USING_SENSOR_IRQ_METHOD*/
264}
265
266
267//All commands for 'master'
268//COMMAND ARRAY ------------------------------------------------------------------------------
269const commandList_t masterCommands[] = {
270 {"help", helpHandler, "help"},
271 {"set gpio", setGpioLevel, "set gpio level"},
272 {"get gpio", getGpioLevel, "get gpio level"},
273 {"dis gpio", disGpioMode, "disable gpio"},
274 {"temperature", setTemperature, "Temperature"},
275 {"humidity", setHumidity, "Humidity"},
276 {"pressure", setPressure, "Pressure"},
277};
278
280{
281 cmd.begin(&Serial, masterCommands, sizeof(masterCommands));
282 cmd.commandPrompt(ON); //enable the command prompt
283 cmd.echo(true); //Echo incoming characters to theoutput port
284 cmd.errorMessages(ON); //error messages are enabled - it will tell us if we issue any unrecognised commands
285 //Error messaged do NOT work for quick set and get commands
286}
287
288bool helpHandler(Commander &Cmdr)
289{
290 Serial.println("Help:");
291 Serial.println("\tCustom firmware valid gpio : 1, 5, 6, 14, 15, 16, 19, 20");
292 Serial.println("\tset gpio [gpio num] [level]");
293 Serial.println("\tget gpio [gpio num] [pullup]");
294 Serial.println("\tdis gpio [gpio num]");
295 Serial.println("\ttemperature [on/off] : range 0 ~ 1");
296 Serial.println("\thumidity [on/off] : range 0 ~ 1");
297 Serial.println("\tpressure [sample_rate/HZ] : range 0~1000");
298 return 0;
299}
300
301bool setTemperature(Commander &Cmdr)
302{
303 float sample_rate;
304 int items = Cmdr.countItems();
305 if (items < 1) {
306 return 0;
307 }
308 Cmdr.getFloat(sample_rate);
309 bhy.configure(BoschSensorID::TEMPERATURE, sample_rate, 0);
310 return 0;
311}
312
313bool setHumidity(Commander &Cmdr)
314{
315 float sample_rate;
316 int items = Cmdr.countItems();
317 if (items < 1) {
318 return 0;
319 }
320 Cmdr.getFloat(sample_rate);
321 bhy.configure(BoschSensorID::HUMIDITY, sample_rate, 0);
322 return 0;
323}
324
325bool setPressure(Commander &Cmdr)
326{
327 float sample_rate;
328 int items = Cmdr.countItems();
329 if (items < 1) {
330 return 0;
331 }
332 Cmdr.getFloat(sample_rate);
333 bhy.configure(BoschSensorID::BAROMETER, sample_rate, 0);
334 return 0;
335}
336
337
338/*
339* GPIO Comparison Table
340* M1SCX = N.A ! INVALID PIN
341* M1SDX = N.A ! INVALID PIN
342* M1SDI = N.A ! INVALID PIN
343* M2SCX = 14 ! OK
344* M2SDX = 15 ! OK
345* M2SDI = 16 ! OK
346* MCSB1 = 1 ! OK
347* MCSB2 = 4 ! aux BMM150
348* M3SCL = 17 ! aux BMM150
349* M3SDA = 18 ! aux BMM150
350* MCSB3 = 5 ! OK
351* MCSB4 = 6 ! OK
352* JTAG_CLK = 19 ! OK
353* JTAG_DIO = 20 ! OK
354* RESV1 = 2 ! INVALID PIN
355* RESV2 = 3 ! INVALID PIN
356* RESV3 = N.A ! INVALID PIN
357* */
358bool setGpioLevel(Commander &Cmdr)
359{
360 int values[2] = {0, 0};
361 int items = Cmdr.countItems();
362 if (items < 2) {
363 return false;
364 }
365 for (int n = 0; n < 2; n++) {
366 Cmdr.getInt(values[n]);
367 }
368 uint8_t pin = values[0];
369 uint8_t level = values[1];
370 bhy.digitalWrite(pin, level);
371 return 0;
372}
373
374bool getGpioLevel(Commander &Cmdr)
375{
376 int values[2] = {0, 0};
377 int items = Cmdr.countItems();
378 if (items < 1) {
379 return 0;
380 }
381 if (items > 2 )items = 2;
382 for (int n = 0; n < items; n++) {
383 Cmdr.getInt(values[n]);
384 }
385 bool pullup = false;
386 uint8_t pin = values[0];
387 if (items == 2 ) {
388 pullup = values[1];
389 }
390 uint8_t level = bhy.digitalRead(pin, pullup);
391 Serial.print("Get GPIO : "); Serial.print(pin);
392 Serial.print(" level is "); Serial.println(level);
393 return 0;
394}
395
396bool disGpioMode(Commander &Cmdr)
397{
398 int values[1] = {0};
399 int items = Cmdr.countItems();
400 if (items < 1) {
401 return 0;
402 }
403 Cmdr.getInt(values[0]);
404 uint8_t pin = values[0];
405 bhy.disableGpio(pin);
406 return 0;
407}
@license MIT License
@license MIT License
uint8_t level
SensorTemperature temperature(bhy)
SensorPressure pressure(bhy)
SensorHumidity humidity(bhy)
bool helpHandler(Commander &Cmdr)
SensorBHI260AP bhy
bool getGpioLevel(Commander &Cmdr)
bool setTemperature(Commander &Cmdr)
bool disGpioMode(Commander &Cmdr)
bool setHumidity(Commander &Cmdr)
volatile bool isInterruptTriggered
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
bool setPressure(Commander &Cmdr)
const commandList_t masterCommands[]
bool setGpioLevel(Commander &Cmdr)
void parse_bme280_sensor_data(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
void digitalWrite(uint8_t pin, uint8_t level)
digitalWrite
uint8_t digitalRead(uint8_t pin, bool pullup=false)
digitalRead
void disableGpio(uint8_t pin)
disableGpio
void setBootFromFlash(bool boot_from_flash)
setBootFromFlash