ESPHome  2024.12.2
pulse_counter_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
6 
7 #include <cinttypes>
8 
9 #if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3)
10 #include <driver/pcnt.h>
11 #define HAS_PCNT
12 #endif
13 
14 namespace esphome {
15 namespace pulse_counter {
16 
21 };
22 
23 #ifdef HAS_PCNT
24 using pulse_counter_t = int16_t;
25 #else
26 using pulse_counter_t = int32_t;
27 #endif
28 
30  virtual bool pulse_counter_setup(InternalGPIOPin *pin) = 0;
31  virtual pulse_counter_t read_raw_value() = 0;
32 
36  uint32_t filter_us{0};
38 };
39 
41  static void gpio_intr(BasicPulseCounterStorage *arg);
42 
43  bool pulse_counter_setup(InternalGPIOPin *pin) override;
45 
46  volatile pulse_counter_t counter{0};
47  volatile uint32_t last_pulse{0};
48 
50 };
51 
52 #ifdef HAS_PCNT
54  bool pulse_counter_setup(InternalGPIOPin *pin) override;
56 
57  pcnt_unit_t pcnt_unit;
58  pcnt_channel_t pcnt_channel;
59 };
60 #endif
61 
62 PulseCounterStorageBase *get_storage(bool hw_pcnt = false);
63 
65  public:
66  explicit PulseCounterSensor(bool hw_pcnt = false) : storage_(*get_storage(hw_pcnt)) {}
67 
68  void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
69  void set_rising_edge_mode(PulseCounterCountMode mode) { storage_.rising_edge_mode = mode; }
70  void set_falling_edge_mode(PulseCounterCountMode mode) { storage_.falling_edge_mode = mode; }
71  void set_filter_us(uint32_t filter) { storage_.filter_us = filter; }
72  void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; }
73 
74  void set_total_pulses(uint32_t pulses);
75 
77  void setup() override;
78  void update() override;
79  float get_setup_priority() const override { return setup_priority::DATA; }
80  void dump_config() override;
81 
82  protected:
85  uint32_t last_time_{0};
86  uint32_t current_total_{0};
87  sensor::Sensor *total_sensor_{nullptr};
88 };
89 
90 } // namespace pulse_counter
91 } // namespace esphome
void setup()
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition: gpio.h:66
PulseCounterStorageBase * get_storage(bool hw_pcnt)
void set_total_sensor(sensor::Sensor *total_sensor)
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void set_rising_edge_mode(PulseCounterCountMode mode)
virtual pulse_counter_t read_raw_value()=0
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base-class for all sensors.
Definition: sensor.h:57
virtual bool pulse_counter_setup(InternalGPIOPin *pin)=0
void set_falling_edge_mode(PulseCounterCountMode mode)