ESPHome  2024.12.2
pulse_width.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
6 
7 namespace esphome {
8 namespace pulse_width {
9 
12  public:
13  void setup(InternalGPIOPin *pin) {
14  pin->setup();
15  this->pin_ = pin->to_isr();
16  this->last_rise_ = micros();
18  }
19  static void gpio_intr(PulseWidthSensorStore *arg);
20  uint32_t get_pulse_width_us() const { return this->last_width_; }
21  float get_pulse_width_s() const { return this->last_width_ / 1e6f; }
22  uint32_t get_last_rise() const { return last_rise_; }
23 
24  protected:
26  volatile uint32_t last_width_{0};
27  volatile uint32_t last_rise_{0};
28 };
29 
31  public:
32  void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
33  void setup() override { this->store_.setup(this->pin_); }
34  void dump_config() override;
35  float get_setup_priority() const override { return setup_priority::DATA; }
36  void update() override;
37 
38  protected:
41 };
42 
43 } // namespace pulse_width
44 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void setup(InternalGPIOPin *pin)
Definition: pulse_width.h:13
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition: gpio.h:66
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void set_pin(InternalGPIOPin *pin)
Definition: pulse_width.h:32
virtual void setup()=0
uint32_t IRAM_ATTR HOT micros()
Definition: core.cpp:27
float get_setup_priority() const override
Definition: pulse_width.h:35
Store data in a class that doesn't use multiple-inheritance (vtables in flash)
Definition: pulse_width.h:11
virtual ISRInternalGPIOPin to_isr() const =0
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base-class for all sensors.
Definition: sensor.h:57
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition: gpio.h:81
static void gpio_intr(PulseWidthSensorStore *arg)
Definition: pulse_width.cpp:9