105 return "Portrait Upright";
107 return "Portrait Upside Down";
109 return "Landscape Left";
111 return "Landscape Right";
151 ESP_LOGCONFIG(TAG,
"MSA3xx:");
152 LOG_I2C_DEVICE(
this);
159 " Offsets: {%.3f m/s², %.3f m/s², %.3f m/s²}\n"
160 " Transform: {mirror_x=%s, mirror_y=%s, mirror_z=%s, swap_xy=%s}",
165 LOG_UPDATE_INTERVAL(
this);
167#ifdef USE_BINARY_SENSOR
168 LOG_BINARY_SENSOR(
" ",
"Tap", this->tap_binary_sensor_);
169 LOG_BINARY_SENSOR(
" ",
"Double Tap", this->double_tap_binary_sensor_);
170 LOG_BINARY_SENSOR(
" ",
"Active", this->active_binary_sensor_);
174 LOG_SENSOR(
" ",
"Acceleration X", this->acceleration_x_sensor_);
175 LOG_SENSOR(
" ",
"Acceleration Y", this->acceleration_y_sensor_);
176 LOG_SENSOR(
" ",
"Acceleration Z", this->acceleration_z_sensor_);
179#ifdef USE_TEXT_SENSOR
180 LOG_TEXT_SENSOR(
" ",
"Orientation XY", this->orientation_xy_text_sensor_);
181 LOG_TEXT_SENSOR(
" ",
"Orientation Z", this->orientation_z_text_sensor_);
186 uint8_t accel_data[6];
191 auto raw_to_x_bit = [](uint16_t lsb, uint16_t msb, uint8_t data_bits) -> uint16_t {
192 return ((msb << 8) | lsb) >> (16 - data_bits);
195 auto lpf = [](
float new_value,
float old_value,
float alpha = 0.5f) {
196 return alpha * new_value + (1.0f - alpha) * old_value;
201 this->device_params_.accel_data_width);
204 this->device_params_.accel_data_width);
207 this->device_params_.accel_data_width);
209 this->
data_.x = lpf(ldexp(this->
data_.lsb_x, this->device_params_.scale_factor_exp) *
GRAVITY_EARTH, this->data_.x);
210 this->
data_.y = lpf(ldexp(this->
data_.lsb_y, this->device_params_.scale_factor_exp) *
GRAVITY_EARTH, this->data_.y);
211 this->
data_.z = lpf(ldexp(this->
data_.lsb_z, this->device_params_.scale_factor_exp) *
GRAVITY_EARTH, this->data_.z);
244 ESP_LOGV(TAG,
"Updating");
247 ESP_LOGV(TAG,
"Not ready for update");
250 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,
257 if (this->acceleration_x_sensor_ !=
nullptr)
258 this->acceleration_x_sensor_->publish_state(this->
data_.x);
259 if (this->acceleration_y_sensor_ !=
nullptr)
260 this->acceleration_y_sensor_->publish_state(this->
data_.y);
261 if (this->acceleration_z_sensor_ !=
nullptr)
262 this->acceleration_z_sensor_->publish_state(this->
data_.z);
265#ifdef USE_TEXT_SENSOR
266 if (this->orientation_xy_text_sensor_ !=
nullptr &&
267 (this->
status_.orientation.orient_xy != this->status_.orientation_old.orient_xy ||
268 this->status_.never_published)) {
271 if (this->orientation_z_text_sensor_ !=
nullptr &&
272 (this->
status_.orientation.orient_z != this->status_.orientation_old.orient_z || this->status_.never_published)) {
278 this->
status_.never_published =
false;
298 if (
reg.has_value()) {
299 reg_odr.
raw =
reg.value();
317 if (
reg.has_value()) {
318 power_mode_bandwidth.
raw =
reg.value();
320 power_mode_bandwidth.
raw = 0xde;
342 auto offset_g_to_lsb = [](
float accel) -> int8_t {
344 return static_cast<int8_t
>(acccel_clamped *
LSB_COEFF);
347 offset[0] = offset_g_to_lsb(offset_x);
348 offset[1] = offset_g_to_lsb(offset_y);
349 offset[2] = offset_g_to_lsb(offset_z);
351 ESP_LOGV(TAG,
"Offset (%.3f, %.3f, %.3f)=>LSB(%d, %d, %d)", offset_x, offset_y, offset_z, offset[0], offset[1],
358 return (int64_t) (value << (64 - bits)) >> (64 - bits);
362 uint32_t cooldown_ms,
void *bs,
const char *desc) {
363 if (
state && now - last_ms > cooldown_ms) {
364 ESP_LOGV(TAG,
"%s detected", desc);
367#ifdef USE_BINARY_SENSOR
372 }
else if (!
state && now - last_ms > cooldown_ms && bs !=
nullptr) {
373#ifdef USE_BINARY_SENSOR
379#ifdef USE_BINARY_SENSOR
380#define BS_OPTIONAL_PTR(x) ((void *) (x))
382#define BS_OPTIONAL_PTR(x) (nullptr)
390 BS_OPTIONAL_PTR(this->tap_binary_sensor_),
"Tap");
393 BS_OPTIONAL_PTR(this->double_tap_binary_sensor_),
"Double Tap");
396 BS_OPTIONAL_PTR(this->active_binary_sensor_),
"Activity");
398 if (this->
status_.motion_int.orientation_interrupt) {
399 ESP_LOGVV(TAG,
"Orientation changed");
void mark_failed()
Mark this component as failed.
void status_set_warning()
void status_clear_warning()
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Base class for all binary_sensor-type classes.
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.
bool write_byte(uint8_t a_register, uint8_t data) const
bool read_byte(uint8_t a_register, uint8_t *data)
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
void process_motions_(RegMotionInterrupt old)
Trigger orientation_trigger_
int64_t twos_complement_(uint64_t value, uint8_t bits)
struct esphome::msa3xx::MSA3xxComponent::@146 device_params_
void setup_odr_(DataRate rate)
bool read_motion_status_()
void setup_range_resolution_(Range range, Resolution resolution)
void setup_offset_(float offset_x, float offset_y, float offset_z)
void dump_config() override
void setup_power_mode_bandwidth_(PowerMode power_mode, Bandwidth bandwidth)
struct esphome::msa3xx::MSA3xxComponent::@148 status_
struct esphome::msa3xx::MSA3xxComponent::@147 data_
void set_transform(bool mirror_x, bool mirror_y, bool mirror_z, bool swap_xy)
void set_offset(float offset_x, float offset_y, float offset_z)
const char * orientation_xy_to_string(OrientationXY orientation)
const char * range_to_string(Range range)
const uint32_t DOUBLE_TAP_COOLDOWN_MS
const char * orientation_z_to_string(bool orientation)
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)
const char * bandwidth_to_string(Bandwidth bandwidth)
const float GRAVITY_EARTH
const uint32_t ACTIVITY_COOLDOWN_MS
const char * model_to_string(Model model)
const uint32_t TAP_COOLDOWN_MS
ESPHOME_LOG_TAG(TAG, "msa3xx")
const uint8_t RESOLUTION[]
const char * res_to_string(Resolution resolution)
const char * power_mode_to_string(PowerMode power_mode)
const uint8_t MSA_3XX_PART_ID
uint32_t IRAM_ATTR HOT millis()
bool single_tap_interrupt
bool double_tap_interrupt
Bandwidth low_power_bandwidth