Advertisement
Guest User

elev_caller.lua

a guest
Oct 10th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1. local term = require("term")
  2. local event = require("event")
  3. local component = require("component")
  4. local gpu = component.gpu
  5. local modem = component.modem
  6. local button = require("buttonAPI")
  7.  
  8. local ELEV_ADDRESS = "429c5ba9-6dda-4ceb-8f48-4043c06f4ad1"
  9. local ELEV_PORT = 3539
  10.  
  11. local THIS_FLOOR = "Workshop (Ground Level)"
  12. local GREEN = 0x00AA00
  13. local GREYGREEN = 0x335033
  14. local GREY = 0x444444
  15. local BLUE = 0x0000AA
  16.  
  17. modem.open(3538)
  18. modem.setStrength(100)
  19.  
  20. local function eventFilter(id)
  21.   return (id == "touch" or id == "modem_message")
  22. end
  23.  
  24. local function noOpButtonHandler()
  25. end
  26.  
  27. local function callButtonHandler()
  28.   button.flash("CALL ELEVATOR", 0.1)
  29.   modem.send(ELEV_ADDRESS, ELEV_PORT, THIS_FLOOR)
  30. end
  31.  
  32. local function updateDisplay(status, targetFloor)
  33.   term.setCursorBlink(false)
  34.   button.clear()
  35.   button.clearTable()
  36.  
  37.   local minX = 5
  38.   local minY  = 3
  39.   local buttonHeight = 2
  40.   local buttonWidth = 24
  41.  
  42.   if status == "arrived" then
  43.     if targetFloor == THIS_FLOOR then
  44.       button.setBaseColor(GREEN)
  45.       button.setTable("BOARD WHEN READY" , noOpButtonHandler, minX, minX + buttonWidth, minY, minY + buttonHeight)
  46.       button.screen()
  47.       button.heading("Elevator is Here")
  48.     else
  49.       button.setBaseColor(BLUE)
  50.       button.setTable("CALL ELEVATOR" , callButtonHandler, minX, minX + buttonWidth, minY, minY + buttonHeight)
  51.       button.screen()
  52.       button.heading("At floor: ".. targetFloor)
  53.     end
  54.   elseif status == "onroute" then
  55.     if targetFloor == THIS_FLOOR then
  56.       button.setBaseColor(GREYGREEN)
  57.       button.setTable("ON ROUTE" , noOpButtonHandler, minX, minX + buttonWidth, minY, minY + buttonHeight)
  58.       button.screen()
  59.       button.heading("Elevator has been called")
  60.     else
  61.       button.setBaseColor(GREY)
  62.       button.setTable("ELEVATOR IN MOTION" , noOpButtonHandler, minX, minX + buttonWidth, minY, minY + buttonHeight)
  63.       button.screen()
  64.       button.heading("Moving to: "..targetFloor)
  65.     end
  66.   end
  67.  
  68. end
  69.  
  70. local function getClickOrUpdate()
  71.   local e, _, x, y, _, status, targetFloor = event.pullFiltered(1,eventFilter)  
  72.   if e == "touch" then
  73.     if x == nil or y == nil then
  74.       local h, w = gpu.getResolution()
  75.       gpu.set(h,w,".")
  76.       gpu.set(h,w," ")
  77.     else
  78.       button.checkxy(x,y)
  79.     end
  80.   elseif e == "modem_message" then
  81.     updateDisplay(status, targetFloor)
  82.   end
  83. end
  84.  
  85.  
  86.  
  87. function mainTouch()
  88.   gpu.setResolution(34, 7)
  89.  
  90.   updateDisplay("arrived", "???")  
  91.  
  92.   while(true)  do
  93.     getClickOrUpdate()
  94.   end
  95.  
  96. end
  97.  
  98. mainTouch()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement