From 58eb5423fef613abf68bba394fa7bbd52b4d142f Mon Sep 17 00:00:00 2001 From: Brian Davis Date: Fri, 20 Dec 2024 20:51:43 -0500 Subject: [PATCH] Adding timing budget support for vl53l0x --- esphome/components/vl53l0x/sensor.py | 10 ++++++++++ esphome/components/vl53l0x/vl53l0x_sensor.cpp | 10 +++++++++- esphome/components/vl53l0x/vl53l0x_sensor.h | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/esphome/components/vl53l0x/sensor.py b/esphome/components/vl53l0x/sensor.py index 7b485e3887..ef3f8a4f8f 100644 --- a/esphome/components/vl53l0x/sensor.py +++ b/esphome/components/vl53l0x/sensor.py @@ -20,6 +20,7 @@ VL53L0XSensor = vl53l0x_ns.class_( CONF_SIGNAL_RATE_LIMIT = "signal_rate_limit" CONF_LONG_RANGE = "long_range" +CONF_TIMING_BUDGET = "timing_budget" def check_keys(obj): @@ -37,6 +38,11 @@ def check_timeout(value): raise cv.Invalid("Maximum timeout can not be greater then 60 seconds") return value +def check_timing_budget(value): + value = cv.positive_time_period_microseconds(value) + if value.total_microseconds < 17000 or value.total_microseconds > 4294967295: + raise cv.Invalid("Timing budget must be between 17000us and 4294967295us") + return value CONFIG_SCHEMA = cv.All( sensor.sensor_schema( @@ -54,6 +60,7 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_LONG_RANGE, default=False): cv.boolean, cv.Optional(CONF_TIMEOUT, default="10ms"): check_timeout, cv.Optional(CONF_ENABLE_PIN): pins.gpio_output_pin_schema, + cv.Optional(CONF_TIMING_BUDGET): check_timing_budget, } ) .extend(cv.polling_component_schema("60s")) @@ -73,4 +80,7 @@ async def to_code(config): enable = await cg.gpio_pin_expression(config[CONF_ENABLE_PIN]) cg.add(var.set_enable_pin(enable)) + if CONF_TIMING_BUDGET in config: + cg.add(var.set_timing_budget(config[CONF_TIMING_BUDGET])) + await i2c.register_i2c_device(var, config) diff --git a/esphome/components/vl53l0x/vl53l0x_sensor.cpp b/esphome/components/vl53l0x/vl53l0x_sensor.cpp index b07779a653..258fb2fcba 100644 --- a/esphome/components/vl53l0x/vl53l0x_sensor.cpp +++ b/esphome/components/vl53l0x/vl53l0x_sensor.cpp @@ -28,6 +28,9 @@ void VL53L0XSensor::dump_config() { LOG_PIN(" Enable Pin: ", this->enable_pin_); } ESP_LOGCONFIG(TAG, " Timeout: %u%s", this->timeout_us_, this->timeout_us_ > 0 ? "us" : " (no timeout)"); + if (this->timing_budget_) { + ESP_LOGCONFIG(TAG, " Timing Budget %u%s ", this->timing_budget_, "us"); + } } void VL53L0XSensor::setup() { @@ -230,7 +233,12 @@ void VL53L0XSensor::setup() { reg(0x84) &= ~0x10; reg(0x0B) = 0x01; - measurement_timing_budget_us_ = get_measurement_timing_budget_(); + if (timing_budget_) { + measurement_timing_budget_us_ = timing_budget_; + } else { + measurement_timing_budget_us_ = get_measurement_timing_budget_(); + } + reg(0x01) = 0xE8; set_measurement_timing_budget_(measurement_timing_budget_us_); reg(0x01) = 0x01; diff --git a/esphome/components/vl53l0x/vl53l0x_sensor.h b/esphome/components/vl53l0x/vl53l0x_sensor.h index 971fb458bb..a6ba1a370f 100644 --- a/esphome/components/vl53l0x/vl53l0x_sensor.h +++ b/esphome/components/vl53l0x/vl53l0x_sensor.h @@ -39,6 +39,7 @@ class VL53L0XSensor : public sensor::Sensor, public PollingComponent, public i2c void set_long_range(bool long_range) { long_range_ = long_range; } void set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; } void set_enable_pin(GPIOPin *enable) { this->enable_pin_ = enable; } + void set_timing_budget(uint32_t timing_budget) { timing_budget_ = timing_budget; } protected: uint32_t get_measurement_timing_budget_(); @@ -66,6 +67,7 @@ class VL53L0XSensor : public sensor::Sensor, public PollingComponent, public i2c uint16_t timeout_start_us_; uint16_t timeout_us_{}; + uint32_t timing_budget_{}; static std::list vl53_sensors; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) static bool enable_pin_setup_complete; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)