ESPHome  2024.4.0
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 };
59 #endif
60 
61 PulseCounterStorageBase *get_storage(bool hw_pcnt = false);
62 
64  public:
65  explicit PulseCounterSensor(bool hw_pcnt = false) : storage_(*get_storage(hw_pcnt)) {}
66 
67  void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
68  void set_rising_edge_mode(PulseCounterCountMode mode) { storage_.rising_edge_mode = mode; }
69  void set_falling_edge_mode(PulseCounterCountMode mode) { storage_.falling_edge_mode = mode; }
70  void set_filter_us(uint32_t filter) { storage_.filter_us = filter; }
71  void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; }
72 
73  void set_total_pulses(uint32_t pulses);
74 
76  void setup() override;
77  void update() override;
78  float get_setup_priority() const override { return setup_priority::DATA; }
79  void dump_config() override;
80 
81  protected:
84  uint32_t last_time_{0};
85  uint32_t current_total_{0};
86  sensor::Sensor *total_sensor_{nullptr};
87 };
88 
89 } // namespace pulse_counter
90 } // 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:151
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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)