SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi360_multi_tap.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
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
97SensorMultiTap multiTap(bhy);
98#endif
99
100// #define BOSCH_DATA_INJECT_BHI360
101// #define BOSCH_DATA_INJECT_BHI360_BMM150
102// #define BOSCH_DATA_INJECT_BHI360_BMM150_HEAD_ORIENTATION
103// #define BOSCH_DATA_INJECT_BHI360_BMM350
104// #define BOSCH_DATA_INJECT_BHI360_BMM350_HEAD_ORIENTATION
105// #define BOSCH_DATA_INJECT_BHI360_ENV
106// #define BOSCH_DATA_INJECT_BHI360_HEAD_ORIENTATION
107// #define BOSCH_DATA_INJECT_BHI360_MOTION_AI
108#define BOSCH_SHUTTLE3_BHI360
109// #define BOSCH_SHUTTLE3_BHI360_AUX_BMM150
110// #define BOSCH_SHUTTLE3_BHI360_BMM150
111// #define BOSCH_SHUTTLE3_BHI360_BMM150_BMP580_BME688
112// #define BOSCH_SHUTTLE3_BHI360_BMM150_HEAD_ORIENTATION
113// #define BOSCH_SHUTTLE3_BHI360_BMM350C
114// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BME688_IAQ
115// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BMP580
116// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BMP580_BME688
117// #define BOSCH_SHUTTLE3_BHI360_BMM350C_HEAD_ORIENTATION
118// #define BOSCH_SHUTTLE3_BHI360_BMM350C_POLL
119// #define BOSCH_SHUTTLE3_BHI360_BMM350C_TURBO
120// #define BOSCH_SHUTTLE3_BHI360_BMP580_TEST_EXAMPLE
121// #define BOSCH_SHUTTLE3_BHI360_HW_ACTIVITY
122// #define BOSCH_SHUTTLE3_BHI360_HW_ACTIVITY_TURBO
123// #define BOSCH_SHUTTLE3_BHI360_IMU_HEAD_ORIENTATION
124// #define BOSCH_SHUTTLE3_BHI360_POLLING_STEP_COUNTER
125// #define BOSCH_SHUTTLE3_BHI360_TEMP
126// #define BOSCH_SHUTTLE3_BHI360_TEST_DATA_SOURCE
127// #define BOSCH_SHUTTLE3_BHI360_TURBO
128
129#include <BoschFirmware.h>
130
131// Force update of current firmware, whether it exists or not.
132// Only works when external SPI Flash is connected to BHI360.
133// After uploading firmware once, you can change this to false to speed up boot time.
135
136#if BHI360_IRQ > 0
137#define USING_SENSOR_IRQ_METHOD
138#endif
139
140#ifdef USING_SENSOR_IRQ_METHOD
141volatile bool isInterruptTriggered = false;
142
144{
146}
147#endif /*USING_SENSOR_IRQ_METHOD*/
148
149#ifndef USING_DATA_HELPER
150void parse_multitap(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
151{
152 MultiTapDataType multitap_data = BHI360_NO_TAP;
153 if (data_ptr == NULL) {
154 Serial.println("Data pointer is null");
155 return;
156 }
157 multitap_data = (MultiTapDataType)(*data_ptr);
158 Serial.print("SID: ");
159 Serial.print(sensor_id);
160 Serial.print("; T: ");
161 Serial.print((uint32_t)(*timestamp / 1000000000ULL));
162 Serial.print(".");
163 Serial.print((uint32_t)(*timestamp % 1000000000ULL));
164 Serial.print("; ");
165 Serial.print(bhi360_event_data_multi_tap_string_out[multitap_data]);
166 Serial.println("; ");
167}
168#endif
169
170// Firmware update progress callback
171void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
172{
173 float progress = (float)transferred / total * 100;
174 Serial.print("Upload progress: ");
175 Serial.print(progress);
176 Serial.println("%");
177}
178
179void setup()
180{
181 Serial.begin(115200);
182 while (!Serial);
183
184 // Set the reset pin
185 bhy.setPins(BHI360_RST);
186
187 Serial.println("Initializing Sensors...");
188
189 // Set the firmware array address and firmware size
190 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size);
191
192 // Set the firmware update processing progress callback function
193 // bhy.setUpdateProcessCallback(progress_callback, NULL);
194
195 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
196 // bhy.setMaxiTransferSize(256);
197
198 // Set the processing fifo data buffer size,The default size is 512 bytes.
199 // bhy.setProcessBufferSize(1024);
200
201 Serial.println("Initializing Sensors...");
202
203#ifdef USE_I2C_INTERFACE
204 // Using I2C interface
205 // BHI360_SLAVE_ADDRESS_L = 0x28
206 // BHI360_SLAVE_ADDRESS_H = 0x29
207 if (!bhy.begin(Wire, BHI360_SLAVE_ADDRESS_L, BHI360_SDA, BHI360_SCL)) {
208 Serial.print("Failed to initialize sensor - error code:");
209 Serial.println(bhy.getError());
210 while (1) {
211 delay(1000);
212 }
213 }
214#endif
215
216#ifdef USE_SPI_INTERFACE
217 // Using SPI interface
218 if (!bhy.begin(SPI, BHI360_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
219 Serial.print("Failed to initialize sensor - error code:");
220 Serial.println(bhy.getError());
221 while (1) {
222 delay(1000);
223 }
224 }
225#endif
226
227 Serial.println("Initializing the sensor successfully!");
228
229 // Output all sensors info to Serial
230 BoschSensorInfo info = bhy.getSensorInfo();
231
232#ifdef ARDUINO
233 ArduinoStreamPrinter printer(Serial);
234 info.printInfo(printer);
235#else
236 info.printInfo([](const char *format, ...) -> int {
237 va_list args;
238 va_start(args, format);
239 int result = vprintf(format, args);
240 va_end(args);
241 return result;
242 });
243#endif
244
245 // The tap detect sensor will only report when it changes, so the value is 0 ~ 1
246 float sample_rate = 1.0;
247 uint32_t report_latency_ms = 0; /* Report immediately */
248
249#ifdef USING_DATA_HELPER
250 multiTap.enable(sample_rate, report_latency_ms);
251#else
252 // Enable Multi Tap detector
253 bhy.configure(BoschSensorID::MULTI_TAP, sample_rate, report_latency_ms);
254 // Set the callback function for multi tap events
255 bhy.onResultEvent(BoschSensorID::MULTI_TAP, parse_multitap);
256#endif
257
258#ifdef USING_SENSOR_IRQ_METHOD
259 // Set the specified pin (BHI360_IRQ) ​​to an input pin.
260 // This makes the pin ready to receive external signals.
261 // If the interrupt is already connected, if BHI360_IRQ is equal to -1 then the polling method will be used
262 pinMode(BHI360_IRQ, INPUT);
263
264 // Attach an interrupt service routine (ISR) to the specified pin (BHI360_IRQ).
265 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
266 attachInterrupt(BHI360_IRQ, dataReadyISR, RISING);
267#endif
268
269 MultiTapDataType buffer[8] {};
270 MultiTapDataType multitap_setting = BHI360_TRIPLE_DOUBLE_SINGLE_TAP;
271 MultiTapDetectorConfig multitap_cnfg {};
272
273 Serial.println("--- Multi tap log start ---");
274
275 if (bhy.getMultiTapParamConfig(buffer)) {
276 Serial.print("Multi Tap Info : ");
277 Serial.println(bhi360_event_data_multi_tap_string_out[buffer[0]]);
278 } else {
279 Serial.println("Failed to get multi tap config");
280 }
281
282 if (bhy.setMultiTapParamConfig(multitap_setting)) {
283 Serial.println("Set multi tap config successfully");
284 } else {
285 Serial.println("Failed to set multi tap config");
286 }
287
288 if (bhy.getMultiTapDetectorConfig(multitap_cnfg)) {
289 Serial.print("Single Tap CNFG : ");
290 Serial.println(((multitap_setting & (uint8_t)BHI360_SINGLE_TAP) == (uint8_t)BHI360_SINGLE_TAP) ? "Enabled" : "Disabled");
291 Serial.print(" \t\t -<axis_sel> : ");
292 Serial.println(multitap_cnfg.stap_setting.as_s.axis_sel);
293 Serial.print(" \t\t -<wait_for_timeout> : ");
294 Serial.println(multitap_cnfg.stap_setting.as_s.wait_for_timeout);
295 Serial.print(" \t\t -<max_pks_for_tap> : ");
296 Serial.println(multitap_cnfg.stap_setting.as_s.max_peaks_for_tap);
297 Serial.print(" \t\t -<mode> : ");
298 Serial.println(multitap_cnfg.stap_setting.as_s.mode);
299 Serial.print("Double Tap CNFG : ");
300 Serial.println(((multitap_setting & (uint8_t)BHI360_DOUBLE_TAP) == (uint8_t)BHI360_DOUBLE_TAP) ? "Enabled" : "Disabled");
301 Serial.print(" \t\t -<tap_peak_thrs> : ");
302 Serial.println(multitap_cnfg.dtap_setting.as_s.tap_peak_thres);
303 Serial.print(" \t\t -<max_ges_dur> : ");
304 Serial.println(multitap_cnfg.dtap_setting.as_s.max_gesture_dur);
305 Serial.print("Triple Tap CNFG : ");
306 Serial.println(((multitap_setting & (uint8_t)BHI360_TRIPLE_TAP) == (uint8_t)BHI360_TRIPLE_TAP) ? "Enabled" : "Disabled");
307 Serial.print(" \t\t -<max_dur_bw_pks> : ");
308 Serial.println(multitap_cnfg.ttap_setting.as_s.max_dur_between_peaks);
309 Serial.print(" \t\t -<tap_shock_settl_dur> : ");
310 Serial.println(multitap_cnfg.ttap_setting.as_s.tap_shock_settling_dur);
311 Serial.print(" \t\t -<min_quite_dur_bw_taps> : ");
312 Serial.println(multitap_cnfg.ttap_setting.as_s.min_quite_dur_between_taps);
313 Serial.print(" \t\t -<quite_time_after_ges> : ");
314 Serial.println(multitap_cnfg.ttap_setting.as_s.quite_time_after_gesture);
315
316 multitap_cnfg.stap_setting.as_s.mode = 0; /* Sensitive mode*/
317
318 if (!bhy.setMultiTapDetectorConfig(multitap_cnfg)) {
319 Serial.println("Failed to set multi tap detector config");
320 return;
321 }
322
323 if (bhy.getMultiTapDetectorConfig(multitap_cnfg)) {
324 Serial.print("Multi tap mode after setting : ");
325 Serial.println(multitap_cnfg.stap_setting.as_s.mode);
326 } else {
327 Serial.println("Failed to get multi tap detector config");
328 }
329 } else {
330 Serial.println("Failed to get multi tap detector config");
331 }
332}
333
334void loop()
335{
336
337#ifdef USING_SENSOR_IRQ_METHOD
339 isInterruptTriggered = false;
340#endif /*USING_SENSOR_IRQ_METHOD*/
341
342 /* If the interrupt is connected to the sensor and BHI360_IRQ is not equal to -1,
343 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
344 */
345 bhy.update();
346
347#ifdef USING_SENSOR_IRQ_METHOD
348 }
349#endif /*USING_SENSOR_IRQ_METHOD*/
350
351#ifdef USING_DATA_HELPER
352 uint32_t s;
353 uint32_t ns;
354 if (multiTap.hasUpdated()) {
355 multiTap.getLastTime(s, ns);
356 Serial.print("[T: ");
357 Serial.print(s);
358 Serial.print(".");
359 Serial.print(ns);
360 Serial.print("]: Multi Tap:");
361 MultiTapDataType tap = multiTap.getValue();
362 switch (tap) {
363 case BHI360_NO_TAP:
364 Serial.println("No Tap");
365 break;
366 case BHI360_SINGLE_TAP:
367 Serial.println("Single Tap");
368 break;
369 case BHI360_DOUBLE_TAP:
370 Serial.println("Double Tap");
371 break;
372 case BHI360_DOUBLE_SINGLE_TAP:
373 Serial.println("Double Single Tap");
374 break;
375 case BHI360_TRIPLE_TAP:
376 Serial.println("Triple Tap");
377 break;
378 case BHI360_TRIPLE_SINGLE_TAP:
379 Serial.println("Triple Single Tap");
380 break;
381 case BHI360_TRIPLE_DOUBLE_TAP:
382 Serial.println("Triple Double Tap");
383 break;
384 case BHI360_TRIPLE_DOUBLE_SINGLE_TAP:
385 Serial.println("Triple Double Single Tap");
386 break;
387 default:
388 Serial.println("Unknown Tap");
389 break;
390 }
391 }
392#endif
393 delay(50);
394}
395
396
@license MIT License
@license MIT License
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)
bool force_update_flash_firmware
#define BHI360_IRQ
SensorMultiTap multiTap(bhy)
void loop()
bool getMultiTapDetectorConfig(MultiTapDetectorConfig &config)
Get the current configuration of the Multi Tap Detector algorithm.
bool setMultiTapDetectorConfig(const MultiTapDetectorConfig &config)
Set the configuration for the Multi Tap Detector algorithm.
bool setMultiTapParamConfig(const MultiTapDataType &config)
Set the configuration for the Multi Tap parameters.
bool getMultiTapParamConfig(MultiTapDataType(&config)[8])
Get the current configuration of the Multi Tap parameters.
bhi360_multi_tap_param_detector MultiTapDetectorConfig
bhi360_event_data_multi_tap MultiTapDataType