ESPHome 2026.10.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#ifdef USE_GPIO_BINARY_SENSOR_INTERRUPT
11// Interrupt type strings indexed by edge-triggered InterruptType values:
12// indices 1-3: RISING_EDGE, FALLING_EDGE, ANY_EDGE; other values (e.g. level-triggered) map to UNKNOWN (index 0).
13PROGMEM_STRING_TABLE(InterruptTypeStrings, "UNKNOWN", "RISING_EDGE", "FALLING_EDGE", "ANY_EDGE");
14
15static const LogString *interrupt_type_to_string(gpio::InterruptType type) {
16 return InterruptTypeStrings::get_log_str(static_cast<uint8_t>(type), 0);
17}
18
19static const LogString *gpio_mode_to_string(bool use_interrupt) {
20 return use_interrupt ? LOG_STR("interrupt") : LOG_STR("polling");
21}
22#endif
23#endif
24
25#ifdef USE_GPIO_BINARY_SENSOR_INTERRUPT
27 bool new_state = arg->isr_pin_.digital_read();
28 if (new_state != arg->state_) {
29 arg->state_ = new_state;
30 arg->changed_ = true;
31 // Wake up the component from its disabled loop state
32 if (arg->component_ != nullptr) {
34 }
35 }
36}
37
39 pin->setup();
40 this->isr_pin_ = pin->to_isr();
41 this->component_ = component;
42
43 // Read initial state
44 this->state_ = pin->digital_read();
45
46 // Attach interrupt - from this point on, any changes will be caught by the interrupt
48}
49#endif // USE_GPIO_BINARY_SENSOR_INTERRUPT
50
52#ifdef USE_GPIO_BINARY_SENSOR_INTERRUPT
53 if (this->store_.use_interrupt_) {
54 auto *internal_pin = static_cast<InternalGPIOPin *>(this->pin_);
55 this->store_.setup(internal_pin, this);
57 return;
58 }
59#endif
60 this->pin_->setup();
62}
63
65 LOG_BINARY_SENSOR("", "GPIO Binary Sensor", this);
66 LOG_PIN(" Pin: ", this->pin_);
67#ifdef USE_GPIO_BINARY_SENSOR_INTERRUPT
68 ESP_LOGCONFIG(TAG, " Mode: %s", LOG_STR_ARG(gpio_mode_to_string(this->store_.use_interrupt_)));
69 if (this->store_.use_interrupt_) {
70 ESP_LOGCONFIG(TAG, " Interrupt Type: %s", LOG_STR_ARG(interrupt_type_to_string(this->store_.interrupt_type_)));
71 }
72#else
73 ESP_LOGCONFIG(TAG, " Mode: polling");
74#endif
75}
76
78#ifdef USE_GPIO_BINARY_SENSOR_INTERRUPT
79 if (this->store_.use_interrupt_) {
80 if (this->store_.is_changed()) {
81 // Clear the flag immediately to minimize the window where we might miss changes
82 this->store_.clear_changed();
83 // Read the state and publish it
84 // Note: If the ISR fires between clear_changed() and get_state(), that's fine -
85 // we'll process the new change on the next loop iteration
86 bool state = this->store_.get_state();
87 this->publish_state(state);
88 } else {
89 // No changes, disable the loop until the next interrupt
90 this->disable_loop();
91 }
92 return;
93 }
94#endif
95 this->publish_state(this->pin_->digital_read());
96}
97
99
100} // 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:100
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:43