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
This commit is contained in:
eshifri
2021-10-25 12:36:16 +11:00
committed by GitHub
parent 2b8dfeef59
commit 4e571c1325
13 changed files with 95 additions and 56 deletions
+4 -6
View File
@@ -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);
}
+9
View File
@@ -26,6 +26,15 @@
#include "telemetry/telemetry_sensors.h"
#include <stdio.h>
// 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
@@ -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)
@@ -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);
}
+2 -1
View File
@@ -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);
}
+3 -3
View File
@@ -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);
}
+9 -9
View File
@@ -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();
-6
View File
@@ -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:
+2 -2
View File
@@ -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) {
@@ -23,6 +23,7 @@
#include "widget.h"
#include "widgets_container.h"
#include "draw_functions.h"
template<int N, int O>
class WidgetsContainerImpl: public WidgetsContainer
+3 -1
View File
@@ -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;
}
+36 -19
View File
@@ -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)
+22 -5
View File
@@ -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);