From 4e571c13256b63580fff9a070449e46839071874 Mon Sep 17 00:00:00 2001 From: eshifri <46511024+eshifri@users.noreply.github.com> Date: Sun, 24 Oct 2021 18:36:16 -0700 Subject: [PATCH] Improvements to time and timer display #911 (#912) * Fix time display #911 * move TIMEHOUR definition * change time format * Refactoring * rename * remove unncessary code * Change hour/minute format * pack TimerDisplayOptions structure --- radio/src/gui/colorlcd/draw_functions.cpp | 10 ++-- radio/src/gui/colorlcd/draw_functions.h | 9 +++ .../src/gui/colorlcd/layouts/topbar_impl.cpp | 4 +- radio/src/gui/colorlcd/themes/480_default.cpp | 4 +- radio/src/gui/colorlcd/themes/flysky.cpp | 3 +- radio/src/gui/colorlcd/timeedit.cpp | 6 +- radio/src/gui/colorlcd/view_statistics.cpp | 18 +++--- radio/src/gui/colorlcd/widgets/timer.cpp | 6 -- radio/src/gui/colorlcd/widgets/value.cpp | 4 +- .../colorlcd/widgets/widgets_container_impl.h | 1 + radio/src/lua/api_colorlcd.cpp | 4 +- radio/src/strhelpers.cpp | 55 ++++++++++++------- radio/src/strhelpers.h | 27 +++++++-- 13 files changed, 95 insertions(+), 56 deletions(-) diff --git a/radio/src/gui/colorlcd/draw_functions.cpp b/radio/src/gui/colorlcd/draw_functions.cpp index 5715c55e6..63257519f 100644 --- a/radio/src/gui/colorlcd/draw_functions.cpp +++ b/radio/src/gui/colorlcd/draw_functions.cpp @@ -20,6 +20,7 @@ */ #include "opentx.h" +#include "lcd.h" coord_t drawStringWithIndex(BitmapBuffer * dc, coord_t x, coord_t y, const char * str, int idx, LcdFlags flags, const char * prefix, const char * suffix) { @@ -415,16 +416,13 @@ void drawSensorCustomValue(BitmapBuffer * dc, coord_t x, coord_t y, uint8_t sens } } -// libopemui defines TIMOUR as zero -#if !TIMHOUR -#undef TIMEHOUR -#define TIMEHOUR 0x2000 -#endif void drawTimer(BitmapBuffer * dc, coord_t x, coord_t y, int32_t tme, LcdFlags flags) { char str[LEN_TIMER_STRING]; - getTimerString(str, tme, (flags & TIMEHOUR) != 0); + TimerOptions timerOptions; + timerOptions.options = (flags & TIMEHOUR) != 0 ? SHOW_TIME : SHOW_TIMER; + getTimerString(str, tme, timerOptions); dc->drawText(x, y, str, flags); } diff --git a/radio/src/gui/colorlcd/draw_functions.h b/radio/src/gui/colorlcd/draw_functions.h index 3b7a6cb8e..93787c82c 100644 --- a/radio/src/gui/colorlcd/draw_functions.h +++ b/radio/src/gui/colorlcd/draw_functions.h @@ -26,6 +26,15 @@ #include "telemetry/telemetry_sensors.h" #include +// libopenui defines TIMOUR as zero +#if !TIMEHOUR +#undef TIMEHOUR +#endif + +#if !defined(TIMEHOUR) +#define TIMEHOUR 0x2000 +#endif + #define OPTION_MENU_NO_FOOTER 0x01 #define OPTION_MENU_TITLE_BAR 0x02 #define OPTION_MENU_NO_SCROLLBAR 0x04 diff --git a/radio/src/gui/colorlcd/layouts/topbar_impl.cpp b/radio/src/gui/colorlcd/layouts/topbar_impl.cpp index e632f7b8e..23cff0bdc 100644 --- a/radio/src/gui/colorlcd/layouts/topbar_impl.cpp +++ b/radio/src/gui/colorlcd/layouts/topbar_impl.cpp @@ -80,7 +80,7 @@ void TopbarImpl::paint(BitmapBuffer * dc) { dc->drawSolidFilledRect(0, 0, width(), height(), COLOR_THEME_SECONDARY1); OpenTxTheme::instance()->drawTopLeftBitmap(dc); - + const TimerOptions timerOptions = {.options = SHOW_TIME}; struct gtm t; gettime(&t); char str[10]; @@ -92,7 +92,7 @@ void TopbarImpl::paint(BitmapBuffer * dc) #endif //dc->drawSolidVerticalLine(DATETIME_SEPARATOR_X, 7, 31, COLOR_THEME_SECONDARY1); dc->drawText(DATETIME_MIDDLE, DATETIME_LINE1, str, FONT(XS) | CENTERED | COLOR_THEME_PRIMARY2); - getTimerString(str, getValue(MIXSRC_TX_TIME)); + getTimerString(str, getValue(MIXSRC_TX_TIME), timerOptions); dc->drawText(DATETIME_MIDDLE, DATETIME_LINE2, str, FONT(XS) | CENTERED | COLOR_THEME_PRIMARY2); #if defined(INTERNAL_GPS) diff --git a/radio/src/gui/colorlcd/themes/480_default.cpp b/radio/src/gui/colorlcd/themes/480_default.cpp index 606e24879..77d3e8e2f 100644 --- a/radio/src/gui/colorlcd/themes/480_default.cpp +++ b/radio/src/gui/colorlcd/themes/480_default.cpp @@ -374,7 +374,7 @@ class Theme480: public OpenTxTheme void drawMenuDatetime(BitmapBuffer * dc) const { //dc->drawSolidVerticalLine(DATETIME_SEPARATOR_X, 7, 31, COLOR_THEME_PRIMARY2); - + const TimerOptions timerOptions = {.options = SHOW_TIME}; struct gtm t; gettime(&t); char str[10]; @@ -385,7 +385,7 @@ class Theme480: public OpenTxTheme sprintf(str, "%d %s", t.tm_mday, STR_MONTHS[t.tm_mon]); #endif dc->drawText(DATETIME_MIDDLE, DATETIME_LINE1, str, FONT(XS)|COLOR_THEME_PRIMARY2|CENTERED); - getTimerString(str, getValue(MIXSRC_TX_TIME)); + getTimerString(str, getValue(MIXSRC_TX_TIME), timerOptions); dc->drawText(DATETIME_MIDDLE, DATETIME_LINE2, str, FONT(XS)|COLOR_THEME_PRIMARY2|CENTERED); } diff --git a/radio/src/gui/colorlcd/themes/flysky.cpp b/radio/src/gui/colorlcd/themes/flysky.cpp index 6468360b7..ef580e39f 100644 --- a/radio/src/gui/colorlcd/themes/flysky.cpp +++ b/radio/src/gui/colorlcd/themes/flysky.cpp @@ -391,6 +391,7 @@ class FlyskyTheme: public OpenTxTheme { dc->drawSolidVerticalLine(DATETIME_SEPARATOR_X, 7, 31, COLOR_THEME_PRIMARY2); + const TimerOptions timerOptions = {.options = SHOW_TIME}; struct gtm t; gettime(&t); char str[10]; @@ -403,7 +404,7 @@ class FlyskyTheme: public OpenTxTheme #endif dc->drawText(DATETIME_MIDDLE, DATETIME_LINE1, str, FONT(XS)|COLOR_THEME_PRIMARY2|CENTERED); - getTimerString(str, getValue(MIXSRC_TX_TIME)); + getTimerString(str, getValue(MIXSRC_TX_TIME), timerOptions); dc->drawText(DATETIME_MIDDLE, DATETIME_LINE2, str, FONT(XS)|COLOR_THEME_PRIMARY2|CENTERED); } diff --git a/radio/src/gui/colorlcd/timeedit.cpp b/radio/src/gui/colorlcd/timeedit.cpp index dd91039b1..056429ea2 100644 --- a/radio/src/gui/colorlcd/timeedit.cpp +++ b/radio/src/gui/colorlcd/timeedit.cpp @@ -41,8 +41,8 @@ void TimeEdit::paint(BitmapBuffer * dc) else textColor = COLOR_THEME_SECONDARY1; + TimerOptions timerOptions; + timerOptions.options = (textFlags & TIMEHOUR) != 0 ? SHOW_TIME : SHOW_TIMER; dc->drawText(FIELD_PADDING_LEFT, FIELD_PADDING_TOP, - getTimerString(getValue(), (textFlags & TIMEHOUR) != 0), - textColor); + getTimerString(getValue(), timerOptions), textColor); } - diff --git a/radio/src/gui/colorlcd/view_statistics.cpp b/radio/src/gui/colorlcd/view_statistics.cpp index 086a63583..2d259815f 100644 --- a/radio/src/gui/colorlcd/view_statistics.cpp +++ b/radio/src/gui/colorlcd/view_statistics.cpp @@ -20,9 +20,9 @@ */ #include "view_statistics.h" - -#include "libopenui.h" #include "opentx.h" +#include "draw_functions.h" + StatisticsViewPageGroup::StatisticsViewPageGroup() : TabsGroup(ICON_STATS) { @@ -94,7 +94,7 @@ void StatisticsViewPage::build(FormWindow *window) COLOR_THEME_PRIMARY1); new DynamicText( window, grid.getFieldSlot(3, 0), - [] { return getTimerString(sessionTimer, TIMEHOUR); }, + [] { return getTimerString(sessionTimer); }, COLOR_THEME_PRIMARY1); // Battery data @@ -103,7 +103,7 @@ void StatisticsViewPage::build(FormWindow *window) new DynamicText( window, grid.getFieldSlot(3, 2), [] { - return getTimerString(g_eeGeneral.globalTimer + sessionTimer, TIMEHOUR); + return getTimerString(g_eeGeneral.globalTimer + sessionTimer); }, COLOR_THEME_PRIMARY1); grid.nextLine(); @@ -113,7 +113,7 @@ void StatisticsViewPage::build(FormWindow *window) COLOR_THEME_PRIMARY1); new DynamicText( window, grid.getFieldSlot(3, 0), - [] { return getTimerString(s_timeCumThr, TIMEHOUR); }, + [] { return getTimerString(s_timeCumThr); }, COLOR_THEME_PRIMARY1); // Throttle % data @@ -121,7 +121,7 @@ void StatisticsViewPage::build(FormWindow *window) COLOR_THEME_PRIMARY1); new DynamicText( window, grid.getFieldSlot(3, 2), - [] { return getTimerString(s_timeCum16ThrP / 16, TIMEHOUR); }, + [] { return getTimerString(s_timeCum16ThrP / 16); }, COLOR_THEME_PRIMARY1); grid.nextLine(); @@ -130,15 +130,15 @@ void StatisticsViewPage::build(FormWindow *window) COLOR_THEME_PRIMARY1); new DynamicText( window, grid.getFieldSlot(3, 0), - [] { return getTimerString(timersStates[0].val, TIMEHOUR); }, + [] { return getTimerString(timersStates[0].val); }, COLOR_THEME_PRIMARY1); new DynamicText( window, grid.getFieldSlot(3, 1), - [] { return getTimerString(timersStates[1].val, TIMEHOUR); }, + [] { return getTimerString(timersStates[1].val); }, COLOR_THEME_PRIMARY1); new DynamicText( window, grid.getFieldSlot(3, 2), - [] { return getTimerString(timersStates[2].val, TIMEHOUR); }, + [] { return getTimerString(timersStates[2].val); }, COLOR_THEME_PRIMARY1); grid.nextLine(); diff --git a/radio/src/gui/colorlcd/widgets/timer.cpp b/radio/src/gui/colorlcd/widgets/timer.cpp index f69d99431..e6977075b 100644 --- a/radio/src/gui/colorlcd/widgets/timer.cpp +++ b/radio/src/gui/colorlcd/widgets/timer.cpp @@ -22,12 +22,6 @@ #include "opentx.h" #include "widgets_container_impl.h" -// libopemui defines TIMOUR as zero -#ifdef TIMEHOUR -#undef TIMEHOUR -#define TIMEHOUR 0x2000 -#endif - class TimerWidget : public Widget { public: diff --git a/radio/src/gui/colorlcd/widgets/value.cpp b/radio/src/gui/colorlcd/widgets/value.cpp index ad64e20b0..25bb98aa9 100644 --- a/radio/src/gui/colorlcd/widgets/value.cpp +++ b/radio/src/gui/colorlcd/widgets/value.cpp @@ -101,11 +101,11 @@ class ValueWidget: public Widget if (persistentData->options[2].value.boolValue) { drawSource(dc, xLabel + 1, yLabel + 1, field, COLOR2FLAGS(BLACK)); drawTimer(dc, xValue + 1, yValue + 1, tme, - attrValue | FONT(STD) | COLOR2FLAGS(BLACK)); + attrValue | FONT(STD) | COLOR2FLAGS(BLACK) | TIMEHOUR); } drawSource(dc, xLabel, yLabel, field, attrLabel | color); drawTimer(dc, xValue, yValue, tme, - attrValue | FONT(STD) | color); + attrValue | FONT(STD) | color | TIMEHOUR); } else { if (field >= MIXSRC_FIRST_TELEM) { diff --git a/radio/src/gui/colorlcd/widgets/widgets_container_impl.h b/radio/src/gui/colorlcd/widgets/widgets_container_impl.h index 060f7ea25..a11d4ba18 100644 --- a/radio/src/gui/colorlcd/widgets/widgets_container_impl.h +++ b/radio/src/gui/colorlcd/widgets/widgets_container_impl.h @@ -23,6 +23,7 @@ #include "widget.h" #include "widgets_container.h" +#include "draw_functions.h" template class WidgetsContainerImpl: public WidgetsContainer diff --git a/radio/src/lua/api_colorlcd.cpp b/radio/src/lua/api_colorlcd.cpp index 190d412d3..1837961af 100644 --- a/radio/src/lua/api_colorlcd.cpp +++ b/radio/src/lua/api_colorlcd.cpp @@ -367,7 +367,9 @@ static int luaLcdDrawTimer(lua_State *L) char s[LEN_TIMER_STRING]; int tme = luaL_checkinteger(L, 3); LcdFlags flags = luaL_optunsigned(L, 4, 0); - getTimerString(s, tme, (flags & TIMEHOUR) != 0); + TimerOptions timerOptions; + timerOptions.options = (flags & TIMEHOUR) != 0 ? SHOW_TIME : SHOW_TIMER; + getTimerString(s, tme, timerOptions); drawString(L, s, flags); return 0; } diff --git a/radio/src/strhelpers.cpp b/radio/src/strhelpers.cpp index 6ebf4472f..55268aa2e 100644 --- a/radio/src/strhelpers.cpp +++ b/radio/src/strhelpers.cpp @@ -168,13 +168,18 @@ char *strAppendStringWithIndex(char *dest, const char *s, int idx) #define SECONDSPERDAY (24 * SECONDSPERHOUR) #define SECONDSPERYEAR (365 * SECONDSPERDAY) -char *getTimerStringCase(char *dest, int32_t tme, uint8_t hours, - bool bLowerCase) +char *getFormattedTimerString(char *dest, int32_t tme, TimerOptions timerOptions) { char *s = dest; div_t qr; int val = abs(tme); uint8_t digit_group = 0; + const bool bLowerCase = !(timerOptions.options & SHOW_TIMER_UPPER_CASE); + const bool showTime = timerOptions.options & SHOW_TIME; + uint8_t numDigitGroupRequired = (timerOptions.options >> 2) & 0x7; + const bool hmFormat = timerOptions.options & SHOW_TIMER_HM_FORMAT; + + if(!numDigitGroupRequired) numDigitGroupRequired = 3; if (tme < 0) { tme = -tme; @@ -191,6 +196,10 @@ char *getTimerStringCase(char *dest, int32_t tme, uint8_t hours, val = qr.rem; digit_group++; } + if (digit_group == numDigitGroupRequired) { + *s = 0; + return dest; + } // days qr = div((int)val, SECONDSPERDAY); if (qr.quot != 0 || digit_group != 0) { @@ -201,17 +210,29 @@ char *getTimerStringCase(char *dest, int32_t tme, uint8_t hours, val = qr.rem; digit_group++; } + if (digit_group == numDigitGroupRequired) { + *s = 0; + return dest; + } // hours qr = div((int)val, SECONDSPERHOUR); if (qr.quot != 0 || digit_group != 0) { qr = div((int)val, SECONDSPERHOUR); *s++ = '0' + (qr.quot / 10); *s++ = '0' + (qr.quot % 10); - *s++ = bLowerCase ? 'h' : 'H'; - val = qr.rem; digit_group++; + // if format hm is selected h should be always printed + if (digit_group == numDigitGroupRequired && !hmFormat) { + *s = 0; + return dest; + } + if(numDigitGroupRequired < 3 || hmFormat) + *s++ = bLowerCase ? 'h' : 'H'; + else + *s++ = ':'; + val = qr.rem; } - if (digit_group == 3) { + if (digit_group == numDigitGroupRequired) { *s = 0; return dest; } @@ -219,18 +240,19 @@ char *getTimerStringCase(char *dest, int32_t tme, uint8_t hours, qr = div((int)val, SECONDSPERMIN); *s++ = '0' + (qr.quot / 10); *s++ = '0' + (qr.quot % 10); - *s++ = bLowerCase ? 'm' : 'M'; digit_group++; - if (digit_group == 3) { + if (digit_group == numDigitGroupRequired) { *s = 0; return dest; } + if(!showTime && hmFormat) + *s++ = bLowerCase ? 'm' : 'M'; + else + *s++ = ':'; + // seconds *s++ = '0' + (qr.rem / 10); *s++ = '0' + (qr.rem % 10); - // if ( digit_group != 1 ) { - *s++ = bLowerCase ? 's' : 'S'; - //} *s = 0; return dest; @@ -742,19 +764,14 @@ char *getSourceString(mixsrc_t idx) char *getCurveString(int idx) { return getCurveString(tmpHelpersString, idx); } -char *getTimerString(int32_t tme, uint8_t hours) +char *getTimerString(int32_t tme, TimerOptions timerOptions) { - return getTimerStringCase(tmpHelpersString, tme, hours, true); + return getFormattedTimerString(tmpHelpersString, tme, timerOptions); } -char *getTimerString(int32_t tme, uint8_t hours, bool bLowerCase) +char *getTimerString(char *dest, int32_t tme, TimerOptions timerOptions) { - return getTimerStringCase(tmpHelpersString, tme, hours, bLowerCase); -} - -char *getTimerString(char *dest, int32_t tme, uint8_t hours) -{ - return getTimerStringCase(dest, tme, hours, true); + return getFormattedTimerString(dest, tme, timerOptions); } char *getSwitchPositionName(swsrc_t idx) diff --git a/radio/src/strhelpers.h b/radio/src/strhelpers.h index a0bcbcbd9..ede5a31ee 100644 --- a/radio/src/strhelpers.h +++ b/radio/src/strhelpers.h @@ -22,8 +22,27 @@ #ifndef _STRHELPERS_H_ #define _STRHELPERS_H_ +#include "definitions.h" #include "opentx_types.h" +#define SHOW_TIME 0x1 +#define SHOW_TIMER 0x0 +#define SHOW_TIMER_UPPER_CASE 0x2 +#define SHOW_TIMER_HM_FORMAT 0x5 + +PACK(typedef struct { + uint8_t showTime:1; + uint8_t upperCase:1; + uint8_t numDigitGroups:3; + uint8_t hmFormat:1; + uint8_t reserved:2; +}) TimerDisplayOptions; + +typedef union { + uint8_t options; + TimerDisplayOptions displayOptions; +} TimerOptions; + char hex2zchar(uint8_t hex); char hex2char(uint8_t hex); char zchar2char(int8_t idx); @@ -46,9 +65,8 @@ char *strAppendFilename(char *dest, const char *filename, const int size); char *getStringAtIndex(char *dest, const char *s, int idx); char *strAppendStringWithIndex(char *dest, const char *s, int idx); #define LEN_TIMER_STRING 10 // "-00:00:00" -char *getTimerString(char *dest, int32_t tme, uint8_t hours = 0u); -char *getTimerStringCase(char *dest, int32_t tme, uint8_t hours = 0u, - bool bLowerCase = true); +char *getTimerString(char *dest, int32_t tme, TimerOptions timerOptions = {.options = 0}); +char *getFormattedTimerString(char *dest, int32_t tme, TimerOptions timerOptions); char *getCurveString(char *dest, int idx); char *getGVarString(char *dest, int idx); char *getGVarString(int idx); @@ -65,8 +83,7 @@ char *getSourceString(mixsrc_t idx); char *getSwitchPositionName(swsrc_t idx); char *getSwitchWarningString(swsrc_t idx); char *getCurveString(int idx); -char *getTimerString(int32_t tme, uint8_t hours); -char *getTimerString(int32_t tme, uint8_t hours, bool bLowercase); +char *getTimerString(int32_t tme, TimerOptions timerOptions = {.options = 0}); void splitTimer(char *s0, char *s1, char *s2, char *s3, int tme, bool bLowercase = true);