Skip to content

Commit

Permalink
[HLW8012] Fix setting calibration via command
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-er committed Nov 28, 2023
1 parent 29cfc92 commit 4add81f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
32 changes: 26 additions & 6 deletions lib/HLW8012_1.1.1/src/HLW8012.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(_current) == 0) getCurrent(valid);
if (valid && static_cast<int>(_current) > 0) _current_multiplier *= (value / _current);
if (valid && static_cast<int>(_current) > 0) _current_multiplier *= (expected / _current);
}

void HLW8012::expectedVoltage(float value) {
void HLW8012::expectedVoltage(float expected) {
bool valid = false;
if (static_cast<int>(_voltage) == 0) getVoltage(valid);
if (valid && static_cast<int>(_voltage) > 0) _voltage_multiplier *= (value / _voltage);
if (valid && static_cast<int>(_voltage) > 0) _voltage_multiplier *= (expected / _voltage);
}

void HLW8012::expectedActivePower(float value) {
void HLW8012::expectedActivePower(float expected) {
bool valid = false;
if (static_cast<int>(_power) == 0) getActivePower(valid);
if (valid && static_cast<int>(_power) > 0) _power_multiplier *= (value / _power);
if (valid && static_cast<int>(_power) > 0) _power_multiplier *= (expected / _power);
}

void HLW8012::expectedCurrent(float expected, float measured) {
if (static_cast<int>(expected) == 0 || static_cast<int>(measured) == 0)
return;
_current_multiplier *= (expected / measured);
}

void HLW8012::expectedVoltage(float expected, float measured) {
if (static_cast<int>(expected) == 0 || static_cast<int>(measured) == 0)
return;
_voltage_multiplier *= (expected / measured);
}

void HLW8012::expectedActivePower(float expected, float measured) {
if (static_cast<int>(expected) == 0 || static_cast<int>(measured) == 0)
return;
_power_multiplier *= (expected / measured);
}



void HLW8012::resetMultipliers() {
_calculateDefaultMultipliers();
}
Expand Down
11 changes: 8 additions & 3 deletions lib/HLW8012_1.1.1/src/HLW8012.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down
6 changes: 3 additions & 3 deletions src/_P076_HLW8012.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 4add81f

Please sign in to comment.