11static constexpr uint8_t LIS2DW12_WHO_AM_I_VALUE = 0x44;
12static constexpr float GRAVITY_EARTH = 9.80665f;
13static constexpr uint32_t EVENT_COOLDOWN_MS = 500;
14static constexpr uint32_t POLL_INTERVAL_MS = 50;
15static constexpr uint8_t SIXD_POSITION_MASK = 0x3F;
18PROGMEM_STRING_TABLE(PowerModeStrings,
"High Performance",
"Low Power 1 (12-bit)",
"Low Power 2",
"Low Power 3",
19 "Low Power 4",
"Unknown");
25PROGMEM_STRING_TABLE(DataRateStrings,
"Unknown",
"1.6 Hz",
"12.5 Hz",
"25 Hz",
"50 Hz",
"100 Hz",
"200 Hz",
"400 Hz",
35 if (who_am_i != LIS2DW12_WHO_AM_I_VALUE) {
36 ESP_LOGE(TAG,
"WHO_AM_I check failed. Got 0x%02X, expected 0x%02X", who_am_i, LIS2DW12_WHO_AM_I_VALUE);
48 this->
mark_failed(LOG_STR(
"Soft reset did not complete"));
57 const uint8_t range_idx =
static_cast<uint8_t
>(this->
range_);
58 const float mg = 0.244f * (1u << range_idx) * (lp1 ? 4 : 1);
59 this->
scale_ = mg * 0.001f * GRAVITY_EARTH;
60 this->
shift_ = lp1 ? 4 : 2;
65#ifdef USE_BINARY_SENSOR
67 if (sensor !=
nullptr) {
68 sensor->publish_initial_state(
false);
75 if (this->orientation_text_sensor_ !=
nullptr &&
88 const uint8_t odr_bits =
static_cast<uint8_t
>(this->
data_rate_) << 4;
91 ctrl1 = odr_bits | 0x04;
93 ctrl1 = odr_bits | ((
static_cast<uint8_t
>(this->
power_mode_) - 1) & 0x03);
97 uint8_t ctrl6 = (
static_cast<uint8_t
>(this->
filter_bandwidth_) << 6) | (
static_cast<uint8_t
>(this->
range_) << 4);
118 ESP_LOGCONFIG(TAG,
"LIS2DW12:");
123 " Filter Bandwidth: %s\n"
125 " Offsets: {%.3f m/s², %.3f m/s², %.3f m/s²}\n"
126 " Transform: {mirror_x=%s, mirror_y=%s, mirror_z=%s, swap_xy=%s}",
127 LOG_STR_ARG(PowerModeStrings::get_log_str(this->
power_mode_)),
128 LOG_STR_ARG(DataRateStrings::get_log_str(this->
data_rate_, 0)),
129 LOG_STR_ARG(RangeStrings::get_log_str(this->
range_)),
133 LOG_UPDATE_INTERVAL(
this);
135#ifdef USE_BINARY_SENSOR
143 LOG_SENSOR(
" ",
"Acceleration X", this->acceleration_x_sensor_);
144 LOG_SENSOR(
" ",
"Acceleration Y", this->acceleration_y_sensor_);
145 LOG_SENSOR(
" ",
"Acceleration Z", this->acceleration_z_sensor_);
148#ifdef USE_TEXT_SENSOR
149 LOG_TEXT_SENSOR(
" ",
"Orientation", this->orientation_text_sensor_);
154 uint8_t accel_data[6];
164 std::swap(raw_x, raw_y);
172#ifdef USE_TEXT_SENSOR
173static const char *orientation_to_string(
RegSixdSrc sixd) {
190 const uint8_t
position = sixd.
raw & SIXD_POSITION_MASK;
195 this->orientation_text_sensor_->publish_state(orientation_to_string(sixd));
200 const uint8_t bit = 1 << event;
220#ifdef USE_BINARY_SENSOR
248#ifdef USE_TEXT_SENSOR
259 ESP_LOGV(TAG,
"Acceleration: {x = %+1.3f m/s², y = %+1.3f m/s², z = %+1.3f m/s²}",
x,
y,
z);
262 PUBLISH_STATE(this->acceleration_x_sensor_,
x);
263 PUBLISH_STATE(this->acceleration_y_sensor_,
y);
264 PUBLISH_STATE(this->acceleration_z_sensor_,
z);
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
void disable_loop()
Disable this component's loop.
bool read_byte(uint8_t a_register, uint8_t *data)
LazyCallbackManager< void()> orientation_callback_
uint32_t last_event_ms_[EVENT_COUNT]
void publish_orientation_(RegSixdSrc sixd)
bool configure_registers_()
LazyCallbackManager< void()> double_tap_callback_
binary_sensor::BinarySensor * event_binary_sensors_[EVENT_COUNT]
bool write_reg_(RegisterMap reg, uint8_t value)
virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
void set_offset(float offset_x, float offset_y, float offset_z)
void handle_event_(Event event, bool active, uint32_t now, LazyCallbackManager< void()> &callback)
LazyCallbackManager< void()> tap_callback_
void dump_config() override
LazyCallbackManager< void()> freefall_callback_
FilterBandwidth filter_bandwidth_
LazyCallbackManager< void()> active_callback_
void set_transform(bool mirror_x, bool mirror_y, bool mirror_z, bool swap_xy)
bool read_acceleration_data_(float &x, float &y, float &z)
ESPHOME_LOG_TAG(TAG, "lis2dw12")
PROGMEM_STRING_TABLE(PowerModeStrings, "High Performance", "Low Power 1 (12-bit)", "Low Power 2", "Low Power 3", "Low Power 4", "Unknown")
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
void HOT delay(uint32_t ms)
Application App
Global storage of Application pointer - only one Application can exist.