ESPHome  2021.11.3
pulse_counter_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
6 
7 #ifdef USE_ESP32
8 #include <driver/pcnt.h>
9 #endif
10 
11 namespace esphome {
12 namespace pulse_counter {
13 
18 };
19 
20 #ifdef USE_ESP32
21 using pulse_counter_t = int16_t;
22 #endif
23 #ifdef USE_ESP8266
24 using pulse_counter_t = int32_t;
25 #endif
26 
30 
31  static void gpio_intr(PulseCounterStorage *arg);
32 
33 #ifdef USE_ESP8266
34  volatile pulse_counter_t counter{0};
35  volatile uint32_t last_pulse{0};
36 #endif
37 
39 #ifdef USE_ESP32
40  pcnt_unit_t pcnt_unit;
41 #endif
42 #ifdef USE_ESP8266
44 #endif
47  uint32_t filter_us{0};
49 };
50 
52  public:
53  void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
54  void set_rising_edge_mode(PulseCounterCountMode mode) { storage_.rising_edge_mode = mode; }
55  void set_falling_edge_mode(PulseCounterCountMode mode) { storage_.falling_edge_mode = mode; }
56  void set_filter_us(uint32_t filter) { storage_.filter_us = filter; }
57  void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; }
58 
60  void setup() override;
61  void update() override;
62  float get_setup_priority() const override { return setup_priority::DATA; }
63  void dump_config() override;
64 
65  protected:
68  uint32_t last_time_{0};
69  uint32_t current_total_{0};
71 };
72 
73 } // namespace pulse_counter
74 } // 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:234
void set_rising_edge_mode(PulseCounterCountMode mode)
ClimateMode mode
Definition: climate.h:534
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
Base-class for all sensors.
Definition: sensor.h:47
void set_falling_edge_mode(PulseCounterCountMode mode)