pulse_counter_ulp: CountMode to enum class

This commit is contained in:
brisk 2024-07-13 18:26:42 +09:30
parent 4517698322
commit bd8858e053
3 changed files with 25 additions and 41 deletions

View file

@ -11,7 +11,17 @@ namespace pulse_counter_ulp {
static const char *const TAG = "pulse_counter_ulp";
const char *const EDGE_MODE_TO_STRING[] = {"DISABLE", "INCREMENT", "DECREMENT"};
const char *to_string(CountMode count_mode) {
switch (count_mode) {
case CountMode::disable:
return "disable";
case CountMode::increment:
return "increment";
case CountMode::decrement:
return "decrement";
}
return "UNKNOWM MODE";
}
/* === ULP === */
@ -22,30 +32,8 @@ bool UlpPulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) {
this->pin = pin;
this->pin->setup();
uint32_t rising = 0;
uint32_t falling = 0;
switch (this->rising_edge_mode) {
case PULSE_COUNTER_DISABLE:
rising = 0;
break;
case PULSE_COUNTER_INCREMENT:
rising = +1;
break;
case PULSE_COUNTER_DECREMENT:
rising = -1;
break;
}
switch (this->falling_edge_mode) {
case PULSE_COUNTER_DISABLE:
falling = 0;
break;
case PULSE_COUNTER_INCREMENT:
falling = +1;
break;
case PULSE_COUNTER_DECREMENT:
falling = -1;
break;
}
auto rising = static_cast<uint32_t>(this->rising_edge_mode);
auto falling = static_cast<uint32_t>(this->falling_edge_mode);
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");
@ -134,8 +122,8 @@ 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", EDGE_MODE_TO_STRING[this->storage_.rising_edge_mode]);
ESP_LOGCONFIG(TAG, " Falling Edge: %s", EDGE_MODE_TO_STRING[this->storage_.falling_edge_mode]);
ESP_LOGCONFIG(TAG, " Rising Edge: %s", to_string(this->storage_.rising_edge_mode));
ESP_LOGCONFIG(TAG, " Falling Edge: %s", to_string(this->storage_.falling_edge_mode));
LOG_UPDATE_INTERVAL(this);
}

View file

@ -13,11 +13,7 @@
namespace esphome {
namespace pulse_counter_ulp {
enum PulseCounterCountMode {
PULSE_COUNTER_DISABLE = 0,
PULSE_COUNTER_INCREMENT,
PULSE_COUNTER_DECREMENT,
};
enum class CountMode { disable = 0, increment = 1, decrement = -1 };
using pulse_counter_t = int32_t;
using timestamp_t = int64_t;
@ -27,8 +23,8 @@ struct UlpPulseCounterStorage {
pulse_counter_t read_raw_value();
InternalGPIOPin *pin;
PulseCounterCountMode rising_edge_mode{PULSE_COUNTER_INCREMENT};
PulseCounterCountMode falling_edge_mode{PULSE_COUNTER_DISABLE};
CountMode rising_edge_mode{CountMode::increment};
CountMode falling_edge_mode{CountMode::disable};
pulse_counter_t last_value{0};
};
@ -37,8 +33,8 @@ class PulseCounterUlpSensor : public sensor::Sensor, public PollingComponent {
explicit PulseCounterUlpSensor() {}
void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
void set_rising_edge_mode(PulseCounterCountMode mode) { storage_.rising_edge_mode = mode; }
void set_falling_edge_mode(PulseCounterCountMode mode) { storage_.falling_edge_mode = mode; }
void set_rising_edge_mode(CountMode mode) { storage_.rising_edge_mode = mode; }
void set_falling_edge_mode(CountMode mode) { storage_.falling_edge_mode = mode; }
void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; }
#ifdef USE_TIME
void set_time_id(time::RealTimeClock *time_id) { time_id_ = time_id; }

View file

@ -24,11 +24,11 @@ from esphome.const import (
from esphome.core import CORE
pulse_counter_ulp_ns = cg.esphome_ns.namespace("pulse_counter_ulp")
PulseCounterCountMode = pulse_counter_ulp_ns.enum("PulseCounterCountMode")
CountMode = pulse_counter_ulp_ns.enum("CountMode", is_class=True)
COUNT_MODES = {
"DISABLE": PulseCounterCountMode.PULSE_COUNTER_DISABLE,
"INCREMENT": PulseCounterCountMode.PULSE_COUNTER_INCREMENT,
"DECREMENT": PulseCounterCountMode.PULSE_COUNTER_DECREMENT,
"DISABLE": CountMode.disable,
"INCREMENT": CountMode.increment,
"DECREMENT": CountMode.decrement,
}
COUNT_MODE_SCHEMA = cv.enum(COUNT_MODES, upper=True)
@ -134,7 +134,7 @@ async def to_code(config):
@automation.register_action(
"pulse_counter.set_total_pulses",
"pulse_counter_ulp.set_total_pulses",
SetTotalPulsesAction,
cv.Schema(
{