From 28d59083ac7bae278b4ec6cf4e57d1cf3e596cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 22 Dec 2017 23:02:41 +0100 Subject: [PATCH] Fix early access bug to HLW8012 instance on HLW8012Sensor --- code/espurna/sensor.ino | 31 ++++++++++++++-------------- code/espurna/sensors/BaseSensor.h | 4 ++++ code/espurna/sensors/HLW8012Sensor.h | 4 +--- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino index 15ade216..6fe6e16e 100644 --- a/code/espurna/sensor.ino +++ b/code/espurna/sensor.ino @@ -421,33 +421,36 @@ void _sensorConfigure() { } #endif // EMON_ANALOG_SUPPORT + // Force sensor to reload config + _sensors[i]->begin(); + #if HLW8012_SUPPORT if (_sensors[i]->getID() == SENSOR_HLW8012_ID) { HLW8012Sensor * sensor = (HLW8012Sensor *) _sensors[i]; - if (value = getSetting("pwrExpectedC", 0).toInt() == 0) { - value = getSetting("pwrRatioC", 0).toFloat(); - if (value > 0) sensor->setCurrentRatio(value); - } else { + if (value = getSetting("pwrExpectedC", 0).toFloat()) { sensor->expectedCurrent(value); setSetting("pwrRatioC", sensor->getCurrentRatio()); + } else { + value = getSetting("pwrRatioC", 0).toFloat(); + if (value > 0) sensor->setCurrentRatio(value); } - if (value = getSetting("pwrExpectedV", 0).toInt() == 0) { - value = getSetting("pwrRatioV", 0).toFloat(); - if (value > 0) sensor->setVoltageRatio(value); - } else { + if (value = getSetting("pwrExpectedV", 0).toInt()) { sensor->expectedVoltage(value); setSetting("pwrRatioV", sensor->getVoltageRatio()); + } else { + value = getSetting("pwrRatioV", 0).toFloat(); + if (value > 0) sensor->setVoltageRatio(value); } - if (value = getSetting("pwrExpectedP", 0).toInt() == 0) { - value = getSetting("pwrRatioP", 0).toFloat(); - if (value > 0) sensor->setPowerRatio(value); - } else { + if (value = getSetting("pwrExpectedP", 0).toInt()) { sensor->expectedPower(value); setSetting("pwrRatioP", sensor->getPowerRatio()); + } else { + value = getSetting("pwrRatioP", 0).toFloat(); + if (value > 0) sensor->setPowerRatio(value); } if (getSetting("pwrResetCalibration", 0).toInt() == 1) { @@ -459,10 +462,6 @@ void _sensorConfigure() { } #endif // HLW8012_SUPPORT - - // Force sensor to reload config - _sensors[i]->begin(); - } // General sensor settings diff --git a/code/espurna/sensors/BaseSensor.h b/code/espurna/sensors/BaseSensor.h index 28c4a03a..f0e18d37 100644 --- a/code/espurna/sensors/BaseSensor.h +++ b/code/espurna/sensors/BaseSensor.h @@ -198,6 +198,8 @@ static void (*_sensor_isrs[16])() = { }; void BaseSensor::attach(BaseSensor * instance, unsigned char gpio, unsigned char mode) { + if ((6 <= gpio) && (gpio <=11)) return; + if (gpio >= 16) return; detach(gpio); if (_sensor_isrs[gpio]) { _isr_sensor_instance[gpio] = instance; @@ -207,6 +209,8 @@ void BaseSensor::attach(BaseSensor * instance, unsigned char gpio, unsigned char } void BaseSensor::detach(unsigned char gpio) { + if ((6 <= gpio) && (gpio <=11)) return; + if (gpio >= 16) return; if (_isr_sensor_instance[gpio]) { detachInterrupt(gpio); _isr_sensor_instance[gpio]->detached(gpio); diff --git a/code/espurna/sensors/HLW8012Sensor.h b/code/espurna/sensors/HLW8012Sensor.h index dbceead7..e39c3792 100644 --- a/code/espurna/sensors/HLW8012Sensor.h +++ b/code/espurna/sensors/HLW8012Sensor.h @@ -22,6 +22,7 @@ class HLW8012Sensor : public BaseSensor { HLW8012Sensor(): BaseSensor() { _count = 7; _sensor_id = SENSOR_HLW8012_ID; + _hlw8012 = new HLW8012(); } ~HLW8012Sensor() { @@ -119,9 +120,6 @@ class HLW8012Sensor : public BaseSensor { // Defined outside the class body void begin() { - if (_hlw8012) delete _hlw8012; - _hlw8012 = new HLW8012(); - // Initialize HLW8012 // void begin(unsigned char cf_pin, unsigned char cf1_pin, unsigned char sel_pin, unsigned char currentWhen = HIGH, bool use_interrupts = false, unsigned long pulse_timeout = PULSE_TIMEOUT); // * cf_pin, cf1_pin and sel_pin are GPIOs to the HLW8012 IC