ESPHome 2026.5.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->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_) {
50 auto *internal_pin = static_cast<InternalGPIOPin *>(this->pin_);
51 this->store_.setup(internal_pin, this);
53 } else {
54 this->pin_->setup();
56 }
57}
58
60 LOG_BINARY_SENSOR("", "GPIO Binary Sensor", this);
61 LOG_PIN(" Pin: ", this->pin_);
62 ESP_LOGCONFIG(TAG, " Mode: %s", LOG_STR_ARG(gpio_mode_to_string(this->store_.use_interrupt_)));
63 if (this->store_.use_interrupt_) {
64 ESP_LOGCONFIG(TAG, " Interrupt Type: %s", LOG_STR_ARG(interrupt_type_to_string(this->store_.interrupt_type_)));
65 }
66}
67
69 if (this->store_.use_interrupt_) {
70 if (this->store_.is_changed()) {
71 // Clear the flag immediately to minimize the window where we might miss changes
72 this->store_.clear_changed();
73 // Read the state and publish it
74 // Note: If the ISR fires between clear_changed() and get_state(), that's fine -
75 // we'll process the new change on the next loop iteration
76 bool state = this->store_.get_state();
77 this->publish_state(state);
78 } else {
79 // No changes, disable the loop until the next interrupt
80 this->disable_loop();
81 }
82 } else {
83 this->publish_state(this->pin_->digital_read());
84 }
85}
86
88
89} // namespace gpio
90} // 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