Skip to content

Commit 9b0f34f

Browse files
committed
Add ui states
1 parent 50270c6 commit 9b0f34f

File tree

1 file changed

+134
-125
lines changed

1 file changed

+134
-125
lines changed

src/SCRIPTS/BF/ui.lua

Lines changed: 134 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
local uiStatus =
2+
{
3+
mainMenu = 1,
4+
pages = 2,
5+
}
6+
17
local pageStatus =
28
{
39
display = 2,
410
editing = 3,
511
saving = 4,
612
popupMenu = 5,
7-
mainMenu = 6,
813
}
914

1015
local uiMsp =
@@ -15,7 +20,8 @@ local uiMsp =
1520

1621
local menuLine = 1
1722
local pageCount = 1
18-
local currentState = pageStatus.mainMenu
23+
local uiState = uiStatus.mainMenu
24+
local pageState = pageStatus.display
1925
local requestTimeout = 80 -- 800ms request timeout
2026
local currentPage = 1
2127
local currentLine = 1
@@ -26,7 +32,7 @@ local saveMaxRetries = 0
2632
local popupMenuActive = false
2733
local lastRunTS = 0
2834
local killEnterBreak = 0
29-
local stopDisplay = true
35+
local stopDisplay = false
3036
local pageScrollY = 0
3137
local mainMenuScrollY = 0
3238

@@ -58,10 +64,10 @@ local function saveSettings(new)
5864
end
5965
protocol.mspWrite(Page.write, payload)
6066
saveTS = getTime()
61-
if currentState == pageStatus.saving then
67+
if pageState == pageStatus.saving then
6268
saveRetries = saveRetries + 1
6369
else
64-
currentState = pageStatus.saving
70+
pageState = pageStatus.saving
6571
saveRetries = 0
6672
saveMaxRetries = protocol.saveMaxRetries or 2 -- default 2
6773
saveTimeout = protocol.saveTimeout or 150 -- default 1.5s
@@ -71,7 +77,7 @@ end
7177

7278
local function invalidatePages()
7379
Page = nil
74-
currentState = pageStatus.display
80+
pageState = pageStatus.display
7581
saveTS = 0
7682
collectgarbage()
7783
end
@@ -220,7 +226,7 @@ local function drawScreen()
220226
local value_options = text_options
221227
if i == currentLine then
222228
value_options = text_options + INVERS
223-
if currentState == pageStatus.editing then
229+
if pageState == pageStatus.editing then
224230
value_options = value_options + BLINK
225231
end
226232
end
@@ -285,128 +291,22 @@ local function drawPopupMenu()
285291
end
286292

287293
function run_ui(event)
288-
getPageCount()
289294
local now = getTime()
290295
-- if lastRunTS old than 500ms
291296
if lastRunTS + 50 < now then
292297
invalidatePages()
293298
if isTelemetryScript then
294-
currentState = pageStatus.display
299+
uiState = uiStatus.pages
295300
else
296-
currentState = pageStatus.mainMenu
301+
uiState = uiStatus.mainMenu
297302
end
298303
end
299304
lastRunTS = now
300-
if (currentState == pageStatus.saving) then
301-
if (saveTS + saveTimeout < now) then
302-
if saveRetries < saveMaxRetries then
303-
saveSettings()
304-
else
305-
-- max retries reached
306-
currentState = pageStatus.display
307-
invalidatePages()
308-
end
309-
end
310-
end
311-
-- process send queue
312-
mspProcessTxQ()
313-
-- navigation
314-
if isTelemetryScript and event == EVT_VIRTUAL_MENU_LONG then -- telemetry script
315-
popupMenuActive = 1
316-
currentState = pageStatus.popupMenu
317-
elseif (not isTelemetryScript) and event == EVT_VIRTUAL_ENTER_LONG then -- standalone
318-
popupMenuActive = 1
319-
killEnterBreak = 1
320-
currentState = pageStatus.popupMenu
321-
-- menu is currently displayed
322-
elseif currentState == pageStatus.popupMenu then
323-
if event == EVT_VIRTUAL_EXIT then
324-
currentState = pageStatus.display
325-
elseif event == EVT_VIRTUAL_PREV then
326-
incPopupMenu(-1)
327-
elseif event == EVT_VIRTUAL_NEXT then
328-
incPopupMenu(1)
329-
elseif event == EVT_VIRTUAL_ENTER then
330-
if killEnterBreak == 1 then
331-
killEnterBreak = 0
332-
else
333-
currentState = pageStatus.display
334-
popupMenuList[popupMenuActive].f()
335-
end
336-
end
337-
-- normal page viewing
338-
elseif currentState <= pageStatus.display then
339-
if not isTelemetryScript and event == EVT_VIRTUAL_PREV_PAGE then
340-
incPage(-1)
341-
killEvents(event) -- X10/T16 issue: pageUp is a long press
342-
elseif (not isTelemetryScript and event == EVT_VIRTUAL_NEXT_PAGE) or (isTelemetryScript and event == EVT_VIRTUAL_MENU) then
343-
incPage(1)
344-
elseif event == EVT_VIRTUAL_PREV or event == EVT_VIRTUAL_PREV_REPT then
345-
incLine(-1)
346-
elseif event == EVT_VIRTUAL_NEXT or event == EVT_VIRTUAL_NEXT_REPT then
347-
incLine(1)
348-
elseif event == EVT_VIRTUAL_ENTER then
349-
if Page then
350-
local field = Page.fields[currentLine]
351-
local idx = field.i or currentLine
352-
if Page.values and Page.values[idx] and (field.ro ~= true) then
353-
currentState = pageStatus.editing
354-
end
355-
end
356-
elseif event == EVT_VIRTUAL_EXIT then
357-
if isTelemetryScript then
358-
return protocol.exitFunc();
359-
else
360-
stopDisplay = true
361-
end
362-
end
363-
-- editing value
364-
elseif currentState == pageStatus.editing then
365-
if event == EVT_VIRTUAL_EXIT or event == EVT_VIRTUAL_ENTER then
366-
currentState = pageStatus.display
367-
elseif event == EVT_VIRTUAL_INC or event == EVT_VIRTUAL_INC_REPT then
368-
incValue(1)
369-
elseif event == EVT_VIRTUAL_DEC or event == EVT_VIRTUAL_DEC_REPT then
370-
incValue(-1)
371-
end
372-
end
373-
local nextPage = currentPage
374-
while Page == nil do
375-
Page = assert(loadScript(SCRIPT_HOME.."/Pages/"..PageFiles[currentPage].script))()
376-
if Page.requiredVersion and apiVersion > 0 and Page.requiredVersion > apiVersion then
377-
incPage(1)
378-
if currentPage == nextPage then
379-
lcd.clear()
380-
lcd.drawText(radio.NoTelem[1], radio.NoTelem[2], "No Pages! API: " .. apiVersion, radio.NoTelem[4])
381-
return 1
382-
end
383-
end
384-
end
385-
if not Page.values and currentState == pageStatus.display then
386-
requestPage()
387-
end
388-
lcd.clear()
389-
if TEXT_BGCOLOR then
390-
lcd.drawFilledRectangle(0, 0, LCD_W, LCD_H, TEXT_BGCOLOR)
391-
end
392-
if currentState ~= pageStatus.mainMenu then
393-
drawScreen()
394-
end
395-
if protocol.rssi() == 0 then
396-
lcd.drawText(radio.NoTelem[1],radio.NoTelem[2],radio.NoTelem[3],radio.NoTelem[4])
305+
if isTelemetryScript then
306+
uiState = uiStatus.pages
397307
end
398-
if currentState == pageStatus.popupMenu then
399-
drawPopupMenu()
400-
elseif currentState == pageStatus.saving then
401-
lcd.drawFilledRectangle(radio.SaveBox.x,radio.SaveBox.y,radio.SaveBox.w,radio.SaveBox.h,backgroundFill)
402-
lcd.drawRectangle(radio.SaveBox.x,radio.SaveBox.y,radio.SaveBox.w,radio.SaveBox.h,SOLID)
403-
if saveRetries <= 0 then
404-
lcd.drawText(radio.SaveBox.x+radio.SaveBox.x_offset,radio.SaveBox.y+radio.SaveBox.h_offset,"Saving...",DBLSIZE + BLINK + (globalTextOptions))
405-
else
406-
lcd.drawText(radio.SaveBox.x+radio.SaveBox.x_offset,radio.SaveBox.y+radio.SaveBox.h_offset,"Retrying",DBLSIZE + (globalTextOptions))
407-
end
408-
end
409-
if currentState == pageStatus.mainMenu and (not isTelemetryScript) then
308+
if uiState == uiStatus.mainMenu then
309+
getPageCount()
410310
if event == EVT_VIRTUAL_EXIT then
411311
return 2
412312
elseif event == EVT_VIRTUAL_NEXT then
@@ -436,19 +336,128 @@ function run_ui(event)
436336
if event == EVT_VIRTUAL_ENTER and attr == INVERS then
437337
invalidatePages()
438338
currentPage = i
439-
currentState = pageStatus.display
339+
pageState = pageStatus.display
340+
uiState = uiStatus.pages
440341
end
441342
if ((i-1)*lineSpacing + yMinLim - mainMenuScrollY) >= yMinLim and ((i-1)*lineSpacing + yMinLim - mainMenuScrollY) <= yMaxLim then
442343
lcd.drawText(6, (i-1)*lineSpacing + yMinLim - mainMenuScrollY, PageFiles[i].title, attr)
443344
end
444345
end
445346
end
347+
elseif uiState == uiStatus.pages then
348+
if (pageState == pageStatus.saving) then
349+
if (saveTS + saveTimeout < now) then
350+
if saveRetries < saveMaxRetries then
351+
saveSettings()
352+
else
353+
-- max retries reached
354+
pageState = pageStatus.display
355+
invalidatePages()
356+
end
357+
end
358+
end
359+
-- navigation
360+
if isTelemetryScript and event == EVT_VIRTUAL_MENU_LONG then -- telemetry script
361+
popupMenuActive = 1
362+
pageState = pageStatus.popupMenu
363+
elseif (not isTelemetryScript) and event == EVT_VIRTUAL_ENTER_LONG then -- standalone
364+
popupMenuActive = 1
365+
killEnterBreak = 1
366+
pageState = pageStatus.popupMenu
367+
-- menu is currently displayed
368+
elseif pageState == pageStatus.popupMenu then
369+
if event == EVT_VIRTUAL_EXIT then
370+
pageState = pageStatus.display
371+
elseif event == EVT_VIRTUAL_PREV then
372+
incPopupMenu(-1)
373+
elseif event == EVT_VIRTUAL_NEXT then
374+
incPopupMenu(1)
375+
elseif event == EVT_VIRTUAL_ENTER then
376+
if killEnterBreak == 1 then
377+
killEnterBreak = 0
378+
else
379+
pageState = pageStatus.display
380+
popupMenuList[popupMenuActive].f()
381+
end
382+
end
383+
-- normal page viewing
384+
elseif pageState <= pageStatus.display then
385+
if not isTelemetryScript and event == EVT_VIRTUAL_PREV_PAGE then
386+
incPage(-1)
387+
killEvents(event) -- X10/T16 issue: pageUp is a long press
388+
elseif (not isTelemetryScript and event == EVT_VIRTUAL_NEXT_PAGE) or (isTelemetryScript and event == EVT_VIRTUAL_MENU) then
389+
incPage(1)
390+
elseif event == EVT_VIRTUAL_PREV or event == EVT_VIRTUAL_PREV_REPT then
391+
incLine(-1)
392+
elseif event == EVT_VIRTUAL_NEXT or event == EVT_VIRTUAL_NEXT_REPT then
393+
incLine(1)
394+
elseif event == EVT_VIRTUAL_ENTER then
395+
if Page then
396+
local field = Page.fields[currentLine]
397+
local idx = field.i or currentLine
398+
if Page.values and Page.values[idx] and (field.ro ~= true) then
399+
pageState = pageStatus.editing
400+
end
401+
end
402+
elseif event == EVT_VIRTUAL_EXIT then
403+
if isTelemetryScript then
404+
return protocol.exitFunc();
405+
else
406+
stopDisplay = true
407+
end
408+
end
409+
-- editing value
410+
elseif pageState == pageStatus.editing then
411+
if event == EVT_VIRTUAL_EXIT or event == EVT_VIRTUAL_ENTER then
412+
pageState = pageStatus.display
413+
elseif event == EVT_VIRTUAL_INC or event == EVT_VIRTUAL_INC_REPT then
414+
incValue(1)
415+
elseif event == EVT_VIRTUAL_DEC or event == EVT_VIRTUAL_DEC_REPT then
416+
incValue(-1)
417+
end
418+
end
419+
local nextPage = currentPage
420+
while Page == nil do
421+
Page = assert(loadScript(SCRIPT_HOME.."/Pages/"..PageFiles[currentPage].script))()
422+
if Page.requiredVersion and apiVersion > 0 and Page.requiredVersion > apiVersion then
423+
incPage(1)
424+
if currentPage == nextPage then
425+
lcd.clear()
426+
lcd.drawText(radio.NoTelem[1], radio.NoTelem[2], "No Pages! API: " .. apiVersion, radio.NoTelem[4])
427+
return 1
428+
end
429+
end
430+
end
431+
if not Page.values and pageState == pageStatus.display then
432+
requestPage()
433+
end
434+
lcd.clear()
435+
if TEXT_BGCOLOR then
436+
lcd.drawFilledRectangle(0, 0, LCD_W, LCD_H, TEXT_BGCOLOR)
437+
end
438+
drawScreen()
439+
if pageState == pageStatus.popupMenu then
440+
drawPopupMenu()
441+
elseif pageState == pageStatus.saving then
442+
lcd.drawFilledRectangle(radio.SaveBox.x,radio.SaveBox.y,radio.SaveBox.w,radio.SaveBox.h,backgroundFill)
443+
lcd.drawRectangle(radio.SaveBox.x,radio.SaveBox.y,radio.SaveBox.w,radio.SaveBox.h,SOLID)
444+
if saveRetries <= 0 then
445+
lcd.drawText(radio.SaveBox.x+radio.SaveBox.x_offset,radio.SaveBox.y+radio.SaveBox.h_offset,"Saving...",DBLSIZE + BLINK + (globalTextOptions))
446+
else
447+
lcd.drawText(radio.SaveBox.x+radio.SaveBox.x_offset,radio.SaveBox.y+radio.SaveBox.h_offset,"Retrying",DBLSIZE + (globalTextOptions))
448+
end
449+
end
450+
if stopDisplay and (not isTelemetryScript) then
451+
invalidatePages()
452+
currentLine = 1
453+
uiState = uiStatus.mainMenu
454+
stopDisplay = false
455+
end
446456
end
447-
if stopDisplay and (not isTelemetryScript) then
448-
currentLine = 1
449-
currentState = pageStatus.mainMenu
450-
stopDisplay = false
451-
collectgarbage()
457+
-- process send queue
458+
mspProcessTxQ()
459+
if protocol.rssi() == 0 then
460+
lcd.drawText(radio.NoTelem[1],radio.NoTelem[2],radio.NoTelem[3],radio.NoTelem[4])
452461
end
453462
processMspReply(mspPollReply())
454463
return 0

0 commit comments

Comments
 (0)