ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
lis2dw12.h
Go to the documentation of this file.
1#pragma once
6#ifdef USE_BINARY_SENSOR
8#endif
9#ifdef USE_SENSOR
11#endif
12#ifdef USE_TEXT_SENSOR
14#endif
15
16namespace esphome::lis2dw12 {
17
18// LIS2DW12 Register Map
19enum class RegisterMap : uint8_t {
20 WHO_AM_I = 0x0F,
21 CTRL1 = 0x20,
22 CTRL2 = 0x21,
23 CTRL3 = 0x22,
24 CTRL4_INT1 = 0x23,
25 CTRL6 = 0x25,
26 OUT_X_L = 0x28,
27 TAP_THS_X = 0x30,
28 TAP_THS_Y = 0x31,
29 TAP_THS_Z = 0x32,
30 INT_DUR = 0x33,
31 WAKE_UP_THS = 0x34,
32 WAKE_UP_DUR = 0x35,
33 FREE_FALL = 0x36,
34 SIXD_SRC = 0x3A,
35 ALL_INT_SRC = 0x3B,
36 CTRL7 = 0x3F,
37};
38
39enum class Range : uint8_t {
40 RANGE_2G = 0b00,
41 RANGE_4G = 0b01,
42 RANGE_8G = 0b10,
43 RANGE_16G = 0b11,
44};
45
46enum class PowerMode : uint8_t {
47 HIGH_PERF = 0,
48 LOW_POWER_1 = 1,
49 LOW_POWER_2 = 2,
50 LOW_POWER_3 = 3,
51 LOW_POWER_4 = 4,
52};
53
54enum class DataRate : uint8_t {
55 RATE_1_6HZ = 0b0001,
56 RATE_12_5HZ = 0b0010,
57 RATE_25HZ = 0b0011,
58 RATE_50HZ = 0b0100,
59 RATE_100HZ = 0b0101,
60 RATE_200HZ = 0b0110,
61 RATE_400HZ = 0b0111,
62 RATE_800HZ = 0b1000,
63 RATE_1600HZ = 0b1001,
64};
65
66enum class FilterBandwidth : uint8_t {
67 BW_ODR_DIV_2 = 0b00,
68 BW_ODR_DIV_4 = 0b01,
69 BW_ODR_DIV_10 = 0b10,
70 BW_ODR_DIV_20 = 0b11,
71};
72
73// ALL_INT_SRC register (0x3B)
75 struct {
76 bool ff_ia : 1;
77 bool wu_ia : 1;
78 bool single_tap : 1;
79 bool double_tap : 1;
80 bool d6d_ia : 1;
82 uint8_t reserved : 2;
83 } __attribute__((packed));
84 uint8_t raw{0};
85};
86
87// SIXD_SRC register (0x3A)
89 struct {
90 bool xl : 1;
91 bool xh : 1;
92 bool yl : 1;
93 bool yh : 1;
94 bool zl : 1;
95 bool zh : 1;
96 bool d6d_ia : 1;
97 uint8_t reserved : 1;
98 } __attribute__((packed));
99 uint8_t raw{0};
100};
101
102// Debounced events, also the bit index into events_on_
110
112 public:
113 void setup() override;
114 void dump_config() override;
115 void loop() override;
116 void update() override;
117
119 void set_low_noise(bool low_noise) { this->low_noise_ = low_noise; }
120 void set_output_data_rate(DataRate rate) { this->data_rate_ = rate; }
121 void set_range(Range range) { this->range_ = range; }
123 void set_offset(float offset_x, float offset_y, float offset_z);
124 void set_transform(bool mirror_x, bool mirror_y, bool mirror_z, bool swap_xy);
125
126 bool read_byte(uint8_t a_register, uint8_t *data) { return this->read_bytes(a_register, data, 1); }
127 virtual bool write_byte(uint8_t a_register, uint8_t data) = 0;
128 virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) = 0;
129
130#ifdef USE_BINARY_SENSOR
137#endif
138
139#ifdef USE_SENSOR
140 SUB_SENSOR(acceleration_x)
141 SUB_SENSOR(acceleration_y)
142 SUB_SENSOR(acceleration_z)
143#endif
144
145#ifdef USE_TEXT_SENSOR
146 SUB_TEXT_SENSOR(orientation)
147#endif
148
149 template<typename F> void add_on_tap_callback(F &&callback) { this->tap_callback_.add(std::forward<F>(callback)); }
150 template<typename F> void add_on_double_tap_callback(F &&callback) {
151 this->double_tap_callback_.add(std::forward<F>(callback));
152 }
153 template<typename F> void add_on_freefall_callback(F &&callback) {
154 this->freefall_callback_.add(std::forward<F>(callback));
155 }
156 template<typename F> void add_on_active_callback(F &&callback) {
157 this->active_callback_.add(std::forward<F>(callback));
158 }
159 template<typename F> void add_on_orientation_callback(F &&callback) {
160 this->orientation_callback_.add(std::forward<F>(callback));
161 }
162
163 protected:
164 bool write_reg_(RegisterMap reg, uint8_t value) { return this->write_byte(static_cast<uint8_t>(reg), value); }
166 bool read_acceleration_data_(float &x, float &y, float &z);
167 void handle_event_(Event event, bool active, uint32_t now, LazyCallbackManager<void()> &callback);
168#ifdef USE_TEXT_SENSOR
170#endif
171
172 float offset_x_{0}, offset_y_{0}, offset_z_{0};
173 float scale_{0}; // m/s² per LSB after shift_
176#ifdef USE_BINARY_SENSOR
178#endif
184
189 bool low_noise_{false};
190 bool mirror_x_{false}, mirror_y_{false}, mirror_z_{false}, swap_xy_{false};
191 uint8_t shift_{2}; // data is left-justified: 14-bit >> 2, 12-bit (LP1) >> 4
192 uint8_t events_on_{0}; // bit per Event: binary sensor currently published on
193#ifdef USE_TEXT_SENSOR
194 uint8_t sixd_last_{0xFF}; // position bits only; 0xFF cannot occur, so the first read publishes
195#endif
196};
197
198} // namespace esphome::lis2dw12
BedjetMode mode
BedJet operating mode.
This class simplifies creating components that periodically check a state.
Definition component.h:514
Base class for all binary_sensor-type classes.
bool read_byte(uint8_t a_register, uint8_t *data)
Definition lis2dw12.h:126
LazyCallbackManager< void()> orientation_callback_
Definition lis2dw12.h:183
uint32_t last_event_ms_[EVENT_COUNT]
Definition lis2dw12.h:174
void set_tap_binary_sensor(binary_sensor::BinarySensor *s)
Definition lis2dw12.h:131
void publish_orientation_(RegSixdSrc sixd)
Definition lis2dw12.cpp:247
LazyCallbackManager< void()> double_tap_callback_
Definition lis2dw12.h:180
binary_sensor::BinarySensor * event_binary_sensors_[EVENT_COUNT]
Definition lis2dw12.h:177
void add_on_freefall_callback(F &&callback)
Definition lis2dw12.h:153
void set_freefall_binary_sensor(binary_sensor::BinarySensor *s)
Definition lis2dw12.h:135
void add_on_double_tap_callback(F &&callback)
Definition lis2dw12.h:150
void set_active_binary_sensor(binary_sensor::BinarySensor *s)
Definition lis2dw12.h:136
bool write_reg_(RegisterMap reg, uint8_t value)
Definition lis2dw12.h:164
virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
SUB_SENSOR(acceleration_x) SUB_SENSOR(acceleration_y) SUB_SENSOR(acceleration_z) SUB_TEXT_SENSOR(orientation) template< typename F > void add_on_tap_callback(F &&callback)
Definition lis2dw12.h:140
void set_double_tap_binary_sensor(binary_sensor::BinarySensor *s)
Definition lis2dw12.h:132
void set_output_data_rate(DataRate rate)
Definition lis2dw12.h:120
void set_offset(float offset_x, float offset_y, float offset_z)
Definition lis2dw12.cpp:338
void add_on_active_callback(F &&callback)
Definition lis2dw12.h:156
void handle_event_(Event event, bool active, uint32_t now, LazyCallbackManager< void()> &callback)
Definition lis2dw12.cpp:257
LazyCallbackManager< void()> tap_callback_
Definition lis2dw12.h:179
void add_on_orientation_callback(F &&callback)
Definition lis2dw12.h:159
void set_power_mode(PowerMode mode)
Definition lis2dw12.h:118
void set_low_noise(bool low_noise)
Definition lis2dw12.h:119
virtual bool write_byte(uint8_t a_register, uint8_t data)=0
LazyCallbackManager< void()> freefall_callback_
Definition lis2dw12.h:181
LazyCallbackManager< void()> active_callback_
Definition lis2dw12.h:182
void set_filter_bandwidth(FilterBandwidth bw)
Definition lis2dw12.h:122
void set_transform(bool mirror_x, bool mirror_y, bool mirror_z, bool swap_xy)
Definition lis2dw12.cpp:344
bool read_acceleration_data_(float &x, float &y, float &z)
Definition lis2dw12.cpp:211
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
bool z
Definition msa3xx.h:1
Range range
Definition msa3xx.h:0
const void size_t len
Definition hal.h:68
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6
uint8_t orientation
Definition tt21100.cpp:9