ESPHome 2025.9.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 {
9namespace gpio {
10
11// Store class for ISR data (no vtables, ISR-safe)
13 public:
15
16 static void gpio_intr(GPIOBinarySensorStore *arg);
17
18 bool get_state() const {
19 // No lock needed: state_ is atomically updated by ISR
20 // Volatile ensures we read the latest value
21 return this->state_;
22 }
23
24 bool is_changed() const {
25 // Simple read of volatile bool - no clearing here
26 return this->changed_;
27 }
28
30 // Separate method to clear the flag
31 this->changed_ = false;
32 }
33
34 protected:
36 volatile bool state_{false};
37 volatile bool last_state_{false};
38 volatile bool changed_{false};
39 Component *component_{nullptr}; // Pointer to the component for enable_loop_soon_any_context()
40};
41
43 public:
44 // No destructor needed: ESPHome components are created at boot and live forever.
45 // Interrupts are only detached on reboot when memory is cleared anyway.
46
47 void set_pin(GPIOPin *pin) { pin_ = pin; }
48 void set_use_interrupt(bool use_interrupt) { use_interrupt_ = use_interrupt; }
50 // ========== INTERNAL METHODS ==========
51 // (In most use cases you won't need these)
53 void setup() override;
54 void dump_config() override;
56 float get_setup_priority() const override;
58 void loop() override;
59
60 protected:
62 bool use_interrupt_{true};
65};
66
67} // namespace gpio
68} // namespace esphome
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio.h:73
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)
uint8_t type
void setup()
@ INTERRUPT_ANY_EDGE
Definition gpio.h:43
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7