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
This commit is contained in:
Otto Winter 2019-10-27 12:30:19 +01:00
parent e0ad5a9009
commit 775b51c6a1
No known key found for this signature in database
GPG key ID: DB66C0BE6013F97E
5 changed files with 13 additions and 28 deletions

View file

@ -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]))

View file

@ -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

View file

@ -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_;

View file

@ -172,7 +172,7 @@ dallas:
as3935_spi:
cs_pin: GPIO12
pin: GPIO13
irq_pin: GPIO13
sensor:
- platform: adc

View file

@ -54,7 +54,7 @@ deep_sleep:
sleep_duration: 50s
as3935_i2c:
pin: GPIO12
irq_pin: GPIO12
sensor: