From fb611ef9861348f1a73e11146b12b742a5741abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 5 Feb 2023 01:46:16 +0100 Subject: [PATCH] fix time display --- src/graphics/Screen.cpp | 30 ++++++++++++++++++------------ src/graphics/Screen.h | 2 +- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index 09cafc348..49dbb0645 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -70,6 +70,8 @@ static char btPIN[16] = "888888"; uint32_t logo_timeout = 5000; // 4 seconds for EACH logo +uint32_t hours_in_month = 730; // 730 hours in a month + // This image definition is here instead of images.h because it's modified dynamically by the drawBattery function uint8_t imgBattery[16] = {0xFF, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xE7, 0x3C}; @@ -386,10 +388,11 @@ static void drawTextMessageFrame(OLEDDisplay *display, OLEDDisplayUiState *state uint32_t days = hours / 24; if (config.display.heading_bold) { - display->drawStringf(1 + x, 0 + y, tempBuf, "%s ago from %s", screen->drawTimeDelta(days, hours, minutes, seconds), + display->drawStringf(1 + x, 0 + y, tempBuf, "%s ago from %s", + screen->drawTimeDelta(days, hours, minutes, seconds).c_str(), (node && node->has_user) ? node->user.short_name : "???"); } - display->drawStringf(0 + x, 0 + y, tempBuf, "%s ago from %s", screen->drawTimeDelta(days, hours, minutes, seconds), + display->drawStringf(0 + x, 0 + y, tempBuf, "%s ago from %s", screen->drawTimeDelta(days, hours, minutes, seconds).c_str(), (node && node->has_user) ? node->user.short_name : "???"); display->setColor(WHITE); @@ -1336,17 +1339,20 @@ void Screen::blink() dispdev.setBrightness(brightness); } -String Screen::drawTimeDelta(uint32_t days, uint32_t hours, uint32_t minutes, uint32_t seconds) +std::string Screen::drawTimeDelta(uint32_t days, uint32_t hours, uint32_t minutes, uint32_t seconds) { - String uptime; - if (days >= 2) - uptime = String(days) + "d"; + std::string uptime; + + if (days > (hours_in_month * 6)) + uptime = "?"; + else if (days >= 2) + uptime = std::to_string(days) + "d"; else if (hours >= 2) - uptime = String(hours) + "h"; + uptime = std::to_string(hours) + "h"; else if (minutes >= 1) - uptime = String(minutes) + "m"; + uptime = std::to_string(minutes) + "m"; else - uptime = String(seconds) + "s"; + uptime = std::to_string(seconds) + "s"; return uptime; } @@ -1714,7 +1720,7 @@ void DebugInfo::drawFrameSettings(OLEDDisplay *display, OLEDDisplayUiState *stat display->setColor(WHITE); // Show uptime as days, hours, minutes OR seconds - String uptime = screen->drawTimeDelta(days, hours, minutes, seconds); + std::string uptime = screen->drawTimeDelta(days, hours, minutes, seconds); uint32_t rtc_sec = getValidTime(RTCQuality::RTCQualityDevice); if (rtc_sec > 0) { @@ -1729,12 +1735,12 @@ void DebugInfo::drawFrameSettings(OLEDDisplay *display, OLEDDisplayUiState *stat int min = (hms % SEC_PER_HOUR) / SEC_PER_MIN; int sec = (hms % SEC_PER_HOUR) % SEC_PER_MIN; // or hms % SEC_PER_MIN - char timebuf[9]; + char timebuf[10]; snprintf(timebuf, sizeof(timebuf), " %02d:%02d:%02d", hour, min, sec); uptime += timebuf; } - display->drawString(x, y + FONT_HEIGHT_SMALL * 1, uptime); + display->drawString(x, y + FONT_HEIGHT_SMALL * 1, uptime.c_str()); // Display Channel Utilization char chUtil[13]; diff --git a/src/graphics/Screen.h b/src/graphics/Screen.h index 1d732ad4c..5e92e73bb 100644 --- a/src/graphics/Screen.h +++ b/src/graphics/Screen.h @@ -207,7 +207,7 @@ class Screen : public concurrency::OSThread } /// generates a very brief time delta display - String drawTimeDelta(uint32_t days, uint32_t hours, uint32_t minutes, uint32_t seconds); + std::string drawTimeDelta(uint32_t days, uint32_t hours, uint32_t minutes, uint32_t seconds); /// Overrides the default utf8 character conversion, to replace empty space with question marks static char customFontTableLookup(const uint8_t ch)