Use enum not bool for pulse counter storage type

Allows for more storage types
This commit is contained in:
brisk 2024-03-20 21:48:11 +10:30
parent b3cff566eb
commit 7e4d37c9f1
2 changed files with 13 additions and 6 deletions

View file

@ -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) {

View file

@ -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; }