Browse Source

Fix early access bug to HLW8012 instance on HLW8012Sensor

fastled
Xose Pérez 7 years ago
parent
commit
28d59083ac
3 changed files with 20 additions and 19 deletions
  1. +15
    -16
      code/espurna/sensor.ino
  2. +4
    -0
      code/espurna/sensors/BaseSensor.h
  3. +1
    -3
      code/espurna/sensors/HLW8012Sensor.h

+ 15
- 16
code/espurna/sensor.ino View File

@ -421,33 +421,36 @@ void _sensorConfigure() {
} }
#endif // EMON_ANALOG_SUPPORT #endif // EMON_ANALOG_SUPPORT
// Force sensor to reload config
_sensors[i]->begin();
#if HLW8012_SUPPORT #if HLW8012_SUPPORT
if (_sensors[i]->getID() == SENSOR_HLW8012_ID) { if (_sensors[i]->getID() == SENSOR_HLW8012_ID) {
HLW8012Sensor * sensor = (HLW8012Sensor *) _sensors[i]; 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); sensor->expectedCurrent(value);
setSetting("pwrRatioC", sensor->getCurrentRatio()); 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); sensor->expectedVoltage(value);
setSetting("pwrRatioV", sensor->getVoltageRatio()); 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); sensor->expectedPower(value);
setSetting("pwrRatioP", sensor->getPowerRatio()); setSetting("pwrRatioP", sensor->getPowerRatio());
} else {
value = getSetting("pwrRatioP", 0).toFloat();
if (value > 0) sensor->setPowerRatio(value);
} }
if (getSetting("pwrResetCalibration", 0).toInt() == 1) { if (getSetting("pwrResetCalibration", 0).toInt() == 1) {
@ -459,10 +462,6 @@ void _sensorConfigure() {
} }
#endif // HLW8012_SUPPORT #endif // HLW8012_SUPPORT
// Force sensor to reload config
_sensors[i]->begin();
} }
// General sensor settings // General sensor settings


+ 4
- 0
code/espurna/sensors/BaseSensor.h View File

@ -198,6 +198,8 @@ static void (*_sensor_isrs[16])() = {
}; };
void BaseSensor::attach(BaseSensor * instance, unsigned char gpio, unsigned char mode) { void BaseSensor::attach(BaseSensor * instance, unsigned char gpio, unsigned char mode) {
if ((6 <= gpio) && (gpio <=11)) return;
if (gpio >= 16) return;
detach(gpio); detach(gpio);
if (_sensor_isrs[gpio]) { if (_sensor_isrs[gpio]) {
_isr_sensor_instance[gpio] = instance; _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) { void BaseSensor::detach(unsigned char gpio) {
if ((6 <= gpio) && (gpio <=11)) return;
if (gpio >= 16) return;
if (_isr_sensor_instance[gpio]) { if (_isr_sensor_instance[gpio]) {
detachInterrupt(gpio); detachInterrupt(gpio);
_isr_sensor_instance[gpio]->detached(gpio); _isr_sensor_instance[gpio]->detached(gpio);


+ 1
- 3
code/espurna/sensors/HLW8012Sensor.h View File

@ -22,6 +22,7 @@ class HLW8012Sensor : public BaseSensor {
HLW8012Sensor(): BaseSensor() { HLW8012Sensor(): BaseSensor() {
_count = 7; _count = 7;
_sensor_id = SENSOR_HLW8012_ID; _sensor_id = SENSOR_HLW8012_ID;
_hlw8012 = new HLW8012();
} }
~HLW8012Sensor() { ~HLW8012Sensor() {
@ -119,9 +120,6 @@ class HLW8012Sensor : public BaseSensor {
// Defined outside the class body // Defined outside the class body
void begin() { void begin() {
if (_hlw8012) delete _hlw8012;
_hlw8012 = new HLW8012();
// Initialize 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); // 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 // * cf_pin, cf1_pin and sel_pin are GPIOs to the HLW8012 IC


Loading…
Cancel
Save