ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
gpio_binary_sensor.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
7
8namespace esphome::gpio {
9
10// Store class for ISR data and configuration (no vtables, ISR-safe)
12 public:
14
15 static void gpio_intr(GPIOBinarySensorStore *arg);
16
17 bool get_state() const {
18 // No lock needed: state_ is atomically updated by ISR
19 // Volatile ensures we read the latest value
20 return this->state_;
21 }
22
23 bool is_changed() const {
24 // Simple read of volatile bool - no clearing here
25 return this->changed_;
26 }
27
29 // Separate method to clear the flag
30 this->changed_ = false;
31 }
32
33 protected:
34 friend class GPIOBinarySensor;
36 Component *component_{nullptr}; // Pointer to the component for enable_loop_soon_any_context()
37 volatile bool state_{false};
38 volatile bool changed_{false};
39 bool use_interrupt_{true};
41};
42
44 public:
45 // No destructor needed: ESPHome components are created at boot and live forever.
46 // Interrupts are only detached on reboot when memory is cleared anyway.
47
48 void set_pin(GPIOPin *pin) { this->pin_ = pin; }
49 void set_use_interrupt(bool use_interrupt) { this->store_.use_interrupt_ = use_interrupt; }
51 // ========== INTERNAL METHODS ==========
52 // (In most use cases you won't need these)
54 void setup() override;
55 void dump_config() override;
57 float get_setup_priority() const override;
59 void loop() override;
60
61 protected:
64};
65
66} // namespace esphome::gpio
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio.h:92
Base class for all binary_sensor-type classes.
void loop() override
Check sensor.
float get_setup_priority() const override
Hardware priority.
void setup() override
Setup pin.
void set_interrupt_type(gpio::InterruptType type)
void set_use_interrupt(bool use_interrupt)
static void gpio_intr(GPIOBinarySensorStore *arg)
const Component * component
Definition component.cpp:34
uint16_t type
void setup()
@ INTERRUPT_ANY_EDGE
Definition gpio.h:52