Add support for GL-R01 I2C - Time of Flight sensor (#8329)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Petr Kejval 2025-07-09 00:50:45 +02:00 committed by GitHub
parent 05c5364490
commit 4158a5c2a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 169 additions and 0 deletions

View File

@ -170,6 +170,7 @@ esphome/components/ft5x06/* @clydebarrow
esphome/components/ft63x6/* @gpambrozio esphome/components/ft63x6/* @gpambrozio
esphome/components/gcja5/* @gcormier esphome/components/gcja5/* @gcormier
esphome/components/gdk101/* @Szewcson esphome/components/gdk101/* @Szewcson
esphome/components/gl_r01_i2c/* @pkejval
esphome/components/globals/* @esphome/core esphome/components/globals/* @esphome/core
esphome/components/gp2y1010au0f/* @zry98 esphome/components/gp2y1010au0f/* @zry98
esphome/components/gp8403/* @jesserockz esphome/components/gp8403/* @jesserockz

View File

@ -0,0 +1,68 @@
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include "gl_r01_i2c.h"
namespace esphome {
namespace gl_r01_i2c {
static const char *const TAG = "gl_r01_i2c";
// Register definitions from datasheet
static const uint8_t REG_VERSION = 0x00;
static const uint8_t REG_DISTANCE = 0x02;
static const uint8_t REG_TRIGGER = 0x10;
static const uint8_t CMD_TRIGGER = 0xB0;
static const uint8_t RESTART_CMD1 = 0x5A;
static const uint8_t RESTART_CMD2 = 0xA5;
static const uint8_t READ_DELAY = 40; // minimum milliseconds from datasheet to safely read measurement result
void GLR01I2CComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up GL-R01 I2C...");
// Verify sensor presence
if (!this->read_byte_16(REG_VERSION, &this->version_)) {
ESP_LOGE(TAG, "Failed to communicate with GL-R01 I2C sensor!");
this->mark_failed();
return;
}
ESP_LOGD(TAG, "Found GL-R01 I2C with version 0x%04X", this->version_);
}
void GLR01I2CComponent::dump_config() {
ESP_LOGCONFIG(TAG, "GL-R01 I2C:");
ESP_LOGCONFIG(TAG, " Firmware Version: 0x%04X", this->version_);
LOG_I2C_DEVICE(this);
LOG_SENSOR(" ", "Distance", this);
}
void GLR01I2CComponent::update() {
// Trigger a new measurement
if (!this->write_byte(REG_TRIGGER, CMD_TRIGGER)) {
ESP_LOGE(TAG, "Failed to trigger measurement!");
this->status_set_warning();
return;
}
// Schedule reading the result after the read delay
this->set_timeout(READ_DELAY, [this]() { this->read_distance_(); });
}
void GLR01I2CComponent::read_distance_() {
uint16_t distance = 0;
if (!this->read_byte_16(REG_DISTANCE, &distance)) {
ESP_LOGE(TAG, "Failed to read distance value!");
this->status_set_warning();
return;
}
if (distance == 0xFFFF) {
ESP_LOGW(TAG, "Invalid measurement received!");
this->status_set_warning();
} else {
ESP_LOGV(TAG, "Distance: %umm", distance);
this->publish_state(distance);
this->status_clear_warning();
}
}
} // namespace gl_r01_i2c
} // namespace esphome

View File

@ -0,0 +1,22 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace gl_r01_i2c {
class GLR01I2CComponent : public sensor::Sensor, public i2c::I2CDevice, public PollingComponent {
public:
void setup() override;
void dump_config() override;
void update() override;
protected:
void read_distance_();
uint16_t version_{0};
};
} // namespace gl_r01_i2c
} // namespace esphome

View File

@ -0,0 +1,36 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import (
CONF_ID,
DEVICE_CLASS_DISTANCE,
STATE_CLASS_MEASUREMENT,
UNIT_MILLIMETER,
)
CODEOWNERS = ["@pkejval"]
DEPENDENCIES = ["i2c"]
gl_r01_i2c_ns = cg.esphome_ns.namespace("gl_r01_i2c")
GLR01I2CComponent = gl_r01_i2c_ns.class_(
"GLR01I2CComponent", i2c.I2CDevice, cg.PollingComponent
)
CONFIG_SCHEMA = (
sensor.sensor_schema(
GLR01I2CComponent,
unit_of_measurement=UNIT_MILLIMETER,
accuracy_decimals=0,
device_class=DEVICE_CLASS_DISTANCE,
state_class=STATE_CLASS_MEASUREMENT,
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x74))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await sensor.register_sensor(var, config)
await i2c.register_i2c_device(var, config)

View File

@ -0,0 +1,12 @@
i2c:
- id: i2c_gl_r01_i2c
scl: ${scl_pin}
sda: ${sda_pin}
sensor:
- platform: gl_r01_i2c
id: tof
name: "ToF sensor"
i2c_id: i2c_gl_r01_i2c
address: 0x74
update_interval: 15s

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO16
sda_pin: GPIO17
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO16
sda_pin: GPIO17
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
<<: !include common.yaml

View File

@ -0,0 +1,5 @@
substitutions:
scl_pin: GPIO5
sda_pin: GPIO4
<<: !include common.yaml