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"; 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 === */ /* === ULP === */
@ -22,30 +32,8 @@ bool UlpPulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) {
this->pin = pin; this->pin = pin;
this->pin->setup(); this->pin->setup();
uint32_t rising = 0; auto rising = static_cast<uint32_t>(this->rising_edge_mode);
uint32_t falling = 0; auto falling = static_cast<uint32_t>(this->falling_edge_mode);
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;
}
if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_UNDEFINED) { 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"); 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() { void PulseCounterUlpSensor::dump_config() {
LOG_SENSOR("", "Pulse Counter", this); LOG_SENSOR("", "Pulse Counter", this);
LOG_PIN(" Pin: ", this->pin_); LOG_PIN(" Pin: ", this->pin_);
ESP_LOGCONFIG(TAG, " Rising Edge: %s", EDGE_MODE_TO_STRING[this->storage_.rising_edge_mode]); ESP_LOGCONFIG(TAG, " Rising Edge: %s", 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, " Falling Edge: %s", to_string(this->storage_.falling_edge_mode));
LOG_UPDATE_INTERVAL(this); LOG_UPDATE_INTERVAL(this);
} }

View file

@ -13,11 +13,7 @@
namespace esphome { namespace esphome {
namespace pulse_counter_ulp { namespace pulse_counter_ulp {
enum PulseCounterCountMode { enum class CountMode { disable = 0, increment = 1, decrement = -1 };
PULSE_COUNTER_DISABLE = 0,
PULSE_COUNTER_INCREMENT,
PULSE_COUNTER_DECREMENT,
};
using pulse_counter_t = int32_t; using pulse_counter_t = int32_t;
using timestamp_t = int64_t; using timestamp_t = int64_t;
@ -27,8 +23,8 @@ struct UlpPulseCounterStorage {
pulse_counter_t read_raw_value(); pulse_counter_t read_raw_value();
InternalGPIOPin *pin; InternalGPIOPin *pin;
PulseCounterCountMode rising_edge_mode{PULSE_COUNTER_INCREMENT}; CountMode rising_edge_mode{CountMode::increment};
PulseCounterCountMode falling_edge_mode{PULSE_COUNTER_DISABLE}; CountMode falling_edge_mode{CountMode::disable};
pulse_counter_t last_value{0}; pulse_counter_t last_value{0};
}; };
@ -37,8 +33,8 @@ class PulseCounterUlpSensor : public sensor::Sensor, public PollingComponent {
explicit PulseCounterUlpSensor() {} explicit PulseCounterUlpSensor() {}
void set_pin(InternalGPIOPin *pin) { pin_ = pin; } void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
void set_rising_edge_mode(PulseCounterCountMode mode) { storage_.rising_edge_mode = mode; } void set_rising_edge_mode(CountMode mode) { storage_.rising_edge_mode = mode; }
void set_falling_edge_mode(PulseCounterCountMode mode) { storage_.falling_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; } void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; }
#ifdef USE_TIME #ifdef USE_TIME
void set_time_id(time::RealTimeClock *time_id) { time_id_ = time_id; } 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 from esphome.core import CORE
pulse_counter_ulp_ns = cg.esphome_ns.namespace("pulse_counter_ulp") 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 = { COUNT_MODES = {
"DISABLE": PulseCounterCountMode.PULSE_COUNTER_DISABLE, "DISABLE": CountMode.disable,
"INCREMENT": PulseCounterCountMode.PULSE_COUNTER_INCREMENT, "INCREMENT": CountMode.increment,
"DECREMENT": PulseCounterCountMode.PULSE_COUNTER_DECREMENT, "DECREMENT": CountMode.decrement,
} }
COUNT_MODE_SCHEMA = cv.enum(COUNT_MODES, upper=True) COUNT_MODE_SCHEMA = cv.enum(COUNT_MODES, upper=True)
@ -134,7 +134,7 @@ async def to_code(config):
@automation.register_action( @automation.register_action(
"pulse_counter.set_total_pulses", "pulse_counter_ulp.set_total_pulses",
SetTotalPulsesAction, SetTotalPulsesAction,
cv.Schema( cv.Schema(
{ {