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