From 7e4d37c9f1bc044c28f9fdc8df9e90209df95eb6 Mon Sep 17 00:00:00 2001 From: brisk Date: Wed, 20 Mar 2024 21:48:11 +1030 Subject: [PATCH] Use enum not bool for pulse counter storage type Allows for more storage types --- .../pulse_counter/pulse_counter_sensor.cpp | 12 ++++++++---- .../components/pulse_counter/pulse_counter_sensor.h | 7 +++++-- 2 files changed, 13 insertions(+), 6 deletions(-) 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; }