Normalize power/energy output formatting
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
|
||||
static Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);
|
||||
@@ -161,6 +163,20 @@ static uint32_t age_seconds(uint32_t ts_utc, uint32_t ts_ms) {
|
||||
return (millis() - ts_ms) / 1000;
|
||||
}
|
||||
|
||||
static int32_t round_power_w(float value) {
|
||||
if (isnan(value)) {
|
||||
return 0;
|
||||
}
|
||||
long rounded = lroundf(value);
|
||||
if (rounded > INT32_MAX) {
|
||||
return INT32_MAX;
|
||||
}
|
||||
if (rounded < INT32_MIN) {
|
||||
return INT32_MIN;
|
||||
}
|
||||
return static_cast<int32_t>(rounded);
|
||||
}
|
||||
|
||||
static bool render_last_error_line(uint8_t y) {
|
||||
if (g_last_error == FaultType::None) {
|
||||
return false;
|
||||
@@ -238,15 +254,15 @@ static void render_sender_status() {
|
||||
static void render_sender_measurement() {
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.printf("E %.1f kWh", g_last_meter.energy_total_kwh);
|
||||
display.printf("E %.2f kWh", g_last_meter.energy_total_kwh);
|
||||
display.setCursor(0, 12);
|
||||
display.printf("P %.0fW", g_last_meter.total_power_w);
|
||||
display.printf("P %dW", static_cast<int>(round_power_w(g_last_meter.total_power_w)));
|
||||
display.setCursor(0, 24);
|
||||
display.printf("L1 %.0fW", g_last_meter.phase_power_w[0]);
|
||||
display.printf("L1 %dW", static_cast<int>(round_power_w(g_last_meter.phase_power_w[0])));
|
||||
display.setCursor(0, 36);
|
||||
display.printf("L2 %.0fW", g_last_meter.phase_power_w[1]);
|
||||
display.printf("L2 %dW", static_cast<int>(round_power_w(g_last_meter.phase_power_w[1])));
|
||||
display.setCursor(0, 48);
|
||||
display.printf("L3 %.0fW", g_last_meter.phase_power_w[2]);
|
||||
display.printf("L3 %dW", static_cast<int>(round_power_w(g_last_meter.phase_power_w[2])));
|
||||
display.display();
|
||||
}
|
||||
|
||||
@@ -336,17 +352,17 @@ static void render_receiver_sender(uint8_t index) {
|
||||
#endif
|
||||
|
||||
display.setCursor(0, 12);
|
||||
display.printf("E %.1f kWh", status.last_data.energy_total_kwh);
|
||||
display.printf("E %.2f kWh", status.last_data.energy_total_kwh);
|
||||
display.setCursor(0, 22);
|
||||
display.printf("L1 %.0fW", status.last_data.phase_power_w[0]);
|
||||
display.printf("L1 %dW", static_cast<int>(round_power_w(status.last_data.phase_power_w[0])));
|
||||
display.setCursor(0, 32);
|
||||
display.printf("L2 %.0fW", status.last_data.phase_power_w[1]);
|
||||
display.printf("L2 %dW", static_cast<int>(round_power_w(status.last_data.phase_power_w[1])));
|
||||
display.setCursor(0, 42);
|
||||
display.printf("L3 %.0fW", status.last_data.phase_power_w[2]);
|
||||
display.printf("L3 %dW", static_cast<int>(round_power_w(status.last_data.phase_power_w[2])));
|
||||
display.setCursor(0, 52);
|
||||
display.print("P");
|
||||
char p_buf[16];
|
||||
snprintf(p_buf, sizeof(p_buf), "%.0fW", status.last_data.total_power_w);
|
||||
snprintf(p_buf, sizeof(p_buf), "%dW", static_cast<int>(round_power_w(status.last_data.total_power_w)));
|
||||
int16_t x1 = 0;
|
||||
int16_t y1 = 0;
|
||||
uint16_t w = 0;
|
||||
|
||||
Reference in New Issue
Block a user