ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
msa3xx.h
Go to the documentation of this file.
1#pragma once
7#ifdef USE_BINARY_SENSOR
9#endif
10#ifdef USE_SENSOR
12#endif
13#ifdef USE_TEXT_SENSOR
15#endif
16
17namespace esphome::msa3xx {
18
19// Combined register map of MSA301 and MSA311
20// Differences
21// What | MSA301 | MSA11 |
22// - Resolution | 14-bit | 12-bit |
23//
24
25// I2c address
26enum class Model : uint8_t {
27 MSA301 = 0x26,
28 MSA311 = 0x62,
29};
30
31// Combined MSA301 and MSA311 register map
32enum class RegisterMap : uint8_t {
33 SOFT_RESET = 0x00,
34 PART_ID = 0x01,
35 ACC_X_LSB = 0x02,
36 ACC_X_MSB = 0x03,
37 ACC_Y_LSB = 0x04,
38 ACC_Y_MSB = 0x05,
39 ACC_Z_LSB = 0x06,
40 ACC_Z_MSB = 0x07,
41 MOTION_INTERRUPT = 0x09,
42 DATA_INTERRUPT = 0x0A,
43 TAP_ACTIVE_STATUS = 0x0B,
44 ORIENTATION_STATUS = 0x0C,
46 RANGE_RESOLUTION = 0x0F,
47 ODR = 0x10,
49 SWAP_POLARITY = 0x12,
50 INT_SET_0 = 0x16,
51 INT_SET_1 = 0x17,
52 INT_MAP_0 = 0x19,
53 INT_MAP_1 = 0x1A,
54 INT_CONFIG = 0x20,
55 INT_LATCH = 0x21,
56 FREEFALL_DURATION = 0x22,
57 FREEFALL_THRESHOLD = 0x23,
59 ACTIVE_DURATION = 0x27,
60 ACTIVE_THRESHOLD = 0x28,
61 TAP_DURATION = 0x2A,
62 TAP_THRESHOLD = 0x2B,
63 ORIENTATION_CONFIG = 0x2C,
64 Z_BLOCK = 0x2D,
65 OFFSET_COMP_X = 0x38,
66 OFFSET_COMP_Y = 0x39,
67 OFFSET_COMP_Z = 0x3A,
68};
69
70enum class Range : uint8_t {
71 RANGE_2G = 0b00,
72 RANGE_4G = 0b01,
73 RANGE_8G = 0b10,
74 RANGE_16G = 0b11,
75};
76
77enum class Resolution : uint8_t {
78 RES_14BIT = 0b00,
79 RES_12BIT = 0b01,
80 RES_10BIT = 0b10,
81 RES_8BIT = 0b11,
82};
83
84enum class PowerMode : uint8_t {
85 NORMAL = 0b00,
86 LOW_POWER = 0b01,
87 SUSPEND = 0b11,
88};
89
90enum class Bandwidth : uint8_t {
91 BW_1_95HZ = 0b0000,
92 BW_3_9HZ = 0b0011,
93 BW_7_81HZ = 0b0100,
94 BW_15_63HZ = 0b0101,
95 BW_31_25HZ = 0b0110,
96 BW_62_5HZ = 0b0111,
97 BW_125HZ = 0b1000,
98 BW_250HZ = 0b1001,
99 BW_500HZ = 0b1010,
100};
101
102enum class DataRate : uint8_t {
103 ODR_1HZ = 0b0000, // not available in normal mode
104 ODR_1_95HZ = 0b0001, // not available in normal mode
105 ODR_3_9HZ = 0b0010,
106 ODR_7_81HZ = 0b0011,
107 ODR_15_63HZ = 0b0100,
108 ODR_31_25HZ = 0b0101,
109 ODR_62_5HZ = 0b0110,
110 ODR_125HZ = 0b0111,
111 ODR_250HZ = 0b1000,
112 ODR_500HZ = 0b1001, // not available in low power mode
113 ODR_1000HZ = 0b1010, // not available in low power mode
114};
115
116enum class OrientationXY : uint8_t {
117 PORTRAIT_UPRIGHT = 0b00,
119 LANDSCAPE_LEFT = 0b10,
120 LANDSCAPE_RIGHT = 0b11,
121};
122
124 struct {
126 bool z : 1;
127 uint8_t reserved : 5;
128 } __attribute__((packed));
129 uint8_t raw;
130};
131
132// 0x09
134 struct {
136 bool reserved_1 : 1;
138 bool reserved_3 : 1;
142 bool reserved_7 : 1;
143 } __attribute__((packed));
144 uint8_t raw;
145};
146
147// 0x0C
149 struct {
150 uint8_t reserved_0_3 : 4;
152 bool orient_z : 1;
153 uint8_t reserved_7 : 1;
154 } __attribute__((packed));
155 uint8_t raw{0x00};
156};
157
158// 0x0f
160 struct {
163 uint8_t reserved_2 : 4;
164 } __attribute__((packed));
165 uint8_t raw{0x00};
166};
167
168// 0x10
170 struct {
172 uint8_t reserved_4 : 1;
176 } __attribute__((packed));
177 uint8_t raw{0xde};
178};
179
180// 0x11
182 struct {
183 uint8_t reserved_0 : 1;
185 uint8_t reserved_5 : 1;
187 } __attribute__((packed));
188 uint8_t raw{0xde};
189};
190
191// 0x12
193 struct {
194 bool x_y_swap : 1;
195 bool z_polarity : 1;
196 bool y_polarity : 1;
197 bool x_polarity : 1;
198 uint8_t reserved : 4;
199 } __attribute__((packed));
200 uint8_t raw{0};
201};
202
203// 0x2a
205 struct {
206 uint8_t duration : 3;
207 uint8_t reserved : 3;
208 bool tap_shock : 1;
209 bool tap_quiet : 1;
210 } __attribute__((packed));
211 uint8_t raw{0x04};
212};
213
215 public:
216 void setup() override;
217 void dump_config() override;
218
219 void loop() override;
220 void update() override;
221
222 void set_model(Model model) { this->model_ = model; }
223 void set_offset(float offset_x, float offset_y, float offset_z);
224 void set_range(Range range) { this->range_ = range; }
225 void set_bandwidth(Bandwidth bandwidth) { this->bandwidth_ = bandwidth; }
227 void set_transform(bool mirror_x, bool mirror_y, bool mirror_z, bool swap_xy);
228
229#ifdef USE_BINARY_SENSOR
231 SUB_BINARY_SENSOR(double_tap)
232 SUB_BINARY_SENSOR(active)
233#endif
234
235#ifdef USE_SENSOR
236 SUB_SENSOR(acceleration_x)
237 SUB_SENSOR(acceleration_y)
238 SUB_SENSOR(acceleration_z)
239#endif
240
241#ifdef USE_TEXT_SENSOR
242 SUB_TEXT_SENSOR(orientation_xy)
243 SUB_TEXT_SENSOR(orientation_z)
244#endif
245
246 Trigger<> *get_tap_trigger() { return &this->tap_trigger_; }
251
252 protected:
254
260 float offset_x_, offset_y_, offset_z_; // in m/s²
262
263 struct {
267
268 struct {
269 int16_t lsb_x, lsb_y, lsb_z;
270 float x, y, z;
271 } data_{};
272
273 struct {
277
281
282 bool never_published{true};
284
285 void setup_odr_(DataRate rate);
288 void setup_offset_(float offset_x, float offset_y, float offset_z);
289
290 bool read_data_();
291 bool read_motion_status_();
292
293 int64_t twos_complement_(uint64_t value, uint8_t bits);
294
295 //
296 // Actons / Triggers
297 //
303
305};
306
307} // namespace esphome::msa3xx
This class simplifies creating components that periodically check a state.
Definition component.h:585
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
void set_model(Model model)
Definition msa3xx.h:222
void process_motions_(RegMotionInterrupt old)
Definition msa3xx.cpp:393
void set_resolution(Resolution resolution)
Definition msa3xx.h:226
void set_bandwidth(Bandwidth bandwidth)
Definition msa3xx.h:225
int64_t twos_complement_(uint64_t value, uint8_t bits)
Definition msa3xx.cpp:365
void setup_odr_(DataRate rate)
Definition msa3xx.cpp:303
struct esphome::msa3xx::MSA3xxComponent::@141 device_params_
RegOrientationStatus orientation_old
Definition msa3xx.h:276
void setup_range_resolution_(Range range, Resolution resolution)
Definition msa3xx.cpp:337
struct esphome::msa3xx::MSA3xxComponent::@142 data_
void setup_offset_(float offset_x, float offset_y, float offset_z)
Definition msa3xx.cpp:347
RegMotionInterrupt motion_int
Definition msa3xx.h:274
void setup_power_mode_bandwidth_(PowerMode power_mode, Bandwidth bandwidth)
Definition msa3xx.cpp:320
RegOrientationStatus orientation
Definition msa3xx.h:275
struct esphome::msa3xx::MSA3xxComponent::@143 status_
Trigger * get_orientation_trigger()
Definition msa3xx.h:248
SUB_BINARY_SENSOR(tap) SUB_BINARY_SENSOR(double_tap) SUB_BINARY_SENSOR(active) SUB_SENSOR(acceleration_x) SUB_SENSOR(acceleration_y) SUB_SENSOR(acceleration_z) SUB_TEXT_SENSOR(orientation_xy) SUB_TEXT_SENSOR(orientation_z) Trigger<> *get_tap_trigger()
Definition msa3xx.h:230
void set_transform(bool mirror_x, bool mirror_y, bool mirror_z, bool swap_xy)
Definition msa3xx.cpp:296
void set_range(Range range)
Definition msa3xx.h:224
void set_offset(float offset_x, float offset_y, float offset_z)
Definition msa3xx.cpp:290
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:32
Resolution resolution
Definition msa3xx.h:1
PowerMode power_mode
Definition msa3xx.h:3
Range range
Definition msa3xx.h:0
static void uint32_t