10static constexpr uint8_t LIS2DW12_WHO_AM_I_VALUE = 0x44;
11static constexpr float GRAVITY_EARTH = 9.80665f;
12static constexpr uint32_t EVENT_COOLDOWN_MS = 500;
13static constexpr uint32_t POLL_INTERVAL_MS = 50;
14static constexpr uint8_t SIXD_POSITION_MASK = 0x3F;
19 return LOG_STR(
"High Performance");
21 return LOG_STR(
"Low Power 1 (12-bit)");
23 return LOG_STR(
"Low Power 2");
25 return LOG_STR(
"Low Power 3");
27 return LOG_STR(
"Low Power 4");
29 return LOG_STR(
"Unknown");
32static const LogString *range_to_string(
Range range) {
35 return LOG_STR(
"±2g");
37 return LOG_STR(
"±4g");
39 return LOG_STR(
"±8g");
41 return LOG_STR(
"±16g");
43 return LOG_STR(
"Unknown");
46static const LogString *data_rate_to_string(
DataRate rate) {
49 return LOG_STR(
"1.6 Hz");
51 return LOG_STR(
"12.5 Hz");
53 return LOG_STR(
"25 Hz");
55 return LOG_STR(
"50 Hz");
57 return LOG_STR(
"100 Hz");
59 return LOG_STR(
"200 Hz");
61 return LOG_STR(
"400 Hz");
63 return LOG_STR(
"800 Hz");
65 return LOG_STR(
"1600 Hz");
67 return LOG_STR(
"Unknown");
73 return LOG_STR(
"ODR/2");
75 return LOG_STR(
"ODR/4");
77 return LOG_STR(
"ODR/10");
79 return LOG_STR(
"ODR/20");
81 return LOG_STR(
"Unknown");
90 if (who_am_i != LIS2DW12_WHO_AM_I_VALUE) {
91 ESP_LOGE(TAG,
"WHO_AM_I check failed. Got 0x%02X, expected 0x%02X", who_am_i, LIS2DW12_WHO_AM_I_VALUE);
105 this->
mark_failed(LOG_STR(
"Soft reset did not complete"));
110 this->
mark_failed(LOG_STR(
"Register configuration failed"));
116 const uint8_t range_idx =
static_cast<uint8_t
>(this->
range_);
117 const float mg = 0.244f * (1u << range_idx) * (lp1 ? 4 : 1);
118 this->
scale_ = mg * 0.001f * GRAVITY_EARTH;
119 this->
shift_ = lp1 ? 4 : 2;
124#ifdef USE_BINARY_SENSOR
126 if (sensor !=
nullptr) {
127 sensor->publish_initial_state(
false);
132#ifdef USE_TEXT_SENSOR
134 if (this->orientation_text_sensor_ !=
nullptr &&
147 const uint8_t odr_bits =
static_cast<uint8_t
>(this->
data_rate_) << 4;
150 ctrl1 = odr_bits | 0x04;
152 ctrl1 = odr_bits | ((
static_cast<uint8_t
>(this->
power_mode_) - 1) & 0x03);
156 uint8_t ctrl6 = (
static_cast<uint8_t
>(this->
filter_bandwidth_) << 6) | (
static_cast<uint8_t
>(this->
range_) << 4);
177 ESP_LOGCONFIG(TAG,
"LIS2DW12:");
182 " Filter Bandwidth: %s\n"
184 " Offsets: {%.3f m/s², %.3f m/s², %.3f m/s²}\n"
185 " Transform: {mirror_x=%s, mirror_y=%s, mirror_z=%s, swap_xy=%s}",
186 LOG_STR_ARG(power_mode_to_string(this->
power_mode_)),
187 LOG_STR_ARG(data_rate_to_string(this->
data_rate_)), LOG_STR_ARG(range_to_string(this->
range_)),
191 LOG_UPDATE_INTERVAL(
this);
193#ifdef USE_BINARY_SENSOR
201 LOG_SENSOR(
" ",
"Acceleration X", this->acceleration_x_sensor_);
202 LOG_SENSOR(
" ",
"Acceleration Y", this->acceleration_y_sensor_);
203 LOG_SENSOR(
" ",
"Acceleration Z", this->acceleration_z_sensor_);
206#ifdef USE_TEXT_SENSOR
207 LOG_TEXT_SENSOR(
" ",
"Orientation", this->orientation_text_sensor_);
212 uint8_t accel_data[6];
217 int16_t raw_x =
static_cast<int16_t
>(
encode_uint16(accel_data[1], accel_data[0])) >> this->
shift_;
218 int16_t raw_y =
static_cast<int16_t
>(
encode_uint16(accel_data[3], accel_data[2])) >> this->
shift_;
219 int16_t raw_z =
static_cast<int16_t
>(
encode_uint16(accel_data[5], accel_data[4])) >> this->
shift_;
222 std::swap(raw_x, raw_y);
230#ifdef USE_TEXT_SENSOR
231static const char *orientation_to_string(
RegSixdSrc sixd) {
248 const uint8_t
position = sixd.
raw & SIXD_POSITION_MASK;
253 this->orientation_text_sensor_->publish_state(orientation_to_string(sixd));
258 const uint8_t bit = 1 << event;
278#ifdef USE_BINARY_SENSOR
311#ifdef USE_TEXT_SENSOR
324 ESP_LOGV(TAG,
"Acceleration: {x = %+1.3f m/s², y = %+1.3f m/s², z = %+1.3f m/s²}",
x,
y,
z);
327 if (this->acceleration_x_sensor_ !=
nullptr)
328 this->acceleration_x_sensor_->publish_state(
x);
329 if (this->acceleration_y_sensor_ !=
nullptr)
330 this->acceleration_y_sensor_->publish_state(
y);
331 if (this->acceleration_z_sensor_ !=
nullptr)
332 this->acceleration_z_sensor_->publish_state(
z);
BedjetMode mode
BedJet operating mode.
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.
void status_set_warning()
void disable_loop()
Disable this component's loop.
void status_clear_warning()
void publish_state(bool new_state)
Publish a new state to the front-end.
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")
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void HOT delay(uint32_t ms)
Application App
Global storage of Application pointer - only one Application can exist.