lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

im new to LUA and using it for the Taranis model airplane transmitter, a hobby project.

Im trying to write a custom menu for setting only the parameters in the radio I want to change.

Im kind of stuck: i have set up a basic menu, and once you pick a page listed it will load that page.if you hit another button, it will return to the picklist again.

However, although all pages load for the first time, it will always crash after load for the second or third time. It must be somthing with upvalues created where I dont see them??

Any help possible would be appreciated.

Code attached below.

Met vriendelijke groet

Guido ter Horst


local version="1.1"

--put the menus to load here, with selection as the starting menu item
--there needs to be a directory in the Scripts/MENUS directory with the same name, containing the menu content
--local menus= dofile( '/SCRIPTS/MENUS/menus.lua' )  --load the pages
local currentMenu=1
local createMenu
--local currentPage
local locked=true

local function cleanup()
  currentPage=nil
  config=nil
  collectgarbage()
end

local function init()
  if not currentPage then
    cleanup()
    currentPage = createMenu()
end
currentPage.init()
end

local function background()
  locked = true --resetting the lock status after releasing it to escape menu
if not currentPage.background() then
      cleanup()
      currentPage = createMenu()
end
end

local function display(event)
  if not currentPage.display(event) then
      cleanup()
      currentPage = createMenu()
end
end

--[[
UI to choose the task
--]]
createMenu = function()
local pages = dofile( "/SCRIPTS/MENUS/" .. menus[currentMenu].name .. '/pages.lua' )  --load the pagelist
  config = dofile( '/SCRIPTS/MENUS/' .. menus[currentMenu].fileName .. '.lua' ) --load menu specific configurations

local function dummy()
return true
end

local function display(event)
    menus[currentMenu].selection=config.selectPage(menus[currentMenu].selection,#pages,event)    

    if config.activateMenuItem(event)   then
      currentPage=nil
      collectgarbage()
      currentPage = 
        dofile( "/SCRIPTS/MENUS/" .. menus[currentMenu].name .. '/page_' .. pages[ menus[currentMenu].selection ].id .. '.lua' )
      init()    --force the load of the init of the new page 
      return true
    end

    if event == EVT_PAGE_LONG then
      killEvents(event)
      currentMenu = currentMenu  - 1
      if currentMenu < 1 then
        currentMenu = #menus
      end
      return false
    elseif event == EVT_PAGE_BREAK then
      currentMenu = currentMenu  + 1
      if currentMenu > #menus then
        currentMenu = 1
      end
      return false
    end
   
    if event == EVT_EXIT_BREAK then
      locked = false
    elseif locked == true then
      lcd.lock()
    end
    
    lcd.clear()
for i=1,7 do
local att = 0
if i == 4 then
att = INVERS
end
local ii = i + menus[currentMenu].selection - 4
if ii >= 1 and ii <= #pages then
lcd.drawText( 10, -8 + 9 * i, pages [ ii ].id, att )
lcd.drawText( 20, -8 + 9 * i, pages [ ii ].desc, att )
end
end

lcd.drawText( 150, 2, pages.title, DBLSIZE )
    lcd.drawText( 140, 20, menus[currentMenu].name,0 )
    lcd.drawText( 162, 56, "version " .. version, SMLSIZE)

return true
end

return { init=dummy, background="" display=display }
end

return { init=init, background="" run=display }