SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bma_universal_compatibility.ino
Go to the documentation of this file.
1
40#include <AccelerometerDrv.hpp>
41
42#ifndef SENSOR_SDA
43#define SENSOR_SDA 21
44#endif
45
46#ifndef SENSOR_SCL
47#define SENSOR_SCL 22
48#endif
49
50#ifndef SENSOR_IRQ
51#define SENSOR_IRQ 39
52#endif
53
54// Board compatibility layer: instantiate all possible sensors.
55// Only the detected one will be initialized; the others remain idle.
59
60// Base class pointer for polymorphic operation (features, config, poll)
62
63enum class DetectedModel {
64 NONE,
65 BMA422,
66 BMA423,
68};
69
71
72volatile bool isInterruptTriggered = false;
73
74
75// ============================================================
76// Probe & init: try each sensor in order, keep the first hit
77// ============================================================
79{
80 if (bma422.begin(Wire, BMA4XX_I2C_ADDR_SDO_HIGH, SENSOR_SDA, SENSOR_SCL)) {
81 sensor = &bma422;
83 Serial.println("[probe] Detected BMA422");
84 return true;
85 }
86
87 if (bma423.begin(Wire, BMA4XX_I2C_ADDR_SDO_LOW, SENSOR_SDA, SENSOR_SCL)) {
88 sensor = &bma423;
90 Serial.println("[probe] Detected BMA423");
91 return true;
92 }
93
94 if (bma456h.begin(Wire, BMA4XX_I2C_ADDR_SDO_HIGH, SENSOR_SDA, SENSOR_SCL)) {
95 sensor = &bma456h;
97 Serial.println("[probe] Detected BMA456H");
98 return true;
99 }
100
101 return false;
102}
103
104
105// ============================================================
106// Callback dispatcher
107// uses the already-known DetectedModel enum.
108// ============================================================
110{
111 switch (detectedModel) {
114 switch (t) {
116 Serial.println(" [callback] SINGLE TAP"); break;
118 Serial.println(" [callback] DOUBLE TAP"); break;
119 default: break;
120 }
121 });
122 break;
125 switch (t) {
127 Serial.println(" [callback] SINGLE TAP"); break;
129 Serial.println(" [callback] DOUBLE TAP"); break;
131 Serial.println(" [callback] TRIPLE TAP"); break;
132 default: break;
133 }
134 });
135 break;
136 default:
137 break;
138 }
139}
140
142{
143 switch (detectedModel) {
146 Serial.println(" [callback] STEP DETECTED");
147 });
148 bma423.setOnStepCountCallback([](uint32_t count) {
149 Serial.print(" [callback] STEP COUNT: ");
150 Serial.println(count);
151 });
152 break;
155 Serial.println(" [callback] STEP DETECTED");
156 });
157 bma456h.setOnStepCountCallback([](uint32_t count) {
158 Serial.print(" [callback] STEP COUNT: ");
159 Serial.println(count);
160 });
161 break;
162 default:
163 break;
164 }
165}
166
168{
169 auto fn = [](ActivityType a) {
170 Serial.print(" [callback] ACTIVITY: ");
171 switch (a) {
172 case ActivityType::STATIONARY: Serial.println("Stationary"); break;
173 case ActivityType::WALKING: Serial.println("Walking"); break;
174 case ActivityType::RUNNING: Serial.println("Running"); break;
175 default: Serial.println("Unknown"); break;
176 }
177 };
178
179 switch (detectedModel) {
182 break;
185 break;
186 default:
187 break;
188 }
189}
190
192{
195 Serial.println(" [callback] TILT DETECTED");
196 });
197 }
198}
199
201{
202 auto fn = [](AccelerometerData data) {
203 Serial.print(" [callback] DataReady: ");
204 Serial.print(data.mps2.x, 2); Serial.print(", ");
205 Serial.print(data.mps2.y, 2); Serial.print(", ");
206 Serial.println(data.mps2.z, 2);
207 };
208
209 switch (detectedModel) {
212 break;
215 break;
218 break;
219 default:
220 break;
221 }
222}
223
224
225// ============================================================
226// Capability-based feature setup
227// ============================================================
229{
230 auto caps = sensor->getCapabilities();
231
232 Serial.print("Chip: ");
233 Serial.print(sensor->getModelName());
234 Serial.print(", Capabilities: 0x");
235 Serial.println(static_cast<uint32_t>(caps), HEX);
236
238 Serial.println(" + enableDataReady");
239 sensor->enableDataReady(true);
240 }
241
243 Serial.println(" + enableAnyMotion");
245 SensorBMA4XX::MotionAxesConfig(1, 1, 1), true
246 );
247 }
248
250 Serial.println(" + setRemapAxes");
252 }
253
255 Serial.println(" + enableTapDetector");
258 }
259
261 Serial.println(" + enableStepDetector");
262 sensor->enableStepCounter(true, 1, true);
263 sensor->enableStepDetector(true, true);
265 }
266
268 Serial.println(" + enableActivityRecognition");
271 }
272
274 Serial.println(" + enableTiltDetector");
277 }
278}
279
280
281void setup()
282{
283 Serial.begin(115200);
284 while (!Serial);
285
286 pinMode(SENSOR_IRQ, INPUT);
287 attachInterrupt(SENSOR_IRQ, +[]() {
289 }, RISING);
290
291 // ---- Step 1: Auto-detect sensor ----
292 if (!initSensor()) {
293 while (1) {
294 Serial.println("No BMA4XX sensor detected!");
295 delay(1000);
296 }
297 }
298
299 Serial.print("Using: ");
300 Serial.println(sensor->getModelName());
301
302 // ---- Step 2: Configure accelerometer (base class API) ----
306 100.0f,
309 Serial.println("Failed to configure accelerometer");
310 while (1);
311 }
312
313 // ---- Step 3: Configure interrupt pin (base class API) ----
316 false, // level trigger
317 false, // active high
318 true, // output enable
319 false); // input disable
320
321 // ---- Step 4: Enable features based on capabilities ----
323
324 // ---- Step 5: Set data ready callback ----
326
327 delay(1000);
328 Serial.println();
329 Serial.println("=== System ready. Waiting for events... ===");
330 Serial.println();
331}
332
333void loop()
334{
336 isInterruptTriggered = false;
337 sensor->update();
338 }
339
340 static unsigned long lastPoll = 0;
341 if (millis() - lastPoll > 5000) {
342 lastPoll = millis();
343 Serial.print("[poll] Temperature: ");
344 Serial.print(sensor->getTemperature());
345 Serial.println(" C");
346 }
347 delay(10);
348}
@license MIT License
@ TOP_LAYER_RIGHT_CORNER
@ PIN1
Interrupt pin 1.
void setupActivityCallback()
SensorBMA423 bma423
DetectedModel detectedModel
volatile bool isInterruptTriggered
void setupTiltCallback()
void setupDataReadyCallback()
SensorBMA456H bma456h
void setupTapCallback()
SensorBMA4XX * sensor
SensorBMA422 bma422
void setupStepCallbacks()
bool begin(CommInterface interface, SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr=0)
Initialize the sensor using custom callback interface.
void setOnDataReadyCallback(std::function< void(AccelerometerData)> cb)
Set the callback for data ready events.
void setOnStepDetectedCallback(std::function< void()> cb)
Set the callback for step detection events.
void setOnDataReadyCallback(std::function< void(AccelerometerData)> cb)
Set the callback for data ready events.
void setOnTapCallback(std::function< void(TapType)> cb)
Set the callback for tap events.
void setOnActivityCallback(std::function< void(ActivityType)> cb)
Set the callback for activity events.
void setOnStepCountCallback(std::function< void(uint32_t)> cb)
Set the callback for step count events.
void setOnTiltDetectedCallback(std::function< void(void)> cb)
Set the callback for tilt detection events.
void setOnTapCallback(std::function< void(TapType)> cb)
Set the callback for tap events.
void setOnStepDetectedCallback(std::function< void()> cb)
Set the callback for step detection events.
void setOnActivityCallback(std::function< void(ActivityType)> cb)
Set the callback for activity events.
void setOnDataReadyCallback(std::function< void(AccelerometerData)> cb)
Set the callback for data ready events.
void setOnStepCountCallback(std::function< void(uint32_t)> cb)
Set the callback for step count events.
virtual bool enableStepCounter(bool enable, uint16_t step_counter_wm=1, bool reset_counter=false)
Enable or disable step counter (default: not supported)
virtual void update()=0
Update the sensor data, this method is implemented by a subclass.
virtual bool setRemapAxes(SensorRemap remap)
Set the remapping of axes for the BMA4XX sensor.
virtual bool enableStepDetector(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable or disable step detector (default: not supported)
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.
virtual bool enableAnyMotionDetection(const MotionAxesConfig &cfg, bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable or disable any-motion detection (default: not supported)
virtual bool enableTapDetector(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable or disable tap detection (default: not supported)
virtual bool enableTiltDetector(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable or disable tilt detection (default: not supported)
virtual bool enableActivityRecognition(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable or disable activity recognition (default: not supported)
const char * getModelName() const
Get the model name of the BMA4XX sensor.
float getTemperature() const
Get the temperature reading from the sensor.
virtual BMA4XXCapability::Capability getCapabilities() const =0
Get supported capabilities.
virtual bool enableDataReady(bool enable=true, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable data ready interrupt (default: not supported)
bool configAccelerometer(OperationMode mode, AccelFullScaleRange range, float data_rate_hz, AccelBandwidth bandwidth, AccelPerfMode perf_mode)
Configure the accelerometer.
@ OSR2_AVG2
OSR2 mode with 2 samples averaged.
ActivityType
Activity detection type.
TapType
Tap detection type.
@ CIC_AVG_MODE
Averaging filter mode: samples are averaged based on configured bandwidth and ODR.
Structure representing accelerometer data.