Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix negative watt values #5

Merged
merged 3 commits into from Sep 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/emporia_vue_utility.h
Expand Up @@ -297,16 +297,27 @@ class EmporiaVueUtility : public Component, public UARTDevice {
int32_t watts;

// Read the instant watts value
watts = bswap32(mr->watts);
// (it's actually a 24-bit int)
watts = (bswap32(mr->watts) & 0xFFFFFF);

// Bit 1 of the left most byte indicates a negative value
if (watts & 0x800000) {
if (watts == 0x800000) {
// Exactly "negative zero", which means "missing data"
ESP_LOGI(TAG, "Instant Watts value missing");
return;
} else if (watts & 0xC00000) {
// This is either more than 12MW being returned,
// or it's a negative number in 1's complement.
// Since the returned value is a 24-bit value
// and "watts" is a 32-bit signed int, we can
// get away with this.
watts -= 0xFFFFFF;
} else {
// If we get here, then hopefully it's a negative
// number in signed magnitude format
watts = (watts ^ 0x800000) * -1;
}
watts -= 0x800000;
}

if ((watts >= WATTS_MAX) || (watts < WATTS_MIN)) {
Expand Down