ESPHome 2026.3.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 {
6namespace gpio {
7
8static const char *const TAG = "gpio.binary_sensor";
9
10#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
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
25 bool new_state = arg->isr_pin_.digital_read();
26 if (new_state != arg->last_state_) {
27 arg->state_ = new_state;
28 arg->last_state_ = new_state;
29 arg->changed_ = true;
30 // Wake up the component from its disabled loop state
31 if (arg->component_ != nullptr) {
33 }
34 }
35}
36
38 pin->setup();
39 this->isr_pin_ = pin->to_isr();
40 this->component_ = component;
41
42 // Read initial state
43 this->last_state_ = pin->digital_read();
44 this->state_ = this->last_state_;
45
46 // Attach interrupt - from this point on, any changes will be caught by the interrupt
48}
49
51 if (this->use_interrupt_ && !this->pin_->is_internal()) {
52 ESP_LOGD(TAG, "GPIO is not internal, falling back to polling mode");
53 this->use_interrupt_ = false;
54 }
55
56 if (this->use_interrupt_) {
57 auto *internal_pin = static_cast<InternalGPIOPin *>(this->pin_);
58 this->store_.setup(internal_pin, this->interrupt_type_, this);
60 } else {
61 this->pin_->setup();
63 }
64}
65
67 LOG_BINARY_SENSOR("", "GPIO Binary Sensor", this);
68 LOG_PIN(" Pin: ", this->pin_);
69 ESP_LOGCONFIG(TAG, " Mode: %s", LOG_STR_ARG(gpio_mode_to_string(this->use_interrupt_)));
70 if (this->use_interrupt_) {
71 ESP_LOGCONFIG(TAG, " Interrupt Type: %s", LOG_STR_ARG(interrupt_type_to_string(this->interrupt_type_)));
72 }
73}
74
76 if (this->use_interrupt_) {
77 if (this->store_.is_changed()) {
78 // Clear the flag immediately to minimize the window where we might miss changes
79 this->store_.clear_changed();
80 // Read the state and publish it
81 // Note: If the ISR fires between clear_changed() and get_state(), that's fine -
82 // we'll process the new change on the next loop iteration
83 bool state = this->store_.get_state();
84 this->publish_state(state);
85 } else {
86 // No changes, disable the loop until the next interrupt
87 this->disable_loop();
88 }
89 } else {
90 this->publish_state(this->pin_->digital_read());
91 }
92}
93
95
96} // namespace gpio
97} // namespace esphome
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 ESPDEPRECATED("Override dump_summary(char*, size_t) instead. Will be removed in 2026.7.0.", "2026.1.0") virtual std boo is_internal)()
Get a summary of this pin as a string.
Definition gpio.h:88
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.
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, gpio::InterruptType type, Component *component)
static void gpio_intr(GPIOBinarySensorStore *arg)
const Component * component
Definition component.cpp:37
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:29
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7