Skip to content

Commit

Permalink
fix: timer hesitates and skips numbers issue: EdgeTX/edgetx#1509 (#59)
Browse files Browse the repository at this point in the history
Co-authored-by: Shmuely <[email protected]>
  • Loading branch information
offer-shmuely and offer-shmuely-cognyte authored Apr 19, 2022
1 parent 9c0a5cc commit ad87017
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions sdcard/horus/WIDGETS/Timer2/main.lua
Original file line number Diff line number Diff line change
@@ -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 },
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 }

0 comments on commit ad87017

Please sign in to comment.