ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
gpio_binary_sensor.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace gpio {
6
7static const char *const TAG = "gpio.binary_sensor";
8
10 bool new_state = arg->isr_pin_.digital_read();
11 if (new_state != arg->last_state_) {
12 arg->state_ = new_state;
13 arg->last_state_ = new_state;
14 arg->changed_ = true;
15 // Wake up the component from its disabled loop state
16 if (arg->component_ != nullptr) {
18 }
19 }
20}
21
23 pin->setup();
24 this->isr_pin_ = pin->to_isr();
25 this->component_ = component;
26
27 // Read initial state
28 this->last_state_ = pin->digital_read();
29 this->state_ = this->last_state_;
30
31 // Attach interrupt - from this point on, any changes will be caught by the interrupt
33}
34
36 if (this->use_interrupt_ && !this->pin_->is_internal()) {
37 ESP_LOGD(TAG, "GPIO is not internal, falling back to polling mode");
38 this->use_interrupt_ = false;
39 }
40
41 if (this->use_interrupt_) {
42 auto *internal_pin = static_cast<InternalGPIOPin *>(this->pin_);
43 this->store_.setup(internal_pin, this->interrupt_type_, this);
45 } else {
46 this->pin_->setup();
48 }
49}
50
52 LOG_BINARY_SENSOR("", "GPIO Binary Sensor", this);
53 LOG_PIN(" Pin: ", this->pin_);
54 const char *mode = this->use_interrupt_ ? "interrupt" : "polling";
55 ESP_LOGCONFIG(TAG, " Mode: %s", mode);
56 if (this->use_interrupt_) {
57 const char *interrupt_type;
58 switch (this->interrupt_type_) {
60 interrupt_type = "RISING_EDGE";
61 break;
63 interrupt_type = "FALLING_EDGE";
64 break;
66 interrupt_type = "ANY_EDGE";
67 break;
68 default:
69 interrupt_type = "UNKNOWN";
70 break;
71 }
72 ESP_LOGCONFIG(TAG, " Interrupt Type: %s", interrupt_type);
73 }
74}
75
77 if (this->use_interrupt_) {
78 if (this->store_.is_changed()) {
79 // Clear the flag immediately to minimize the window where we might miss changes
80 this->store_.clear_changed();
81 // Read the state and publish it
82 // Note: If the ISR fires between clear_changed() and get_state(), that's fine -
83 // we'll process the new change on the next loop iteration
84 bool state = this->store_.get_state();
85 this->publish_state(state);
86 } else {
87 // No changes, disable the loop until the next interrupt
88 this->disable_loop();
89 }
90 } else {
91 this->publish_state(this->pin_->digital_read());
92 }
93}
94
96
97} // namespace gpio
98} // namespace esphome
BedjetMode mode
BedJet operating mode.
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 is_internal()
Definition gpio.h:69
virtual bool digital_read()=0
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:88
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)
uint8_t type
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:42
@ INTERRUPT_RISING_EDGE
Definition gpio.h:41
@ INTERRUPT_ANY_EDGE
Definition gpio.h:43
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:49
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7