ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
gpio_binary_sensor.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
4
5namespace esphome::gpio {
6
7static const char *const TAG = "gpio.binary_sensor";
8
9#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
10// Interrupt type strings indexed by edge-triggered InterruptType values:
11// indices 1-3: RISING_EDGE, FALLING_EDGE, ANY_EDGE; other values (e.g. level-triggered) map to UNKNOWN (index 0).
12PROGMEM_STRING_TABLE(InterruptTypeStrings, "UNKNOWN", "RISING_EDGE", "FALLING_EDGE", "ANY_EDGE");
13
14static const LogString *interrupt_type_to_string(gpio::InterruptType type) {
15 return InterruptTypeStrings::get_log_str(static_cast<uint8_t>(type), 0);
16}
17
18static const LogString *gpio_mode_to_string(bool use_interrupt) {
19 return use_interrupt ? LOG_STR("interrupt") : LOG_STR("polling");
20}
21#endif
22
24 bool new_state = arg->isr_pin_.digital_read();
25 if (new_state != arg->state_) {
26 arg->state_ = new_state;
27 arg->changed_ = true;
28 // Wake up the component from its disabled loop state
29 if (arg->component_ != nullptr) {
31 }
32 }
33}
34
36 pin->setup();
37 this->isr_pin_ = pin->to_isr();
38 this->component_ = component;
39
40 // Read initial state
41 this->state_ = pin->digital_read();
42
43 // Attach interrupt - from this point on, any changes will be caught by the interrupt
45}
46
48 if (this->store_.use_interrupt_) {
49 auto *internal_pin = static_cast<InternalGPIOPin *>(this->pin_);
50 this->store_.setup(internal_pin, this);
52 } else {
53 this->pin_->setup();
55 }
56}
57
59 LOG_BINARY_SENSOR("", "GPIO Binary Sensor", this);
60 LOG_PIN(" Pin: ", this->pin_);
61 ESP_LOGCONFIG(TAG, " Mode: %s", LOG_STR_ARG(gpio_mode_to_string(this->store_.use_interrupt_)));
62 if (this->store_.use_interrupt_) {
63 ESP_LOGCONFIG(TAG, " Interrupt Type: %s", LOG_STR_ARG(interrupt_type_to_string(this->store_.interrupt_type_)));
64 }
65}
66
68 if (this->store_.use_interrupt_) {
69 if (this->store_.is_changed()) {
70 // Clear the flag immediately to minimize the window where we might miss changes
71 this->store_.clear_changed();
72 // Read the state and publish it
73 // Note: If the ISR fires between clear_changed() and get_state(), that's fine -
74 // we'll process the new change on the next loop iteration
75 bool state = this->store_.get_state();
76 this->publish_state(state);
77 } else {
78 // No changes, disable the loop until the next interrupt
79 this->disable_loop();
80 }
81 } else {
82 this->publish_state(this->pin_->digital_read());
83 }
84}
85
87
88} // namespace esphome::gpio
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void disable_loop()
Disable this component's loop.
virtual void setup()=0
virtual bool digital_read()=0
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:107
virtual ISRInternalGPIOPin to_isr() const =0
void publish_state(bool new_state)
Publish a new state to the front-end.
bool state
The current state of this binary sensor. Also used as the backing storage for StatefulEntityBase.
void publish_initial_state(bool new_state)
Publish the initial state, this will not make the callback manager send callbacks and is meant only f...
void loop() override
Check sensor.
float get_setup_priority() const override
Hardware priority.
void setup() override
Setup pin.
void setup(InternalGPIOPin *pin, Component *component)
static void gpio_intr(GPIOBinarySensorStore *arg)
const Component * component
Definition component.cpp:34
uint16_t type
PROGMEM_STRING_TABLE(InterruptTypeStrings, "UNKNOWN", "RISING_EDGE", "FALLING_EDGE", "ANY_EDGE")
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:41