SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bma423_step_detector.ino
Go to the documentation of this file.
1
29#include <AccelerometerDrv.hpp>
30
31#ifndef SENSOR_SDA
32#define SENSOR_SDA 21
33#endif
34
35#ifndef SENSOR_SCL
36#define SENSOR_SCL 22
37#endif
38
39#ifndef SENSOR_IRQ
40#define SENSOR_IRQ 39
41#endif
42
44
45volatile bool isInterruptTriggered = false;
46
48{
49 Serial.println("Step detected");
50}
51
52void onStepCount(uint32_t stepCount)
53{
54 Serial.print("Step count: ");
55 Serial.println(stepCount);
56}
57
58void setup()
59{
60 bool rslt;
61
62 Serial.begin(115200);
63 while (!Serial);
64
65 pinMode(SENSOR_IRQ, INPUT);
66 attachInterrupt(SENSOR_IRQ, +[]() {
68 }, RISING);
69
70 // Sensor initialization
71 rslt = accelSensor.begin(Wire, BMA4XX_I2C_ADDR_SDO_HIGH, SENSOR_SDA, SENSOR_SCL);
72 if (!rslt) {
73 while (1) {
74 Serial.println("BMA4xx sensor initialization failed");
75 delay(1000);
76 }
77 }
78
79 Serial.print(accelSensor.getModelName());
80 Serial.println(" Sensor initialized successfully");
81
82 // Set sensor orientation, based on the hardware placement setting
84 if (!rslt) {
85 Serial.println("Failed to set remap axes");
86 }
87
88 // Accelerometer configuration
89 // The desired operation mode. Allowed values are SUSPEND, NORMAL.
91 // The desired full-scale range. Allowed values are FS_2G, FS_4G, FS_8G, FS_16G.
93 // The desired bandwidth. Allowed values are OSR4_AVG1, OSR2_AVG2, NORMAL_AVG4, etc.
95 // The desired data rate in Hz. Allowed values are 0.78, 1.56, 3.12, 6.25, 12.5, 25, 50, 100, 200, 400, 800, 1600.
96 float data_rate_hz = 100.0f;
97 // The desired performance mode. Allowed values are CIC_AVG_MODE, CONTINUOUS_MODE
99
100 if (!accelSensor.configAccelerometer(operationMode, fullScaleRange, data_rate_hz, bandwidth, perfMode)) {
101 Serial.println("Failed to configure accelerometer");
102 while (1);
103 }
104
105 bool feature_enable = true; // Enable feature
106 bool interrupt_enable = true; //True: hardware interrupt enabled
108 bool reset_counter = true; // True: reset step counter on new activity
109 /* Setting watermark level 1, the output step resolution is 20 steps.
110 * Eg: 1 means, 1 * 20 = 20. Every 20 steps once output triggers
111 */
112 uint16_t step_counter_wm = 1;
113
115 pin_map, // Which pin should the hardware interrupt be routed to?
116 false, // level trigger
117 false, // active high
118 true, // output enable
119 false); // input disable
120 if (!rslt) {
121 Serial.println("Failed to set interrupt pin configuration");
122 while (1);
123 }
124
125 // Enable step counter feature
126 rslt = accelSensor.enableStepCounter(feature_enable, step_counter_wm, reset_counter);
127 if (!rslt) {
128 Serial.println("Failed to enable step counter");
129 while (1);
130 }
131
132 // Enable step detector feature
133 rslt = accelSensor.enableStepDetector(feature_enable, interrupt_enable, pin_map);
134 if (!rslt) {
135 Serial.println("Failed to enable step detector");
136 while (1);
137 }
138
139 // Set step count callback
141
142 // Set step detected callback
144
145 delay(3000);
146
147 Serial.println("Now you can move the sensor.");
148}
149
150void loop()
151{
153 isInterruptTriggered = false;
155 }
156 delay(30);
157}
@license MIT License
OperationMode
Enumeration of sensor operation modes.
@ TOP_LAYER_RIGHT_CORNER
AccelFullScaleRange
Enumeration of accelerometer full-scale range settings.
InterruptPinMap
Enumeration of interrupt pin mapping options.
@ PIN1
Interrupt pin 1.
void onStepCount(uint32_t stepCount)
void setup()
#define SENSOR_SCL
volatile bool isInterruptTriggered
#define SENSOR_SDA
SensorBMA423 accelSensor
void onStepDetected()
#define SENSOR_IRQ
void loop()
bool begin(CommInterface interface, SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr=0)
Initialize the sensor using custom callback interface.
void setOnStepDetectedCallback(std::function< void()> cb)
Set the callback for step detection events.
bool enableStepCounter(bool enable, uint16_t step_counter_wm=1, bool reset_counter=false) override
Enable or disable step counter.
bool enableStepDetector(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable or disable step detector.
void setOnStepCountCallback(std::function< void(uint32_t)> cb)
Set the callback for step count events.
void update() override
Update sensor state and process interrupts.
virtual bool setRemapAxes(SensorRemap remap)
Set the remapping of axes for the BMA4XX sensor.
bool setInterruptPinConfig(InterruptPinMap pin_map, bool edge_trigger=false, bool active_low=true, bool output_en=true, bool input_en=false)
Configure the electrical properties of an interrupt pin.
const char * getModelName() const
Get the model name of the BMA4XX sensor.
bool configAccelerometer(OperationMode mode, AccelFullScaleRange range, float data_rate_hz, AccelBandwidth bandwidth, AccelPerfMode perf_mode)
Configure the accelerometer.
AccelBandwidth
BMA4 Accelerometer averaging filter configuration These values configure the internal averaging filte...
@ OSR2_AVG2
OSR2 mode with 2 samples averaged.
AccelPerfMode
BMA4 Accelerometer performance mode configuration Configures the internal filter behavior for sample ...
@ CIC_AVG_MODE
Averaging filter mode: samples are averaged based on configured bandwidth and ODR.