From 4add81f3742a3f20da23a1ed8ea059f02c466377 Mon Sep 17 00:00:00 2001 From: TD-er Date: Tue, 28 Nov 2023 14:56:58 +0100 Subject: [PATCH] [HLW8012] Fix setting calibration via command Fixes: #4893 --- lib/HLW8012_1.1.1/src/HLW8012.cpp | 32 +++++++++++++++++++++++++------ lib/HLW8012_1.1.1/src/HLW8012.h | 11 ++++++++--- src/_P076_HLW8012.ino | 6 +++--- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/lib/HLW8012_1.1.1/src/HLW8012.cpp b/lib/HLW8012_1.1.1/src/HLW8012.cpp index 0480eeb55b..00b03386f3 100644 --- a/lib/HLW8012_1.1.1/src/HLW8012.cpp +++ b/lib/HLW8012_1.1.1/src/HLW8012.cpp @@ -186,24 +186,44 @@ void HLW8012::resetEnergy() { _cf_pulse_count_total = 0; } -void HLW8012::expectedCurrent(float value) { +void HLW8012::expectedCurrent(float expected) { bool valid = false; if (static_cast(_current) == 0) getCurrent(valid); - if (valid && static_cast(_current) > 0) _current_multiplier *= (value / _current); + if (valid && static_cast(_current) > 0) _current_multiplier *= (expected / _current); } -void HLW8012::expectedVoltage(float value) { +void HLW8012::expectedVoltage(float expected) { bool valid = false; if (static_cast(_voltage) == 0) getVoltage(valid); - if (valid && static_cast(_voltage) > 0) _voltage_multiplier *= (value / _voltage); + if (valid && static_cast(_voltage) > 0) _voltage_multiplier *= (expected / _voltage); } -void HLW8012::expectedActivePower(float value) { +void HLW8012::expectedActivePower(float expected) { bool valid = false; if (static_cast(_power) == 0) getActivePower(valid); - if (valid && static_cast(_power) > 0) _power_multiplier *= (value / _power); + if (valid && static_cast(_power) > 0) _power_multiplier *= (expected / _power); } +void HLW8012::expectedCurrent(float expected, float measured) { + if (static_cast(expected) == 0 || static_cast(measured) == 0) + return; + _current_multiplier *= (expected / measured); +} + +void HLW8012::expectedVoltage(float expected, float measured) { + if (static_cast(expected) == 0 || static_cast(measured) == 0) + return; + _voltage_multiplier *= (expected / measured); +} + +void HLW8012::expectedActivePower(float expected, float measured) { + if (static_cast(expected) == 0 || static_cast(measured) == 0) + return; + _power_multiplier *= (expected / measured); +} + + + void HLW8012::resetMultipliers() { _calculateDefaultMultipliers(); } diff --git a/lib/HLW8012_1.1.1/src/HLW8012.h b/lib/HLW8012_1.1.1/src/HLW8012.h index 1a83c4b9d9..06718a047d 100644 --- a/lib/HLW8012_1.1.1/src/HLW8012.h +++ b/lib/HLW8012_1.1.1/src/HLW8012.h @@ -94,9 +94,14 @@ class HLW8012 { void setResistors(float current, float voltage_upstream, float voltage_downstream); - void expectedCurrent(float current); - void expectedVoltage(float current); - void expectedActivePower(float power); + void expectedCurrent(float expected); + void expectedVoltage(float expected); + void expectedActivePower(float expected); + + void expectedCurrent(float expected, float measured); + void expectedVoltage(float expected, float measured); + void expectedActivePower(float expected, float measured); + float getCurrentMultiplier() { return _current_multiplier; }; float getVoltageMultiplier() { return _voltage_multiplier; }; diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 1925b3283f..a623395b8f 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -511,17 +511,17 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) bool changed = false; if (CalibVolt != 0) { - Plugin_076_hlw->expectedVoltage(CalibVolt); + Plugin_076_hlw->expectedVoltage(CalibVolt, UserVar[event->BaseVarIndex]); changed = true; } if (definitelyGreaterThan(CalibCurr, 0.0f)) { - Plugin_076_hlw->expectedCurrent(CalibCurr); + Plugin_076_hlw->expectedCurrent(CalibCurr, UserVar[event->BaseVarIndex + 1]); changed = true; } if (!essentiallyEqual(CalibAcPwr, 0.0f)) { - Plugin_076_hlw->expectedActivePower(CalibAcPwr); + Plugin_076_hlw->expectedActivePower(CalibAcPwr, UserVar[event->BaseVarIndex + 2]); changed = true; }