From 5ba2a20f4e671acf47fc9d07e732b0efa1d6b605 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 30 Dec 2023 10:45:53 +0200 Subject: [PATCH] Fix negative value handling --- Software/src/devboard/webserver/webserver.cpp | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/Software/src/devboard/webserver/webserver.cpp b/Software/src/devboard/webserver/webserver.cpp index cd731c7f..dcc79da6 100644 --- a/Software/src/devboard/webserver/webserver.cpp +++ b/Software/src/devboard/webserver/webserver.cpp @@ -126,7 +126,7 @@ String processor(const String& var) { content += ""; // Start a new block with a specific background color - content += "
"; + content += "
"; // Display LED color content += "

LED color: "; @@ -159,7 +159,7 @@ String processor(const String& var) { content += "

"; // Start a new block with a specific background color - content += "
"; + content += "
"; // Display which components are used content += "

Inverter protocol: "; @@ -216,16 +216,35 @@ String processor(const String& var) { content += "

"; // Start a new block with a specific background color - content += "
"; + content += "
"; // Display battery statistics within this block float socFloat = static_cast(SOC) / 100.0; // Convert to float and divide by 100 float sohFloat = static_cast(StateOfHealth) / 100.0; // Convert to float and divide by 100 float voltageFloat = static_cast(battery_voltage) / 10.0; // Convert to float and divide by 10 - float tempMaxFloat = static_cast(temperature_max) / 10.0; // Convert to float and divide by 10 - float tempMinFloat = static_cast(temperature_min) / 10.0; // Convert to float and divide by 10 + float currentFloat = 0; + if (battery_current > 32767) { //Handle negative values on this unsigned value + currentFloat = static_cast(-(65535 - battery_current)) / 10.0; // Convert to float and divide by 10 + } else { + currentFloat = static_cast(battery_current) / 10.0; // Convert to float and divide by 10 + } + + float tempMaxFloat = 0; + float tempMinFloat = 0; + if (temperature_max > 32767) { //Handle negative values on this unsigned value + tempMaxFloat = static_cast(-(65535 - temperature_max)) / 10.0; // Convert to float and divide by 10 + } else { + tempMaxFloat = static_cast(temperature_max) / 10.0; // Convert to float and divide by 10 + } + if (temperature_min > 32767) { //Handle negative values on this unsigned value + tempMinFloat = static_cast(-(65535 - temperature_min)) / 10.0; // Convert to float and divide by 10 + } else { + tempMinFloat = static_cast(temperature_min) / 10.0; // Convert to float and divide by 10 + } + char socString[10]; char sohString[10]; + char currentString[10]; char voltageString[10]; char tempMaxString[10]; char tempMinString[10]; @@ -234,13 +253,14 @@ String processor(const String& var) { dtostrf(socFloat, 6, 2, socString); dtostrf(sohFloat, 6, 2, sohString); dtostrf(voltageFloat, 6, 1, voltageString); + dtostrf(currentFloat, 6, 1, currentString); dtostrf(tempMaxFloat, 6, 1, tempMaxString); dtostrf(tempMinFloat, 6, 1, tempMinString); content += "

SOC: " + String(socString) + "

"; content += "

SOH: " + String(sohString) + "

"; content += "

Voltage: " + String(voltageString) + " V

"; - content += "

Current: " + String(battery_current) + " A

"; + content += "

Current: " + String(currentString) + " A

"; content += "

Power: " + String(stat_batt_power) + " W

"; content += "

Total capacity: " + String(capacity_Wh) + " Wh

"; content += "

Remaining capacity: " + String(remaining_capacity_Wh) + " Wh

";