Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Initilize()
- os.loadAPI("device")
- os.loadAPI("bases")
- --devices
- monitor = device.getDevice("monitor")
- dial = device.getDevice("ep_dialling_device")
- port = device.getDevice("ep_controller")
- --check devices
- --setup monitor
- monitor.clear()
- monitor.setTextScale(.5)
- --set Default Parameters
- menuMode="portal" --menu --baselist portal
- page=0 --current page
- mpage=0 --max page
- --user Params
- homeBase="Home Base" --where is this portal?
- curBase="Home Base" -- selected Base from list
- curFloor = "Basement"
- buttons={}
- panels={}
- floors={}
- dialerItems={}
- getDialerEntrys()
- --default screen
- menubuttons()
- floorbuttons()
- homescreen()
- --program start
- parallel.waitForAll(mainLoop,uiLoop)
- end
- function showFloorPage(pageNum)
- menuMode="portal"
- if pageNum == 0 then
- return
- end
- mpage=getFloorPages()
- setPanelText("header",curBase.." (pg "..page.."/"..mpage..")")
- clearBtns()
- for off=page,page+8 do
- if off <= #floors then
- showBtn(floors[off]["floor"])
- end
- end
- end
- function showBasePage(pageNum)
- menuMode="baselist"
- if pageNum == 0 then
- return
- end
- clearBtns()
- for off=page,page+8 do
- if off <= bases.getBaseCount() then
- showBtn(bases.getBaseName(off))
- end
- end
- end
- function clearBtns()
- setButtonText("btn1","")
- setButtonText("btn2","")
- setButtonText("btn3","")
- setButtonText("btn4","")
- setButtonText("btn5","")
- setButtonText("btn6","")
- setButtonText("btn7","")
- setButtonText("btn8","")
- end
- function showBtn(btnText)
- if buttons["btn1"]["text"] == "" then
- setButtonText("btn1",btnText)
- return
- end
- if buttons["btn2"]["text"] == "" then
- setButtonText("btn2",btnText)
- return
- end
- if buttons["btn3"]["text"] == "" then
- setButtonText("btn3",btnText)
- return
- end
- if buttons["btn4"]["text"] == "" then
- setButtonText("btn4",btnText)
- return
- end
- if buttons["btn5"]["text"] == "" then
- setButtonText("btn5",btnText)
- return
- end
- if buttons["btn6"]["text"] == "" then
- setButtonText("btn6",btnText)
- return
- end
- if buttons["btn7"]["text"] == "" then
- setButtonText("btn7",btntext)
- return
- end
- if buttons["btn8"]["text"] == "" then
- setButtonText("btn8",btnText)
- return
- end
- end
- function getBasePages()
- --needs coding
- if #bases == 0 then
- return 0
- end
- if #bases <= 8 then
- return 1
- end
- if #bases <= 16 then
- return 2
- end
- if #bases <= 24 then
- return 3
- end
- if #bases <= 32 then
- return 4
- end
- end
- function getFloorPages()
- if #floors == 0 then
- return 0
- end
- if #floors <=8 then
- return 1
- end
- if #floors <=16 then
- return 2
- end
- if #floors <=24 then
- return 3
- end
- if #floors <= 32 then
- return 3
- end
- end
- function getFloors(baseName)
- floors={}
- local cnt=0
- for fD=1,#dialerItems do
- if dialerItems[fD]["base"] ==baseName then
- cnt=cnt+1
- floors[cnt]={idx=dialerItems[fD]["idx"],floor=dialerItems[fD]["floor"]}
- end
- end
- mpage=#floors
- print("Got "..cnt.." Floors at the base "..baseName)
- end
- function getDialerEntrys()
- local cnt = dial.getStoredCount()
- local line=0
- local dialerLine=""
- if cnt == 0 then
- return
- end
- for dI=1,cnt do
- line=line+1
- dialerLine=dial.getStoredName(dI-1)
- --if dialerline has no , in it ass it to other
- if string.find(dialerLine,",") == nil then
- dialerLine = homeBase..","..dialerLine
- end
- for k,v in string.gmatch(dialerLine,"(.+),(.+)") do
- print("Dialer Item found - ".."B:"..k.." F:"..v.." I:"..dI-1)
- dialerItems[line]={idx=dI-1,base=k,floor=v}
- bases.addBase(k)
- end
- end
- end
- function floorbuttons()
- --page1
- setButton("btn1","",btn,btn,5,2,2,15,colors.green,colors.green)
- setButton("btn2","",btn,btn,9,2,2,15,colors.blue,colors.blue)
- setButton("btn3","",btn,btn,13,2,2,15,colors.purple,colors.purple)
- setButton("btn4","",btn,btn,17,2,2,15,colors.pink,colors.pink)
- setButton("btn5","",btn,btn,5,2,20,15,colors.green,colors.green)
- setButton("btn6","",btn,btn,9,2,20,15,colors.blue,colors.blue)
- setButton("btn7","",btn,btn,13,2,20,15,colors.purple,colors.purple)
- setButton("btn8","",btn,btn,17,2,20,15,colors.pink,colors.pink)
- --page 2
- --setButton("floor5","5th Floor",2,flr5,flr5,6,2,2,12,colors.green,colors.green)
- --setButton("basement1","Power Room",2,base1,base1,10,2,2,12,colors.red,colors.red)
- end
- function homescreen(n)
- print("Home Screen")
- menuMode="portal"
- curBase=homeBase
- clearBtns()
- getFloors(homeBase)
- page=1
- mpage = getFloorPages()
- --menubuttons()
- setPanelText("header",curBase.." (pg "..page.."/"..mpage..")")
- --floorbuttons()
- showFloorPage(page)
- end
- function baseScreen1(n)
- print("Base List Screen")
- menuMode="baselist"
- clearBtns()
- page=1
- mpage = getBasePages()
- setPanelText("header","Base List ".."(pg "..page.."/"..mpage..")")
- --needs finishing
- showBasePage(page)
- end
- function menubuttons()
- setButton("Home","Home",homescreen,homescreen,22,3,2,5,colors.blue,colors.blue)
- setButton("Bases","Bases",baseScreen1,baseScreen1,22,3,9,6,colors.green,colors.green)
- setButton("<","<",prev,prev,22,2,18,2,colors.orange,colors.orange)
- setButton(">",">",forward,forward,22,2,23,2,colors.purple,colors.purple)
- setPanel("header","",1,1,2,33,colors.green,colors.green)
- end
- function setup1()
- end
- function prev()
- if menuMode == "baselist" then
- baseListPrev()
- return
- end
- if menuMode == "menu" then
- --future Options
- return
- end
- page = page - 1
- if page < 1 then
- page = 1
- end
- monitor.clear()
- setPanelText("header",curBase.." (pg "..page.."/"..mpage..")")
- showFloorPage(page)
- update()
- end
- function forward()
- if menuMode == "baselist" then
- baseListForward()
- return
- end
- if menuMode=="menu" then
- --add other options future
- return
- end
- page = page + 1
- if page > mpage then
- page = mpage
- end
- monitor.clear()
- setPanelText("header",curBase.." (pg "..page.."/"..mpage..")")
- showFloorPage(page)
- update()
- end
- function baseListPrev()
- page = page - 1
- if page <=0 then
- page = 0
- end
- setPanelText("header","Base List (pg "..page.."/"..mpage..")")
- showBasePage(page)
- update()
- end
- function baseListForward()
- page = page + 1
- if page > getBasePages() then
- page = mpage
- end
- setPanelText("header","Base List (pg "..page.."/"..mpage..")")
- showBasePage(page)
- update()
- end
- function setButton(name,text,onFunc,offFunc,ymin,ymax,xmin,xmax,onColor,offColor)
- buttons[name] = {active=false,xmin=xmin,xmax=xmin+xmax,ymin=ymin,ymax=ymin+ymax,text=text, onFunc=onFunc, offFunc=offFunc, onColor=onColor, offColor=offColor}
- end
- function setPanel(name,text,ymin,ymax,xmin,xmax,color)
- panels[name] = {name=name, text=text, xmin=xmin, xmax = xmin+xmax, ymin = ymin, ymax = ymin+ymax,color=color}
- end
- function setPanelColor(name,color)
- panels[name]["color"] = color
- end
- function setPanelText(name,text)
- panels[name]["text"] = text
- end
- function DI(Text)
- if Text == "" then
- print("no Floor Text")
- return 999
- end
- if string.lower(Text) == string.lower(curFloor) then
- print("Current Floor")
- return 999
- end
- print("looking for dial entry "..Text)
- cnt = dial.getStoredCount()
- print("Items in list "..cnt)
- for I=1,cnt do
- name = dial.getStoredName(I-1)
- if string.lower(name) == string.lower(Text) then
- print("Found Entry at Index "..I-1)
- return I-1
- end
- end
- print("Floor Not registered in dialer")
- return 999
- end
- function btn(btnName)
- if btnName == "" then
- return
- end
- if menuMode == "portal" then
- portalBtn(btnName)
- return
- end
- if menuMode=="menu" then
- menuBtn(btnName)
- return
- end
- if menuMode=="baselist" then
- baseBtn(btnName)
- return
- end
- end
- function baseBtn(btnName)
- if bases.baseExists(btnName) == true then
- curBase=btnName
- getFloors(btnName)
- showFloorPage(1)
- return
- end
- print("Base "..btnName.." does not exists.")
- end
- function menuBtn(btnName)
- end
- function portalBtn(btnName)
- local dL = curBase..","..btnName
- local dN = DI(dL)
- if dN < 999 then
- checkstate()
- dial.dialStored(dN)
- sleep(2)
- checkstate()
- end
- end
- function checkstate()
- if port.isPortalActive() == true then
- dial.terminate()
- end
- end
- function setButtonText(name,text)
- if buttons[name] ~= nil then
- buttons[name]["text"] = text
- end
- end
- function setPanelText(name,text)
- panels[name]["text"] = text
- end
- function setPanelColor(name,color)
- panels[name]["color"] = color
- end
- function fill(text, color, bData)
- monitor.setBackgroundColor(color)
- local yspot = math.floor((bData.ymin + bData.ymax)/2)
- local xspot = math.floor((bData.xmax - bData.xmin - string.len(text))/2)+1
- for j = bData.ymin, bData.ymax do
- monitor.setCursorPos(bData.xmin, j)
- if j == yspot then
- for k = 0, bData.xmax - bData.xmin - string.len(text)+1 do
- if k == xspot then monitor.write(text)
- else monitor.write(" ")
- end
- end
- else
- for i = bData.xmin, bData.xmax do
- monitor.write(" ")
- end
- end
- end
- monitor.setBackgroundColor(colors.black)
- end
- function temp()
- --do not delete
- end
- function checkxy(x, y)
- local retv = false
- local btnText = ""
- local tempFunc = temp
- for name, data in pairs(buttons) do
- if y>=data.ymin and y <= data.ymax then
- if x>=data.xmin and x<= data.xmax then
- btnText = data.text
- print("Button "..data.text.." was pressed.")
- if data.active == true then
- tempFunc = data.onFunc
- else
- tempFunc = data.offFunc
- end
- --data.active = not data.active
- retv = true
- end
- end
- end
- if tempFunc ~=temp then
- tempFunc(btnText)
- end
- return retv
- end
- function update()
- local currColor
- for name,data in pairs(buttons) do
- local on = data.active
- if on == true then
- currColor = data.oncolor
- else
- currColor = data.offColor
- end
- fill(data.text, currColor, data)
- end
- for name,data in pairs(panels) do
- fill(data.text, data.color, data)
- end
- end
- function mainLoop()
- while true do
- update()
- sleep(1)
- end
- end
- function uiLoop()
- while true do
- event,side,x,y=os.pullEvent("monitor_touch")
- checkxy(x,y)
- sleep(.01)
- end
- end
- Initilize()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement