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