ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
tfluna.cpp
Go to the documentation of this file.
1#include "tfluna.h"
2#include <cstddef>
3#include "esphome/core/log.h"
5
6namespace esphome::tfluna {
7
8// see https://files.waveshare.com/upload/a/ac/SJ-PM-TF-Luna_A05_Product_Manual.pdf
9static constexpr uint8_t SAVE_REGISTER = 0x20;
10static constexpr uint8_t VERSION_REVISION_REGISTER = 0x0A;
11static constexpr uint8_t DISTANCE_LOW_REGISTER = 0x00;
12static constexpr uint8_t RESTORE_FACTORY_DEFAULTS_REGISTER = 0x29;
13static constexpr uint8_t SHUTDOWN_REBOOT_REGISTER = 0x21;
14static constexpr uint8_t MODE_REGISTER = 0x23;
15static constexpr uint8_t MODE_TRIGGER = 0x01;
16static constexpr uint8_t TRIGGER_ONESHOT_REGISTER = 0x24;
17static constexpr uint8_t MAX_READ_ATTEMPTS = 5;
18static constexpr uint8_t READ_RETRY_MS = 5;
19ESPHOME_LOG_TAG(TAG, "tfluna");
20
21static constexpr uint32_t SETUP_TIMEOUT_ID = 0;
22static constexpr uint32_t READ_RETRY_TIMEOUT_ID = 1;
23
24void TFLuna::dump_config() {
25 ESP_LOGCONFIG(TAG, "TF-Luna (i2c):");
26 LOG_I2C_DEVICE(this);
27 LOG_UPDATE_INTERVAL(this);
28 ESP_LOGCONFIG(TAG, " Firmware: %s", this->version_);
29
30#ifdef USE_SENSOR
31 LOG_SENSOR(" ", "Distance:", this->distance_sensor_);
32 LOG_SENSOR(" ", "Temperature:", this->temperature_sensor_);
33 LOG_SENSOR(" ", "Signal Strength:", this->signal_strength_sensor_);
34 LOG_SENSOR(" ", "Timestamp:", this->timestamp_sensor_);
35#endif
36#ifdef USE_TEXT_SENSOR
37 LOG_TEXT_SENSOR(" ", "Version:", this->version_text_sensor_);
38#endif
39#ifdef USE_BUTTON
40 LOG_BUTTON(" ", "Factory Reset:", this->reset_button_);
41 LOG_BUTTON(" ", "Restart:", this->restart_button_);
42#endif
43}
44
46 uint8_t mode;
47 if (!this->read_byte(MODE_REGISTER, &mode)) {
48 return false;
49 }
50 if (mode != MODE_TRIGGER) {
51 if (!this->write_byte(MODE_REGISTER, MODE_TRIGGER)) {
52 return false;
53 }
54
55 if (!this->write_byte(SAVE_REGISTER, 1)) {
56 return false;
57 }
58 }
59
60 uint8_t raw_version[3];
61 if (!this->read_bytes(VERSION_REVISION_REGISTER, raw_version, sizeof(raw_version))) {
62 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
63 return true;
64 } else {
65 snprintf(this->version_, sizeof(this->version_), "%d.%d.%d", raw_version[2], raw_version[1], raw_version[0]);
66#ifdef USE_TEXT_SENSOR
67 PUBLISH_STATE(this->version_text_sensor_, this->version_);
68#endif
69 }
70 return true;
71}
72
73void TFLuna::setup() {
74 if (!this->configure_()) {
75 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
76 }
77}
78
80 this->reconfigure_pending_ = !this->configure_();
81 if (this->reconfigure_pending_) {
82 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
83 }
84}
85
87 uint8_t buf[8];
88 if (!this->read_bytes(DISTANCE_LOW_REGISTER, buf, sizeof(buf))) {
89 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
90 return false;
91 }
92 // Layout:
93 // buf[0..1] = distance (LE), buf[2..3] = signal (LE),
94 // buf[4..5] = temperature (LE), buf[6..7] = timestamp (LE)
95 uint16_t distance = encode_value_le<uint16_t>(buf);
96 uint16_t signal_strength = encode_value_le<uint16_t>(&buf[2]);
97 int16_t temperature_raw = encode_value_le<int16_t>(&buf[4]);
98 uint16_t timestamp = encode_value_le<uint16_t>(&buf[6]);
99
100 if (timestamp == this->previous_timestamp_) {
101 return false;
102 }
103
104#ifdef USE_SENSOR
105 PUBLISH_STATE(this->timestamp_sensor_, timestamp);
106 // distance is unreliable when strength is below 100 or saturated at 0xFFFF
107 PUBLISH_STATE(this->distance_sensor_, signal_strength < 100 || signal_strength == 0xFFFF ? NAN : distance);
108
109 PUBLISH_STATE(this->temperature_sensor_, temperature_raw / (float) 100);
110
111 PUBLISH_STATE(this->signal_strength_sensor_, signal_strength);
112#endif
113 this->status_clear_warning();
115 return true;
116}
117
119 if (this->read_data_()) {
120 this->attempt_ = 0;
121 } else {
122 if (this->attempt_ < MAX_READ_ATTEMPTS) {
123 this->attempt_++;
124 this->set_timeout(READ_RETRY_TIMEOUT_ID, READ_RETRY_MS, [this]() { this->read_data_timeout_(); });
125 } else {
126 this->status_set_warning(LOG_STR("Hung device, restarting..."));
127 this->restart();
128 }
129 }
130}
131
132void TFLuna::update() {
133 if (this->is_failed()) {
134 return;
135 }
136 this->cancel_timeout(READ_RETRY_TIMEOUT_ID);
137 this->attempt_ = 0;
138 if (this->reconfigure_pending_) {
139 this->reconfigure_();
140 if (this->reconfigure_pending_) {
141 return;
142 }
143 }
144 if (!this->write_byte(TRIGGER_ONESHOT_REGISTER, 0x01)) {
145 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
146 return;
147 }
148 this->read_data_timeout_();
149}
150
151void TFLuna::factory_reset() {
152 if (this->is_failed()) {
153 return;
154 }
155 if (!this->write_byte(RESTORE_FACTORY_DEFAULTS_REGISTER, 1)) {
156 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
157 return;
158 }
159 this->reconfigure_pending_ = false;
160 this->status_set_warning(LOG_STR("Factory reset issued; waiting for device to become ready"));
161 this->set_timeout(SETUP_TIMEOUT_ID, 100, [this]() { this->reconfigure_(); });
162}
163
164void TFLuna::restart() {
165 if (this->is_failed()) {
166 return;
167 }
168 if (!this->write_byte(SHUTDOWN_REBOOT_REGISTER, 0x02)) {
169 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
170 return;
171 }
172}
173
174} // namespace esphome::tfluna
BedjetMode mode
BedJet operating mode.
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool is_failed() const
Definition component.h:287
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:309
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
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:249
uint16_t previous_timestamp_
Definition tfluna.h:48
ESPHOME_LOG_TAG(TAG, "tfluna")
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...
Definition helpers.h:1084
static void uint32_t
uint16_t timestamp
Definition tt21100.cpp:2