SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_klio_recognition.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
88
89// 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.
90#define BOSCH_BHI260_KLIO
91
92// Firmware is stored in flash and booted from flash,Depends on BHI260 hardware connected to SPI Flash
93// #define BOSCH_BHI260_KLIO_FLASH
94// #define BOSCH_BHI260_KLIO_TURBO_FLASH
95
96#include <BoschFirmware.h>
97
98// Force update of current firmware, whether it exists or not.
99// Only works when external SPI Flash is connected to BHI260.
100// After uploading firmware once, you can change this to false to speed up boot time.
102
103
104/* Action 1 mode, BHI260 should point upwards, for action instructions,
105see action1.gif in the example directory */
107uint8_t action1_pattern[] = {
108 0x52, 0x42, 0x31, 0x06, 0x03, 0xfd, 0xad, 0x80,
109 0x40, 0x0a, 0xd7, 0x23, 0x3c, 0x78, 0xe2, 0x44,
110 0xbf, 0x63, 0xe1, 0x0d, 0xc0, 0x19, 0x39, 0x97,
111 0xbf, 0xdb, 0x93, 0x04, 0x3f, 0xce, 0x07, 0xb7,
112 0x3e, 0x5e, 0xda, 0xf0, 0x3d, 0xe3, 0x6f, 0x8f,
113 0x3e, 0x65, 0x7c, 0x4f, 0x40, 0x46, 0x3f, 0xb4,
114 0x3f, 0xdf, 0xd1, 0x3d, 0xbf, 0xfa, 0x5a, 0x82,
115 0xbf, 0x35, 0xf6, 0x16, 0x3e, 0xbe, 0x70, 0x82,
116 0x40, 0xaa, 0x21, 0x70, 0x41, 0xcb, 0x27, 0xf0,
117 0x40, 0x19, 0x06, 0xd9, 0xbf, 0x3a, 0x10, 0xa7,
118 0xbf, 0x27, 0x07, 0x31, 0x3f, 0x27, 0x23, 0xc9,
119 0xbd, 0x44, 0x29, 0x2f, 0x40, 0xa6, 0x61, 0x97,
120 0xc0, 0x29, 0x5d, 0x21, 0xbe, 0x82, 0xd4, 0x0d,
121 0x3e, 0xc0, 0xf0, 0x15, 0x3d, 0x00, 0xbc, 0xda,
122 0x3d, 0x14, 0x0c, 0xc5, 0xbd, 0x46, 0xa0, 0x03,
123 0x3e, 0xca, 0x5c, 0x95, 0x3d, 0x24, 0xe5, 0x13,
124 0x3c, 0x70, 0x0a, 0x81, 0x3c, 0x69, 0x22, 0xd6,
125 0x3c, 0x51, 0xa4, 0xdf, 0x3e, 0x4c, 0xa8, 0x55,
126 0xbf, 0xe1, 0xe8, 0xc7, 0xbd, 0xe8, 0x7c, 0xbe,
127 0x3d, 0xf7, 0x5b, 0x21, 0x3c
128};
129
130#if BHI260_IRQ > 0
131#define USING_SENSOR_IRQ_METHOD
132#endif
133
134#ifdef USING_SENSOR_IRQ_METHOD
135volatile bool isInterruptTriggered = false;
136
138{
140}
141#endif /*USING_SENSOR_IRQ_METHOD*/
142
143// Firmware update progress callback
144void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
145{
146 float progress = (float)transferred / total * 100;
147 Serial.print("Upload progress: ");
148 Serial.print(progress);
149 Serial.println("%");
150}
151
152void setup()
153{
154 Serial.begin(115200);
155 while (!Serial);
156
157 // Set the reset pin
158 bhy.setPins(BHI260_RST);
159
160 // Set the firmware array address and firmware size
161 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size, bosch_firmware_type, force_update_flash_firmware);
162
163 // Set the firmware update processing progress callback function
164 // bhy.setUpdateProcessCallback(progress_callback, NULL);
165
166 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
167 // bhy.setMaxiTransferSize(256);
168
169 // Set the processing fifo data buffer size,The default size is 512 bytes.
170 // bhy.setProcessBufferSize(1024);
171
172 // Set to load firmware from flash
173 bhy.setBootFromFlash(bosch_firmware_type);
174
175 Serial.println("Initializing Sensors...");
176
177#ifdef USE_I2C_INTERFACE
178 // Using I2C interface
179 // BHI260AP_SLAVE_ADDRESS_L = 0x28
180 // BHI260AP_SLAVE_ADDRESS_H = 0x29
181 if (!bhy.begin(Wire, BHI260AP_SLAVE_ADDRESS_L, BHI260_SDA, BHI260_SCL)) {
182 Serial.print("Failed to initialize sensor - error code:");
183 Serial.println(bhy.getError());
184 while (1) {
185 delay(1000);
186 }
187 }
188#endif
189
190#ifdef USE_SPI_INTERFACE
191 // Using SPI interface
192 if (!bhy.begin(SPI, BHI260_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
193 Serial.print("Failed to initialize sensor - error code:");
194 Serial.println(bhy.getError());
195 while (1) {
196 delay(1000);
197 }
198 }
199#endif
200
201 Serial.println("Initializing the sensor successfully!");
202
203 // Output all sensors info to Serial
204 BoschSensorInfo info = bhy.getSensorInfo();
205
206#ifdef ARDUINO
207 ArduinoStreamPrinter printer(Serial);
208 info.printInfo(printer);
209#else
210 info.printInfo([](const char *format, ...) -> int {
211 va_list args;
212 va_start(args, format);
213 int result = vprintf(format, args);
214 va_end(args);
215 return result;
216 });
217#endif
218
219 // Attempt to initialize the KLIO sensor.
220 if (!klio.begin()) {
221 while (1) {
222 Serial.println("Failed to initialize Klio sensor. Are you currently using a firmware that includes Klio sensor functionality?");
223 delay(1000);
224 }
225 }
226
227 // Call the getMaxPatterns() method of the klio object to get the maximum number of patterns allowed by the KLIO sensor.
228 // This method returns a value of type uint8_t representing the maximum number of patterns and stores it in the variable max_patterns.
229 uint8_t max_patterns = klio.getMaxPatterns();
230 Serial.print("Klio sensor max patterns:");
231 Serial.println(max_patterns);
232
233 // Set the callback function for the recognition event of the KLIO sensor.
234 // The callback function takes a pattern ID, a count value, and a pointer to user data as parameters.
235 // When a recognition event occurs, the callback function will be called, and it will print
236 // information about the recognized pattern (pattern ID and count) to the serial monitor.
237 // The user data pointer setting can be set to nullptr, or custom data can be passed in.
238 // If the recognition action is successful, the pointer is passed to the callback function
239 klio.setRecognitionCallback([](uint8_t pattern_id, float count, void *user_data) {
240 Serial.print("<-Recognition[Id:");
241 Serial.print(pattern_id);
242 Serial.print(" Count:");
243 Serial.print(count);
244 Serial.print("]");
245 }, nullptr);
246
247 // Try to write a pattern to the KLIO sensor.
248 // The pattern ID is specified by action1_pattern_id,
249 // and the pattern data is stored in the action1_pattern array.
250 // The size of the pattern data is determined by sizeof(action1_pattern).
253 sizeof(action1_pattern))) {
254 Serial.println("Klio write pattern failed!");
255 }
256
257 // Start the recognition process for a specific pattern.
258 // Pass the address of action1_pattern_id (indicating the pattern to be recognized)
259 // and the number of patterns (1 in this case) to the recognition function.
261
262 // Define the sample rate at which data will be read from the KLIO sensor.
263 // Here, the sample rate is set to 25Hz, meaning data will be read 25 times per second.
264 float sample_rate = 25.0;
265
266 // Define the report latency in milliseconds.
267 // A value of 0 means that the sensor will report data immediately as it is measured.
268 uint32_t report_latency_ms = 0;
269
270 // Enable the KLIO sensor with he specified sample rate and report latency.
271 // Once enabled, the sensor will start collecting and reporting data according to these settings.
272 klio.enable(sample_rate, report_latency_ms);
273
274#ifdef USING_SENSOR_IRQ_METHOD
275 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
276 // This makes the pin ready to receive external signals.
277 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
278 pinMode(BHI260_IRQ, INPUT);
279
280 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
281 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
282 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
283#endif
284}
285
286
287void loop()
288{
289#ifdef USING_SENSOR_IRQ_METHOD
291 isInterruptTriggered = false;
292#endif /*USING_SENSOR_IRQ_METHOD*/
293
294 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
295 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
296 */
297 bhy.update();
298
299#ifdef USING_SENSOR_IRQ_METHOD
300 }
301#endif /*USING_SENSOR_IRQ_METHOD*/
302 delay(50);
303}
@license MIT License
@license MIT License
SensorBHI260AP bhy
uint8_t action1_pattern_id
uint8_t action1_pattern[]
#define BHI260_SCL
void dataReadyISR()
volatile bool isInterruptTriggered
#define BHI260_IRQ
SensorBHI260AP_Klio klio(bhy)
#define BHI260_SDA
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
bool force_update_flash_firmware
#define BHI260_RST
bool writePattern(uint8_t idx, const uint8_t *parameter_data, uint16_t size)
Write a pattern to the KLIO sensor.
bool begin()
Initialize the interaction with the KLIO functionality of the BHI260AP sensor.
bool enable(float sample_rate, uint32_t report_latency_ms)
Enable the KLIO sensor with specified sample rate and report latency.
uint8_t getMaxPatterns()
Retrieve the maximum number of recognition patterns allowed.
bool recognition(const uint8_t *pattern_ids, size_t size)
Start the recognition function of the KLIO sensor for the specified pattern IDs.
void setRecognitionCallback(RecognitionCallback cb, void *user_data)
Set the callback function for the recognition event.
void setBootFromFlash(bool boot_from_flash)
setBootFromFlash