mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	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:
		
							
								
								
									
										0
									
								
								esphome/components/gl_r01_i2c/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								esphome/components/gl_r01_i2c/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										68
									
								
								esphome/components/gl_r01_i2c/gl_r01_i2c.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								esphome/components/gl_r01_i2c/gl_r01_i2c.cpp
									
									
									
									
									
										Normal 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 | ||||
							
								
								
									
										22
									
								
								esphome/components/gl_r01_i2c/gl_r01_i2c.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								esphome/components/gl_r01_i2c/gl_r01_i2c.h
									
									
									
									
									
										Normal 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 | ||||
							
								
								
									
										36
									
								
								esphome/components/gl_r01_i2c/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								esphome/components/gl_r01_i2c/sensor.py
									
									
									
									
									
										Normal 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) | ||||
		Reference in New Issue
	
	Block a user