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;
19static const char *
const TAG =
"tfluna";
20static const char *
const READ_RETRY_TIMEOUT =
"read_retry";
22void TFLuna::dump_config() {
23 ESP_LOGCONFIG(TAG,
"TF-Luna (i2c):");
25 LOG_UPDATE_INTERVAL(
this);
26 ESP_LOGCONFIG(TAG,
" Firmware: %s", this->
version_);
29 LOG_SENSOR(
" ",
"Distance:", this->distance_sensor_);
30 LOG_SENSOR(
" ",
"Temperature:", this->temperature_sensor_);
31 LOG_SENSOR(
" ",
"Signal Strength:", this->signal_strength_sensor_);
32 LOG_SENSOR(
" ",
"Timestamp:", this->timestamp_sensor_);
35 LOG_TEXT_SENSOR(
" ",
"Version:", this->version_text_sensor_);
38 LOG_BUTTON(
" ",
"Factory Reset:", this->reset_button_);
39 LOG_BUTTON(
" ",
"Restart:", this->restart_button_);
46 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
50 if (mode != MODE_TRIGGER) {
51 if (!this->
write_byte(MODE_REGISTER, MODE_TRIGGER)) {
52 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
58 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
64 uint8_t raw_version[3];
65 if (!this->
read_bytes(VERSION_REVISION_REGISTER, raw_version,
sizeof(raw_version))) {
69 snprintf(this->
version_,
sizeof(this->
version_),
"%d.%d.%d", raw_version[2], raw_version[1], raw_version[0]);
71 if (this->version_text_sensor_ !=
nullptr) {
72 this->version_text_sensor_->publish_state(
version_);
80 if (!this->
read_bytes(DISTANCE_LOW_REGISTER, buf,
sizeof(buf))) {
97 if (this->timestamp_sensor_ !=
nullptr) {
98 this->timestamp_sensor_->publish_state(
timestamp);
100 if (this->distance_sensor_ !=
nullptr) {
102 this->distance_sensor_->publish_state(signal_strength < 100 || signal_strength == 0xFFFF ? NAN : distance);
105 if (this->temperature_sensor_ !=
nullptr) {
106 this->temperature_sensor_->publish_state(temperature_raw / (
float) 100);
109 if (this->signal_strength_sensor_ !=
nullptr) {
110 this->signal_strength_sensor_->publish_state(signal_strength);
122 if (this->
attempt_ < MAX_READ_ATTEMPTS) {
132void TFLuna::update() {
135 if (!this->
write_byte(TRIGGER_ONESHOT_REGISTER, 0x01)) {
142void TFLuna::factory_reset() {
143 if (!this->
write_byte(RESTORE_FACTORY_DEFAULTS_REGISTER, 1)) {
144 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
151void TFLuna::restart() {
152 if (!this->
write_byte(SHUTDOWN_REBOOT_REGISTER, 0x02)) {
153 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_()
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.