SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmi8658_pedometer_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#endif /* USE_I2C*/
65
66#ifndef IMU_CS
67#define IMU_CS 34 // IMU CS PIN
68#endif
69
70#ifndef IMU_IRQ
71#define IMU_IRQ 33 // IMU INT PIN
72#endif
73
74#ifndef OLED_SDA
75#define OLED_SDA 22 // Display Wire SDA Pin
76#endif
77
78#ifndef OLED_SCL
79#define OLED_SCL 21 // Display Wire SCL Pin
80#endif
81
83
84bool interruptFlag = false;
85
86
88{
89 // T_BEAM_S3_SUPREME The PMU voltage needs to be turned on to use the sensor
90#if defined(ARDUINO_T_BEAM_S3_SUPREME)
91 XPowersAXP2101 power;
92 power.begin(Wire1, AXP2101_SLAVE_ADDRESS, 42, 41);
93 power.disableALDO1();
94 power.disableALDO2();
95 delay(250);
96 power.setALDO1Voltage(3300); power.enableALDO1();
97 power.setALDO2Voltage(3300); power.enableALDO2();
98#endif
99}
100
101void setFlag(void)
102{
103 interruptFlag = true;
104}
105
106
108{
109 uint32_t val = qmi.getPedometerCounter();
110 Serial.print("Detected Pedometer event : ");
111 Serial.println(val);
112}
113
114void setup()
115{
116 Serial.begin(115200);
117 while (!Serial);
118
119 beginPower();
120
121 bool ret = false;
122#ifdef USE_I2C
123 ret = qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL);
124#else
125#if defined(SPI_MOSI) && defined(SPI_SCK) && defined(SPI_MISO)
126 ret = qmi.begin(SPI, IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK);
127#else
128 ret = qmi.begin(SPI, IMU_CS);
129#endif
130#endif
131
132 if (!ret) {
133 Serial.println("Failed to find QMI8658 - check your wiring!");
134 while (1) {
135 delay(1000);
136 }
137 }
138
139 /* Get chip id*/
140 Serial.print("Device ID:");
141 Serial.println(qmi.getChipID(), HEX);
142
143 // Equipped with acceleration sensor, 2G, ORR62.5HZ
145
146 // Enable the accelerometer
148
149 //* Indicates the count of sample batch/window for calculation
150 uint16_t ped_sample_cnt = 50; //50 samples
151 //* Indicates the threshold of the valid peak-to-peak detection
152 uint16_t ped_fix_peak2peak = 200; //200mg
153 //* Indicates the threshold of the peak detection comparing to average
154 uint16_t ped_fix_peak = 100; //100mg
155 //* Indicates the maximum duration (timeout window) for a step.
156 //* Reset counting calculation if no peaks detected within this duration.
157 uint16_t ped_time_up = 200; // 200 samples 4s
158 //* Indicates the minimum duration for a step.
159 //* The peaks detected within this duration (quiet time) is ignored.
160 uint8_t ped_time_low = 20; //20 samples
161 //* Indicates the minimum continuous steps to start the valid step counting.
162 //* If the continuously detected steps is lower than this count and timeout,the steps will not be take into account;
163 //* if yes, the detected steps will all be taken into account and counting is started to count every following step before timeout.
164 //* This is useful to screen out the fake steps detected by non-step vibrations
165 //* The timeout duration is defined by ped_time_up.
166 uint8_t ped_time_cnt_entry = 10; //10 steps entry count
167 //* Recommended 0
168 uint8_t ped_fix_precision = 0;
169 //* The amount of steps when to update the pedometer output registers.
170 uint8_t ped_sig_count = 4; //Every 4 valid steps is detected, update the registers once (added by 4).
171
172 qmi.configPedometer(ped_sample_cnt,
173 ped_fix_peak2peak,
174 ped_fix_peak,
175 ped_time_up,
176 ped_time_low,
177 ped_time_cnt_entry,
178 ped_fix_precision,
179 ped_sig_count);
180
181 // Enable the step counter and enable the interrupt
183 Serial.println("Enable pedometer failed!");
184 while (1);
185 }
186
187 // Set the step counter callback function
189
190 /*
191 * When the QMI8658 is configured as Wom, the interrupt level is arbitrary,
192 * not absolute high or low, and it is in the jump transition state
193 */
194 attachInterrupt(IMU_IRQ, setFlag, CHANGE);
195}
196
197
198void loop()
199{
200 if (interruptFlag) {
201 interruptFlag = false;
202 qmi.update();
203 }
204 delay(500);
205}
#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.
bool configPedometer(uint16_t sample_count, uint16_t peak_to_peak, uint16_t peak_threshold, uint16_t time_up, uint8_t time_low=20, uint8_t entry_count=10, uint8_t fix_precision=0, uint8_t sig_count=4)
Configure pedometer parameters.
void setPedometerEventCallBack(EventCallBack_t cb)
uint8_t getChipID() override
Get the chip ID / WHO_AM_I value.
bool enablePedometer(IntPin pin=IntPin::DISABLE)
Enable pedometer.
bool configAccelerometer(AccelRange range, AccelODR odr, LpfMode lpfOdr=LPF_MODE_0)
Configure the accelerometer.
uint32_t getPedometerCounter()
Get the Pedometer counter.
uint16_t update()
Update sensor status and process events.
bool enableAccelerometer()
Enable the accelerometer.
void setFlag(void)
SensorQMI8658 qmi