Skip to content

Popup improvements #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local template = loadScript(radio.templateHome.."accelerometer.lua")
local template = loadScript(radio.templateHome.."acc_cal.lua")
if template then
template = template()
else
Expand All @@ -20,17 +20,11 @@ labels[#labels + 1] = { t = "Make sure the craft is level", x = x, y = inc.y(lin
labels[#labels + 1] = { t = "and stable, then press", x = x, y = inc.y(lineSpacing) }
labels[#labels + 1] = { t = "[ENTER] to calibrate, or", x = x, y = inc.y(lineSpacing) }
labels[#labels + 1] = { t = "[EXIT] to cancel.", x = x, y = inc.y(lineSpacing) }
fields[#fields + 1] = { x = x, y = inc.y(lineSpacing), value = "", ro = true, onClick = function(self) self.accCal(self) end }
fields[#fields + 1] = { x = x, y = inc.y(lineSpacing), value = "", ro = true }

return {
write = 205, -- MSP_ACC_CALIBRATION
title = "Accelerometer",
reboot = false,
eepromWrite = false,
minBytes = 0,
labels = labels,
fields = fields,
accCal = function(self)
protocol.mspRead(self.write)
end,
title = "Accelerometer",
labels = labels,
fields = fields,
init = assert(loadScript("acc_cal.lua"))(),
}
29 changes: 29 additions & 0 deletions src/SCRIPTS/BF/CONFIRM/vtx_tables.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local template = loadScript(radio.templateHome.."vtx_tables.lua")
if template then
template = template()
else
template = assert(loadScript(radio.templateHome.."default_template.lua"))()
end
local margin = template.margin
local indent = template.indent
local lineSpacing = template.lineSpacing
local tableSpacing = template.tableSpacing
local sp = template.listSpacing.field
local yMinLim = radio.yMinLimit
local x = margin
local y = yMinLim - lineSpacing
local inc = { x = function(val) x = x + val return x end, y = function(val) y = y + val return y end }
local labels = {}
local fields = {}

labels[#labels + 1] = { t = "Download VTX tables? Press", x = x, y = inc.y(lineSpacing) }
labels[#labels + 1] = { t = "[ENTER] to download, or", x = x, y = inc.y(lineSpacing) }
labels[#labels + 1] = { t = "[EXIT] to cancel.", x = x, y = inc.y(lineSpacing) }
fields[#fields + 1] = { x = x, y = inc.y(lineSpacing), value = "", ro = true }

return {
title = "VTX Tables",
labels = labels,
fields = fields,
init = assert(loadScript("vtx_tables.lua"))(),
}
24 changes: 24 additions & 0 deletions src/SCRIPTS/BF/acc_cal.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local MSP_ACC_CALIBRATION = 205
local accCalibrated = false
local lastRunTS = 0
local INTERVAL = 500

local function processMspReply(cmd,rx_buf)
if cmd == MSP_ACC_CALIBRATION then
accCalibrated = true
end
end

local function accCal()
if lastRunTS == 0 or lastRunTS + INTERVAL < getTime() then
lastRunTS = getTime()
if not accCalibrated then
protocol.mspRead(MSP_ACC_CALIBRATION)
end
end
mspProcessTxQ()
processMspReply(mspPollReply())
return accCalibrated
end

return { f = accCal, t = "Calibrating Accelerometer" }
20 changes: 4 additions & 16 deletions src/SCRIPTS/BF/background.lua
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
local sensorId = -1
local dataInitialised = false
local data_init = nil
local rssiEnabled = true
local rssiTask = nil

local function getSensorValue()
if sensorId == -1 then
local sensor = getFieldInfo(protocol.stateSensor)
if type(sensor) == "table" then
sensorId = sensor['id'] or -1
end
end
return getValue(sensorId)
end

local function modelActive(sensorValue)
return type(sensorValue) == "number" and sensorValue > 0
local function modelActive()
return getValue(protocol.stateSensor) > 0
end

local function run_bg()
local sensorValue = getSensorValue()
if modelActive(sensorValue) then
if modelActive() then
-- Send data when the telemetry connection is available
-- assuming when sensor value higher than 0 there is an telemetry connection
if not dataInitialised then
if not data_init then
data_init = assert(loadScript("data_init.lua"))()
end

dataInitialised = data_init()
dataInitialised = data_init.f()

if dataInitialised then
data_init = nil
Expand Down
2 changes: 1 addition & 1 deletion src/SCRIPTS/BF/data_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ local function init()
return apiVersionReceived and timeIsSet
end

return init
return { f = init, t = "Waiting for API version" }
2 changes: 1 addition & 1 deletion src/SCRIPTS/BF/mcu_id.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ local function getMCUId()
return MCUIdReceived
end

return getMCUId
return { f = getMCUId, t = "Waiting for device ID" }
118 changes: 63 additions & 55 deletions src/SCRIPTS/BF/ui.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
local uiStatus =
{
init = 1,
pages = 2,
mainMenu = 3,
mainMenu = 2,
pages = 3,
confirm = 4,
}

local pageStatus =
{
display = 2,
editing = 3,
saving = 4,
popupMenu = 5,
display = 1,
editing = 2,
saving = 3,
}

local uiMsp =
Expand All @@ -20,6 +20,7 @@ local uiMsp =
}

local uiState = uiStatus.init
local prevUiState
local pageState = pageStatus.display
local requestTimeout = 80
local currentPage = 1
Expand All @@ -32,7 +33,7 @@ local popupMenuActive = 1
local killEnterBreak = 0
local pageScrollY = 0
local mainMenuScrollY = 0
local PageFiles, Page, init, popupMenuList
local PageFiles, Page, init, popupMenu

local backgroundFill = TEXT_BGCOLOR or ERASE
local foregroundColor = LINE_COLOR or SOLID
Expand Down Expand Up @@ -72,30 +73,26 @@ local function eepromWrite()
protocol.mspRead(uiMsp.eepromWrite)
end

local function accCal()
local function confirm(page)
prevUiState = uiState
uiState = uiStatus.confirm
invalidatePages()
currentField = 1
Page = assert(loadScript("Pages/accelerometer.lua"))()
Page = assert(loadScript(page))()
collectgarbage()
end

local function getVtxTables()
uiState = uiStatus.init
PageFiles = nil
invalidatePages()
io.close(io.open("/BF/VTX/"..mcuId..".lua", 'w'))
return 0
end

local function createPopupMenu()
popupMenuList = {
{ t = "save page", f = saveSettings },
{ t = "reload", f = invalidatePages },
{ t = "reboot", f = rebootFc },
{ t = "acc cal", f = accCal },
}
popupMenuActive = 1
popupMenu = {}
if uiState == uiStatus.pages then
popupMenu[#popupMenu + 1] = { t = "save page", f = saveSettings }
popupMenu[#popupMenu + 1] = { t = "reload", f = invalidatePages }
end
popupMenu[#popupMenu + 1] = { t = "reboot", f = rebootFc }
popupMenu[#popupMenu + 1] = { t = "acc cal", f = function() confirm("CONFIRM/acc_cal.lua") end }
if apiVersion >= 1.042 then
popupMenuList[#popupMenuList + 1] = { t = "vtx tables", f = getVtxTables }
popupMenu[#popupMenu + 1] = { t = "vtx tables", f = function() confirm("CONFIRM/vtx_tables.lua") end }
end
end

Expand Down Expand Up @@ -162,7 +159,7 @@ local function incMainMenu(inc)
end

local function incPopupMenu(inc)
popupMenuActive = clipValue(popupMenuActive + inc, 1, #popupMenuList)
popupMenuActive = clipValue(popupMenuActive + inc, 1, #popupMenu)
end

local function requestPage()
Expand Down Expand Up @@ -251,13 +248,13 @@ local function drawPopupMenu()
local w = radio.MenuBox.w
local h_line = radio.MenuBox.h_line
local h_offset = radio.MenuBox.h_offset
local h = #popupMenuList * h_line + h_offset*2
local h = #popupMenu * h_line + h_offset*2

lcd.drawFilledRectangle(x,y,w,h,backgroundFill)
lcd.drawRectangle(x,y,w-1,h-1,foregroundColor)
lcd.drawText(x+h_line/2,y+h_offset,"Menu:",globalTextOptions)

for i,e in ipairs(popupMenuList) do
for i,e in ipairs(popupMenu) do
local textOptions = globalTextOptions
if popupMenuActive == i then
textOptions = textOptions + INVERS
Expand All @@ -267,18 +264,35 @@ local function drawPopupMenu()
end

local function run_ui(event)
if uiState == uiStatus.init then
if popupMenu then
drawPopupMenu()
if event == EVT_VIRTUAL_EXIT then
popupMenu = nil
elseif event == EVT_VIRTUAL_PREV then
incPopupMenu(-1)
elseif event == EVT_VIRTUAL_NEXT then
incPopupMenu(1)
elseif event == EVT_VIRTUAL_ENTER then
if killEnterBreak == 1 then
killEnterBreak = 0
else
popupMenu[popupMenuActive].f()
popupMenu = nil
end
end
elseif uiState == uiStatus.init then
lcd.clear()
drawScreenTitle("Betaflight Config")
init = init or assert(loadScript("ui_init.lua"))()
if not init() then
lcd.drawText(6, radio.yMinLimit, init.t)
if not init.f() then
return 0
end
init = nil
createPopupMenu()
PageFiles = assert(loadScript("pages.lua"))()
invalidatePages()
uiState = uiStatus.mainMenu
uiState = prevUiState or uiStatus.mainMenu
prevUiState = nil
elseif uiState == uiStatus.mainMenu then
if event == EVT_VIRTUAL_EXIT then
return 2
Expand All @@ -288,6 +302,9 @@ local function run_ui(event)
incMainMenu(-1)
elseif event == EVT_VIRTUAL_ENTER then
uiState = uiStatus.pages
elseif event == EVT_VIRTUAL_ENTER_LONG then
killEnterBreak = 1
createPopupMenu()
end
lcd.clear()
local yMinLim = radio.yMinLimit
Expand Down Expand Up @@ -322,21 +339,6 @@ local function run_ui(event)
invalidatePages()
end
end
elseif pageState == pageStatus.popupMenu then
if event == EVT_VIRTUAL_EXIT then
pageState = pageStatus.display
elseif event == EVT_VIRTUAL_PREV then
incPopupMenu(-1)
elseif event == EVT_VIRTUAL_NEXT then
incPopupMenu(1)
elseif event == EVT_VIRTUAL_ENTER then
if killEnterBreak == 1 then
killEnterBreak = 0
else
pageState = pageStatus.display
return popupMenuList[popupMenuActive].f() or 0
end
end
elseif pageState == pageStatus.display then
if event == EVT_VIRTUAL_PREV_PAGE then
incPage(-1)
Expand All @@ -350,17 +352,13 @@ local function run_ui(event)
elseif event == EVT_VIRTUAL_ENTER then
if Page then
local f = Page.fields[currentField]
if f.onClick then
f.onClick(Page)
end
if Page.values and f.vals and Page.values[f.vals[#f.vals]] and not f.ro then
pageState = pageStatus.editing
end
end
elseif event == EVT_VIRTUAL_ENTER_LONG then
popupMenuActive = 1
killEnterBreak = 1
pageState = pageStatus.popupMenu
createPopupMenu()
elseif event == EVT_VIRTUAL_EXIT then
invalidatePages()
currentField = 1
Expand All @@ -380,17 +378,15 @@ local function run_ui(event)
end
end
if not Page then
Page = assert(loadScript("Pages/"..PageFiles[currentPage].script))()
Page = assert(loadScript("PAGES/"..PageFiles[currentPage].script))()
collectgarbage()
end
if not Page.values and pageState == pageStatus.display then
requestPage()
end
lcd.clear()
drawScreen()
if pageState == pageStatus.popupMenu then
drawPopupMenu()
elseif pageState == pageStatus.saving then
if pageState == pageStatus.saving then
local saveMsg = "Saving..."
if saveRetries > 0 then
saveMsg = "Retrying"
Expand All @@ -399,6 +395,18 @@ local function run_ui(event)
lcd.drawRectangle(radio.SaveBox.x,radio.SaveBox.y,radio.SaveBox.w,radio.SaveBox.h,SOLID)
lcd.drawText(radio.SaveBox.x+radio.SaveBox.x_offset,radio.SaveBox.y+radio.SaveBox.h_offset,saveMsg,DBLSIZE + globalTextOptions)
end
elseif uiState == uiStatus.confirm then
lcd.clear()
drawScreen()
if event == EVT_VIRTUAL_ENTER then
uiState = uiStatus.init
init = Page.init
invalidatePages()
elseif event == EVT_VIRTUAL_EXIT then
invalidatePages()
uiState = prevUiState
prevUiState = nil
end
end
if protocol.rssi() == 0 then
lcd.drawText(radio.NoTelem[1],radio.NoTelem[2],radio.NoTelem[3],radio.NoTelem[4])
Expand Down
Loading