ESPHome  2023.9.3
pulse_counter_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
6 
7 #if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3)
8 #include <driver/pcnt.h>
9 #define HAS_PCNT
10 #endif
11 
12 namespace esphome {
13 namespace pulse_counter {
14 
19 };
20 
21 #ifdef HAS_PCNT
22 using pulse_counter_t = int16_t;
23 #else
24 using pulse_counter_t = int32_t;
25 #endif
26 
28  virtual bool pulse_counter_setup(InternalGPIOPin *pin) = 0;
29  virtual pulse_counter_t read_raw_value() = 0;
30 
34  uint32_t filter_us{0};
36 };
37 
39  static void gpio_intr(BasicPulseCounterStorage *arg);
40 
41  bool pulse_counter_setup(InternalGPIOPin *pin) override;
43 
44  volatile pulse_counter_t counter{0};
45  volatile uint32_t last_pulse{0};
46 
48 };
49 
50 #ifdef HAS_PCNT
52  bool pulse_counter_setup(InternalGPIOPin *pin) override;
54 
55  pcnt_unit_t pcnt_unit;
56 };
57 #endif
58 
59 PulseCounterStorageBase *get_storage(bool hw_pcnt = false);
60 
62  public:
63  explicit PulseCounterSensor(bool hw_pcnt = false) : storage_(*get_storage(hw_pcnt)) {}
64 
65  void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
66  void set_rising_edge_mode(PulseCounterCountMode mode) { storage_.rising_edge_mode = mode; }
67  void set_falling_edge_mode(PulseCounterCountMode mode) { storage_.falling_edge_mode = mode; }
68  void set_filter_us(uint32_t filter) { storage_.filter_us = filter; }
69  void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; }
70 
71  void set_total_pulses(uint32_t pulses);
72 
74  void setup() override;
75  void update() override;
76  float get_setup_priority() const override { return setup_priority::DATA; }
77  void dump_config() override;
78 
79  protected:
82  uint32_t last_time_{0};
83  uint32_t current_total_{0};
84  sensor::Sensor *total_sensor_{nullptr};
85 };
86 
87 } // namespace pulse_counter
88 } // namespace esphome
void setup()
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
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:282
void set_rising_edge_mode(PulseCounterCountMode mode)
virtual pulse_counter_t read_raw_value()=0
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:151
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)