Advertisement
Guest User

portal

a guest
Nov 20th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.45 KB | None | 0 0
  1. function Initilize()
  2.  os.loadAPI("device")
  3.  os.loadAPI("bases")
  4.   --devices
  5.  monitor = device.getDevice("monitor")
  6.  dial = device.getDevice("ep_dialling_device")
  7.  port = device.getDevice("ep_controller")
  8.  
  9.  --check devices
  10.  
  11.  --setup monitor
  12.  monitor.clear()
  13.  monitor.setTextScale(.5)
  14.  
  15.  --set Default Parameters
  16.  menuMode="portal" --menu --baselist portal
  17.  page=0 --current page
  18.  mpage=0 --max page
  19.  
  20.  --user Params
  21.  homeBase="Home Base" --where is this portal?
  22.  curBase="Home Base" -- selected Base from list
  23.  curFloor = "Basement"
  24.  
  25.  
  26.   buttons={}
  27.   panels={}
  28.   floors={}
  29.   dialerItems={}
  30.      
  31.   getDialerEntrys()
  32.  
  33.  
  34.   --default screen
  35.   menubuttons()
  36.   floorbuttons()
  37.   homescreen()
  38.    
  39.        
  40.   --program start
  41.   parallel.waitForAll(mainLoop,uiLoop)
  42. end
  43.  
  44. function showFloorPage(pageNum)
  45. menuMode="portal"
  46.   if pageNum == 0 then
  47.     return
  48.   end
  49.  mpage=getFloorPages()
  50.  setPanelText("header",curBase.." (pg "..page.."/"..mpage..")")
  51.  
  52.  clearBtns()
  53.  for off=page,page+8 do
  54.    if off <= #floors then
  55.      showBtn(floors[off]["floor"])
  56.    end
  57.  end
  58. end
  59.  
  60.  
  61. function showBasePage(pageNum)
  62.    menuMode="baselist"
  63.     if pageNum == 0 then
  64.    return
  65.   end
  66.   clearBtns()
  67.   for off=page,page+8 do
  68.    if off <= bases.getBaseCount() then
  69.      showBtn(bases.getBaseName(off))
  70.    end
  71.   end  
  72. end
  73.  
  74.  
  75.  
  76. function clearBtns()
  77.  setButtonText("btn1","")
  78.  setButtonText("btn2","")
  79.  setButtonText("btn3","")
  80.  setButtonText("btn4","")
  81.  setButtonText("btn5","")
  82.  setButtonText("btn6","")
  83.  setButtonText("btn7","")
  84.  setButtonText("btn8","")
  85. end
  86.  
  87. function showBtn(btnText)
  88.   if buttons["btn1"]["text"] == "" then
  89.      setButtonText("btn1",btnText)
  90.      return
  91.   end
  92.  
  93.   if buttons["btn2"]["text"] == "" then
  94.     setButtonText("btn2",btnText)
  95.     return
  96.   end
  97.  
  98.   if buttons["btn3"]["text"] == "" then
  99.     setButtonText("btn3",btnText)
  100.    return
  101.   end
  102.  
  103.  if buttons["btn4"]["text"] == "" then
  104.    setButtonText("btn4",btnText)
  105.   return
  106.  end
  107.  
  108.  if buttons["btn5"]["text"] == "" then
  109.    setButtonText("btn5",btnText)
  110.    return
  111.  end
  112.  
  113.  if buttons["btn6"]["text"] == "" then
  114.    setButtonText("btn6",btnText)
  115.    return
  116.  end
  117.  
  118.  if buttons["btn7"]["text"] == "" then
  119.     setButtonText("btn7",btntext)
  120.     return
  121.  end
  122.  
  123.   if buttons["btn8"]["text"] == "" then
  124.     setButtonText("btn8",btnText)
  125.     return
  126.   end
  127. end
  128.  
  129. function getBasePages()
  130.  --needs coding
  131. if #bases == 0 then
  132.   return 0
  133. end
  134.  
  135. if #bases <= 8 then
  136.  return 1
  137. end
  138.  
  139.  if #bases <= 16 then
  140.   return 2
  141.  end
  142.  
  143. if #bases <= 24 then
  144.   return 3  
  145. end
  146.  
  147. if #bases <= 32 then
  148.  return 4
  149.  end
  150.  
  151. end
  152.  
  153. function getFloorPages()
  154.   if #floors == 0 then
  155.     return 0
  156.   end
  157.  
  158.   if #floors <=8 then
  159.     return 1
  160.   end
  161.   if #floors <=16 then
  162.     return 2
  163.   end
  164.   if #floors <=24 then
  165.     return 3
  166.   end
  167.   if #floors <= 32 then
  168.     return 3
  169.   end
  170. end
  171.  
  172. function getFloors(baseName)
  173.   floors={}
  174.   local cnt=0
  175.  
  176.   for fD=1,#dialerItems do
  177.   if dialerItems[fD]["base"] ==baseName then
  178.     cnt=cnt+1
  179.     floors[cnt]={idx=dialerItems[fD]["idx"],floor=dialerItems[fD]["floor"]}
  180.   end
  181.   end
  182.   mpage=#floors
  183.   print("Got "..cnt.." Floors at the base "..baseName)
  184. end
  185.  
  186. function getDialerEntrys()
  187.  local cnt = dial.getStoredCount()
  188.  local line=0
  189.  local dialerLine=""
  190.  if cnt == 0 then
  191.   return
  192.  end
  193.  for dI=1,cnt do
  194.    line=line+1
  195.    dialerLine=dial.getStoredName(dI-1)
  196.    --if dialerline has no , in it ass it to other
  197.    if string.find(dialerLine,",") == nil then
  198.      dialerLine = homeBase..","..dialerLine
  199.    end
  200.    
  201.    for k,v in string.gmatch(dialerLine,"(.+),(.+)") do
  202.     print("Dialer Item found - ".."B:"..k.." F:"..v.." I:"..dI-1)
  203.     dialerItems[line]={idx=dI-1,base=k,floor=v}
  204.     bases.addBase(k)
  205.   end
  206. end
  207. end
  208.  
  209. function floorbuttons()
  210.  --page1
  211.  setButton("btn1","",btn,btn,5,2,2,15,colors.green,colors.green)
  212.  setButton("btn2","",btn,btn,9,2,2,15,colors.blue,colors.blue)
  213.  setButton("btn3","",btn,btn,13,2,2,15,colors.purple,colors.purple)
  214.  setButton("btn4","",btn,btn,17,2,2,15,colors.pink,colors.pink)
  215.  setButton("btn5","",btn,btn,5,2,20,15,colors.green,colors.green)
  216.  setButton("btn6","",btn,btn,9,2,20,15,colors.blue,colors.blue)
  217.  setButton("btn7","",btn,btn,13,2,20,15,colors.purple,colors.purple)
  218.  setButton("btn8","",btn,btn,17,2,20,15,colors.pink,colors.pink)
  219.  
  220.   --page 2
  221.  --setButton("floor5","5th Floor",2,flr5,flr5,6,2,2,12,colors.green,colors.green)
  222.  --setButton("basement1","Power Room",2,base1,base1,10,2,2,12,colors.red,colors.red)
  223. end
  224.  
  225. function homescreen(n)
  226.  print("Home Screen")
  227.  menuMode="portal"
  228.  curBase=homeBase
  229.  clearBtns()
  230.  getFloors(homeBase)
  231.  page=1
  232.  mpage = getFloorPages()
  233.  --menubuttons()
  234.  setPanelText("header",curBase.." (pg "..page.."/"..mpage..")")
  235.  --floorbuttons()  
  236.  showFloorPage(page)
  237. end
  238.  
  239. function baseScreen1(n)
  240.  print("Base List Screen")
  241.  menuMode="baselist"
  242.  clearBtns()
  243.  page=1
  244.  mpage = getBasePages()
  245.  setPanelText("header","Base List ".."(pg "..page.."/"..mpage..")")
  246.   --needs finishing
  247.  showBasePage(page)
  248. end
  249.  
  250. function menubuttons()
  251.  setButton("Home","Home",homescreen,homescreen,22,3,2,5,colors.blue,colors.blue)
  252.  setButton("Bases","Bases",baseScreen1,baseScreen1,22,3,9,6,colors.green,colors.green)
  253.  setButton("<","<",prev,prev,22,2,18,2,colors.orange,colors.orange)
  254.  setButton(">",">",forward,forward,22,2,23,2,colors.purple,colors.purple)
  255.  setPanel("header","",1,1,2,33,colors.green,colors.green)
  256. end
  257.  
  258. function setup1()
  259. end
  260.  
  261. function prev()
  262.   if menuMode == "baselist" then
  263.     baseListPrev()
  264.     return
  265.   end
  266.  
  267.   if menuMode == "menu" then
  268.     --future Options
  269.     return
  270.   end
  271.   page = page - 1
  272. if page < 1 then
  273.  page = 1
  274. end
  275. monitor.clear()
  276. setPanelText("header",curBase.." (pg "..page.."/"..mpage..")")
  277. showFloorPage(page)
  278. update()
  279. end
  280.  
  281. function forward()
  282.  if menuMode == "baselist" then
  283.     baseListForward()
  284.     return
  285.  end
  286.  if menuMode=="menu" then
  287.     --add other options future
  288.     return
  289.  end
  290.  
  291.     page = page + 1
  292.  if page > mpage then
  293.   page = mpage
  294.   end
  295.   monitor.clear()
  296.   setPanelText("header",curBase.." (pg "..page.."/"..mpage..")")
  297.   showFloorPage(page)
  298.   update()
  299. end
  300.  
  301. function baseListPrev()
  302.  page = page - 1
  303.  if page <=0 then
  304.   page = 0
  305.   end
  306.   setPanelText("header","Base List (pg "..page.."/"..mpage..")")
  307.   showBasePage(page)
  308.   update()
  309. end
  310.  
  311. function baseListForward()
  312.   page = page + 1
  313.   if page > getBasePages() then
  314.     page = mpage
  315.   end
  316.   setPanelText("header","Base List (pg "..page.."/"..mpage..")")  
  317.  showBasePage(page)
  318.  update()
  319. end
  320.  
  321. function setButton(name,text,onFunc,offFunc,ymin,ymax,xmin,xmax,onColor,offColor)
  322.   buttons[name] = {active=false,xmin=xmin,xmax=xmin+xmax,ymin=ymin,ymax=ymin+ymax,text=text, onFunc=onFunc, offFunc=offFunc, onColor=onColor, offColor=offColor}
  323. end
  324.  
  325.  
  326. function setPanel(name,text,ymin,ymax,xmin,xmax,color)
  327.   panels[name] = {name=name, text=text, xmin=xmin, xmax = xmin+xmax, ymin = ymin, ymax = ymin+ymax,color=color}
  328. end
  329.  
  330.  
  331. function setPanelColor(name,color)
  332.  panels[name]["color"] = color
  333. end
  334.  
  335. function setPanelText(name,text)
  336.  panels[name]["text"] = text
  337. end
  338.  
  339.  
  340. function DI(Text)
  341.  if Text == "" then
  342.   print("no Floor Text")
  343.   return 999
  344.  end
  345.  if string.lower(Text) == string.lower(curFloor) then
  346.   print("Current Floor")
  347.   return 999
  348.  end
  349.  print("looking for dial entry "..Text)
  350.  cnt = dial.getStoredCount()
  351.  print("Items in list "..cnt)
  352.   for I=1,cnt do
  353.    name = dial.getStoredName(I-1)
  354.   if string.lower(name) == string.lower(Text) then
  355.     print("Found Entry at Index "..I-1)
  356.     return I-1
  357.    end
  358.   end
  359.   print("Floor Not registered in dialer")
  360.   return 999
  361. end
  362.  
  363. function btn(btnName)
  364.  if btnName == "" then
  365.    return
  366.  end
  367.  
  368.  if menuMode == "portal" then
  369.   portalBtn(btnName)
  370.   return
  371.  end
  372.  
  373.  if menuMode=="menu" then
  374.   menuBtn(btnName)
  375.   return
  376. end
  377.  
  378. if menuMode=="baselist" then
  379.   baseBtn(btnName)
  380.   return
  381. end
  382. end
  383.  
  384. function baseBtn(btnName)
  385. if bases.baseExists(btnName) == true then
  386.  curBase=btnName
  387.  getFloors(btnName)
  388.  showFloorPage(1)
  389.  return
  390. end
  391. print("Base "..btnName.." does not exists.")
  392. end
  393.  
  394. function menuBtn(btnName)
  395. end
  396.  
  397. function portalBtn(btnName)
  398.  local dL = curBase..","..btnName
  399.  local dN = DI(dL)
  400.  if dN < 999 then
  401.    checkstate()
  402.    dial.dialStored(dN)
  403.    sleep(2)
  404.   checkstate()
  405.  end
  406. end
  407.  
  408.  
  409. function checkstate()
  410.  if port.isPortalActive() == true then
  411.   dial.terminate()
  412.  end
  413. end
  414.  
  415. function setButtonText(name,text)
  416.  if buttons[name] ~= nil then
  417.    buttons[name]["text"] = text
  418.  end
  419. end
  420.  
  421.  
  422. function setPanelText(name,text)
  423.  panels[name]["text"] = text
  424. end
  425.  
  426. function setPanelColor(name,color)
  427.   panels[name]["color"] = color
  428. end
  429.  
  430.  
  431.  
  432. function fill(text, color, bData)
  433.   monitor.setBackgroundColor(color)
  434.   local yspot = math.floor((bData.ymin + bData.ymax)/2)
  435.   local xspot = math.floor((bData.xmax - bData.xmin - string.len(text))/2)+1
  436.   for j = bData.ymin, bData.ymax do
  437.     monitor.setCursorPos(bData.xmin, j)
  438.     if j == yspot then
  439.       for k = 0, bData.xmax - bData.xmin - string.len(text)+1 do
  440.         if k == xspot then monitor.write(text)
  441.         else monitor.write(" ")
  442.         end
  443.       end
  444.     else
  445.       for i = bData.xmin, bData.xmax do
  446.         monitor.write(" ")
  447.       end
  448.     end
  449.   end
  450.   monitor.setBackgroundColor(colors.black)
  451. end
  452.  
  453. function temp()
  454. --do not delete
  455. end
  456.  
  457. function checkxy(x, y)
  458.   local retv = false
  459.   local btnText = ""
  460.   local tempFunc = temp  
  461. for name, data in pairs(buttons) do
  462.     if y>=data.ymin and  y <= data.ymax then
  463.       if x>=data.xmin and x<= data.xmax then
  464.          btnText = data.text
  465.          print("Button "..data.text.." was pressed.")
  466.        if data.active == true then
  467.         tempFunc = data.onFunc
  468.        else
  469.          tempFunc = data.offFunc
  470.         end  
  471.         --data.active = not data.active
  472.         retv = true
  473.       end
  474.     end
  475.  end
  476.  if tempFunc ~=temp then
  477.   tempFunc(btnText)
  478.  end
  479.  
  480.   return retv
  481. end
  482.  
  483. function update()
  484.   local currColor
  485.   for name,data in pairs(buttons) do
  486.     local on = data.active
  487.     if on == true then
  488.       currColor = data.oncolor
  489.     else
  490.       currColor = data.offColor
  491.     end
  492.    
  493.       fill(data.text, currColor, data)
  494.     end
  495.  
  496.  for name,data in pairs(panels) do
  497.   fill(data.text, data.color, data)  
  498.  end
  499.  
  500. end
  501.  
  502. function mainLoop()
  503. while true do
  504.  update()
  505.  sleep(1)
  506. end
  507. end
  508.  
  509. function uiLoop()
  510.   while true do
  511.    event,side,x,y=os.pullEvent("monitor_touch")  
  512.    checkxy(x,y)
  513.    sleep(.01)
  514.   end
  515. end
  516.  
  517. Initilize()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement