SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi360_no_motion.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 BHI360_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
58#ifndef BHI360_IRQ
59#define BHI360_IRQ 37
60#endif
61
62#ifndef BHI360_CS
63#define BHI360_CS 36
64#endif
65
66#else //* I2C */
67
68#ifndef BHI360_SDA
69#define BHI360_SDA 2
70#endif
71
72#ifndef BHI360_SCL
73#define BHI360_SCL 3
74#endif
75
76// If BHI360_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
77#ifndef BHI360_IRQ
78#define BHI360_IRQ 8
79#endif
80#endif /*USE_SPI_INTERFACE*/
81
82#ifndef BHI360_RST
83#define BHI360_RST -1
84#endif
85
87SensorNoMotion noMotion(bhy);
88
89// #define BOSCH_DATA_INJECT_BHI360
90// #define BOSCH_DATA_INJECT_BHI360_BMM150
91// #define BOSCH_DATA_INJECT_BHI360_BMM150_HEAD_ORIENTATION
92// #define BOSCH_DATA_INJECT_BHI360_BMM350
93// #define BOSCH_DATA_INJECT_BHI360_BMM350_HEAD_ORIENTATION
94// #define BOSCH_DATA_INJECT_BHI360_ENV
95// #define BOSCH_DATA_INJECT_BHI360_HEAD_ORIENTATION
96// #define BOSCH_DATA_INJECT_BHI360_MOTION_AI
97#define BOSCH_SHUTTLE3_BHI360
98// #define BOSCH_SHUTTLE3_BHI360_AUX_BMM150
99// #define BOSCH_SHUTTLE3_BHI360_BMM150
100// #define BOSCH_SHUTTLE3_BHI360_BMM150_BMP580_BME688
101// #define BOSCH_SHUTTLE3_BHI360_BMM150_HEAD_ORIENTATION
102// #define BOSCH_SHUTTLE3_BHI360_BMM350C
103// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BME688_IAQ
104// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BMP580
105// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BMP580_BME688
106// #define BOSCH_SHUTTLE3_BHI360_BMM350C_HEAD_ORIENTATION
107// #define BOSCH_SHUTTLE3_BHI360_BMM350C_POLL
108// #define BOSCH_SHUTTLE3_BHI360_BMM350C_TURBO
109// #define BOSCH_SHUTTLE3_BHI360_BMP580_TEST_EXAMPLE
110// #define BOSCH_SHUTTLE3_BHI360_HW_ACTIVITY
111// #define BOSCH_SHUTTLE3_BHI360_HW_ACTIVITY_TURBO
112// #define BOSCH_SHUTTLE3_BHI360_IMU_HEAD_ORIENTATION
113// #define BOSCH_SHUTTLE3_BHI360_POLLING_STEP_COUNTER
114// #define BOSCH_SHUTTLE3_BHI360_TEMP
115// #define BOSCH_SHUTTLE3_BHI360_TEST_DATA_SOURCE
116// #define BOSCH_SHUTTLE3_BHI360_TURBO
117
118// The selected firmware for running the BHI360 is included by including `#include <BoschFirmware.h>`.
119#include <BoschFirmware.h>
120
121
122#if BHI360_IRQ > 0
123#define USING_SENSOR_IRQ_METHOD
124#endif
125
126#ifdef USING_SENSOR_IRQ_METHOD
127volatile bool isInterruptTriggered = false;
128
130{
132}
133#endif /*USING_SENSOR_IRQ_METHOD*/
134
135
136// Firmware update progress callback
137void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
138{
139 float progress = (float)transferred / total * 100;
140 Serial.print("Upload progress: ");
141 Serial.print(progress);
142 Serial.println("%");
143}
144
145
146void setup()
147{
148 Serial.begin(115200);
149 while (!Serial);
150
151 // Set the reset pin
152 bhy.setPins(BHI360_RST);
153
154 Serial.println("Initializing Sensors...");
155
156 // Set the firmware array address and firmware size
157 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size);
158
159 // Set the firmware update processing progress callback function
160 // bhy.setUpdateProcessCallback(progress_callback, NULL);
161
162 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
163 // bhy.setMaxiTransferSize(256);
164
165 // Set the processing fifo data buffer size,The default size is 512 bytes.
166 // bhy.setProcessBufferSize(1024);
167
168#ifdef USE_I2C_INTERFACE
169 // Using I2C interface
170 // BHI360_SLAVE_ADDRESS_L = 0x28
171 // BHI360_SLAVE_ADDRESS_H = 0x29
172 if (!bhy.begin(Wire, BHI360_SLAVE_ADDRESS_L, BHI360_SDA, BHI360_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, BHI360_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#ifdef USING_SENSOR_IRQ_METHOD
195 // Set the specified pin (BHI360_IRQ) ​​to an input pin.
196 // This makes the pin ready to receive external signals.
197 // If the interrupt is already connected, if BHI360_IRQ is equal to -1 then the polling method will be used
198 pinMode(BHI360_IRQ, INPUT);
199
200 // Attach an interrupt service routine (ISR) to the specified pin (BHI360_IRQ).
201 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
202 attachInterrupt(BHI360_IRQ, dataReadyISR, RISING);
203#endif
204
205 // Enable no motion detection
206 // A single trigger can be performed by passing MotionMode::ONCE,
207 // noMotion.enable(MotionMode::ONCE);
208
209 // Continuous triggering can be achieved by passing MotionMode::CONTINUOUS.
210 noMotion.enable(MotionMode::CONTINUOUS);
211
212 // If no action is detected, the callback function will be triggered.
213 noMotion.setOnMotionCallback([]() {
214 Serial.print("[");
215 Serial.print(millis());
216 Serial.print("] ");
217 Serial.print("No motion detected.");
218 Serial.print(" state :");
219 Serial.println(noMotion.isEnabled() ? "continuous mode" : "disabled");
220 });
221
222 Serial.println("Sensor is ready, please keep the sensor stationary.");
223}
224
225
226void loop()
227{
228#ifdef USING_SENSOR_IRQ_METHOD
230 isInterruptTriggered = false;
231#endif /*USING_SENSOR_IRQ_METHOD*/
232
233 /* If the interrupt is connected to the sensor and BHI360_IRQ is not equal to -1,
234 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
235 */
236 bhy.update();
237
238#ifdef USING_SENSOR_IRQ_METHOD
239 }
240#endif /*USING_SENSOR_IRQ_METHOD*/
241
242
243 delay(50);
244}
@license MIT License
@license MIT License
SensorNoMotion noMotion(bhy)
SensorBHI360 bhy
#define BHI360_SCL
void setup()
#define BHI360_SDA
void dataReadyISR()
volatile bool isInterruptTriggered
#define BHI360_RST
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
#define BHI360_IRQ
void loop()