SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bma423_tap.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.print("Tap detected: ");
50 switch (tap) {
52 Serial.println("Single Tap");
53 break;
55 Serial.println("Double Tap");
56 break;
57 default:
58 Serial.println("No Tap");
59 break;
60 }
61}
62
63void setup()
64{
65 bool rslt;
66
67 Serial.begin(115200);
68 while (!Serial);
69
70 pinMode(SENSOR_IRQ, INPUT);
71 attachInterrupt(SENSOR_IRQ, +[]() {
73 }, RISING);
74
75 // Sensor initialization
76 rslt = accelSensor.begin(Wire, BMA4XX_I2C_ADDR_SDO_HIGH, SENSOR_SDA, SENSOR_SCL);
77 if (!rslt) {
78 while (1) {
79 Serial.println("BMA4xx sensor initialization failed");
80 delay(1000);
81 }
82 }
83
84 Serial.print(accelSensor.getModelName());
85 Serial.println(" Sensor initialized successfully");
86
87 // Set sensor orientation, based on the hardware placement setting
89 if (!rslt) {
90 Serial.println("Failed to set remap axes");
91 }
92
93 // Accelerometer configuration
94 // The desired operation mode. Allowed values are SUSPEND, NORMAL.
96 // The desired full-scale range. Allowed values are FS_2G, FS_4G, FS_8G, FS_16G.
98 // The desired bandwidth. Allowed values are OSR4_AVG1, OSR2_AVG2, NORMAL_AVG4, etc.
100 // 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.
101 float data_rate_hz = 200.0f; // A minimum sampling frequency of 200Hz is required for tapping.
102 // The desired performance mode. Allowed values are CIC_AVG_MODE, CONTINUOUS_MODE
104
105 if (!accelSensor.configAccelerometer(operationMode, fullScaleRange, data_rate_hz, bandwidth, perfMode)) {
106 Serial.println("Failed to configure accelerometer");
107 while (1);
108 }
109
110 bool feature_enable = true; // Enable feature
111 bool interrupt_enable = true; //True: hardware interrupt enabled
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 // This function selects the tap detection tap type. Allowed values are DOUBLE_TAP or SINGLE_TAP
126 // If not selected, the default is double-tap
128 accelSensor.selectTapType(tapType);
129
130 // Enable tap detector feature
131 rslt = accelSensor.enableTapDetector(feature_enable, interrupt_enable, pin_map);
132 if (!rslt) {
133 Serial.println("Failed to enable tap detector");
134 while (1);
135 }
136
137 // Set tap detection callback
139
140 delay(3000);
141
142 Serial.println("Now you can tap once, twice, or three times.");
143}
144
145void loop()
146{
148 isInterruptTriggered = false;
150 }
151 delay(30);
152}
@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 setup()
void onTapDetected(TapType tap)
#define SENSOR_SCL
volatile bool isInterruptTriggered
#define SENSOR_SDA
SensorBMA423 accelSensor
#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 setOnTapCallback(std::function< void(TapType)> cb)
Set the callback for tap events.
bool selectTapType(TapType tap)
Select tap detection tap type.
bool enableTapDetector(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable or disable tap detection.
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.
TapType
Tap detection type.
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.