ESPHome 2026.4.3
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->state_) {
27 arg->state_ = new_state;
28 arg->changed_ = true;
29 // Wake up the component from its disabled loop state
30 if (arg->component_ != nullptr) {
32 }
33 }
34}
35
37 pin->setup();
38 this->isr_pin_ = pin->to_isr();
39 this->component_ = component;
40
41 // Read initial state
42 this->state_ = pin->digital_read();
43
44 // Attach interrupt - from this point on, any changes will be caught by the interrupt
46}
47
49 if (this->store_.use_interrupt_ && !this->pin_->is_internal()) {
50 ESP_LOGD(TAG, "GPIO is not internal, falling back to polling mode");
51 this->store_.use_interrupt_ = false;
52 }
53
54 if (this->store_.use_interrupt_) {
55 auto *internal_pin = static_cast<InternalGPIOPin *>(this->pin_);
56 this->store_.setup(internal_pin, this);
58 } else {
59 this->pin_->setup();
61 }
62}
63
65 LOG_BINARY_SENSOR("", "GPIO Binary Sensor", this);
66 LOG_PIN(" Pin: ", this->pin_);
67 ESP_LOGCONFIG(TAG, " Mode: %s", LOG_STR_ARG(gpio_mode_to_string(this->store_.use_interrupt_)));
68 if (this->store_.use_interrupt_) {
69 ESP_LOGCONFIG(TAG, " Interrupt Type: %s", LOG_STR_ARG(interrupt_type_to_string(this->store_.interrupt_type_)));
70 }
71}
72
74 if (this->store_.use_interrupt_) {
75 if (this->store_.is_changed()) {
76 // Clear the flag immediately to minimize the window where we might miss changes
77 this->store_.clear_changed();
78 // Read the state and publish it
79 // Note: If the ISR fires between clear_changed() and get_state(), that's fine -
80 // we'll process the new change on the next loop iteration
81 bool state = this->store_.get_state();
82 this->publish_state(state);
83 } else {
84 // No changes, disable the loop until the next interrupt
85 this->disable_loop();
86 }
87 } else {
88 this->publish_state(this->pin_->digital_read());
89 }
90}
91
93
94} // namespace gpio
95} // 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 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:40
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7