Advertisement
Guest User

portal

a guest
Nov 20th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.47 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.      print("Debug")
  199.      dialerLine = homeBase..","..dialerLine
  200.    end
  201.    
  202.    for k,v in string.gmatch(dialerLine,"(.+),(.+)") do
  203.     print("Dialer Item found - ".."B:"..k.." F:"..v.." I:"..dI-1)
  204.     dialerItems[line]={idx=dI-1,base=k,floor=v}
  205.     bases.addBase(k)
  206.   end
  207. end
  208. end
  209.  
  210. function floorbuttons()
  211.  --page1
  212.  setButton("btn1","",btn,btn,5,2,2,15,colors.green,colors.green)
  213.  setButton("btn2","",btn,btn,9,2,2,15,colors.blue,colors.blue)
  214.  setButton("btn3","",btn,btn,13,2,2,15,colors.purple,colors.purple)
  215.  setButton("btn4","",btn,btn,17,2,2,15,colors.pink,colors.pink)
  216.  setButton("btn5","",btn,btn,5,2,20,15,colors.green,colors.green)
  217.  setButton("btn6","",btn,btn,9,2,20,15,colors.blue,colors.blue)
  218.  setButton("btn7","",btn,btn,13,2,20,15,colors.purple,colors.purple)
  219.  setButton("btn8","",btn,btn,17,2,20,15,colors.pink,colors.pink)
  220.  
  221.   --page 2
  222.  --setButton("floor5","5th Floor",2,flr5,flr5,6,2,2,12,colors.green,colors.green)
  223.  --setButton("basement1","Power Room",2,base1,base1,10,2,2,12,colors.red,colors.red)
  224. end
  225.  
  226. function homescreen(n)
  227.  print("Home Screen")
  228.  menuMode="portal"
  229.  curBase=homeBase
  230.  clearBtns()
  231.  getFloors(homeBase)
  232.  page=1
  233.  mpage = getFloorPages()
  234.  --menubuttons()
  235.  setPanelText("header",curBase.." (pg "..page.."/"..mpage..")")
  236.  --floorbuttons()  
  237.  showFloorPage(page)
  238. end
  239.  
  240. function baseScreen1(n)
  241.  print("Base List Screen")
  242.  menuMode="baselist"
  243.  clearBtns()
  244.  page=1
  245.  mpage = getBasePages()
  246.  setPanelText("header","Base List ".."(pg "..page.."/"..mpage..")")
  247.   --needs finishing
  248.  showBasePage(page)
  249. end
  250.  
  251. function menubuttons()
  252.  setButton("Home","Home",homescreen,homescreen,22,3,2,5,colors.blue,colors.blue)
  253.  setButton("Bases","Bases",baseScreen1,baseScreen1,22,3,9,6,colors.green,colors.green)
  254.  setButton("<","<",prev,prev,22,2,18,2,colors.orange,colors.orange)
  255.  setButton(">",">",forward,forward,22,2,23,2,colors.purple,colors.purple)
  256.  setPanel("header","",1,1,2,33,colors.green,colors.green)
  257. end
  258.  
  259. function setup1()
  260. end
  261.  
  262. function prev()
  263.   if menuMode == "baselist" then
  264.     baseListPrev()
  265.     return
  266.   end
  267.  
  268.   if menuMode == "menu" then
  269.     --future Options
  270.     return
  271.   end
  272.   page = page - 1
  273. if page < 1 then
  274.  page = 1
  275. end
  276. monitor.clear()
  277. setPanelText("header",curBase.." (pg "..page.."/"..mpage..")")
  278. showFloorPage(page)
  279. update()
  280. end
  281.  
  282. function forward()
  283.  if menuMode == "baselist" then
  284.     baseListForward()
  285.     return
  286.  end
  287.  if menuMode=="menu" then
  288.     --add other options future
  289.     return
  290.  end
  291.  
  292.     page = page + 1
  293.  if page > mpage then
  294.   page = mpage
  295.   end
  296.   monitor.clear()
  297.   setPanelText("header",curBase.." (pg "..page.."/"..mpage..")")
  298.   showFloorPage(page)
  299.   update()
  300. end
  301.  
  302. function baseListPrev()
  303.  page = page - 1
  304.  if page <=0 then
  305.   page = 0
  306.   end
  307.   setPanelText("header","Base List (pg "..page.."/"..mpage..")")
  308.   showBasePage(page)
  309.   update()
  310. end
  311.  
  312. function baseListForward()
  313.   page = page + 1
  314.   if page > getBasePages() then
  315.     page = mpage
  316.   end
  317.   setPanelText("header","Base List (pg "..page.."/"..mpage..")")  
  318.  showBasePage(page)
  319.  update()
  320. end
  321.  
  322. function setButton(name,text,onFunc,offFunc,ymin,ymax,xmin,xmax,onColor,offColor)
  323.   buttons[name] = {active=false,xmin=xmin,xmax=xmin+xmax,ymin=ymin,ymax=ymin+ymax,text=text, onFunc=onFunc, offFunc=offFunc, onColor=onColor, offColor=offColor}
  324. end
  325.  
  326.  
  327. function setPanel(name,text,ymin,ymax,xmin,xmax,color)
  328.   panels[name] = {name=name, text=text, xmin=xmin, xmax = xmin+xmax, ymin = ymin, ymax = ymin+ymax,color=color}
  329. end
  330.  
  331.  
  332. function setPanelColor(name,color)
  333.  panels[name]["color"] = color
  334. end
  335.  
  336. function setPanelText(name,text)
  337.  panels[name]["text"] = text
  338. end
  339.  
  340.  
  341. function DI(Text)
  342.  if Text == "" then
  343.   print("no Floor Text")
  344.   return 999
  345.  end
  346.  if string.lower(Text) == string.lower(curFloor) then
  347.   print("Current Floor")
  348.   return 999
  349.  end
  350.  print("looking for dial entry "..Text)
  351.  cnt = dial.getStoredCount()
  352.  print("Items in list "..cnt)
  353.   for I=1,cnt do
  354.    name = dial.getStoredName(I-1)
  355.   if string.lower(name) == string.lower(Text) then
  356.     print("Found Entry at Index "..I-1)
  357.     return I-1
  358.    end
  359.   end
  360.   print("Floor Not registered in dialer")
  361.   return 999
  362. end
  363.  
  364. function btn(btnName)
  365.  if btnName == "" then
  366.    return
  367.  end
  368.  
  369.  if menuMode == "portal" then
  370.   portalBtn(btnName)
  371.   return
  372.  end
  373.  
  374.  if menuMode=="menu" then
  375.   menuBtn(btnName)
  376.   return
  377. end
  378.  
  379. if menuMode=="baselist" then
  380.   baseBtn(btnName)
  381.   return
  382. end
  383. end
  384.  
  385. function baseBtn(btnName)
  386. if bases.baseExists(btnName) == true then
  387.  curBase=btnName
  388.  getFloors(btnName)
  389.  showFloorPage(1)
  390.  return
  391. end
  392. print("Base "..btnName.." does not exists.")
  393. end
  394.  
  395. function menuBtn(btnName)
  396. end
  397.  
  398. function portalBtn(btnName)
  399.  local dL = curBase..","..btnName
  400.  local dN = DI(dL)
  401.  if dN < 999 then
  402.    checkstate()
  403.    dial.dialStored(dN)
  404.    sleep(2)
  405.   checkstate()
  406.  end
  407. end
  408.  
  409.  
  410. function checkstate()
  411.  if port.isPortalActive() == true then
  412.   dial.terminate()
  413.  end
  414. end
  415.  
  416. function setButtonText(name,text)
  417.  if buttons[name] ~= nil then
  418.    buttons[name]["text"] = text
  419.  end
  420. end
  421.  
  422.  
  423. function setPanelText(name,text)
  424.  panels[name]["text"] = text
  425. end
  426.  
  427. function setPanelColor(name,color)
  428.   panels[name]["color"] = color
  429. end
  430.  
  431.  
  432.  
  433. function fill(text, color, bData)
  434.   monitor.setBackgroundColor(color)
  435.   local yspot = math.floor((bData.ymin + bData.ymax)/2)
  436.   local xspot = math.floor((bData.xmax - bData.xmin - string.len(text))/2)+1
  437.   for j = bData.ymin, bData.ymax do
  438.     monitor.setCursorPos(bData.xmin, j)
  439.     if j == yspot then
  440.       for k = 0, bData.xmax - bData.xmin - string.len(text)+1 do
  441.         if k == xspot then monitor.write(text)
  442.         else monitor.write(" ")
  443.         end
  444.       end
  445.     else
  446.       for i = bData.xmin, bData.xmax do
  447.         monitor.write(" ")
  448.       end
  449.     end
  450.   end
  451.   monitor.setBackgroundColor(colors.black)
  452. end
  453.  
  454. function temp()
  455. --do not delete
  456. end
  457.  
  458. function checkxy(x, y)
  459.   local retv = false
  460.   local btnText = ""
  461.   local tempFunc = temp  
  462. for name, data in pairs(buttons) do
  463.     if y>=data.ymin and  y <= data.ymax then
  464.       if x>=data.xmin and x<= data.xmax then
  465.          btnText = data.text
  466.          print("Button "..data.text.." was pressed.")
  467.        if data.active == true then
  468.         tempFunc = data.onFunc
  469.        else
  470.          tempFunc = data.offFunc
  471.         end  
  472.         --data.active = not data.active
  473.         retv = true
  474.       end
  475.     end
  476.  end
  477.  if tempFunc ~=temp then
  478.   tempFunc(btnText)
  479.  end
  480.  
  481.   return retv
  482. end
  483.  
  484. function update()
  485.   local currColor
  486.   for name,data in pairs(buttons) do
  487.     local on = data.active
  488.     if on == true then
  489.       currColor = data.oncolor
  490.     else
  491.       currColor = data.offColor
  492.     end
  493.    
  494.       fill(data.text, currColor, data)
  495.     end
  496.  
  497.  for name,data in pairs(panels) do
  498.   fill(data.text, data.color, data)  
  499.  end
  500.  
  501. end
  502.  
  503. function mainLoop()
  504. while true do
  505.  update()
  506.  sleep(1)
  507. end
  508. end
  509.  
  510. function uiLoop()
  511.   while true do
  512.    event,side,x,y=os.pullEvent("monitor_touch")  
  513.    checkxy(x,y)
  514.    sleep(.01)
  515.   end
  516. end
  517.  
  518. Initilize()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement