ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
pulse_width.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
6
7namespace esphome::pulse_width {
8
11 public:
13 pin->setup();
14 this->pin_ = pin->to_isr();
15 this->last_rise_ = micros();
17 }
18 static void gpio_intr(PulseWidthSensorStore *arg);
19 uint32_t get_pulse_width_us() const { return this->last_width_; }
20 float get_pulse_width_s() const { return this->last_width_ / 1e6f; }
21 uint32_t get_last_rise() const { return last_rise_; }
22
23 protected:
25 volatile uint32_t last_width_{0};
26 volatile uint32_t last_rise_{0};
27};
28
30 public:
31 void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
32 void setup() override { this->store_.setup(this->pin_); }
33 void dump_config() override;
34 void update() override;
35
36 protected:
39};
40
41} // namespace esphome::pulse_width
virtual void setup()=0
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio.h:92
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:107
virtual ISRInternalGPIOPin to_isr() const =0
This class simplifies creating components that periodically check a state.
Definition component.h:585
void set_pin(InternalGPIOPin *pin)
Definition pulse_width.h:31
Store data in a class that doesn't use multiple-inheritance (vtables in flash)
Definition pulse_width.h:10
static void gpio_intr(PulseWidthSensorStore *arg)
Base-class for all sensors.
Definition sensor.h:47
@ INTERRUPT_ANY_EDGE
Definition gpio.h:52
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
static void uint32_t