SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_tilt_detector.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
97SensorStepCounter stepCounter(bhy);
98SensorStepDetector stepDetector(bhy);
99#endif
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#ifndef USING_DATA_HELPER
146void tilt_detector_process_callback(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
147{
148 Serial.print(bhy.getSensorName(sensor_id));
149 Serial.println(" detected.");
150}
151#endif
152
153// Firmware update progress callback
154void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
155{
156 float progress = (float)transferred / total * 100;
157 Serial.print("Upload progress: ");
158 Serial.print(progress);
159 Serial.println("%");
160}
161
162void setup()
163{
164 Serial.begin(115200);
165 while (!Serial);
166
167 // Set the reset pin
168 bhy.setPins(BHI260_RST);
169
170 Serial.println("Initializing Sensors...");
171
172 // Set the firmware array address and firmware size
173 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size, bosch_firmware_type, force_update_flash_firmware);
174
175 // Set the firmware update processing progress callback function
176 // bhy.setUpdateProcessCallback(progress_callback, NULL);
177
178 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
179 // bhy.setMaxiTransferSize(256);
180
181 // Set the processing fifo data buffer size,The default size is 512 bytes.
182 // bhy.setProcessBufferSize(1024);
183
184 // Set to load firmware from flash
185 bhy.setBootFromFlash(bosch_firmware_type);
186
187 Serial.println("Initializing Sensors...");
188
189#ifdef USE_I2C_INTERFACE
190 // Using I2C interface
191 // BHI260AP_SLAVE_ADDRESS_L = 0x28
192 // BHI260AP_SLAVE_ADDRESS_H = 0x29
193 if (!bhy.begin(Wire, BHI260AP_SLAVE_ADDRESS_L, BHI260_SDA, BHI260_SCL)) {
194 Serial.print("Failed to initialize sensor - error code:");
195 Serial.println(bhy.getError());
196 while (1) {
197 delay(1000);
198 }
199 }
200#endif
201
202#ifdef USE_SPI_INTERFACE
203 // Using SPI interface
204 if (!bhy.begin(SPI, BHI260_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
205 Serial.print("Failed to initialize sensor - error code:");
206 Serial.println(bhy.getError());
207 while (1) {
208 delay(1000);
209 }
210 }
211#endif
212
213 Serial.println("Initializing the sensor successfully!");
214
215 // Output all sensors info to Serial
216 BoschSensorInfo info = bhy.getSensorInfo();
217
218#ifdef ARDUINO
219 ArduinoStreamPrinter printer(Serial);
220 info.printInfo(printer);
221#else
222 info.printInfo([](const char *format, ...) -> int {
223 va_list args;
224 va_start(args, format);
225 int result = vprintf(format, args);
226 va_end(args);
227 return result;
228 });
229#endif
230
231 // The stepcount sensor will only report when it changes, so the value is 0 ~ 1
232 float sample_rate = 1.0;
233 uint32_t report_latency_ms = 0; /* Report immediately */
234
235#ifdef USING_DATA_HELPER
236 stepDetector.enable(sample_rate, report_latency_ms);
237 stepCounter.enable(sample_rate, report_latency_ms);
238#else
239 // Enable Step detector
240 bhy.configure(BoschSensorID::TILT_DETECTOR, sample_rate, report_latency_ms);
241 bhy.onResultEvent(BoschSensorID::TILT_DETECTOR, tilt_detector_process_callback);
242#endif
243
244#ifdef USING_SENSOR_IRQ_METHOD
245 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
246 // This makes the pin ready to receive external signals.
247 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
248 pinMode(BHI260_IRQ, INPUT);
249
250 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
251 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
252 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
253#endif
254}
255
256
257void loop()
258{
259 uint32_t s;
260 uint32_t ns;
261
262#ifdef USING_SENSOR_IRQ_METHOD
264 isInterruptTriggered = false;
265#endif /*USING_SENSOR_IRQ_METHOD*/
266
267 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
268 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
269 */
270 bhy.update();
271
272#ifdef USING_SENSOR_IRQ_METHOD
273 }
274#endif /*USING_SENSOR_IRQ_METHOD*/
275
276#ifdef USING_DATA_HELPER
277 if (stepCounter.hasUpdated()) {
278 stepCounter.getLastTime(s, ns);
279#ifdef PLATFORM_HAS_PRINTF
280 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "]: Step Count:", s, ns);
281#else
282 Serial.print("[T: ");
283 Serial.print(s);
284 Serial.print(".");
285 Serial.print(ns);
286 Serial.print("]: Step Count:");
287#endif
288 Serial.println(stepCounter.getStepCount());
289 }
290 if (stepDetector.hasUpdated()) {
291 if (stepDetector.isDetected()) {
292 stepDetector.getLastTime(s, ns);
293#ifdef PLATFORM_HAS_PRINTF
294 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "]:", s, ns);
295#else
296 Serial.print("[T: ");
297 Serial.print(s);
298 Serial.print(".");
299 Serial.print(ns);
300 Serial.print("]:");
301#endif
302 Serial.println("Step detector detects steps");
303 }
304 }
305#endif
306 delay(50);
307}
308
309
@license MIT License
@license MIT License
SensorStepDetector stepDetector(bhy)
SensorStepCounter stepCounter(bhy)
SensorBHI260AP bhy
#define BHI260_SCL
void dataReadyISR()
volatile bool isInterruptTriggered
#define BHI260_IRQ
#define BHI260_SDA
void tilt_detector_process_callback(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
bool force_update_flash_firmware
#define BHI260_RST
void setBootFromFlash(bool boot_from_flash)
setBootFromFlash