ESPHome  2024.4.1
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 #include <cinttypes>
9 
10 namespace esphome {
11 namespace pulse_meter {
12 
13 class PulseMeterSensor : public sensor::Sensor, public Component {
14  public:
18  };
19 
20  void set_pin(InternalGPIOPin *pin) { this->pin_ = pin; }
21  void set_filter_us(uint32_t filter) { this->filter_us_ = filter; }
22  void set_timeout_us(uint32_t timeout) { this->timeout_us_ = timeout; }
23  void set_total_sensor(sensor::Sensor *sensor) { this->total_sensor_ = sensor; }
25 
26  void set_total_pulses(uint32_t pulses);
27 
28  void setup() override;
29  void loop() override;
30  float get_setup_priority() const override;
31  void dump_config() override;
32 
33  protected:
34  static void edge_intr(PulseMeterSensor *sensor);
35  static void pulse_intr(PulseMeterSensor *sensor);
36 
37  InternalGPIOPin *pin_{nullptr};
38  uint32_t filter_us_ = 0;
39  uint32_t timeout_us_ = 1000000UL * 60UL * 5UL;
42 
43  // Variables used in the loop
44  enum class MeterState { INITIAL, RUNNING, TIMED_OUT };
46  uint32_t total_pulses_ = 0;
48 
49  // This struct (and the two pointers) are used to pass data between the ISR and loop.
50  // These two pointers are exchanged each loop.
51  // Therefore you can't use data in the pointer to loop receives to set values in the pointer to loop sends.
52  // As a result it's easiest if you only use these pointers to send data from the ISR to the loop.
53  // (except for resetting the values)
54  struct State {
55  uint32_t last_detected_edge_us_ = 0;
56  uint32_t count_ = 0;
57  };
59  volatile State *set_ = state_;
60  volatile State *get_ = state_ + 1;
61 
62  // Only use these variables in the ISR
65  uint32_t last_intr_ = 0;
66  bool in_pulse_ = false;
67  bool last_pin_val_ = false;
68 };
69 
70 } // namespace pulse_meter
71 } // 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)
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
void set_total_sensor(sensor::Sensor *sensor)