ESPHome  2023.8.3
pulse_meter_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/hal.h"
6 #include "esphome/core/helpers.h"
7 
8 namespace esphome {
9 namespace pulse_meter {
10 
11 class PulseMeterSensor : public sensor::Sensor, public Component {
12  public:
16  };
17 
18  void set_pin(InternalGPIOPin *pin) { this->pin_ = pin; }
19  void set_filter_us(uint32_t filter) { this->filter_us_ = filter; }
20  void set_timeout_us(uint32_t timeout) { this->timeout_us_ = timeout; }
21  void set_total_sensor(sensor::Sensor *sensor) { this->total_sensor_ = sensor; }
23  void set_total_pulses(uint32_t pulses) { this->total_pulses_ = pulses; }
24 
25  void setup() override;
26  void loop() override;
27  float get_setup_priority() const override;
28  void dump_config() override;
29 
30  protected:
31  static void edge_intr(PulseMeterSensor *sensor);
32  static void pulse_intr(PulseMeterSensor *sensor);
33 
34  InternalGPIOPin *pin_{nullptr};
35  uint32_t filter_us_ = 0;
36  uint32_t timeout_us_ = 1000000UL * 60UL * 5UL;
39 
40  // Variables used in the loop
41  bool initialized_ = false;
42  uint32_t total_pulses_ = 0;
44 
45  // This struct (and the two pointers) are used to pass data between the ISR and loop.
46  // These two pointers are exchanged each loop.
47  // Therefore you can't use data in the pointer to loop receives to set values in the pointer to loop sends.
48  // As a result it's easiest if you only use these pointers to send data from the ISR to the loop.
49  // (except for resetting the values)
50  struct State {
51  uint32_t last_detected_edge_us_ = 0;
52  uint32_t count_ = 0;
53  };
55  volatile State *set_ = state_;
56  volatile State *get_ = state_ + 1;
57 
58  // Only use these variables in the ISR
61  uint32_t last_intr_ = 0;
62  bool in_pulse_ = false;
63  bool last_pin_val_ = false;
64 };
65 
66 } // namespace pulse_meter
67 } // namespace esphome
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition: gpio.h:66
static void edge_intr(PulseMeterSensor *sensor)
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:151
static void pulse_intr(PulseMeterSensor *sensor)
void set_filter_mode(InternalFilterMode mode)
Base-class for all sensors.
Definition: sensor.h:57
void set_total_sensor(sensor::Sensor *sensor)