SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bma456h_step_detector.ino
Go to the documentation of this file.
1
30#include <AccelerometerDrv.hpp>
31
32#ifndef SENSOR_SDA
33#define SENSOR_SDA 21
34#endif
35
36#ifndef SENSOR_SCL
37#define SENSOR_SCL 22
38#endif
39
40#ifndef SENSOR_IRQ
41#define SENSOR_IRQ 39
42#endif
43
44// Configuring using BMA456H features
46
47volatile bool isInterruptTriggered = false;
48
50{
51 Serial.println("Step detected");
52}
53
54void onStepCount(uint32_t stepCount)
55{
56 Serial.print("Step count: ");
57 Serial.println(stepCount);
58}
59
60void setup()
61{
62 bool rslt;
63
64 Serial.begin(115200);
65 while (!Serial);
66
67 pinMode(SENSOR_IRQ, INPUT);
68 attachInterrupt(SENSOR_IRQ, +[]() {
70 }, RISING);
71
72 // Sensor initialization
73 rslt = accelSensor.begin(Wire, BMA4XX_I2C_ADDR_SDO_HIGH, SENSOR_SDA, SENSOR_SCL);
74 if (!rslt) {
75 while (1) {
76 Serial.println("BMA4xx sensor initialization failed");
77 delay(1000);
78 }
79 }
80
81 Serial.print(accelSensor.getModelName());
82 Serial.println(" Sensor initialized successfully");
83
84 // Set sensor orientation, based on the hardware placement setting
86 if (!rslt) {
87 Serial.println("Failed to set remap axes");
88 }
89
90 // Accelerometer configuration
91 // The desired operation mode. Allowed values are SUSPEND, NORMAL.
93 // The desired full-scale range. Allowed values are FS_2G, FS_4G, FS_8G, FS_16G.
95 // The desired bandwidth. Allowed values are OSR4_AVG1, OSR2_AVG2, NORMAL_AVG4, etc.
97 // 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.
98 float data_rate_hz = 100.0f;
99 // The desired performance mode. Allowed values are CIC_AVG_MODE, CONTINUOUS_MODE
101
102 if (!accelSensor.configAccelerometer(operationMode, fullScaleRange, data_rate_hz, bandwidth, perfMode)) {
103 Serial.println("Failed to configure accelerometer");
104 while (1);
105 }
106
107 bool feature_enable = true; // Enable feature
108 bool interrupt_enable = true; //True: hardware interrupt enabled
110 bool reset_counter = true; // True: reset step counter on new activity
111 /* Setting watermark level 1, the output step resolution is 20 steps.
112 * Eg: 1 means, 1 * 20 = 20. Every 20 steps once output triggers
113 */
114 uint16_t step_counter_wm = 1;
115
117 pin_map, // Which pin should the hardware interrupt be routed to?
118 false, // level trigger
119 false, // active high
120 true, // output enable
121 false); // input disable
122 if (!rslt) {
123 Serial.println("Failed to set interrupt pin configuration");
124 while (1);
125 }
126
127 // Enable step counter feature
128 rslt = accelSensor.enableStepCounter(feature_enable, step_counter_wm, reset_counter);
129 if (!rslt) {
130 Serial.println("Failed to enable step counter");
131 while (1);
132 }
133
134 // Enable step detector feature
135 rslt = accelSensor.enableStepDetector(feature_enable, interrupt_enable, pin_map);
136 if (!rslt) {
137 Serial.println("Failed to enable step detector");
138 while (1);
139 }
140
141 // Set step count callback
143
144 // Set step detected callback
146
147 delay(3000);
148
149 Serial.println("Now you can move the sensor.");
150}
151
152void loop()
153{
155 isInterruptTriggered = false;
157 }
158 delay(30);
159}
@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)
SensorBMA456H accelSensor
void setup()
#define SENSOR_SCL
volatile bool isInterruptTriggered
#define SENSOR_SDA
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.
void update() override
Update sensor state and process interrupts.
bool enableStepCounter(bool enable, uint16_t step_counter_wm=1, bool reset_counter=false) override
Enable or disable step counter.
void setOnStepCountCallback(std::function< void(uint32_t)> cb)
Set the callback for step count events.
bool enableStepDetector(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable or disable step detector.
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.