1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-14 17:22:20 +01:00

Merge branch 'binary_sensor_gpio_polling' into integration

This commit is contained in:
J. Nick Koston
2025-06-18 12:28:59 +02:00
2 changed files with 12 additions and 3 deletions

View File

@@ -12,12 +12,17 @@ void IRAM_ATTR GPIOBinarySensorStore::gpio_intr(GPIOBinarySensorStore *arg) {
arg->state_ = new_state; arg->state_ = new_state;
arg->last_state_ = new_state; arg->last_state_ = new_state;
arg->changed_ = true; arg->changed_ = true;
// Wake up the component from its disabled loop state
if (arg->component_ != nullptr) {
arg->component_->enable_loop_soon_from_isr();
}
} }
} }
void GPIOBinarySensorStore::setup(InternalGPIOPin *pin, gpio::InterruptType type) { void GPIOBinarySensorStore::setup(InternalGPIOPin *pin, gpio::InterruptType type, Component *component) {
pin->setup(); pin->setup();
this->isr_pin_ = pin->to_isr(); this->isr_pin_ = pin->to_isr();
this->component_ = component;
// Read initial state // Read initial state
this->last_state_ = pin->digital_read(); this->last_state_ = pin->digital_read();
@@ -35,7 +40,7 @@ void GPIOBinarySensor::setup() {
if (this->use_interrupt_) { if (this->use_interrupt_) {
auto *internal_pin = static_cast<InternalGPIOPin *>(this->pin_); auto *internal_pin = static_cast<InternalGPIOPin *>(this->pin_);
this->store_.setup(internal_pin, this->interrupt_type_); this->store_.setup(internal_pin, this->interrupt_type_, this);
this->publish_initial_state(this->store_.get_state()); this->publish_initial_state(this->store_.get_state());
} else { } else {
this->pin_->setup(); this->pin_->setup();
@@ -78,6 +83,9 @@ void GPIOBinarySensor::loop() {
// we'll process the new change on the next loop iteration // we'll process the new change on the next loop iteration
bool state = this->store_.get_state(); bool state = this->store_.get_state();
this->publish_state(state); this->publish_state(state);
} else {
// No changes, disable the loop until the next interrupt
this->disable_loop();
} }
} else { } else {
this->publish_state(this->pin_->digital_read()); this->publish_state(this->pin_->digital_read());

View File

@@ -11,7 +11,7 @@ namespace gpio {
// Store class for ISR data (no vtables, ISR-safe) // Store class for ISR data (no vtables, ISR-safe)
class GPIOBinarySensorStore { class GPIOBinarySensorStore {
public: public:
void setup(InternalGPIOPin *pin, gpio::InterruptType type); void setup(InternalGPIOPin *pin, gpio::InterruptType type, Component *component);
static void gpio_intr(GPIOBinarySensorStore *arg); static void gpio_intr(GPIOBinarySensorStore *arg);
@@ -36,6 +36,7 @@ class GPIOBinarySensorStore {
volatile bool state_{false}; volatile bool state_{false};
volatile bool last_state_{false}; volatile bool last_state_{false};
volatile bool changed_{false}; volatile bool changed_{false};
Component *component_{nullptr}; // Pointer to the component for enable_loop_soon_from_isr()
}; };
class GPIOBinarySensor : public binary_sensor::BinarySensor, public Component { class GPIOBinarySensor : public binary_sensor::BinarySensor, public Component {