ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
msa3xx.cpp
Go to the documentation of this file.
1#include "msa3xx.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome::msa3xx {
7
8static const char *const TAG = "msa3xx";
9
10const uint8_t MSA_3XX_PART_ID = 0x13;
11
12const float GRAVITY_EARTH = 9.80665f;
13const float LSB_COEFF = 1000.0f / (GRAVITY_EARTH * 3.9); // LSB to 1 LSB = 3.9mg = 0.0039g
14const float G_OFFSET_MIN = -4.5f; // -127...127 LSB = +- 0.4953g = +- 4.857 m/s^2 => +- 4.5 for the safe
15const float G_OFFSET_MAX = 4.5f; // -127...127 LSB = +- 0.4953g = +- 4.857 m/s^2 => +- 4.5 for the safe
16
17const uint8_t RESOLUTION[] = {14, 12, 10, 8};
18
22
23const char *model_to_string(Model model) {
24 switch (model) {
25 case Model::MSA301:
26 return "MSA301";
27 case Model::MSA311:
28 return "MSA311";
29 default:
30 return "Unknown";
31 }
32}
33
35 switch (power_mode) {
37 return "Normal";
39 return "Low Power";
41 return "Suspend";
42 default:
43 return "Unknown";
44 }
45}
46
48 switch (resolution) {
50 return "14-bit";
52 return "12-bit";
54 return "10-bit";
56 return "8-bit";
57 default:
58 return "Unknown";
59 }
60}
61
63 switch (range) {
64 case Range::RANGE_2G:
65 return "±2g";
66 case Range::RANGE_4G:
67 return "±4g";
68 case Range::RANGE_8G:
69 return "±8g";
71 return "±16g";
72 default:
73 return "Unknown";
74 }
75}
76
77const char *bandwidth_to_string(Bandwidth bandwidth) {
78 switch (bandwidth) {
80 return "1.95 Hz";
82 return "3.9 Hz";
84 return "7.81 Hz";
86 return "15.63 Hz";
88 return "31.25 Hz";
90 return "62.5 Hz";
92 return "125 Hz";
94 return "250 Hz";
96 return "500 Hz";
97 default:
98 return "Unknown";
99 }
100}
101
103 switch (orientation) {
105 return "Portrait Upright";
107 return "Portrait Upside Down";
109 return "Landscape Left";
111 return "Landscape Right";
112 default:
113 return "Unknown";
114 }
115}
116
117const char *orientation_z_to_string(bool orientation) { return orientation ? "Downwards looking" : "Upwards looking"; }
118
120 uint8_t part_id{0xff};
121 if (!this->read_byte(static_cast<uint8_t>(RegisterMap::PART_ID), &part_id) || (part_id != MSA_3XX_PART_ID)) {
122 ESP_LOGE(TAG, "Part ID is wrong or missing. Got 0x%02X", part_id);
123 this->mark_failed();
124 return;
125 }
126
127 // Resolution LSB/g
128 // Range : MSA301 : MSA311
129 // S2g : 1024 (2^10) : 4096 (2^12)
130 // S4g : 512 (2^9) : 2048 (2^11)
131 // S8g : 256 (2^8) : 1024 (2^10)
132 // S16g : 128 (2^7) : 512 (2^9)
133 if (this->model_ == Model::MSA301) {
134 this->device_params_.accel_data_width = 14;
135 this->device_params_.scale_factor_exp = static_cast<uint8_t>(this->range_) - 12;
136 } else if (this->model_ == Model::MSA311) {
137 this->device_params_.accel_data_width = 12;
138 this->device_params_.scale_factor_exp = static_cast<uint8_t>(this->range_) - 10;
139 } else {
140 ESP_LOGE(TAG, "Unknown model");
141 this->mark_failed();
142 return;
143 }
144
145 this->setup_odr_(this->data_rate_);
147 this->setup_range_resolution_(this->range_, this->resolution_); // 2g...16g, 14...8 bit
148 this->setup_offset_(this->offset_x_, this->offset_y_, this->offset_z_); // calibration offsets
149 this->write_byte(static_cast<uint8_t>(RegisterMap::TAP_DURATION), 0b11000100); // set tap duration 250ms
150 this->write_byte(static_cast<uint8_t>(RegisterMap::SWAP_POLARITY), this->swap_.raw); // set axes polarity
151 this->write_byte(static_cast<uint8_t>(RegisterMap::INT_SET_0), 0b01110111); // enable all interrupts
152 this->write_byte(static_cast<uint8_t>(RegisterMap::INT_SET_1), 0b00011000); // including orientation
153}
154
156 ESP_LOGCONFIG(TAG, "MSA3xx:");
157 LOG_I2C_DEVICE(this);
158 if (this->is_failed()) {
159 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
160 }
161 ESP_LOGCONFIG(TAG,
162 " Model: %s\n"
163 " Power Mode: %s\n"
164 " Bandwidth: %s\n"
165 " Range: %s\n"
166 " Resolution: %s\n"
167 " Offsets: {%.3f m/s², %.3f m/s², %.3f m/s²}\n"
168 " Transform: {mirror_x=%s, mirror_y=%s, mirror_z=%s, swap_xy=%s}",
171 this->offset_x_, this->offset_y_, this->offset_z_, YESNO(this->swap_.x_polarity),
172 YESNO(this->swap_.y_polarity), YESNO(this->swap_.z_polarity), YESNO(this->swap_.x_y_swap));
173 LOG_UPDATE_INTERVAL(this);
174
175#ifdef USE_BINARY_SENSOR
176 LOG_BINARY_SENSOR(" ", "Tap", this->tap_binary_sensor_);
177 LOG_BINARY_SENSOR(" ", "Double Tap", this->double_tap_binary_sensor_);
178 LOG_BINARY_SENSOR(" ", "Active", this->active_binary_sensor_);
179#endif
180
181#ifdef USE_SENSOR
182 LOG_SENSOR(" ", "Acceleration X", this->acceleration_x_sensor_);
183 LOG_SENSOR(" ", "Acceleration Y", this->acceleration_y_sensor_);
184 LOG_SENSOR(" ", "Acceleration Z", this->acceleration_z_sensor_);
185#endif
186
187#ifdef USE_TEXT_SENSOR
188 LOG_TEXT_SENSOR(" ", "Orientation XY", this->orientation_xy_text_sensor_);
189 LOG_TEXT_SENSOR(" ", "Orientation Z", this->orientation_z_text_sensor_);
190#endif
191}
192
194 uint8_t accel_data[6];
195 if (!this->read_bytes(static_cast<uint8_t>(RegisterMap::ACC_X_LSB), accel_data, 6)) {
196 return false;
197 }
198
199 auto raw_to_x_bit = [](uint16_t lsb, uint16_t msb, uint8_t data_bits) -> uint16_t {
200 return ((msb << 8) | lsb) >> (16 - data_bits);
201 };
202
203 auto lpf = [](float new_value, float old_value, float alpha = 0.5f) {
204 return alpha * new_value + (1.0f - alpha) * old_value;
205 };
206
207 this->data_.lsb_x =
208 this->twos_complement_(raw_to_x_bit(accel_data[0], accel_data[1], this->device_params_.accel_data_width),
209 this->device_params_.accel_data_width);
210 this->data_.lsb_y =
211 this->twos_complement_(raw_to_x_bit(accel_data[2], accel_data[3], this->device_params_.accel_data_width),
212 this->device_params_.accel_data_width);
213 this->data_.lsb_z =
214 this->twos_complement_(raw_to_x_bit(accel_data[4], accel_data[5], this->device_params_.accel_data_width),
215 this->device_params_.accel_data_width);
216
217 this->data_.x = lpf(ldexp(this->data_.lsb_x, this->device_params_.scale_factor_exp) * GRAVITY_EARTH, this->data_.x);
218 this->data_.y = lpf(ldexp(this->data_.lsb_y, this->device_params_.scale_factor_exp) * GRAVITY_EARTH, this->data_.y);
219 this->data_.z = lpf(ldexp(this->data_.lsb_z, this->device_params_.scale_factor_exp) * GRAVITY_EARTH, this->data_.z);
220
221 return true;
222}
223
225 if (!this->read_byte(static_cast<uint8_t>(RegisterMap::MOTION_INTERRUPT), &this->status_.motion_int.raw)) {
226 return false;
227 }
228
229 if (!this->read_byte(static_cast<uint8_t>(RegisterMap::ORIENTATION_STATUS), &this->status_.orientation.raw)) {
230 return false;
231 }
232
233 return true;
234}
235
237 if (!this->is_ready()) {
238 return;
239 }
240
241 RegMotionInterrupt old_motion_int = this->status_.motion_int;
242
243 if (!this->read_data_() || !this->read_motion_status_()) {
244 this->status_set_warning();
245 return;
246 }
247
248 this->process_motions_(old_motion_int);
249}
250
252 ESP_LOGV(TAG, "Updating");
253
254 if (!this->is_ready()) {
255 ESP_LOGV(TAG, "Not ready for update");
256 return;
257 }
258 ESP_LOGV(TAG, "Acceleration: {x = %+1.3f m/s², y = %+1.3f m/s², z = %+1.3f m/s²}; ", this->data_.x, this->data_.y,
259 this->data_.z);
260
261 ESP_LOGV(TAG, "Orientation: {XY = %s, Z = %s}", orientation_xy_to_string(this->status_.orientation.orient_xy),
262 orientation_z_to_string(this->status_.orientation.orient_z));
263
264#ifdef USE_SENSOR
265 if (this->acceleration_x_sensor_ != nullptr)
266 this->acceleration_x_sensor_->publish_state(this->data_.x);
267 if (this->acceleration_y_sensor_ != nullptr)
268 this->acceleration_y_sensor_->publish_state(this->data_.y);
269 if (this->acceleration_z_sensor_ != nullptr)
270 this->acceleration_z_sensor_->publish_state(this->data_.z);
271#endif
272
273#ifdef USE_TEXT_SENSOR
274 if (this->orientation_xy_text_sensor_ != nullptr &&
275 (this->status_.orientation.orient_xy != this->status_.orientation_old.orient_xy ||
276 this->status_.never_published)) {
277 this->orientation_xy_text_sensor_->publish_state(orientation_xy_to_string(this->status_.orientation.orient_xy));
278 }
279 if (this->orientation_z_text_sensor_ != nullptr &&
280 (this->status_.orientation.orient_z != this->status_.orientation_old.orient_z || this->status_.never_published)) {
281 this->orientation_z_text_sensor_->publish_state(orientation_z_to_string(this->status_.orientation.orient_z));
282 }
283 this->status_.orientation_old = this->status_.orientation;
284#endif
285
286 this->status_.never_published = false;
287 this->status_clear_warning();
288}
289
290void MSA3xxComponent::set_offset(float offset_x, float offset_y, float offset_z) {
291 this->offset_x_ = offset_x;
292 this->offset_y_ = offset_y;
293 this->offset_z_ = offset_z;
294}
295
296void MSA3xxComponent::set_transform(bool mirror_x, bool mirror_y, bool mirror_z, bool swap_xy) {
297 this->swap_.x_polarity = mirror_x;
298 this->swap_.y_polarity = mirror_y;
299 this->swap_.z_polarity = mirror_z;
300 this->swap_.x_y_swap = swap_xy;
301}
302
304 RegOutputDataRate reg_odr;
305 auto reg = this->read_byte(static_cast<uint8_t>(RegisterMap::ODR));
306 if (reg.has_value()) {
307 reg_odr.raw = reg.value();
308 } else {
309 reg_odr.raw = 0x0F; // defaut from datasheet
310 }
311
312 reg_odr.x_axis_disable = false;
313 reg_odr.y_axis_disable = false;
314 reg_odr.z_axis_disable = false;
315 reg_odr.odr = rate;
316
317 this->write_byte(static_cast<uint8_t>(RegisterMap::ODR), reg_odr.raw);
318}
319
321 // 0x11 POWER_MODE_BANDWIDTH
322 auto reg = this->read_byte(static_cast<uint8_t>(RegisterMap::POWER_MODE_BANDWIDTH));
323
324 RegPowerModeBandwidth power_mode_bandwidth;
325 if (reg.has_value()) {
326 power_mode_bandwidth.raw = reg.value();
327 } else {
328 power_mode_bandwidth.raw = 0xde; // defaut from datasheet
329 }
330
331 power_mode_bandwidth.power_mode = power_mode;
332 power_mode_bandwidth.low_power_bandwidth = bandwidth;
333
334 this->write_byte(static_cast<uint8_t>(RegisterMap::POWER_MODE_BANDWIDTH), power_mode_bandwidth.raw);
335}
336
339 reg.raw = this->read_byte(static_cast<uint8_t>(RegisterMap::RANGE_RESOLUTION)).value_or(0x00);
340 reg.range = range;
341 if (this->model_ == Model::MSA301) {
343 }
344 this->write_byte(static_cast<uint8_t>(RegisterMap::RANGE_RESOLUTION), reg.raw);
345}
346
347void MSA3xxComponent::setup_offset_(float offset_x, float offset_y, float offset_z) {
348 uint8_t offset[3];
349
350 auto offset_g_to_lsb = [](float accel) -> int8_t {
351 float acccel_clamped = clamp(accel, G_OFFSET_MIN, G_OFFSET_MAX);
352 return static_cast<int8_t>(acccel_clamped * LSB_COEFF);
353 };
354
355 offset[0] = offset_g_to_lsb(offset_x);
356 offset[1] = offset_g_to_lsb(offset_y);
357 offset[2] = offset_g_to_lsb(offset_z);
358
359 ESP_LOGV(TAG, "Offset (%.3f, %.3f, %.3f)=>LSB(%d, %d, %d)", offset_x, offset_y, offset_z, offset[0], offset[1],
360 offset[2]);
361
362 this->write_bytes(static_cast<uint8_t>(RegisterMap::OFFSET_COMP_X), (uint8_t *) &offset, 3);
363}
364
365int64_t MSA3xxComponent::twos_complement_(uint64_t value, uint8_t bits) {
366 return (int64_t) (value << (64 - bits)) >> (64 - bits);
367}
368
369void binary_event_debounce(bool state, bool old_state, uint32_t now, uint32_t &last_ms, Trigger<> &trigger,
370 uint32_t cooldown_ms, void *bs, const char *desc) {
371 if (state && now - last_ms > cooldown_ms) {
372 ESP_LOGV(TAG, "%s detected", desc);
373 trigger.trigger();
374 last_ms = now;
375#ifdef USE_BINARY_SENSOR
376 if (bs != nullptr) {
377 static_cast<binary_sensor::BinarySensor *>(bs)->publish_state(true);
378 }
379#endif
380 } else if (!state && now - last_ms > cooldown_ms && bs != nullptr) {
381#ifdef USE_BINARY_SENSOR
382 static_cast<binary_sensor::BinarySensor *>(bs)->publish_state(false);
383#endif
384 }
385}
386
387#ifdef USE_BINARY_SENSOR
388#define BS_OPTIONAL_PTR(x) ((void *) (x))
389#else
390#define BS_OPTIONAL_PTR(x) (nullptr)
391#endif
392
394 uint32_t now = millis();
395
396 binary_event_debounce(this->status_.motion_int.single_tap_interrupt, old.single_tap_interrupt, now,
397 this->status_.last_tap_ms, this->tap_trigger_, TAP_COOLDOWN_MS,
398 BS_OPTIONAL_PTR(this->tap_binary_sensor_), "Tap");
399 binary_event_debounce(this->status_.motion_int.double_tap_interrupt, old.double_tap_interrupt, now,
400 this->status_.last_double_tap_ms, this->double_tap_trigger_, DOUBLE_TAP_COOLDOWN_MS,
401 BS_OPTIONAL_PTR(this->double_tap_binary_sensor_), "Double Tap");
402 binary_event_debounce(this->status_.motion_int.active_interrupt, old.active_interrupt, now,
403 this->status_.last_action_ms, this->active_trigger_, ACTIVITY_COOLDOWN_MS,
404 BS_OPTIONAL_PTR(this->active_binary_sensor_), "Activity");
405
406 if (this->status_.motion_int.orientation_interrupt) {
407 ESP_LOGVV(TAG, "Orientation changed");
409 }
410}
411
412} // namespace esphome::msa3xx
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
bool is_ready() const
void status_clear_warning()
Definition component.h:289
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:461
Base class for all binary_sensor-type classes.
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:251
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:217
void process_motions_(RegMotionInterrupt old)
Definition msa3xx.cpp:393
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_
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
void setup_power_mode_bandwidth_(PowerMode power_mode, Bandwidth bandwidth)
Definition msa3xx.cpp:320
struct esphome::msa3xx::MSA3xxComponent::@143 status_
void set_transform(bool mirror_x, bool mirror_y, bool mirror_z, bool swap_xy)
Definition msa3xx.cpp:296
void set_offset(float offset_x, float offset_y, float offset_z)
Definition msa3xx.cpp:290
bool state
Definition fan.h:2
Resolution resolution
Definition msa3xx.h:1
PowerMode power_mode
Definition msa3xx.h:3
Range range
Definition msa3xx.h:0
const char * orientation_xy_to_string(OrientationXY orientation)
Definition msa3xx.cpp:102
const char * range_to_string(Range range)
Definition msa3xx.cpp:62
const uint32_t DOUBLE_TAP_COOLDOWN_MS
Definition msa3xx.cpp:20
const float G_OFFSET_MAX
Definition msa3xx.cpp:15
const float G_OFFSET_MIN
Definition msa3xx.cpp:14
const char * orientation_z_to_string(bool orientation)
Definition msa3xx.cpp:117
void binary_event_debounce(bool state, bool old_state, uint32_t now, uint32_t &last_ms, Trigger<> &trigger, uint32_t cooldown_ms, void *bs, const char *desc)
Definition msa3xx.cpp:369
const char * bandwidth_to_string(Bandwidth bandwidth)
Definition msa3xx.cpp:77
const float GRAVITY_EARTH
Definition msa3xx.cpp:12
const uint32_t ACTIVITY_COOLDOWN_MS
Definition msa3xx.cpp:21
const char * model_to_string(Model model)
Definition msa3xx.cpp:23
const uint32_t TAP_COOLDOWN_MS
Definition msa3xx.cpp:19
const uint8_t RESOLUTION[]
Definition msa3xx.cpp:17
const char * res_to_string(Resolution resolution)
Definition msa3xx.cpp:47
const float LSB_COEFF
Definition msa3xx.cpp:13
const char * power_mode_to_string(PowerMode power_mode)
Definition msa3xx.cpp:34
const uint8_t MSA_3XX_PART_ID
Definition msa3xx.cpp:10
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t
uint8_t orientation
Definition tt21100.cpp:9