Skip to content

Commit

Permalink
Update TELEMETRY/wdgts2_sub.lua, WIDGETS/widgets.lua
Browse files Browse the repository at this point in the history
- Add error handling for nonexistent widget file
  • Loading branch information
Matze-Jung committed Dec 7, 2019
1 parent aa760cb commit 6c7ea24
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/SCRIPTS/TELEMETRY/wdgts2_sub.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ local layout = {
{
id="value",
opts={
src=function() return getGraphRange(5).min..' / '..getGraphRange(5).max end,
src=function() return getGraphRange and getGraphRange(5).min..' / '..getGraphRange(5).max or '?' end,
lbl="min / max",
unit="dB",
style=SMLSIZE,
Expand Down
37 changes: 30 additions & 7 deletions src/SCRIPTS/WIDGETS/widgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ local function run(event, zone)
local wdgt = widgets[col][row]

if wdgt.opts then
if not wdgt.func then
lcd.drawRectangle(x, y, w, h)
if type(wdgt.func) ~= "table" then
if wdgt.func then
lcd.drawText(x+w/2-4, y+h/2-4, "id?", SMLSIZE+BLINK)
else
lcd.drawRectangle(x, y, w, h)
end
else
if wdgt.opts.parent then
wdgt.func.run(event, {x=x, y=y, w=w , h=h})
Expand All @@ -80,6 +84,16 @@ local function run(event, zone)
end
end

local function file_exists(name)
local f=io.open(name,"r")
if f~=nil then
io.close(f)
return true
end

return
end

local function init()
for col=1, #layout, 1
do
Expand All @@ -94,13 +108,22 @@ local function init()
local c = layout[col][row]
local w = { run=function()end }
if c.id then
local wdgtFile = "/SCRIPTS/WIDGETS/"..(c.id)..".lua"

w.opts = c.opts or {}
w.func = c.id ~= ""
and assert(loadScript("/SCRIPTS/WIDGETS/"..(c.id)..".lua"))(event)
or false
w.func = false

if c.id ~= "" then
if file_exists(wdgtFile) then
w.func = assert(loadScript(wdgtFile))(event)

-- initalize widget
if w.func and w.func.init then w.func.init() end
else
w.func = true
end
end

-- initalize widget
if w.func and w.func.init then w.func.init() end
end
widgets[col][row] = w
end
Expand Down

0 comments on commit 6c7ea24

Please sign in to comment.