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;
21static constexpr uint32_t SETUP_TIMEOUT_ID = 0;
22static constexpr uint32_t READ_RETRY_TIMEOUT_ID = 1;
24void TFLuna::dump_config() {
25 ESP_LOGCONFIG(TAG,
"TF-Luna (i2c):");
27 LOG_UPDATE_INTERVAL(
this);
28 ESP_LOGCONFIG(TAG,
" Firmware: %s", this->
version_);
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_);
37 LOG_TEXT_SENSOR(
" ",
"Version:", this->version_text_sensor_);
40 LOG_BUTTON(
" ",
"Factory Reset:", this->reset_button_);
41 LOG_BUTTON(
" ",
"Restart:", this->restart_button_);
48 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
52 if (mode != MODE_TRIGGER) {
53 if (!this->
write_byte(MODE_REGISTER, MODE_TRIGGER)) {
54 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
60 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
66 uint8_t raw_version[3];
67 if (!this->
read_bytes(VERSION_REVISION_REGISTER, raw_version,
sizeof(raw_version))) {
71 snprintf(this->
version_,
sizeof(this->
version_),
"%d.%d.%d", raw_version[2], raw_version[1], raw_version[0]);
73 if (this->version_text_sensor_ !=
nullptr) {
74 this->version_text_sensor_->publish_state(
version_);
82 if (!this->
read_bytes(DISTANCE_LOW_REGISTER, buf,
sizeof(buf))) {
99 if (this->timestamp_sensor_ !=
nullptr) {
100 this->timestamp_sensor_->publish_state(
timestamp);
102 if (this->distance_sensor_ !=
nullptr) {
104 this->distance_sensor_->publish_state(signal_strength < 100 || signal_strength == 0xFFFF ? NAN : distance);
107 if (this->temperature_sensor_ !=
nullptr) {
108 this->temperature_sensor_->publish_state(temperature_raw / (
float) 100);
111 if (this->signal_strength_sensor_ !=
nullptr) {
112 this->signal_strength_sensor_->publish_state(signal_strength);
124 if (this->
attempt_ < MAX_READ_ATTEMPTS) {
134void TFLuna::update() {
137 if (!this->
write_byte(TRIGGER_ONESHOT_REGISTER, 0x01)) {
144void TFLuna::factory_reset() {
145 if (!this->
write_byte(RESTORE_FACTORY_DEFAULTS_REGISTER, 1)) {
146 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
153void TFLuna::restart() {
154 if (!this->
write_byte(SHUTDOWN_REBOOT_REGISTER, 0x02)) {
155 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
BedjetMode mode
BedJet operating mode.
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
void status_set_warning()
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.
void status_clear_warning()
bool write_byte(uint8_t a_register, uint8_t data) const
bool read_byte(uint8_t a_register, uint8_t *data)
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
uint16_t previous_timestamp_
void read_data_timeout_()
ESPHOME_LOG_TAG(TAG, "tfluna")
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.