From ad87017732a7aff2086ca980bb30887afa7ecc4e Mon Sep 17 00:00:00 2001 From: Offer Shmuely Date: Tue, 19 Apr 2022 03:16:58 +0300 Subject: [PATCH] fix: timer hesitates and skips numbers issue: edgetx/edgetx#1509 (#59) Co-authored-by: Shmuely --- sdcard/horus/WIDGETS/Timer2/main.lua | 46 +++++++++++++++------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/sdcard/horus/WIDGETS/Timer2/main.lua b/sdcard/horus/WIDGETS/Timer2/main.lua index caed6cf8..962dd593 100644 --- a/sdcard/horus/WIDGETS/Timer2/main.lua +++ b/sdcard/horus/WIDGETS/Timer2/main.lua @@ -1,7 +1,7 @@ -- A Timer version that fill better the widget area -- Offer Shmuely -- Date: 2022 --- ver: 0.6 +-- ver: 0.7 local options = { { "TextColor", COLOR, YELLOW }, @@ -16,7 +16,7 @@ local function log(s) end local function create(zone, options) - local wgt = { zone=zone, options=options} + local wgt = { zone = zone, options = options } wgt.options.use_days = wgt.options.use_days % 2 -- modulo due to bug that cause the value to be other than 0|1 return wgt end @@ -34,35 +34,38 @@ end ------------------------------------------------------------ local function formatTime(wgt, t1) - local dd_raw = t1.value/86400 -- 24*3600 + local dd_raw = t1.value local isNegative = false if dd_raw < 0 then isNegative = true dd_raw = math.abs(dd_raw) end log("dd_raw: " .. dd_raw) - local dd = math.floor(dd_raw) - local hh_raw = (dd_raw - dd) * 24 - local hh = math.floor(hh_raw) - local mm_raw = (hh_raw - hh) * 60 - local mm = math.floor(mm_raw) - local ss_raw = (mm_raw - mm) * 60 - local ss = math.floor(ss_raw) + + local dd = math.floor(dd_raw / 86400) + dd_raw = dd_raw - dd * 86400 + local hh = math.floor(dd_raw / 3600) + dd_raw = dd_raw - hh * 3600 + local mm = math.floor(dd_raw / 60) + dd_raw = dd_raw - mm * 60 + local ss = math.floor(dd_raw) + if dd == 0 and hh == 0 then -- less then 1 hour, 59:59 - time_str = string.format("%02d:%02d",mm, ss) + time_str = string.format("%02d:%02d", mm, ss) + elseif dd == 0 then -- lass then 24 hours, 23:59:59 - time_str = string.format("%02d:%02d:%02d",hh, mm, ss) + time_str = string.format("%02d:%02d:%02d", hh, mm, ss) + else -- more than 24 hours - if wgt.options.use_days == 0 then -- 25:59:59 - time_str = string.format("%02d:%02d:%02d",dd*24 + hh, mm, ss) + time_str = string.format("%02d:%02d:%02d", dd * 24 + hh, mm, ss) else -- 5d 23:59:59 - time_str = string.format("%dd %02d:%02d:%02d",dd, hh, mm, ss) + time_str = string.format("%dd %02d:%02d:%02d", dd, hh, mm, ss) end end @@ -88,24 +91,24 @@ local function getFontSize(wgt, txt) log("wide_txt: " .. wide_txt) local w,h = lcd.sizeText(wide_txt, XXLSIZE) - log(string.format("XXLSIZE w: %d, h: %d, %s", w,h, time_str)) + log(string.format("XXLSIZE w: %d, h: %d, %s", w, h, time_str)) if w < wgt.zone.w and h <= wgt.zone.h then return XXLSIZE end w,h = lcd.sizeText(wide_txt, DBLSIZE) - log(string.format("DBLSIZE w: %d, h: %d, %s", w,h, time_str)) + log(string.format("DBLSIZE w: %d, h: %d, %s", w, h, time_str)) if w < wgt.zone.w and h <= wgt.zone.h then return DBLSIZE end w,h = lcd.sizeText(wide_txt, MIDSIZE) - log(string.format("MIDSIZE w: %d, h: %d, %s", w,h, time_str)) + log(string.format("MIDSIZE w: %d, h: %d, %s", w, h, time_str)) if w < wgt.zone.w and h <= wgt.zone.h then return MIDSIZE end - log(string.format("SMLSIZE w: %d, h: %d, %s", w,h, time_str)) + log(string.format("SMLSIZE w: %d, h: %d, %s", w, h, time_str)) return SMLSIZE end @@ -114,7 +117,7 @@ local function refresh(wgt, event, touchState) if (wgt.options == nil) then log("refresh(wgt.options=nil)") return end if (wgt.options.Timer == nil) then log("refresh(wgt.options.Timer=nil)") return end - local t1 = model.getTimer(wgt.options.Timer-1) + local t1 = model.getTimer(wgt.options.Timer - 1) -- calculate timer info local timerInfo = getTimerHeader(wgt, t1) @@ -126,7 +129,7 @@ local function refresh(wgt, event, touchState) local zone_w = wgt.zone.w local zone_h = wgt.zone.h - if isNegative==true then + if isNegative == true then textColor = RED else textColor = wgt.options.TextColor @@ -159,6 +162,7 @@ local function refresh(wgt, event, touchState) -- draw timer time lcd.drawText(wgt.zone.x + dx, wgt.zone.y + dy, time_str, font_size + textColor) + --lcd.drawText(wgt.zone.x+100, wgt.zone.y, string.format("%d%%", getUsage()), SMLSIZE + CUSTOM_COLOR) end return { name="Timer2", options=options, create=create, update=update, background=background, refresh=refresh }