Skip to content

Commit

Permalink
Fix negative value handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dalathegreat committed Dec 30, 2023
1 parent 81df81a commit 5ba2a20
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions Software/src/devboard/webserver/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ String processor(const String& var) {
content += "</style>";

// Start a new block with a specific background color
content += "<div style='background-color: #303E47; padding: 10px; margin-bottom: 10px;'>";
content += "<div style='background-color: #303E47; padding: 10px; margin-bottom: 10px;border-radius: 50px'>";

// Display LED color
content += "<h4>LED color: ";
Expand Down Expand Up @@ -159,7 +159,7 @@ String processor(const String& var) {
content += "</div>";

// Start a new block with a specific background color
content += "<div style='background-color: #2D3F2F; padding: 10px; margin-bottom: 10px;'>";
content += "<div style='background-color: #2D3F2F; padding: 10px; margin-bottom: 10px; border-radius: 50px'>";

// Display which components are used
content += "<h4 style='color: white;'>Inverter protocol: ";
Expand Down Expand Up @@ -216,16 +216,35 @@ String processor(const String& var) {
content += "</div>";

// Start a new block with a specific background color
content += "<div style='background-color: #333; padding: 10px; margin-bottom: 10px;'>";
content += "<div style='background-color: #333; padding: 10px; margin-bottom: 10px; border-radius: 50px'>";

// Display battery statistics within this block
float socFloat = static_cast<float>(SOC) / 100.0; // Convert to float and divide by 100
float sohFloat = static_cast<float>(StateOfHealth) / 100.0; // Convert to float and divide by 100
float voltageFloat = static_cast<float>(battery_voltage) / 10.0; // Convert to float and divide by 10
float tempMaxFloat = static_cast<float>(temperature_max) / 10.0; // Convert to float and divide by 10
float tempMinFloat = static_cast<float>(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<float>(-(65535 - battery_current)) / 10.0; // Convert to float and divide by 10

This comment has been minimized.

Copy link
@matvey83

matvey83 Dec 30, 2023

Collaborator

lets factor out static_cast<float>(-(65535 - battery_current)) into a function, as it's repeated a few times here.

Perhaps division by 10 should also be part of the function.

} else {
currentFloat = static_cast<float>(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<float>(-(65535 - temperature_max)) / 10.0; // Convert to float and divide by 10
} else {
tempMaxFloat = static_cast<float>(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<float>(-(65535 - temperature_min)) / 10.0; // Convert to float and divide by 10
} else {
tempMinFloat = static_cast<float>(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];
Expand All @@ -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 += "<h4 style='color: white;'>SOC: " + String(socString) + "</h4>";
content += "<h4 style='color: white;'>SOH: " + String(sohString) + "</h4>";
content += "<h4 style='color: white;'>Voltage: " + String(voltageString) + " V</h4>";
content += "<h4 style='color: white;'>Current: " + String(battery_current) + " A</h4>";
content += "<h4 style='color: white;'>Current: " + String(currentString) + " A</h4>";
content += "<h4 style='color: white;'>Power: " + String(stat_batt_power) + " W</h4>";
content += "<h4>Total capacity: " + String(capacity_Wh) + " Wh</h4>";
content += "<h4>Remaining capacity: " + String(remaining_capacity_Wh) + " Wh</h4>";
Expand Down

1 comment on commit 5ba2a20

@MarkoBolt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Negative values for Power is not in there yet.

Please sign in to comment.