mirror of
https://github.com/esphome/esphome.git
synced 2025-09-21 20:52:20 +01:00
Avoid polling for GPIO binary sensors when possible
This commit is contained in:
@@ -16,7 +16,6 @@ void IRAM_ATTR GPIOBinarySensorStore::gpio_intr(GPIOBinarySensorStore *arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GPIOBinarySensorStore::setup(InternalGPIOPin *pin, gpio::InterruptType type) {
|
void GPIOBinarySensorStore::setup(InternalGPIOPin *pin, gpio::InterruptType type) {
|
||||||
this->pin_ = pin;
|
|
||||||
pin->setup();
|
pin->setup();
|
||||||
this->isr_pin_ = pin->to_isr();
|
this->isr_pin_ = pin->to_isr();
|
||||||
|
|
||||||
@@ -28,19 +27,6 @@ void GPIOBinarySensorStore::setup(InternalGPIOPin *pin, gpio::InterruptType type
|
|||||||
pin->attach_interrupt(&GPIOBinarySensorStore::gpio_intr, this, type);
|
pin->attach_interrupt(&GPIOBinarySensorStore::gpio_intr, this, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GPIOBinarySensorStore::detach() {
|
|
||||||
if (this->pin_ != nullptr) {
|
|
||||||
this->pin_->detach_interrupt();
|
|
||||||
this->pin_ = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GPIOBinarySensor::~GPIOBinarySensor() {
|
|
||||||
if (this->use_interrupt_) {
|
|
||||||
this->store_.detach();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void GPIOBinarySensor::setup() {
|
void GPIOBinarySensor::setup() {
|
||||||
if (this->use_interrupt_ && !this->pin_->is_internal()) {
|
if (this->use_interrupt_ && !this->pin_->is_internal()) {
|
||||||
ESP_LOGW(TAG, "Interrupts not supported for this pin type, falling back to polling");
|
ESP_LOGW(TAG, "Interrupts not supported for this pin type, falling back to polling");
|
||||||
|
@@ -12,7 +12,6 @@ namespace gpio {
|
|||||||
class GPIOBinarySensorStore {
|
class GPIOBinarySensorStore {
|
||||||
public:
|
public:
|
||||||
void setup(InternalGPIOPin *pin, gpio::InterruptType type);
|
void setup(InternalGPIOPin *pin, gpio::InterruptType type);
|
||||||
void detach();
|
|
||||||
|
|
||||||
static void gpio_intr(GPIOBinarySensorStore *arg);
|
static void gpio_intr(GPIOBinarySensorStore *arg);
|
||||||
|
|
||||||
@@ -29,7 +28,6 @@ class GPIOBinarySensorStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
InternalGPIOPin *pin_{nullptr};
|
|
||||||
ISRInternalGPIOPin isr_pin_;
|
ISRInternalGPIOPin isr_pin_;
|
||||||
volatile bool state_{false};
|
volatile bool state_{false};
|
||||||
volatile bool last_state_{false};
|
volatile bool last_state_{false};
|
||||||
@@ -38,8 +36,6 @@ class GPIOBinarySensorStore {
|
|||||||
|
|
||||||
class GPIOBinarySensor : public binary_sensor::BinarySensor, public Component {
|
class GPIOBinarySensor : public binary_sensor::BinarySensor, public Component {
|
||||||
public:
|
public:
|
||||||
~GPIOBinarySensor();
|
|
||||||
|
|
||||||
void set_pin(GPIOPin *pin) { pin_ = pin; }
|
void set_pin(GPIOPin *pin) { pin_ = pin; }
|
||||||
void set_use_interrupt(bool use_interrupt) { use_interrupt_ = use_interrupt; }
|
void set_use_interrupt(bool use_interrupt) { use_interrupt_ = use_interrupt; }
|
||||||
void set_interrupt_type(gpio::InterruptType type) { interrupt_type_ = type; }
|
void set_interrupt_type(gpio::InterruptType type) { interrupt_type_ = type; }
|
||||||
|
Reference in New Issue
Block a user