SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_orientation.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// #define USE_I2C_INTERFACE true
37// #define USE_SPI_INTERFACE true
38
39#if !defined(USE_I2C_INTERFACE) && !defined(USE_SPI_INTERFACE)
40#define USE_I2C_INTERFACE
41#warning "No interface type is selected, use I2C interface"
42#endif
43
44#if defined(USE_SPI_INTERFACE)
45#ifndef SPI_MOSI
46#define SPI_MOSI 33
47#endif
48
49#ifndef SPI_MISO
50#define SPI_MISO 34
51#endif
52
53#ifndef SPI_SCK
54#define SPI_SCK 35
55#endif
56
57// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
58#ifndef BHI260_IRQ
59#define BHI260_IRQ 37
60#endif
61
62#ifndef BHI260_CS
63#define BHI260_CS 36
64#endif
65
66#else //* I2C */
67
68#ifndef BHI260_SDA
69#define BHI260_SDA 2
70#endif
71
72#ifndef BHI260_SCL
73#define BHI260_SCL 3
74#endif
75
76// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
77#ifndef BHI260_IRQ
78#define BHI260_IRQ 8
79#endif
80#endif /*USE_SPI_INTERFACE*/
81
82#ifndef BHI260_RST
83#define BHI260_RST -1
84#endif
85
87
88/*
89* Define the USING_DATA_HELPER use of data assistants.
90* No callback function will be used. Data can be obtained directly through
91* the data assistant. Note that this method is not a thread-safe function.
92* Please pay attention to protecting data access security.
93* */
94#define USING_DATA_HELPER
95
96#ifdef USING_DATA_HELPER
97SensorOrientation orientation(bhy);
98#endif
99
100
101// 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.
102#define BOSCH_APP30_SHUTTLE_BHI260_FW
103// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150FW
104// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X
105// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390
106// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO
107// #define BOSCH_BHI260_AUX_BEM280
108// #define BOSCH_BHI260_AUX_BMM150_BEM280
109// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO
110// #define BOSCH_BHI260_AUX_BMM150_GPIO
111// #define BOSCH_BHI260_GPIO
112
113// Firmware is stored in flash and booted from flash,Depends on BHI260 hardware connected to SPI Flash
114// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150_FLASH
115// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X_FLASH
116// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390_FLASH
117// #define BOSCH_APP30_SHUTTLE_BHI260_FLASH
118// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO_FLASH
119// #define BOSCH_BHI260_AUX_BEM280_FLASH
120// #define BOSCH_BHI260_AUX_BMM150_BEM280_FLASH
121// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO_FLASH
122// #define BOSCH_BHI260_AUX_BMM150_GPIO_FLASH
123// #define BOSCH_BHI260_GPIO_FLASH
124
125#include <BoschFirmware.h>
126
127// Force update of current firmware, whether it exists or not.
128// Only works when external SPI Flash is connected to BHI260.
129// After uploading firmware once, you can change this to false to speed up boot time.
131
132#if BHI260_IRQ > 0
133#define USING_SENSOR_IRQ_METHOD
134#endif
135
136#ifdef USING_SENSOR_IRQ_METHOD
137volatile bool isInterruptTriggered = false;
138
140{
142}
143#endif /*USING_SENSOR_IRQ_METHOD*/
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 print_orientation(uint8_t direction)
155{
156 char report[256];
157 switch (direction) {
159 sprintf( report, "\r\n ________________ " \
160 "\r\n | | " \
161 "\r\n | | " \
162 "\r\n | | " \
163 "\r\n | | " \
164 "\r\n | | " \
165 "\r\n | * | " \
166 "\r\n |________________| \r\n" );
167
168 break;
170 sprintf( report, "\r\n ________________ " \
171 "\r\n | | " \
172 "\r\n | * | " \
173 "\r\n | | " \
174 "\r\n | | " \
175 "\r\n | | " \
176 "\r\n | | " \
177 "\r\n |________________| \r\n" );
178 break;
180 sprintf( report, "\r\n ________________ " \
181 "\r\n | | " \
182 "\r\n | * | " \
183 "\r\n | | " \
184 "\r\n | | " \
185 "\r\n | | " \
186 "\r\n | | " \
187 "\r\n |________________| \r\n" );
188 break;
190 sprintf( report, "\r\n ________________ " \
191 "\r\n | | " \
192 "\r\n | | " \
193 "\r\n | | " \
194 "\r\n | | " \
195 "\r\n | | " \
196 "\r\n | * | " \
197 "\r\n |________________| \r\n" );
198 break;
199 default:
200 sprintf( report, "None of the 3D orientation axes is set in BHI260 - accelerometer.\r\n" );
201 break;
202 }
203
204 Serial.println(direction);
205 Serial.println(report);
206}
207
208#ifndef USING_DATA_HELPER
209void orientation_process_callback(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
210{
211 uint8_t direction = *data_ptr;
212 Serial.print(bhy.getSensorName(sensor_id));
213 Serial.print(":");
214 print_orientation(direction);
215}
216#endif
217
218void setup()
219{
220 Serial.begin(115200);
221 while (!Serial);
222
223 // Set the reset pin
224 bhy.setPins(BHI260_RST);
225
226 Serial.println("Initializing Sensors...");
227
228 // Set the firmware array address and firmware size
229 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size, bosch_firmware_type, force_update_flash_firmware);
230
231 // Set the firmware update processing progress callback function
232 // bhy.setUpdateProcessCallback(progress_callback, NULL);
233
234 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
235 // bhy.setMaxiTransferSize(256);
236
237 // Set the processing fifo data buffer size,The default size is 512 bytes.
238 // bhy.setProcessBufferSize(1024);
239
240 // Set to load firmware from flash
241 bhy.setBootFromFlash(bosch_firmware_type);
242
243#ifdef USE_I2C_INTERFACE
244 // Using I2C interface
245 // BHI260AP_SLAVE_ADDRESS_L = 0x28
246 // BHI260AP_SLAVE_ADDRESS_H = 0x29
247 if (!bhy.begin(Wire, BHI260AP_SLAVE_ADDRESS_L, BHI260_SDA, BHI260_SCL)) {
248 Serial.print("Failed to initialize sensor - error code:");
249 Serial.println(bhy.getError());
250 while (1) {
251 delay(1000);
252 }
253 }
254#endif
255
256#ifdef USE_SPI_INTERFACE
257 // Using SPI interface
258 if (!bhy.begin(SPI, BHI260_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
259 Serial.print("Failed to initialize sensor - error code:");
260 Serial.println(bhy.getError());
261 while (1) {
262 delay(1000);
263 }
264 }
265#endif
266
267 Serial.println("Init BHI260AP Sensor success!");
268
269 // Output all sensors info to Serial
270 BoschSensorInfo info = bhy.getSensorInfo();
271
272#ifdef ARDUINO
273 ArduinoStreamPrinter printer(Serial);
274 info.printInfo(printer);
275#else
276 info.printInfo([](const char *format, ...) -> int {
277 va_list args;
278 va_start(args, format);
279 int result = vprintf(format, args);
280 va_end(args);
281 return result;
282 });
283#endif
284
285 // The orientation sensor will only report when it changes, so the value is 0 ~ 1
286 float sample_rate = 1;
287 uint32_t report_latency_ms = 0; /* Report immediately */
288
289#ifdef USING_DATA_HELPER
290 orientation.enable(sample_rate, report_latency_ms);
291#else
292 // Enable direction detection
293 bhy.configure(BoschSensorID::DEVICE_ORIENTATION, sample_rate, report_latency_ms);
294 // Set the direction detection result output processing function
295 bhy.onResultEvent(BoschSensorID::DEVICE_ORIENTATION, orientation_process_callback);
296#endif
297
298#ifdef USING_SENSOR_IRQ_METHOD
299 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
300 // This makes the pin ready to receive external signals.
301 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
302 pinMode(BHI260_IRQ, INPUT);
303
304 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
305 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
306 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
307#endif
308}
309
310
311void loop()
312{
313#ifdef USING_SENSOR_IRQ_METHOD
315 isInterruptTriggered = false;
316#endif /*USING_SENSOR_IRQ_METHOD*/
317
318 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
319 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
320 */
321 bhy.update();
322
323#ifdef USING_SENSOR_IRQ_METHOD
324 }
325#endif /*USING_SENSOR_IRQ_METHOD*/
326
327#ifdef USING_DATA_HELPER
328 if (orientation.hasUpdated()) {
329 print_orientation(orientation.getOrientation());
330 }
331#endif
332 delay(50);
333}
334
@license MIT License
@license MIT License
@ DIRECTION_TOP_RIGHT
@ DIRECTION_TOP_LEFT
@ DIRECTION_BOTTOM_LEFT
@ DIRECTION_BOTTOM_RIGHT
SensorBHI260AP bhy
SensorOrientation orientation(bhy)
#define BHI260_SCL
void setup()
void dataReadyISR()
volatile bool isInterruptTriggered
void print_orientation(uint8_t direction)
#define BHI260_IRQ
#define BHI260_SDA
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
bool force_update_flash_firmware
#define BHI260_RST
void loop()
void setBootFromFlash(bool boot_from_flash)
setBootFromFlash