SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_activity.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// #define USE_I2C_INTERFACE true
36// #define USE_SPI_INTERFACE true
37
38#if !defined(USE_I2C_INTERFACE) && !defined(USE_SPI_INTERFACE)
39#define USE_I2C_INTERFACE
40#warning "No interface type is selected, use I2C interface"
41#endif
42
43#if defined(USE_SPI_INTERFACE)
44#ifndef SPI_MOSI
45#define SPI_MOSI 33
46#endif
47
48#ifndef SPI_MISO
49#define SPI_MISO 34
50#endif
51
52#ifndef SPI_SCK
53#define SPI_SCK 35
54#endif
55
56// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
57#ifndef BHI260_IRQ
58#define BHI260_IRQ 37
59#endif
60
61#ifndef BHI260_CS
62#define BHI260_CS 36
63#endif
64
65#else //* I2C */
66
67#ifndef BHI260_SDA
68#define BHI260_SDA 2
69#endif
70
71#ifndef BHI260_SCL
72#define BHI260_SCL 3
73#endif
74
75// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
76#ifndef BHI260_IRQ
77#define BHI260_IRQ 8
78#endif
79#endif /*USE_SPI_INTERFACE*/
80
81#ifndef BHI260_RST
82#define BHI260_RST -1
83#endif
84
86SensorActivity activity(bhy);
87
88// 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.
89#define BOSCH_APP30_SHUTTLE_BHI260_FW
90// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150FW
91// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X
92// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390
93// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO
94// #define BOSCH_BHI260_AUX_BEM280
95// #define BOSCH_BHI260_AUX_BMM150_BEM280
96// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO
97// #define BOSCH_BHI260_AUX_BMM150_GPIO
98// #define BOSCH_BHI260_GPIO
99
100// Firmware is stored in flash and booted from flash,Depends on BHI260 hardware connected to SPI Flash
101// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150_FLASH
102// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X_FLASH
103// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390_FLASH
104// #define BOSCH_APP30_SHUTTLE_BHI260_FLASH
105// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO_FLASH
106// #define BOSCH_BHI260_AUX_BEM280_FLASH
107// #define BOSCH_BHI260_AUX_BMM150_BEM280_FLASH
108// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO_FLASH
109// #define BOSCH_BHI260_AUX_BMM150_GPIO_FLASH
110// #define BOSCH_BHI260_GPIO_FLASH
111
112#include <BoschFirmware.h>
113
114// Force update of current firmware, whether it exists or not.
115// Only works when external SPI Flash is connected to BHI260.
116// After uploading firmware once, you can change this to false to speed up boot time.
118
119#if BHI260_IRQ > 0
120#define USING_SENSOR_IRQ_METHOD
121#endif
122
123#ifdef USING_SENSOR_IRQ_METHOD
124volatile bool isInterruptTriggered = false;
125
127{
129}
130#endif /*USING_SENSOR_IRQ_METHOD*/
131
132// Firmware update progress callback
133void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
134{
135 float progress = (float)transferred / total * 100;
136 Serial.print("Upload progress: ");
137 Serial.print(progress);
138 Serial.println("%");
139}
140
141void setup()
142{
143 Serial.begin(115200);
144 while (!Serial);
145
146 // Set the reset pin
147 bhy.setPins(BHI260_RST);
148
149 Serial.println("Initializing Sensors...");
150
151 // Set the firmware array address and firmware size
152 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size, bosch_firmware_type, force_update_flash_firmware);
153
154 // Set the firmware update processing progress callback function
155 // bhy.setUpdateProcessCallback(progress_callback, NULL);
156
157 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
158 // bhy.setMaxiTransferSize(256);
159
160 // Set the processing fifo data buffer size,The default size is 512 bytes.
161 // bhy.setProcessBufferSize(1024);
162
163 // Set to load firmware from flash
164 bhy.setBootFromFlash(bosch_firmware_type);
165
166 Serial.println("Initializing Sensors...");
167
168#ifdef USE_I2C_INTERFACE
169 // Using I2C interface
170 // BHI260AP_SLAVE_ADDRESS_L = 0x28
171 // BHI260AP_SLAVE_ADDRESS_H = 0x29
172 if (!bhy.begin(Wire, BHI260AP_SLAVE_ADDRESS_L, BHI260_SDA, BHI260_SCL)) {
173 Serial.print("Failed to initialize sensor - error code:");
174 Serial.println(bhy.getError());
175 while (1) {
176 delay(1000);
177 }
178 }
179#endif
180
181#ifdef USE_SPI_INTERFACE
182 // Using SPI interface
183 if (!bhy.begin(SPI, BHI260_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
184 Serial.print("Failed to initialize sensor - error code:");
185 Serial.println(bhy.getError());
186 while (1) {
187 delay(1000);
188 }
189 }
190#endif
191
192 Serial.println("Initializing the sensor successfully!");
193
194 // Output all sensors info to Serial
195 BoschSensorInfo info = bhy.getSensorInfo();
196
197#ifdef ARDUINO
198 ArduinoStreamPrinter printer(Serial);
199 info.printInfo(printer);
200#else
201 info.printInfo([](const char *format, ...) -> int {
202 va_list args;
203 va_start(args, format);
204 int result = vprintf(format, args);
205 va_end(args);
206 return result;
207 });
208#endif
209
210 float sample_rate = 1.0;
211 uint32_t report_latency_ms = 0;
223 activity.enable(sample_rate, report_latency_ms);
224
225#ifdef USING_SENSOR_IRQ_METHOD
226 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
227 // This makes the pin ready to receive external signals.
228 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
229 pinMode(BHI260_IRQ, INPUT);
230
231 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
232 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
233 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
234#endif
235}
236
237
238void loop()
239{
240
241 if (activity.hasUpdated()) {
242
243 if (activity.isActivitySet(SensorActivity::STILL_ACTIVITY_ENDED)) {
244 Serial.println("Still activity ended is set.");
245 }
246 if (activity.isActivitySet(SensorActivity::WALKING_ACTIVITY_ENDED)) {
247 Serial.println("Walking activity ended is set.");
248 }
249 if (activity.isActivitySet(SensorActivity::RUNNING_ACTIVITY_ENDED)) {
250 Serial.println("Running activity ended is set.");
251 }
252 if (activity.isActivitySet(SensorActivity::ON_BICYCLE_ACTIVITY_ENDED)) {
253 Serial.println("On Bicycle activity ended is set.");
254 }
255 if (activity.isActivitySet(SensorActivity::IN_VEHICLE_ACTIVITY_ENDED)) {
256 Serial.println("In Vehicle activity ended is set.");
257 }
258 if (activity.isActivitySet(SensorActivity::TILTING_ACTIVITY_ENDED)) {
259 Serial.println("Tilting activity ended is set.");
260 }
261 if (activity.isActivitySet(SensorActivity::IN_VEHICLE_STILL_ENDED)) {
262 Serial.println("In Vehicle still ended is set.");
263 }
264 if (activity.isActivitySet(SensorActivity::STILL_ACTIVITY_STARTED)) {
265 Serial.println("Still activity started is set.");
266 }
267 if (activity.isActivitySet(SensorActivity::WALKING_ACTIVITY_STARTED)) {
268 Serial.println("Walking activity started is set.");
269 }
270 if (activity.isActivitySet(SensorActivity::RUNNING_ACTIVITY_STARTED)) {
271 Serial.println("Running activity started is set.");
272 }
273 if (activity.isActivitySet(SensorActivity::ON_BICYCLE_ACTIVITY_STARTED)) {
274 Serial.println("On Bicycle activity started is set.");
275 }
276 if (activity.isActivitySet(SensorActivity::IN_VEHICLE_ACTIVITY_STARTED)) {
277 Serial.println("In Vehicle activity started is set.");
278 }
279 if (activity.isActivitySet(SensorActivity::TILTING_ACTIVITY_STARTED)) {
280 Serial.println("Tilting activity started is set.");
281 }
282 if (activity.isActivitySet(SensorActivity::IN_VEHICLE_STILL_STARTED)) {
283 Serial.println("In Vehicle still started is set.");
284 }
285 }
286
287#ifdef USING_SENSOR_IRQ_METHOD
289 isInterruptTriggered = false;
290#endif /*USING_SENSOR_IRQ_METHOD*/
291
292 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
293 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
294 */
295 bhy.update();
296
297#ifdef USING_SENSOR_IRQ_METHOD
298 }
299#endif /*USING_SENSOR_IRQ_METHOD*/
300
301 delay(50);
302}
303
304
305
@license MIT License
@license MIT License
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
SensorActivity activity(bhy)
void loop()
void setBootFromFlash(bool boot_from_flash)
setBootFromFlash