From 1fd5cbe5fd8f5d4bd40e07979d1fffd97dfacfa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Mon, 18 Dec 2017 18:12:09 +0100 Subject: [PATCH] Playing with the sensor interrupt routines --- code/espurna/config/sensors.h | 2 +- code/espurna/sensor.ino | 30 ++++++++++++++++-------------- code/espurna/sensors/BaseSensor.h | 2 +- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 8eb4c1ef..afa1514c 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -153,7 +153,7 @@ #endif #define COUNTER_REPORT_EVERY 12 // Report counter every this updates (1 minute) -#define COUNTER_DEBOUNCE 10 // Do not register events within less than 10 millis +#define COUNTER_DEBOUNCE 50 // Do not register events within less than 10 millis #define COUNTER_TOPIC "counter" // Default topic for MQTT, API and InfluxDB //-------------------------------------------------------------------------------- diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index e1685bd6..091bc870 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -88,7 +88,7 @@ String _sensorUnits(magnitude_t type) { if (type == MAGNITUDE_POWER_FACTOR) return String("%"); if (type == MAGNITUDE_ENERGY) return String("J"); if (type == MAGNITUDE_ENERGY_DELTA) return String("J"); - if (type == MAGNITUDE_EVENTS) return String("/m"); + if (type == MAGNITUDE_EVENTS) return String("/min"); if (type == MAGNITUDE_PM1dot0) return String("µg/m3"); if (type == MAGNITUDE_PM2dot5) return String("µg/m3"); if (type == MAGNITUDE_PM10) return String("µg/m3"); @@ -186,13 +186,21 @@ void _sensorPost() { } // ----------------------------------------------------------------------------- -// Values +// Interrupts // ----------------------------------------------------------------------------- +#if COUNTER_SUPPORT -void sensorISR() { - _sensors[_sensor_isr]->InterruptHandler(); +unsigned char _event_sensor_id = 0; +void isrEventSensor() { + _sensors[_event_sensor_id]->InterruptHandler(); } +#endif // COUNTER_SUPPORT + +// ----------------------------------------------------------------------------- +// Values +// ----------------------------------------------------------------------------- + void sensorRegister(BaseSensor * sensor) { _sensors.push_back(sensor); } @@ -220,11 +228,6 @@ unsigned char magnitudeType(unsigned char index) { return MAGNITUDE_NONE; } -void sensorInterrupt(unsigned char sensor_id, unsigned char gpio, int mode) { - _sensor_isr = sensor_id; - attachInterrupt(gpio, sensorISR, mode); -} - void sensorInit() { #if DHT_SUPPORT @@ -273,11 +276,10 @@ void sensorInit() { #endif #if COUNTER_SUPPORT - if (_sensor_isr == 0xFF) { - #include "sensors/EventSensor.h" - sensorRegister(new EventSensor(COUNTER_PIN, COUNTER_PIN_MODE, COUNTER_DEBOUNCE)); - sensorInterrupt(sensorCount()-1, COUNTER_PIN, COUNTER_INTERRUPT_MODE); - } + #include "sensors/EventSensor.h" + sensorRegister(new EventSensor(COUNTER_PIN, COUNTER_PIN_MODE, COUNTER_DEBOUNCE)); + _event_sensor_id = sensorCount() - 1; + attachInterrupt(COUNTER_PIN, isrEventSensor, COUNTER_INTERRUPT_MODE); #endif } diff --git a/code/espurna/sensors/BaseSensor.h b/code/espurna/sensors/BaseSensor.h index adb61c58..90cdbd4f 100644 --- a/code/espurna/sensors/BaseSensor.h +++ b/code/espurna/sensors/BaseSensor.h @@ -49,7 +49,7 @@ class BaseSensor { ~BaseSensor() {} // General interrupt handler - void InterruptHandler() {} + virtual void InterruptHandler() {} // Loop-like method, call it in your main loop virtual void tick() {}