local name, folder, extension = "Test", "Apps/", ".txt" local switch, fileSave, fileLoad, filesLoad, indexLoad, timeB4, timeB5 local function isSwitch(value) if (value) then local info = system.getSwitchInfo(value) return info and info.assigned end end local function setFiles() local index = 1 local current = 1 local files = {"Empty"} local folder = folder local extension = extension local chars = #extension local emulator = select(2, system.getDeviceType()) if (emulator == 0) then folder = "/"..folder:sub(1, -2) -- dir() hack end for name, type in dir(folder) do if (type == "file" and name:sub(-chars):lower() == extension) then name = name:sub(1, -chars-1) index = index + 1 files[index] = name if (name == fileLoad) then current = index end end end filesLoad = files indexLoad = current end local function initForm(id) setFiles() form.addRow(2) form.addLabel({label="Switch"}) form.addInputbox(switch, true, function(value) if (not isSwitch(value)) then value = nil end switch = value system.pSave("switch", switch) end) form.addRow(2) form.addLabel({label="Name (Save)", width=200}) form.addTextbox(fileSave, 63, function(value) fileSave = value system.pSave("fileSave", fileSave) form.setButton(4, "S", fileSave:len() > 0 and ENABLED or DISABLED) end) form.addRow(2) form.addLabel({label="Name (Load)", width=200}) form.addSelectbox(filesLoad, indexLoad, true, function(value) fileLoad = value > 1 and filesLoad[value] or "" system.pSave("fileLoad", fileLoad) form.setButton(5, "L", fileLoad:len() > 0 and ENABLED or DISABLED) end) form.setButton(4, "S", timeB4 and HIGHLIGHTED or fileSave:len() > 0 and ENABLED or DISABLED) form.setButton(5, "L", timeB5 and HIGHLIGHTED or fileLoad:len() > 0 and ENABLED or DISABLED) end local function saveConfig() if (fileSave:len() > 0) then local file = io.open(folder..fileSave..extension, "w+") if (file) then local space = " " if (tonumber(system.getVersion():match("%d+")) >= 5) then local info = system.getSwitchInfo(switch) if (info and info.assigned) then io.write(file, info.label..space..info.mode..space..info.value) end end io.close(file) fileSave = "" system.pSave("fileSave", fileSave) timeB4 = system.getTimeCounter() print("saved") end end end local function loadConfig() if (fileLoad:len() > 0) then local file = io.open(folder..fileLoad..extension, "r") if (file) then if (tonumber(system.getVersion():match("%d+")) >= 5) then local line = io.readline(file, true) if (line) then local label,mode,value = line:match("(%S+) (%S+) (%S+)") switch = system.createSwitch(label:upper(), mode:upper(), tonumber(value)) system.pSave("switch", switch) end end io.close(file) fileLoad = "" system.pSave("fileLoad", fileLoad) timeB5 = system.getTimeCounter() print("loaded") end end end local function keyForm(key) if (key == KEY_4) then saveConfig() form.reinit(1) elseif (key == KEY_5) then form.preventDefault() loadConfig() form.reinit(1) end end local function loop() if (timeB4 or timeB5) then local time = system.getTimeCounter() local limit = 1000 if (timeB4 and time - timeB4 > limit) then timeB4 = nil if (formID == 1) then form.setButton(4, "S", fileSave:len() > 0 and ENABLED or DISABLED) end end if (timeB5 and time - timeB5 > limit) then timeB5 = nil if (formID == 1) then form.setButton(5, "L", fileLoad:len() > 0 and ENABLED or DISABLED) end end end end local function init(code) switch = system.pLoad("switch") fileSave = system.pLoad("fileSave", "") fileLoad = system.pLoad("fileLoad", "") system.registerForm(1, MENU_MAIN, name, initForm, keyForm) end return {init=init, loop=loop, author="Thorn Jeti Forum", version="1.0", name=name}