ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
lis3dh.h
Go to the documentation of this file.
1#pragma once
2
7
8namespace esphome::lis3dh {
9
10// Register map (see ST datasheet DocID18198)
11static constexpr uint8_t LIS3DH_REG_OUT_ADC3_L = 0x0C; // temperature (ADC3) low byte
12static constexpr uint8_t LIS3DH_REG_WHO_AM_I = 0x0F;
13static constexpr uint8_t LIS3DH_REG_TEMP_CFG = 0x1F;
14static constexpr uint8_t LIS3DH_REG_CTRL_REG1 = 0x20; // ODR / low-power / axis enable
15static constexpr uint8_t LIS3DH_REG_CTRL_REG2 = 0x21; // high-pass filter
16static constexpr uint8_t LIS3DH_REG_CTRL_REG3 = 0x22; // INT1 pad routing
17static constexpr uint8_t LIS3DH_REG_CTRL_REG4 = 0x23; // BDU / full-scale / high-resolution
18static constexpr uint8_t LIS3DH_REG_CTRL_REG5 = 0x24; // interrupt latching
19static constexpr uint8_t LIS3DH_REG_CTRL_REG6 = 0x25; // INT2 pad routing / polarity
20static constexpr uint8_t LIS3DH_REG_REFERENCE = 0x26; // high-pass filter reference
21static constexpr uint8_t LIS3DH_REG_OUT_X_L = 0x28; // acceleration data block (auto-increment)
22static constexpr uint8_t LIS3DH_REG_INT1_CFG = 0x30;
23static constexpr uint8_t LIS3DH_REG_INT1_SRC = 0x31;
24static constexpr uint8_t LIS3DH_REG_INT1_THS = 0x32;
25static constexpr uint8_t LIS3DH_REG_INT1_DURATION = 0x33;
26
27static constexpr uint8_t LIS3DH_WHO_AM_I_VALUE = 0x33;
28
29// Sub-address auto-increment bit: OR into the register for multi-byte reads.
30static constexpr uint8_t LIS3DH_AUTO_INCREMENT = 0x80;
31
32// CTRL_REG1
33static constexpr uint8_t LIS3DH_CTRL_REG1_AXES_EN = 0x07; // Xen | Yen | Zen
34static constexpr uint8_t LIS3DH_CTRL_REG1_LPEN = 0x08; // low-power enable
35// CTRL_REG2
36static constexpr uint8_t LIS3DH_CTRL_REG2_HP_IA1 = 0x01; // high-pass filter the IA1 interrupt
37// CTRL_REG4
38static constexpr uint8_t LIS3DH_CTRL_REG4_HR = 0x08; // high-resolution enable
39static constexpr uint8_t LIS3DH_CTRL_REG4_BDU = 0x80; // block data update
40// CTRL_REG5
41static constexpr uint8_t LIS3DH_CTRL_REG5_LIR_INT1 = 0x08; // latch IA1 interrupt
42// CTRL_REG3 / CTRL_REG6 interrupt routing
43static constexpr uint8_t LIS3DH_CTRL_REG3_I1_IA1 = 0x40; // route IA1 to INT1 pad
44static constexpr uint8_t LIS3DH_CTRL_REG6_I2_IA1 = 0x40; // route IA1 to INT2 pad
45static constexpr uint8_t LIS3DH_CTRL_REG6_INT_POLARITY = 0x02; // 1 = active low
46// TEMP_CFG_REG
47static constexpr uint8_t LIS3DH_TEMP_CFG_ADC_EN = 0x80;
48static constexpr uint8_t LIS3DH_TEMP_CFG_TEMP_EN = 0x40;
49
50// The temperature sensor is uncalibrated and reports relative changes only,
51// so this fixed reference is just a rough starting point for the °C output.
52static constexpr float LIS3DH_TEMP_REFERENCE_C = 25.0f;
53
54// Full-scale range: value is the index into the scale tables and the
55// FS field of CTRL_REG4 (shifted left by 4).
62
63// Output data rate: value is the ODR field of CTRL_REG1 (bits 7:4).
75
76// Operating mode: selects the resolution (and thus power usage).
77enum LIS3DHOperatingMode : uint8_t {
79 LIS3DH_MODE_NORMAL = 1, // 10-bit
81};
82
83// Physical interrupt pad the motion interrupt is routed to.
88
90 public:
91 void setup() override;
92 void dump_config() override;
93 void on_shutdown() override;
94
96 void set_data_rate(LIS3DHDataRate data_rate) { this->data_rate_ = data_rate; }
98 // threshold is the INT1_THS value and axes_cfg the INT1_CFG mask, both computed in Python.
99 void set_interrupt(LIS3DHInterruptPin pin, uint8_t threshold, uint8_t duration, uint8_t axes_cfg, bool latched,
100 bool active_high, bool high_pass) {
101 this->interrupt_pin_ = pin;
102 this->interrupt_threshold_ = threshold;
104 this->interrupt_axes_cfg_ = axes_cfg;
105 this->interrupt_latched_ = latched;
106 this->interrupt_active_high_ = active_high;
107 this->interrupt_high_pass_ = high_pass;
108 }
109
110 template<typename F> void add_temperature_listener(F &&cb) { this->temperature_callback_.add(std::forward<F>(cb)); }
111
112 protected:
113 bool update_data(motion::MotionData &data) override;
115 bool setup_temperature_();
116 bool setup_interrupt_();
118
122
123 // Motion (activity) interrupt configuration; a zero axis mask means disabled.
131
133};
134
135} // namespace esphome::lis3dh
BedjetMode mode
BedJet operating mode.
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
LIS3DHInterruptPin interrupt_pin_
Definition lis3dh.h:124
LIS3DHOperatingMode operating_mode_
Definition lis3dh.h:121
void set_data_rate(LIS3DHDataRate data_rate)
Definition lis3dh.h:96
void add_temperature_listener(F &&cb)
Definition lis3dh.h:110
LazyCallbackManager< void(float)> temperature_callback_
Definition lis3dh.h:132
bool update_data(motion::MotionData &data) override
Definition lis3dh.cpp:151
void set_interrupt(LIS3DHInterruptPin pin, uint8_t threshold, uint8_t duration, uint8_t axes_cfg, bool latched, bool active_high, bool high_pass)
Definition lis3dh.h:99
void set_range(LIS3DHRange range)
Definition lis3dh.h:95
void set_operating_mode(LIS3DHOperatingMode mode)
Definition lis3dh.h:97
Range range
Definition msa3xx.h:0
uint8_t duration
Definition msa3xx.h:0
@ LIS3DH_INT_PIN_INT1
Definition lis3dh.h:85
@ LIS3DH_INT_PIN_INT2
Definition lis3dh.h:86
@ LIS3DH_DATA_RATE_200HZ
Definition lis3dh.h:70
@ LIS3DH_DATA_RATE_10HZ
Definition lis3dh.h:66
@ LIS3DH_DATA_RATE_50HZ
Definition lis3dh.h:68
@ LIS3DH_DATA_RATE_100HZ
Definition lis3dh.h:69
@ LIS3DH_DATA_RATE_1620HZ
Definition lis3dh.h:72
@ LIS3DH_DATA_RATE_25HZ
Definition lis3dh.h:67
@ LIS3DH_DATA_RATE_1HZ
Definition lis3dh.h:65
@ LIS3DH_DATA_RATE_1344HZ
Definition lis3dh.h:73
@ LIS3DH_DATA_RATE_400HZ
Definition lis3dh.h:71
@ LIS3DH_MODE_LOW_POWER
Definition lis3dh.h:78
@ LIS3DH_MODE_HIGH_RESOLUTION
Definition lis3dh.h:80
@ LIS3DH_MODE_NORMAL
Definition lis3dh.h:79