diff --git a/esphome/components/pulse_counter/pulse_counter_sensor.cpp b/esphome/components/pulse_counter/pulse_counter_sensor.cpp index 281a61a66a..b4d34a257f 100644 --- a/esphome/components/pulse_counter/pulse_counter_sensor.cpp +++ b/esphome/components/pulse_counter/pulse_counter_sensor.cpp @@ -9,12 +9,16 @@ static const char *const TAG = "pulse_counter"; const char *const EDGE_MODE_TO_STRING[] = {"DISABLE", "INCREMENT", "DECREMENT"}; #ifdef HAS_PCNT -PulseCounterStorageBase *get_storage(bool hw_pcnt) { - return (hw_pcnt ? (PulseCounterStorageBase *) (new HwPulseCounterStorage) - : (PulseCounterStorageBase *) (new BasicPulseCounterStorage)); +PulseCounterStorageBase *get_storage(Storage storage) { + switch (storage) { + case Storage::basic: + return new BasicPulseCounterStorage; + case Storage::pcnt: + return new HwPulseCounterStorage; + } } #else -PulseCounterStorageBase *get_storage(bool) { return new BasicPulseCounterStorage; } +PulseCounterStorageBase *get_storage(Storage) { return new BasicPulseCounterStorage; } #endif void IRAM_ATTR BasicPulseCounterStorage::gpio_intr(BasicPulseCounterStorage *arg) { diff --git a/esphome/components/pulse_counter/pulse_counter_sensor.h b/esphome/components/pulse_counter/pulse_counter_sensor.h index ef9f73f95c..9978ddc9db 100644 --- a/esphome/components/pulse_counter/pulse_counter_sensor.h +++ b/esphome/components/pulse_counter/pulse_counter_sensor.h @@ -20,6 +20,8 @@ enum PulseCounterCountMode { PULSE_COUNTER_DECREMENT, }; +enum class Storage { basic, pcnt }; + #ifdef HAS_PCNT using pulse_counter_t = int16_t; #else @@ -58,11 +60,12 @@ struct HwPulseCounterStorage : public PulseCounterStorageBase { }; #endif -PulseCounterStorageBase *get_storage(bool hw_pcnt = false); +PulseCounterStorageBase *get_storage(Storage storage = Storage::basic); class PulseCounterSensor : public sensor::Sensor, public PollingComponent { public: - explicit PulseCounterSensor(bool hw_pcnt = false) : storage_(*get_storage(hw_pcnt)) {} + explicit PulseCounterSensor(Storage storage = Storage::basic) : storage_(*get_storage(storage)) {} + explicit PulseCounterSensor(int storage = 0) : PulseCounterSensor(Storage(storage)) {} void set_pin(InternalGPIOPin *pin) { pin_ = pin; } void set_rising_edge_mode(PulseCounterCountMode mode) { storage_.rising_edge_mode = mode; }