ESPHome  2022.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 #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 
30 
31  static void gpio_intr(PulseCounterStorage *arg);
32 
33 #ifndef HAS_PCNT
34  volatile pulse_counter_t counter{0};
35  volatile uint32_t last_pulse{0};
36 #endif
37 
39 #ifdef HAS_PCNT
40  pcnt_unit_t pcnt_unit;
41 #else
43 #endif
46  uint32_t filter_us{0};
48 };
49 
51  public:
52  void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
53  void set_rising_edge_mode(PulseCounterCountMode mode) { storage_.rising_edge_mode = mode; }
54  void set_falling_edge_mode(PulseCounterCountMode mode) { storage_.falling_edge_mode = mode; }
55  void set_filter_us(uint32_t filter) { storage_.filter_us = filter; }
56  void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; }
57 
59  void setup() override;
60  void update() override;
61  float get_setup_priority() const override { return setup_priority::DATA; }
62  void dump_config() override;
63 
64  protected:
67  uint32_t last_time_{0};
68  uint32_t current_total_{0};
70 };
71 
72 } // namespace pulse_counter
73 } // 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
void set_total_sensor(sensor::Sensor *total_sensor)
static void gpio_intr(PulseCounterStorage *arg)
This class simplifies creating components that periodically check a state.
Definition: component.h:266
void set_rising_edge_mode(PulseCounterCountMode mode)
ClimateMode mode
Definition: climate.h:538
Definition: a4988.cpp:4
Base-class for all sensors.
Definition: sensor.h:47
void set_falling_edge_mode(PulseCounterCountMode mode)