Add LoRa telemetry, fault counters, and time sync status
This commit is contained in:
@@ -19,6 +19,9 @@ static uint32_t g_last_read_ts = 0;
|
||||
static uint32_t g_last_tx_ts = 0;
|
||||
static uint32_t g_last_read_ms = 0;
|
||||
static uint32_t g_last_tx_ms = 0;
|
||||
static FaultType g_last_error = FaultType::None;
|
||||
static uint32_t g_last_error_ts = 0;
|
||||
static uint32_t g_last_error_ms = 0;
|
||||
|
||||
static const SenderStatus *g_statuses = nullptr;
|
||||
static uint8_t g_status_count = 0;
|
||||
@@ -108,6 +111,12 @@ void display_set_last_tx(bool ok, uint32_t ts_utc) {
|
||||
g_last_tx_ms = millis();
|
||||
}
|
||||
|
||||
void display_set_last_error(FaultType type, uint32_t ts_utc, uint32_t ts_ms) {
|
||||
g_last_error = type;
|
||||
g_last_error_ts = ts_utc;
|
||||
g_last_error_ms = ts_ms;
|
||||
}
|
||||
|
||||
void display_set_receiver_status(bool ap_mode, const char *ssid, bool mqtt_ok) {
|
||||
g_ap_mode = ap_mode;
|
||||
g_wifi_ssid = ssid ? ssid : "";
|
||||
@@ -137,6 +146,41 @@ static uint32_t age_seconds(uint32_t ts_utc, uint32_t ts_ms) {
|
||||
return (millis() - ts_ms) / 1000;
|
||||
}
|
||||
|
||||
static bool render_last_error_line(uint8_t y) {
|
||||
if (g_last_error == FaultType::None) {
|
||||
return false;
|
||||
}
|
||||
const char *label = "unk";
|
||||
if (g_last_error == FaultType::MeterRead) {
|
||||
label = "meter";
|
||||
} else if (g_last_error == FaultType::Decode) {
|
||||
label = "decode";
|
||||
} else if (g_last_error == FaultType::LoraTx) {
|
||||
label = "lora";
|
||||
}
|
||||
display.setCursor(0, y);
|
||||
display.printf("Err: %s %lus", label, static_cast<unsigned long>(age_seconds(g_last_error_ts, g_last_error_ms)));
|
||||
return true;
|
||||
}
|
||||
|
||||
static void render_last_sync_line(uint8_t y, bool include_time) {
|
||||
display.setCursor(0, y);
|
||||
uint32_t last_sync = time_get_last_sync_utc();
|
||||
if (last_sync == 0 || !time_is_synced()) {
|
||||
display.print("Sync: --");
|
||||
return;
|
||||
}
|
||||
uint32_t age = time_get_last_sync_age_sec();
|
||||
if (include_time) {
|
||||
time_t t = last_sync;
|
||||
struct tm timeinfo;
|
||||
localtime_r(&t, &timeinfo);
|
||||
display.printf("Sync: %lus %02d:%02d", static_cast<unsigned long>(age), timeinfo.tm_hour, timeinfo.tm_min);
|
||||
} else {
|
||||
display.printf("Sync: %lus ago", static_cast<unsigned long>(age));
|
||||
}
|
||||
}
|
||||
|
||||
static void render_sender_status() {
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
@@ -157,8 +201,13 @@ static void render_sender_status() {
|
||||
if (strlen(g_test_code) > 0) {
|
||||
display.setCursor(0, 48);
|
||||
display.printf("Test %s", g_test_code);
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
if (!render_last_error_line(48)) {
|
||||
render_last_sync_line(48, true);
|
||||
}
|
||||
}
|
||||
|
||||
display.display();
|
||||
}
|
||||
@@ -193,25 +242,40 @@ static void render_receiver_status() {
|
||||
display.setCursor(0, 24);
|
||||
display.printf("MQTT: %s", g_mqtt_ok ? "OK" : "RETRY");
|
||||
|
||||
char time_buf[8];
|
||||
time_get_local_hhmm(time_buf, sizeof(time_buf));
|
||||
display.setCursor(0, 36);
|
||||
display.printf("Time: %s", time_buf);
|
||||
|
||||
uint32_t latest = 0;
|
||||
bool link_valid = false;
|
||||
int16_t link_rssi = 0;
|
||||
float link_snr = 0.0f;
|
||||
if (g_statuses) {
|
||||
for (uint8_t i = 0; i < g_status_count; ++i) {
|
||||
if (g_statuses[i].has_data && g_statuses[i].last_update_ts_utc > latest) {
|
||||
latest = g_statuses[i].last_update_ts_utc;
|
||||
link_valid = g_statuses[i].last_data.link_valid;
|
||||
link_rssi = g_statuses[i].last_data.link_rssi_dbm;
|
||||
link_snr = g_statuses[i].last_data.link_snr_db;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
display.setCursor(0, 36);
|
||||
display.setCursor(0, 48);
|
||||
if (latest == 0 || !time_is_synced()) {
|
||||
display.print("Last upd: --:--");
|
||||
display.print("Upd --:--");
|
||||
} else {
|
||||
time_t t = latest;
|
||||
struct tm timeinfo;
|
||||
localtime_r(&t, &timeinfo);
|
||||
display.printf("Last upd: %02d:%02d", timeinfo.tm_hour, timeinfo.tm_min);
|
||||
display.printf("Upd %02d:%02d", timeinfo.tm_hour, timeinfo.tm_min);
|
||||
}
|
||||
if (link_valid) {
|
||||
display.printf(" R:%d S:%.1f", link_rssi, link_snr);
|
||||
}
|
||||
|
||||
render_last_error_line(56);
|
||||
display.display();
|
||||
}
|
||||
|
||||
@@ -258,7 +322,11 @@ static void render_receiver_sender(uint8_t index) {
|
||||
display.setCursor(0, 48);
|
||||
display.printf("L2 %.0fV %.0fW", status.last_data.phase_voltage_v[1], status.last_data.phase_power_w[1]);
|
||||
display.setCursor(0, 56);
|
||||
display.printf("L3 %.0fV %.0fW", status.last_data.phase_voltage_v[2], status.last_data.phase_power_w[2]);
|
||||
if (status.last_data.link_valid) {
|
||||
display.printf("R:%d S:%.1f", status.last_data.link_rssi_dbm, status.last_data.link_snr_db);
|
||||
} else {
|
||||
display.printf("L3 %.0fV %.0fW", status.last_data.phase_voltage_v[2], status.last_data.phase_power_w[2]);
|
||||
}
|
||||
display.display();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user