SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmi8658_any_motion.ino
Go to the documentation of this file.
1
45#include <stdarg.h>
46#include <ImuDrv.hpp>
47#include "DevicesPins.h"
48
49// Select one interface.
50// #define USE_I2C_INTERFACE
51// #define USE_SPI_INTERFACE
52
53#if !defined(USE_I2C_INTERFACE) && !defined(USE_SPI_INTERFACE)
54#define USE_I2C_INTERFACE
55#endif
56
57#if defined(USE_SPI_INTERFACE)
58#ifndef SPI_MOSI
59#define SPI_MOSI 35
60#endif
61#ifndef SPI_MISO
62#define SPI_MISO 37
63#endif
64#ifndef SPI_SCK
65#define SPI_SCK 36
66#endif
67#ifndef IMU_CS
68#define IMU_CS 34
69#endif
70#else
71#ifndef IMU_SDA
72#define IMU_SDA 17
73#endif
74#ifndef IMU_SCL
75#define IMU_SCL 18
76#endif
77#endif
78
79#ifndef IMU_IRQ
80#define IMU_IRQ 33
81#endif
82
83#ifdef ARDUINO_T_BEAM_S3_SUPREME
84#include <XPowersAXP2101.tpp>
85#endif
86
87static void serialPrintFmt(const char *fmt, ...)
88{
89 char buf[256];
90 va_list args;
91 va_start(args, fmt);
92 vsnprintf(buf, sizeof(buf), fmt, args);
93 va_end(args);
94 Serial.print(buf);
95}
96
98{
99#if defined(ARDUINO_T_BEAM_S3_SUPREME)
100 XPowersAXP2101 power;
101 power.begin(Wire1, AXP2101_SLAVE_ADDRESS, 42, 41);
102 power.disableALDO1();
103 power.disableALDO2();
104 delay(250);
105 power.setALDO1Voltage(3300);
106 power.enableALDO1();
107 power.setALDO2Voltage(3300);
108 power.enableALDO2();
109#endif
110}
111
113// Interrupt flag
114volatile bool isInterruptTriggered = false;
115
116constexpr float kAccelOdrHz = 224.0f;
117// Any-motion parameter set used by configMotionDetect(...):
118// - thresholdX/Y/Z (mg): larger value means less sensitive to small movement.
119// - window (samples): consecutive samples above threshold needed to trigger.
120constexpr float kAnyThresholdMg = 125.0f;
121constexpr uint8_t kAnyWindow = 3;
122
123
125{
126 Serial.println("[EVENT] Any motion detected!");
127}
128
129void setup()
130{
131 Serial.begin(115200);
132 while (!Serial);
133
134 setupPower();
135 Serial.println("QMI8658 Any-Motion Example");
136
137#ifdef USE_I2C_INTERFACE
138 if (!imu.begin(Wire, QMI8658_H_SLAVE_ADDRESS, IMU_SDA, IMU_SCL)) {
139 Serial.println("Failed to initialize QMI8658!");
140 while (1) delay(1000);
141 }
142#endif
143
144#ifdef USE_SPI_INTERFACE
145 if (!imu.begin(SPI, IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
146 Serial.println("Failed to initialize QMI8658!");
147 while (1) delay(1000);
148 }
149#endif
150
152 // Accelerometer: FS_2G(000), FS_4G(001), FS_8G(010), FS_16G(011)
153 // ODR: 1000, 500, 250, 125, 62.5, 31.25, 128, 21, 11, 3 Hz (6DOF: 448/224/112/56/28 Hz)
156
159 kAnyWindow);
160 bool en = imu.enableMotionDetect(SensorQMI8658::IntPin::PIN1);
162
163 serialPrintFmt("Any-Motion config=%s enable=%s\n", cfg ? "OK" : "FAIL", en ? "OK" : "FAIL");
164 Serial.println("Move sensor to trigger Any-Motion.");
165
166 int irq = digitalPinToInterrupt(IMU_IRQ);
167 if (irq >= 0) {
168 attachInterrupt(irq, +[]() {
170 }, CHANGE);
171 serialPrintFmt("IRQ attached on pin %d (CHANGE trigger)\n", IMU_IRQ);
172 } else {
173 serialPrintFmt("IRQ pin %d has no interrupt mapping, polling only\n", IMU_IRQ);
174 }
175}
176
177void loop()
178{
179 static uint32_t lastPollMs = 0;
180 // Fallback poll period when no IRQ arrives (ms).
181 constexpr uint32_t kPollIntervalMs = 50;
182
183 bool doUpdate = false;
185 isInterruptTriggered = false;
186 doUpdate = true;
187 }
188
189 uint32_t now = millis();
190 if (!doUpdate && (now - lastPollMs >= kPollIntervalMs)) {
191 doUpdate = true;
192 }
193
194 if (doUpdate) {
195 lastPollMs = now;
196 imu.update();
197 }
198 delay(10);
199}
@license MIT License
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.
bool enableAccel() override
Enable the accelerometer.
bool enableMotionDetect(IntPin pin=IntPin::PIN1)
Enable motion detection on specified pin.
@ ANY_MOTION
Any motion detection.
void setAnyMotionCallback(MotionCallback callback)
Set callback for any-motion event.
bool configAccel(AccelFullScaleRange range, float data_rate_hz, LpfMode lpf=LpfMode::MODE_0)
Configure the accelerometer with specified parameters.
uint16_t update()
Update sensor status and process events.
void setPins(int pin)
Set interrupt pin.
bool configMotionDetect(MotionType type, float threshold_x, float threshold_y, float threshold_z, uint8_t duration)
Configure motion detection parameters.
char buf[64]
constexpr float kAccelOdrHz
void setupPower()
void setup()
volatile bool isInterruptTriggered
#define IMU_IRQ
#define IMU_SCL
constexpr uint8_t kAnyWindow
void anyMotionCallback()
SensorQMI8658 imu
#define IMU_SDA
void loop()
constexpr float kAnyThresholdMg
constexpr uint32_t kPollIntervalMs
uint32_t lastPollMs