From 775b51c6a1d0dfb603759b2ac3488ebf51eeb09d Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sun, 27 Oct 2019 12:30:19 +0100 Subject: [PATCH] AS3935 Use normal pin polling for IRQ (#805) * AS3935 Use normal pin polling for IRQ See also https://github.com/esphome/feature-requests/issues/452 * Fix tests --- esphome/components/as3935/__init__.py | 11 +++++------ esphome/components/as3935/as3935.cpp | 13 ++++--------- esphome/components/as3935/as3935.h | 13 ++----------- tests/test1.yaml | 2 +- tests/test2.yaml | 2 +- 5 files changed, 13 insertions(+), 28 deletions(-) diff --git a/esphome/components/as3935/__init__.py b/esphome/components/as3935/__init__.py index f8ac4eea01..de25060623 100644 --- a/esphome/components/as3935/__init__.py +++ b/esphome/components/as3935/__init__.py @@ -1,12 +1,11 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome import pins -from esphome.const import CONF_PIN, CONF_INDOOR, CONF_WATCHDOG_THRESHOLD, \ +from esphome.const import CONF_INDOOR, CONF_WATCHDOG_THRESHOLD, \ CONF_NOISE_LEVEL, CONF_SPIKE_REJECTION, CONF_LIGHTNING_THRESHOLD, \ CONF_MASK_DISTURBER, CONF_DIV_RATIO, CONF_CAPACITANCE from esphome.core import coroutine - AUTO_LOAD = ['sensor', 'binary_sensor'] MULTI_CONF = True @@ -15,10 +14,10 @@ CONF_AS3935_ID = 'as3935_id' as3935_ns = cg.esphome_ns.namespace('as3935') AS3935 = as3935_ns.class_('AS3935Component', cg.Component) +CONF_IRQ_PIN = 'irq_pin' AS3935_SCHEMA = cv.Schema({ cv.GenerateID(): cv.declare_id(AS3935), - cv.Required(CONF_PIN): cv.All(pins.internal_gpio_input_pin_schema, - pins.validate_has_interrupt), + cv.Required(CONF_IRQ_PIN): pins.gpio_input_pin_schema, cv.Optional(CONF_INDOOR, default=True): cv.boolean, cv.Optional(CONF_NOISE_LEVEL, default=2): cv.int_range(min=1, max=7), @@ -35,8 +34,8 @@ AS3935_SCHEMA = cv.Schema({ def setup_as3935(var, config): yield cg.register_component(var, config) - pin = yield cg.gpio_pin_expression(config[CONF_PIN]) - cg.add(var.set_pin(pin)) + irq_pin = yield cg.gpio_pin_expression(config[CONF_IRQ_PIN]) + cg.add(var.set_irq_pin(irq_pin)) cg.add(var.set_indoor(config[CONF_INDOOR])) cg.add(var.set_noise_level(config[CONF_NOISE_LEVEL])) cg.add(var.set_watchdog_threshold(config[CONF_WATCHDOG_THRESHOLD])) diff --git a/esphome/components/as3935/as3935.cpp b/esphome/components/as3935/as3935.cpp index 0fc8f086d2..f8272e6036 100644 --- a/esphome/components/as3935/as3935.cpp +++ b/esphome/components/as3935/as3935.cpp @@ -9,10 +9,8 @@ static const char *TAG = "as3935"; void AS3935Component::setup() { ESP_LOGCONFIG(TAG, "Setting up AS3935..."); - this->pin_->setup(); - this->store_.pin = this->pin_->to_isr(); - LOG_PIN(" Interrupt Pin: ", this->pin_); - this->pin_->attach_interrupt(AS3935ComponentStore::gpio_intr, &this->store_, RISING); + this->irq_pin_->setup(); + LOG_PIN(" IRQ Pin: ", this->irq_pin_); // Write properties to sensor this->write_indoor(this->indoor_); @@ -27,13 +25,13 @@ void AS3935Component::setup() { void AS3935Component::dump_config() { ESP_LOGCONFIG(TAG, "AS3935:"); - LOG_PIN(" Interrupt Pin: ", this->pin_); + LOG_PIN(" Interrupt Pin: ", this->irq_pin_); } float AS3935Component::get_setup_priority() const { return setup_priority::DATA; } void AS3935Component::loop() { - if (!this->store_.interrupt) + if (!this->irq_pin_->digital_read()) return; uint8_t int_value = this->read_interrupt_register_(); @@ -53,7 +51,6 @@ void AS3935Component::loop() { this->energy_sensor_->publish_state(energy); } this->thunder_alert_binary_sensor_->publish_state(false); - this->store_.interrupt = false; } void AS3935Component::write_indoor(bool indoor) { @@ -222,7 +219,5 @@ uint8_t AS3935Component::read_register_(uint8_t reg, uint8_t mask) { return value; } -void ICACHE_RAM_ATTR AS3935ComponentStore::gpio_intr(AS3935ComponentStore *arg) { arg->interrupt = true; } - } // namespace as3935 } // namespace esphome diff --git a/esphome/components/as3935/as3935.h b/esphome/components/as3935/as3935.h index ca7d409282..d0e53e7832 100644 --- a/esphome/components/as3935/as3935.h +++ b/esphome/components/as3935/as3935.h @@ -50,14 +50,6 @@ enum AS3935Values { NOISE_INT = 0x01 }; -/// Store data in a class that doesn't use multiple-inheritance (vtables in flash) -struct AS3935ComponentStore { - volatile bool interrupt; - - ISRInternalGPIOPin *pin; - static void gpio_intr(AS3935ComponentStore *arg); -}; - class AS3935Component : public Component { public: void setup() override; @@ -65,7 +57,7 @@ class AS3935Component : public Component { float get_setup_priority() const override; void loop() override; - void set_pin(GPIOPin *pin) { pin_ = pin; } + void set_irq_pin(GPIOPin *irq_pin) { irq_pin_ = irq_pin; } void set_distance_sensor(sensor::Sensor *distance_sensor) { distance_sensor_ = distance_sensor; } void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; } void set_thunder_alert_binary_sensor(binary_sensor::BinarySensor *thunder_alert_binary_sensor) { @@ -102,8 +94,7 @@ class AS3935Component : public Component { sensor::Sensor *distance_sensor_; sensor::Sensor *energy_sensor_; binary_sensor::BinarySensor *thunder_alert_binary_sensor_; - GPIOPin *pin_; - AS3935ComponentStore store_; + GPIOPin *irq_pin_; bool indoor_; uint8_t noise_level_; diff --git a/tests/test1.yaml b/tests/test1.yaml index 2fbef9d4f7..66f0220836 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -172,7 +172,7 @@ dallas: as3935_spi: cs_pin: GPIO12 - pin: GPIO13 + irq_pin: GPIO13 sensor: - platform: adc diff --git a/tests/test2.yaml b/tests/test2.yaml index 1f4245d81d..097c29285a 100644 --- a/tests/test2.yaml +++ b/tests/test2.yaml @@ -54,7 +54,7 @@ deep_sleep: sleep_duration: 50s as3935_i2c: - pin: GPIO12 + irq_pin: GPIO12 sensor: