SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_multiple.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
36#ifdef ARDUINO_ARCH_ESP32
37
38#ifndef BHI260_SDA1
39#define BHI260_SDA1 3
40#endif
41
42#ifndef BHI260_SCL1
43#define BHI260_SCL1 2
44#endif
45
46#ifndef BHI260_IRQ1
47#define BHI260_IRQ1 1
48#endif
49
50#ifndef BHI260_RST1
51#define BHI260_RST1 -1
52#endif
53
54
55#ifndef BHI260_SDA2
56#define BHI260_SDA2 11
57#endif
58
59#ifndef BHI260_SCL2
60#define BHI260_SCL2 12
61#endif
62
63#ifndef BHI260_IRQ2
64#define BHI260_IRQ2 10
65#endif
66
67#ifndef BHI260_RST2
68#define BHI260_RST2 -1
69#endif
70
73
74/*
75* Define the USING_DATA_HELPER use of data assistants.
76* No callback function will be used. Data can be obtained directly through
77* the data assistant. Note that this method is not a thread-safe function.
78* Please pay attention to protecting data access security.
79* */
80#define USING_DATA_HELPER
81
82#ifdef USING_DATA_HELPER
83SensorOrientation orientation1(bhy1);
84SensorOrientation orientation2(bhy2);
85#endif
86
87// 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.
88#define BOSCH_APP30_SHUTTLE_BHI260_FW
89// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150FW
90// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X
91// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390
92// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO
93// #define BOSCH_BHI260_AUX_BEM280
94// #define BOSCH_BHI260_AUX_BMM150_BEM280
95// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO
96// #define BOSCH_BHI260_AUX_BMM150_GPIO
97// #define BOSCH_BHI260_GPIO
98
99// Firmware is stored in flash and booted from flash,Depends on BHI260 hardware connected to SPI Flash
100// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150_FLASH
101// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X_FLASH
102// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390_FLASH
103// #define BOSCH_APP30_SHUTTLE_BHI260_FLASH
104// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO_FLASH
105// #define BOSCH_BHI260_AUX_BEM280_FLASH
106// #define BOSCH_BHI260_AUX_BMM150_BEM280_FLASH
107// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO_FLASH
108// #define BOSCH_BHI260_AUX_BMM150_GPIO_FLASH
109// #define BOSCH_BHI260_GPIO_FLASH
110
111#include <BoschFirmware.h>
112
113// Force update of current firmware, whether it exists or not.
114// Only works when external SPI Flash is connected to BHI260.
115// After uploading firmware once, you can change this to false to speed up boot time.
116bool force_update_flash_firmware = false;
117
118bool isInterruptTriggered1 = false;
119bool isInterruptTriggered2 = false;
120
121void dataReady1_ISR()
122{
123 isInterruptTriggered1 = true;
124}
125
126void dataReady2_ISR()
127{
128 isInterruptTriggered2 = true;
129}
130
131void print_orientation(uint8_t direction)
132{
133 char report[256];
134 switch (direction) {
136 sprintf( report, "\r\n ________________ " \
137 "\r\n | | " \
138 "\r\n | | " \
139 "\r\n | | " \
140 "\r\n | | " \
141 "\r\n | | " \
142 "\r\n | * | " \
143 "\r\n |________________| \r\n" );
144
145 break;
147 sprintf( report, "\r\n ________________ " \
148 "\r\n | | " \
149 "\r\n | * | " \
150 "\r\n | | " \
151 "\r\n | | " \
152 "\r\n | | " \
153 "\r\n | | " \
154 "\r\n |________________| \r\n" );
155 break;
157 sprintf( report, "\r\n ________________ " \
158 "\r\n | | " \
159 "\r\n | * | " \
160 "\r\n | | " \
161 "\r\n | | " \
162 "\r\n | | " \
163 "\r\n | | " \
164 "\r\n |________________| \r\n" );
165 break;
167 sprintf( report, "\r\n ________________ " \
168 "\r\n | | " \
169 "\r\n | | " \
170 "\r\n | | " \
171 "\r\n | | " \
172 "\r\n | | " \
173 "\r\n | * | " \
174 "\r\n |________________| \r\n" );
175 break;
176 default:
177 sprintf( report, "None of the 3D orientation axes is set in BHI260 - accelerometer.\r\n" );
178 break;
179 }
180
181 Serial.println(direction);
182 Serial.println(report);
183}
184
185#ifndef USING_DATA_HELPER
186void orientation_process_callback(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
187{
188 uint8_t direction = *data_ptr;
189 if (user_data) {
190 Serial.println((const char *)user_data);
191 }
192 print_orientation(direction);
193}
194#endif
195
196// Firmware update progress callback
197void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
198{
199 float progress = (float)transferred / total * 100;
200 Serial.print("Upload progress: ");
201 Serial.print(progress);
202 Serial.println("%");
203}
204
205
206void initSensor(SensorBHI260AP &sensor, int rst, int sda, int scl, int irq,
207 TwoWire &wire, void(*interrupts)(),
208 void *user_data
210 , SensorOrientation &orientation
211#endif
212 )
213{
214 // Set the reset pin
215 sensor.setPins(rst);
216
217 Serial.println("Initializing Sensors...");
218
219 // Set the firmware array address and firmware size
220 sensor.setFirmware(bosch_firmware_image, bosch_firmware_size, bosch_firmware_type, force_update_flash_firmware);
221
222 // Set the firmware update processing progress callback function
223 // sensor.setUpdateProcessCallback(progress_callback, NULL);
224
225 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
226 // sensor.setMaxiTransferSize(256);
227
228 // Set the processing fifo data buffer size,The default size is 512 bytes.
229 // sensor.setProcessBufferSize(1024);
230
231 // Set to load firmware from flash
232 sensor.setBootFromFlash(bosch_firmware_type);
233
234 // Using I2C interface
235 // BHI260AP_SLAVE_ADDRESS_L = 0x28
236 // BHI260AP_SLAVE_ADDRESS_H = 0x29
237 if (!sensor.begin(wire, BHI260AP_SLAVE_ADDRESS_L, sda, scl)) {
238 Serial.print("Failed to initialize sensor - error code:");
239 Serial.println(sensor.getError());
240 while (1) {
241 delay(1000);
242 }
243 }
244
245 Serial.println("Initializing the sensor successfully!");
246
247 // Output all sensors info to Serial
248 BoschSensorInfo info = sensor.getSensorInfo();
249
250#ifdef ARDUINO
251 ArduinoStreamPrinter printer(Serial);
252 info.printInfo(printer);
253#else
254 info.printInfo([](const char *format, ...) -> int {
255 va_list args;
256 va_start(args, format);
257 int result = vprintf(format, args);
258 va_end(args);
259 return result;
260 });
261#endif
262
263 // The orientation sensor will only report when it changes, so the value is 0 ~ 1
264 float sample_rate = 1;
265 uint32_t report_latency_ms = 0; /* Report immediately */
266
267#ifdef USING_DATA_HELPER
268 orientation.enable(sample_rate, report_latency_ms);
269#else
270 // Enable direction detection
271 sensor.configure(BoschSensorID::DEVICE_ORIENTATION, sample_rate, report_latency_ms);
272 // Set the direction detection result output processing function
273 sensor.onResultEvent(BoschSensorID::DEVICE_ORIENTATION, orientation_process_callback, user_data);
274#endif
275 // Set the specified pin (BHI260_IRQ1) as an input pin.
276 pinMode(irq, INPUT_PULLUP);
277 // Attach an interrupt service routine (ISR) 'dataReadyISR' to the specified pin (irq).
278 attachInterrupt(irq, interrupts, RISING);
279}
280
281void setup()
282{
283 Serial.begin(115200);
284 while (!Serial);
285 const char *sensor1_user_data = "Sensor1";
286 const char *sensor2_user_data = "Sensor2";
287
288 initSensor(bhy1, BHI260_RST1, BHI260_SDA1, BHI260_SCL1, BHI260_IRQ1, Wire, dataReady1_ISR, (void *)sensor1_user_data
290 , orientation1
291#endif
292 );
293 initSensor(bhy2, BHI260_RST2, BHI260_SDA2, BHI260_SCL2, BHI260_IRQ2, Wire1, dataReady2_ISR, (void *)sensor2_user_data
295 , orientation2
296#endif
297 );
298
299 Serial.println("All sensor init done!");
300}
301
302
303void loop()
304{
305 // Update sensor fifo
306 if (isInterruptTriggered1) {
307 isInterruptTriggered1 = false;
308 Serial.println("isInterruptTriggered1:");
309 bhy1.update();
310 }
311
312 if (isInterruptTriggered2) {
313 isInterruptTriggered2 = false;
314 Serial.println("isInterruptTriggered2:");
315 bhy2.update();
316 }
317
318#ifdef USING_DATA_HELPER
319 if (orientation1.hasUpdated()) {
320 print_orientation(orientation1.getOrientation());
321 }
322
323 if (orientation2.hasUpdated()) {
324 print_orientation(orientation2.getOrientation());
325 }
326#endif
327
328
329 delay(50);
330}
331
332#else
333void setup()
334{
335 Serial.begin(115200);
336}
337
338void loop()
339{
340 Serial.println("The example only support your esp32 platform"); delay(1000);
341}
342#endif /*ARDUINO_ARCH_ESP32*/
343
@license MIT License
@license MIT License
@ DIRECTION_TOP_RIGHT
@ DIRECTION_TOP_LEFT
@ DIRECTION_BOTTOM_LEFT
@ DIRECTION_BOTTOM_RIGHT
#define USING_DATA_HELPER
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
bool force_update_flash_firmware
void setup()
void loop()
SensorOrientation orientation(bhy)
void print_orientation(uint8_t direction)
SensorBMA4XX * sensor
bool begin(CommInterface interface, SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr=0)
Initialize the sensor using custom callback interface.
SensorInfo getSensorInfo() const
Get sensor information.