8static const char *
const TAG =
"gl_r01_i2c";
11static const uint8_t REG_VERSION = 0x00;
12static const uint8_t REG_DISTANCE = 0x02;
13static const uint8_t REG_TRIGGER = 0x10;
14static const uint8_t CMD_TRIGGER = 0xB0;
15static const uint8_t RESTART_CMD1 = 0x5A;
16static const uint8_t RESTART_CMD2 = 0xA5;
17static const uint8_t READ_DELAY = 40;
20 ESP_LOGCONFIG(TAG,
"Setting up GL-R01 I2C...");
23 ESP_LOGE(TAG,
"Failed to communicate with GL-R01 I2C sensor!");
27 ESP_LOGD(TAG,
"Found GL-R01 I2C with version 0x%04X", this->
version_);
31 ESP_LOGCONFIG(TAG,
"GL-R01 I2C:");
32 ESP_LOGCONFIG(TAG,
" Firmware Version: 0x%04X", this->
version_);
34 LOG_SENSOR(
" ",
"Distance",
this);
39 if (!this->
write_byte(REG_TRIGGER, CMD_TRIGGER)) {
40 ESP_LOGE(TAG,
"Failed to trigger measurement!");
50 uint16_t distance = 0;
52 ESP_LOGE(TAG,
"Failed to read distance value!");
57 if (distance == 0xFFFF) {
58 ESP_LOGW(TAG,
"Invalid measurement received!");
61 ESP_LOGV(TAG,
"Distance: %umm", distance);
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void dump_config() override
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
bool read_byte_16(uint8_t a_register, uint16_t *data)
void publish_state(float state)
Publish a new state to the front-end.
Providing packet encoding functions for exchanging data with a remote host.