From 42f5ee537637d246f7f2e2c116abf77e9188d789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Wed, 20 Dec 2017 13:39:47 +0100 Subject: [PATCH] Flag sensor changes to avoid reinitializing them if unnecessary --- code/espurna/sensors/BMX280Sensor.h | 4 ++++ code/espurna/sensors/BaseSensor.h | 1 + code/espurna/sensors/DallasSensor.h | 7 ++++++- code/espurna/sensors/EmonADC121Sensor.h | 4 ++++ code/espurna/sensors/EmonADS1X15Sensor.h | 7 +++++++ code/espurna/sensors/EmonAnalogSensor.h | 5 ++++- code/espurna/sensors/EmonSensor.h | 11 +++++++---- code/espurna/sensors/MHZ19Sensor.h | 5 +++++ code/espurna/sensors/PMSX003Sensor.h | 5 +++++ code/espurna/sensors/SI7021Sensor.h | 4 ++++ 10 files changed, 47 insertions(+), 6 deletions(-) diff --git a/code/espurna/sensors/BMX280Sensor.h b/code/espurna/sensors/BMX280Sensor.h index 4e6342e3..93677436 100644 --- a/code/espurna/sensors/BMX280Sensor.h +++ b/code/espurna/sensors/BMX280Sensor.h @@ -22,6 +22,7 @@ class BMX280Sensor : public BaseSensor { // --------------------------------------------------------------------- void setAddress(unsigned char address) { + if (_address != address) _dirty = true; _address = address; } @@ -32,6 +33,9 @@ class BMX280Sensor : public BaseSensor { // Initialization method, must be idempotent void begin() { + if (!_dirty) return; + _dirty = false; + // Discover if (_address == 0) { unsigned char addresses[] = {0x76, 0x77}; diff --git a/code/espurna/sensors/BaseSensor.h b/code/espurna/sensors/BaseSensor.h index 2dbae59d..2790edd4 100644 --- a/code/espurna/sensors/BaseSensor.h +++ b/code/espurna/sensors/BaseSensor.h @@ -116,6 +116,7 @@ class BaseSensor { void detach(unsigned char gpio); int _error = 0; + bool _dirty = true; unsigned char _count = 0; diff --git a/code/espurna/sensors/DallasSensor.h b/code/espurna/sensors/DallasSensor.h index 6f977512..8fff6196 100644 --- a/code/espurna/sensors/DallasSensor.h +++ b/code/espurna/sensors/DallasSensor.h @@ -34,9 +34,11 @@ class DallasSensor : public BaseSensor { // --------------------------------------------------------------------- void setGPIO(unsigned char gpio, bool pullup = false) { + if (_gpio != gpio) _dirty = true; + if (_pullup != pullup) _dirty = true; _gpio = gpio; - _interval = SENSOR_READ_INTERVAL / 2; _pullup = pullup; + _interval = SENSOR_READ_INTERVAL / 2; } // --------------------------------------------------------------------- @@ -46,6 +48,9 @@ class DallasSensor : public BaseSensor { // Initialization method, must be idempotent void begin() { + if (!_dirty) return; + _dirty = false; + // OneWire if (_wire) delete _wire; _wire = new OneWire(_gpio); diff --git a/code/espurna/sensors/EmonADC121Sensor.h b/code/espurna/sensors/EmonADC121Sensor.h index 6e622993..dd1ae1e5 100644 --- a/code/espurna/sensors/EmonADC121Sensor.h +++ b/code/espurna/sensors/EmonADC121Sensor.h @@ -37,6 +37,7 @@ class EmonADC121Sensor : public EmonAnalogSensor { // --------------------------------------------------------------------- void setAddress(unsigned char address) { + if (_address != address) _dirty = true; _address = address; } @@ -47,6 +48,9 @@ class EmonADC121Sensor : public EmonAnalogSensor { // Initialization method, must be idempotent void begin() { + if (!_dirty) return; + _dirty = false; + // Discover if (_address == 0) { unsigned char addresses[] = {0x50, 0x51, 0x52, 0x54, 0x55, 0x56, 0x58, 0x59, 0x5A}; diff --git a/code/espurna/sensors/EmonADS1X15Sensor.h b/code/espurna/sensors/EmonADS1X15Sensor.h index 06f0157c..982a1e95 100644 --- a/code/espurna/sensors/EmonADS1X15Sensor.h +++ b/code/espurna/sensors/EmonADS1X15Sensor.h @@ -106,18 +106,22 @@ class EmonADS1X15Sensor : public EmonSensor { // --------------------------------------------------------------------- void setAddress(unsigned char address) { + if (_address != address) _dirty = true; _address = address; } void setType(unsigned char type) { + if (_type != type) _dirty = true; _type = type; } void setMask(unsigned char mask) { + if (_mask != mask) _dirty = true; _mask = mask; } void setGain(unsigned int gain) { + if (_gain != gain) _dirty = true; _gain = gain; } @@ -128,6 +132,9 @@ class EmonADS1X15Sensor : public EmonSensor { // Initialization method, must be idempotent void begin() { + if (!_dirty) return; + _dirty = false; + // Discover if (_address == 0) { unsigned char addresses[] = {0x48, 0x49, 0x4A, 0x4B}; diff --git a/code/espurna/sensors/EmonAnalogSensor.h b/code/espurna/sensors/EmonAnalogSensor.h index 4c053e6b..c6de3357 100644 --- a/code/espurna/sensors/EmonAnalogSensor.h +++ b/code/espurna/sensors/EmonAnalogSensor.h @@ -26,13 +26,16 @@ class EmonAnalogSensor : public EmonSensor { // Initialization method, must be idempotent void begin() { + if (!_dirty) return; + _dirty = false; + // Just one channel _count = _magnitudes; // Bit depth _resolution = INTERNAL_ADC_RESOLUTION; - // Init analog PIN + // Init analog PIN) pinMode(_gpio, INPUT); // Call the parent class method diff --git a/code/espurna/sensors/EmonSensor.h b/code/espurna/sensors/EmonSensor.h index 09774f9b..a77c3e99 100644 --- a/code/espurna/sensors/EmonSensor.h +++ b/code/espurna/sensors/EmonSensor.h @@ -29,15 +29,18 @@ class EmonSensor : public BaseSensor { } void setVoltage(double voltage) { + if (_voltage != voltage) _dirty = true; _voltage = voltage; } - void setReference(double ref) { - _reference = ref; + void setReference(double reference) { + if (_reference != reference) _dirty = true; + _reference = reference; } - void setCurrentRatio(double ratio) { - _current_ratio = ratio; + void setCurrentRatio(double current_ratio) { + if (_current_ratio != current_ratio) _dirty = true; + _current_ratio = current_ratio; } // --------------------------------------------------------------------- diff --git a/code/espurna/sensors/MHZ19Sensor.h b/code/espurna/sensors/MHZ19Sensor.h index 5df94507..e5709841 100644 --- a/code/espurna/sensors/MHZ19Sensor.h +++ b/code/espurna/sensors/MHZ19Sensor.h @@ -34,6 +34,8 @@ class MHZ19Sensor : public BaseSensor { } void setGPIO(unsigned char pin_rx, unsigned char pin_tx) { + if (_pin_rx != pin_rx) _dirty = true; + if (_pin_tx != pin_tx) _dirty = true; _pin_rx = pin_rx; _pin_tx = pin_tx; } @@ -45,6 +47,9 @@ class MHZ19Sensor : public BaseSensor { // Initialization method, must be idempotent void begin() { + if (!_dirty) return; + _dirty = false; + if (_serial) delete _serial; _serial = new SoftwareSerial(_pin_rx, _pin_tx, false, 256); diff --git a/code/espurna/sensors/PMSX003Sensor.h b/code/espurna/sensors/PMSX003Sensor.h index be8ccaac..3febcbd4 100644 --- a/code/espurna/sensors/PMSX003Sensor.h +++ b/code/espurna/sensors/PMSX003Sensor.h @@ -25,6 +25,8 @@ class PMSX003Sensor : public BaseSensor { } void setGPIO(unsigned char pin_rx, unsigned char pin_tx) { + if (_pin_rx != pin_rx) _dirty = true; + if (_pin_tx != pin_tx) _dirty = true; _pin_rx = pin_rx; _pin_tx = pin_tx; } @@ -36,6 +38,9 @@ class PMSX003Sensor : public BaseSensor { // Initialization method, must be idempotent void begin() { + if (!_dirty) return; + _dirty = false; + if (_serial) delete _serial; if (_pms) delete _pms; diff --git a/code/espurna/sensors/SI7021Sensor.h b/code/espurna/sensors/SI7021Sensor.h index 00750e3e..8840c9e1 100644 --- a/code/espurna/sensors/SI7021Sensor.h +++ b/code/espurna/sensors/SI7021Sensor.h @@ -32,6 +32,7 @@ class SI7021Sensor : public BaseSensor { // --------------------------------------------------------------------- void setAddress(unsigned char address) { + if (_address != address) _dirty = true; _address = address; } @@ -42,6 +43,9 @@ class SI7021Sensor : public BaseSensor { // Initialization method, must be idempotent void begin() { + if (!_dirty) return; + _dirty = false; + // Discover if (_address == 0) { unsigned char addresses[] = {0x40};