From 67b746693f0281b0b95573b3cbf5fb70641fe0ad Mon Sep 17 00:00:00 2001 From: brisk Date: Wed, 11 Sep 2024 14:14:36 +0930 Subject: [PATCH] pulse_counter_ulp: Record and dump all config items --- .../pulse_counter_ulp_sensor.cpp | 23 ++++++++-------- .../pulse_counter_ulp_sensor.h | 27 +++++++++++-------- .../components/pulse_counter_ulp/sensor.py | 1 + 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/esphome/components/pulse_counter_ulp/pulse_counter_ulp_sensor.cpp b/esphome/components/pulse_counter_ulp/pulse_counter_ulp_sensor.cpp index 75ac03bff4..adb29b31c4 100644 --- a/esphome/components/pulse_counter_ulp/pulse_counter_ulp_sensor.cpp +++ b/esphome/components/pulse_counter_ulp/pulse_counter_ulp_sensor.cpp @@ -28,8 +28,7 @@ const char *to_string(CountMode count_mode) { extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_main_bin_start"); extern const uint8_t ulp_main_bin_end[] asm("_binary_ulp_main_bin_end"); -std::unique_ptr UlpProgram::start(gpio_num_t gpio_num, microseconds sleep_duration, - CountMode rising_edge_mode, CountMode falling_edge_mode) { +std::unique_ptr UlpProgram::start(const Config &config) { esp_err_t error = ulp_load_binary(0, ulp_main_bin_start, (ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t)); if (error != ESP_OK) { ESP_LOGE(TAG, "Loading ULP binary failed: %s", esp_err_to_name(error)); @@ -37,6 +36,7 @@ std::unique_ptr UlpProgram::start(gpio_num_t gpio_num, microseconds } /* GPIO used for pulse counting. */ + gpio_num_t gpio_num = static_cast(config.pin_->get_pin()); int rtcio_num = rtc_io_number_get(gpio_num); if (!rtc_gpio_is_valid_gpio(gpio_num)) { ESP_LOGE(TAG, "GPIO used for pulse counting must be an RTC IO"); @@ -54,10 +54,10 @@ std::unique_ptr UlpProgram::start(gpio_num_t gpio_num, microseconds ulp_edge_count = 0; ulp_run_count = 0; ulp_debounce_counter = 3; - ulp_debounce_max_count = 3; + ulp_debounce_max_count = config.debounce_; ulp_next_edge = 0; ulp_io_number = rtcio_num; /* map from GPIO# to RTC_IO# */ - ulp_mean_exec_time = sleep_duration / microseconds{1}; + ulp_mean_exec_time = config.sleep_duration_ / microseconds{1}; /* Initialize selected GPIO as RTC IO, enable input */ rtc_gpio_init(gpio_num); @@ -67,7 +67,7 @@ std::unique_ptr UlpProgram::start(gpio_num_t gpio_num, microseconds /* Set ULP wake up period T * Minimum pulse width has to be T * (ulp_debounce_counter + 1). */ - ulp_set_wakeup_period(0, sleep_duration / std::chrono::microseconds{1}); + ulp_set_wakeup_period(0, config.sleep_duration_ / std::chrono::microseconds{1}); /* Start the program */ error = ulp_run(&ulp_entry - RTC_SLOW_MEM); @@ -103,12 +103,11 @@ void UlpProgram::set_mean_exec_time(microseconds mean_exec_time) { void PulseCounterUlpSensor::setup() { ESP_LOGCONFIG(TAG, "Setting up pulse counter '%s'...", this->name_.c_str()); - this->pin_->setup(); + this->config_.pin_->setup(); if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_UNDEFINED) { ESP_LOGD(TAG, "Did not wake up from sleep, assuming restart or first boot and setting up ULP program"); - this->storage_ = UlpProgram::start(static_cast(this->pin_->get_pin()), this->sleep_duration_, - this->rising_edge_mode, this->falling_edge_mode); + this->storage_ = UlpProgram::start(this->config_); } else { ESP_LOGD(TAG, "Woke up from sleep, skipping set-up of ULP program"); this->storage_ = std::unique_ptr(new UlpProgram); @@ -129,9 +128,11 @@ void PulseCounterUlpSensor::set_total_pulses(uint32_t pulses) { void PulseCounterUlpSensor::dump_config() { LOG_SENSOR("", "Pulse Counter", this); - LOG_PIN(" Pin: ", this->pin_); - ESP_LOGCONFIG(TAG, " Rising Edge: %s", to_string(this->rising_edge_mode)); - ESP_LOGCONFIG(TAG, " Falling Edge: %s", to_string(this->falling_edge_mode)); + LOG_PIN(" Pin: ", this->config_.pin_); + ESP_LOGCONFIG(TAG, " Rising Edge: %s", to_string(this->config_.rising_edge_mode_)); + ESP_LOGCONFIG(TAG, " Falling Edge: %s", to_string(this->config_.falling_edge_mode_)); + ESP_LOGCONFIG(TAG, " Sleep Duration: " PRIu32 " µs", this->config_.sleep_duration_ / microseconds{1}); + ESP_LOGCONFIG(TAG, " Debounce: " PRIu16, this->config_.debounce_); LOG_UPDATE_INTERVAL(this); } diff --git a/esphome/components/pulse_counter_ulp/pulse_counter_ulp_sensor.h b/esphome/components/pulse_counter_ulp/pulse_counter_ulp_sensor.h index 4dadb19cc1..19cd5e9a43 100644 --- a/esphome/components/pulse_counter_ulp/pulse_counter_ulp_sensor.h +++ b/esphome/components/pulse_counter_ulp/pulse_counter_ulp_sensor.h @@ -20,6 +20,13 @@ using microseconds = std::chrono::duration; class UlpProgram { public: + struct Config { + InternalGPIOPin *pin_; + CountMode rising_edge_mode_; + CountMode falling_edge_mode_; + microseconds sleep_duration_; + uint16_t debounce_; + }; struct State { uint16_t edge_count; uint16_t run_count; @@ -29,18 +36,18 @@ class UlpProgram { State peek_state() const; void set_mean_exec_time(microseconds mean_exec_time); - static std::unique_ptr start(gpio_num_t gpio_num, microseconds sleep_duration, CountMode rising_edge_mode, - CountMode falling_edge_mode); + static std::unique_ptr start(const Config &config); }; class PulseCounterUlpSensor : public sensor::Sensor, public PollingComponent { public: explicit PulseCounterUlpSensor() {} - void set_pin(InternalGPIOPin *pin) { pin_ = pin; } - void set_rising_edge_mode(CountMode mode) { this->rising_edge_mode = mode; } - void set_falling_edge_mode(CountMode mode) { this->falling_edge_mode = mode; } - void set_sleep_duration(uint32_t duration_us) { this->sleep_duration_ = duration_us * microseconds{1}; } + void set_pin(InternalGPIOPin *pin) { this->config_.pin_ = pin; } + void set_rising_edge_mode(CountMode mode) { this->config_.rising_edge_mode_ = mode; } + void set_falling_edge_mode(CountMode mode) { this->config_.falling_edge_mode_ = mode; } + void set_sleep_duration(uint32_t duration_us) { this->config_.sleep_duration_ = duration_us * microseconds{1}; } + void set_debounce(uint16_t debounce) { this->config_.debounce_ = debounce; } void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; } void set_total_pulses(uint32_t pulses); @@ -52,14 +59,12 @@ class PulseCounterUlpSensor : public sensor::Sensor, public PollingComponent { void dump_config() override; protected: - InternalGPIOPin *pin_; - CountMode rising_edge_mode{CountMode::increment}; - CountMode falling_edge_mode{CountMode::disable}; + UlpProgram::Config config_{}; + sensor::Sensor *total_sensor_{nullptr}; + std::unique_ptr storage_{}; clock::time_point last_time_{}; - microseconds sleep_duration_{20000}; uint32_t current_total_{0}; - sensor::Sensor *total_sensor_{nullptr}; }; } // namespace pulse_counter_ulp diff --git a/esphome/components/pulse_counter_ulp/sensor.py b/esphome/components/pulse_counter_ulp/sensor.py index 442eedd5ad..471a9a309c 100644 --- a/esphome/components/pulse_counter_ulp/sensor.py +++ b/esphome/components/pulse_counter_ulp/sensor.py @@ -126,6 +126,7 @@ async def to_code(config): cg.add(var.set_rising_edge_mode(count[CONF_RISING_EDGE])) cg.add(var.set_falling_edge_mode(count[CONF_FALLING_EDGE])) cg.add(var.set_sleep_duration(config[CONF_SLEEP_DURATION])) + cg.add(var.set_debounce(config[CONF_DEBOUNCE])) if CONF_TOTAL in config: sens = await sensor.new_sensor(config[CONF_TOTAL])