SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmi8658_tap_detection_deprecated.ino
Go to the documentation of this file.
1
30#include <Arduino.h>
31#include <Wire.h>
32#include <SPI.h>
33#include "SensorQMI8658.hpp"
34#ifdef ARDUINO_T_BEAM_S3_SUPREME
35#include <XPowersAXP2101.tpp> //PMU Library https://github.com/lewisxhe/XPowersLib.git
36#endif
37
38
39// #define USE_I2C //Using the I2C interface
40
41#ifdef USE_I2C
42#ifndef SENSOR_SDA
43#define SENSOR_SDA 17
44#endif
45
46#ifndef SENSOR_SCL
47#define SENSOR_SCL 18
48#endif
49
50#else /* SPI interface */
51
52#ifndef SPI_MOSI
53#define SPI_MOSI (35)
54#endif
55
56#ifndef SPI_SCK
57#define SPI_SCK (36)
58#endif
59
60#ifndef SPI_MISO
61#define SPI_MISO (37)
62#endif
63
64#ifndef IMU_CS
65#define IMU_CS 34 // IMU CS PIN
66#endif
67
68#endif /* USE_I2C*/
69
70
71#ifndef IMU_IRQ
72#define IMU_IRQ 33 // IMU INT PIN
73#endif
74
75#ifndef OLED_SDA
76#define OLED_SDA 22 // Display Wire SDA Pin
77#endif
78
79#ifndef OLED_SCL
80#define OLED_SCL 21 // Display Wire SCL Pin
81#endif
82
84
85
86bool interruptFlag = false;
87
88void setFlag(void)
89{
90 interruptFlag = true;
91}
92
93
95{
96 // T_BEAM_S3_SUPREME The PMU voltage needs to be turned on to use the sensor
97#if defined(ARDUINO_T_BEAM_S3_SUPREME)
98 XPowersAXP2101 power;
99 power.begin(Wire1, AXP2101_SLAVE_ADDRESS, 42, 41);
100 power.disableALDO1();
101 power.disableALDO2();
102 delay(250);
103 power.setALDO1Voltage(3300); power.enableALDO1();
104 power.setALDO2Voltage(3300); power.enableALDO2();
105#endif
106}
107
109{
111 switch (event) {
113 Serial.println("Single-TAP");
114 break;
116 Serial.println("Double-TAP");
117 break;
118 default:
119 break;
120 }
121}
122
123void setup()
124{
125 Serial.begin(115200);
126 while (!Serial);
127
128 beginPower();
129
130 bool ret = false;
131#ifdef USE_I2C
132 ret = qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL);
133#else
134#if defined(SPI_MOSI) && defined(SPI_SCK) && defined(SPI_MISO)
135 ret = qmi.begin(SPI, IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK);
136#else
137 ret = qmi.begin(SPI, IMU_CS);
138#endif
139#endif
140
141 if (!ret) {
142 Serial.println("Failed to find QMI8658 - check your wiring!");
143 while (1) {
144 delay(1000);
145 }
146 }
147
148 /* Get chip id*/
149 Serial.print("Device ID:");
150 Serial.println(qmi.getChipID(), HEX);
151
152 //** The recommended output data rate for detection is higher than 500HZ
154 /*
155 * ACC_RANGE_2G
156 * ACC_RANGE_4G
157 * ACC_RANGE_8G
158 * ACC_RANGE_16G
159 * */
161 /*
162 * ACC_ODR_1000H
163 * ACC_ODR_500Hz
164 * ACC_ODR_250Hz
165 * ACC_ODR_125Hz
166 * ACC_ODR_62_5Hz
167 * ACC_ODR_31_25Hz
168 * ACC_ODR_LOWPOWER_128Hz
169 * ACC_ODR_LOWPOWER_21Hz
170 * ACC_ODR_LOWPOWER_11Hz
171 * ACC_ODR_LOWPOWER_3H
172 * */
174
175 // Enable the accelerometer
177
178 //* Priority definition between the x, y, z axes of acceleration.
179 uint8_t priority = SensorQMI8658::PRIORITY0; //(X > Y> Z)
180 //* Defines the maximum duration (in sample) for a valid peak.
181 //* In a valid peak, the linear acceleration should reach or be higher than the PeakMagThr
182 //* and should return to quiet (no significant movement) within UDMThr, at the end of PeakWindow.
183 uint8_t peakWindow = 20; //20 @500Hz ODR
184 //* Defines the minimum quiet time before the second Tap happen.
185 //* After the first Tap is detected, there should be no significant movement (defined by UDMThr) during the TapWindow.
186 //* The valid second tap should be detected after TapWindow and before DTapWindow.
187 uint16_t tapWindow = 50; //50 @500Hz ODR
188 //* Defines the maximum time for a valid second Tap for Double Tap,
189 //* count start from the first peak of the valid first Tap.
190 uint16_t dTapWindow = 250; //250 @500Hz ODR
191 //* Defines the ratio for calculating the average of the movement
192 //* magnitude. The bigger of Gamma, the bigger weight of the latest data.
193 float alpha = 0.0625;
194 //* Defines the ratio for calculating the average of the movement
195 //* magnitude. The bigger of Gamma, the bigger weight of the latest data.
196 float gamma = 0.25;
197 //* Threshold for peak detection.
198 float peakMagThr = 0.8; //0.8g square
199 //* Undefined Motion threshold. This defines the threshold of the
200 //* Linear Acceleration for quiet status.
201 float UDMTh = 0.4; //0.4g square
202
204 dTapWindow, alpha, gamma, peakMagThr, UDMTh);
205
206 // Enable the Tap Detection and enable the interrupt
208
209 // Set the Tap Detection callback function
211
212 /*
213 * When the QMI8658 is configured as Wom, the interrupt level is arbitrary,
214 * not absolute high or low, and it is in the jump transition state
215 */
216 attachInterrupt(IMU_IRQ, setFlag, CHANGE);
217}
218
219
220void loop()
221{
222 if (interruptFlag) {
223 interruptFlag = false;
224 qmi.update();
225 }
226 delay(50);
227}
#define SENSOR_SCL
#define SENSOR_SDA
bool begin(CommInterface interface, SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr=0)
Initialize the sensor using custom callback interface.
QMI8658 IMU sensor driver class.
uint8_t getChipID() override
Get the chip ID / WHO_AM_I value.
void setTapEventCallBack(EventCallBack_t cb)
bool configTap(TapPriority priority, uint8_t peak_window, uint16_t tap_window, uint16_t double_tap_window, float peak_mag_threshold, float quiet_threshold)
Configure tap detection parameters.
TapEvent getTapStatus()
Get tap status.
bool configAccelerometer(AccelRange range, AccelODR odr, LpfMode lpfOdr=LPF_MODE_0)
Configure the accelerometer.
bool enableTap(IntPin pin=IntPin::PIN1)
Enable tap detection.
uint16_t update()
Update sensor status and process events.
bool enableAccelerometer()
Enable the accelerometer.
uint16_t tapWindow
uint8_t peakWindow
SensorQMI8658 qmi