Advertisement
Guest User

elev_control.lua

a guest
Oct 10th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.53 KB | None | 0 0
  1. local io = require("io")
  2. local term = require("term")
  3. local event = require("event")
  4. local component = require("component")
  5. local gpu = component.gpu
  6. local modem = component.modem
  7. local note = require("note")
  8. local carriage = component.carriage
  9. local button = require("buttonAPI")
  10.  
  11. local touch = true
  12.  
  13. local CALLER_PORT = 3538
  14.  
  15. modem.open(3539)
  16. modem.setStrength(100)
  17.  
  18. floors = {}
  19. numFloors = 3
  20.  
  21. floors[1] = {label="Library", y=145}
  22. floors[2] = {label="Workshop (Ground Level)", y=137}  
  23. floors[3] = {label="Industrial Level", y=127}
  24.  
  25. local function persistYLevel()
  26.   local file = io.open("currY", "w")
  27.   file:write(currentYLevel)
  28.   file:close()
  29. end
  30.  
  31. local function loadYLevel()
  32.   local file = io.open("currY", "r")
  33.   local prevY = nil  
  34.   if file then
  35.     prevY = file:read()
  36.     if prevY ~= nil then
  37.       prevY = tonumber(prevY)
  38.     end
  39.   end
  40.   return prevY  
  41. end
  42.  
  43. local function notifyCallers(status, targetFloor)
  44.   modem.broadcast(CALLER_PORT, status, targetFloor)
  45. end
  46.  
  47. local function move(targetY, direction)
  48. -- 0 is down, 1 is up
  49.   local errMsg = nil
  50.   local done = (targetY == currentYLevel)
  51.   while(not done) do
  52.   carriage.move(direction, false, false)
  53.     os.sleep(.1)
  54.     local energy, wasSuccess, reason, x, y, z = carriage.status()
  55.     if reason then
  56.       errMsg = reason
  57.       if x then
  58.         errMsg = errMsg.."\nx : "..x
  59.         errMsg = errMsg.."\ny : "..y
  60.         errMsg = errMsg.."\nz : "..z
  61.       end
  62.       break  
  63.     end
  64.     if (direction == 0) then
  65.       currentYLevel = currentYLevel - 1
  66.       done = (currentYLevel <= targetY)
  67.     else
  68.       currentYLevel = currentYLevel + 1
  69.       done = (currentYLevel >= targetY)
  70.     end
  71.   end
  72.   persistYLevel()
  73.   return errMsg
  74. end
  75.  
  76. local function moveUp(target)
  77.   return move(target, 1)
  78. end
  79.  
  80. local function moveDown(target)
  81.   return move(target, 0)
  82. end
  83.  
  84. local function goToY(yLevel)
  85.   if yLevel > currentYLevel then
  86.     return moveUp(yLevel)
  87.   else
  88.     if yLevel < currentYLevel then
  89.       return moveDown(yLevel)
  90.     end
  91.   end
  92. end
  93.  
  94. local function goToFloor(floorNum)
  95.   return goToY(floors[floorNum].y)  
  96. end
  97.  
  98. local function goToFloorByName(name)
  99.   for k,v in pairs(floors) do
  100.     if  (v.label == name) then
  101.       notifyCallers("onroute", name)
  102.       local errMsg =  goToY(v.y)  
  103.       if not errMsg then
  104.         notifyCallers("arrived", name)
  105.       else
  106.         notifyCallers("arrived", "???")  
  107.       end
  108.       return errMsg
  109.     end
  110.   end
  111.   return nil
  112. end
  113.  
  114. local function buttonHighlight(name)
  115.   for k,v in pairs(floors) do
  116.     if v.label == name then
  117.       button.setActive(v.label, true)
  118.     else
  119.      button.setActive(v.label, false)
  120.     end
  121.   end
  122. end
  123.  
  124. local function init()
  125.  
  126.   currentYLevel = loadYLevel()
  127.  
  128.   term.clear()
  129.  
  130.   if (currentYLevel == nil) then
  131.     print("Unable to determine current Y level.")
  132.     while(true) do
  133.       io.write("Please provide correct value: ")
  134.       local userYVal = io.read()
  135.       userYVal = tonumber(userYVal)
  136.       if userYVal == nil then
  137.         print("\nNot a valid height value.")
  138.       else
  139.         currentYLevel = userYVal
  140.         break
  141.       end
  142.     end
  143.   end
  144.  
  145.   local currFloor = nil
  146.   for k,v in pairs(floors) do
  147.     if v.y == currentYLevel then
  148.       currFloor = k
  149.     end
  150.   end
  151.  
  152.   if currFloor == nil then
  153.     os.sleep(1)
  154.     notifyCallers("arrived", "???")
  155.     if not touch then
  156.       print("Elevator starting between floors, at height " .. currentYLevel)
  157.     end
  158.   else
  159.     if not touch then
  160.       print("Elevator starting on floor: "..floors[currFloor].label)
  161.     else
  162.       buttonHighlight(floors[currFloor].label)
  163.     end
  164.     os.sleep(1)
  165.     notifyCallers("arrived", floors[currFloor].label)
  166.   end
  167. end
  168.  
  169. local function listAvailableFloors()
  170.  
  171.   for k,v in pairs(floors) do
  172.     io.write("\n"..k..") ".. v.label)
  173.     if v.y == currentYLevel then
  174.       io.write(" {*}")
  175.     end
  176.   end
  177.  
  178. end
  179.  
  180. local function mainCLI()
  181.  
  182.   init()
  183.  
  184.   while(true) do
  185.     print("\n Available Floors: ")
  186.     listAvailableFloors()
  187.  
  188.     local selection = nil
  189.     while(true) do
  190.       io.write("\n\nEnter desired floor number: ")
  191.       selection = io.read()
  192.       selection = tonumber(selection)
  193.       if not selection or (selection < 1) or (selection > numFloors) then
  194.         print("\n Invalid floor.")
  195.       else
  196.         break
  197.       end
  198.     end
  199.  
  200.     term.clear()
  201.     local err = goToFloor(selection)
  202.     if err then
  203.       print(err)
  204.     end
  205.  
  206.   end
  207.  
  208. end
  209.  
  210. local function eventFilter(id)
  211.   return (id == "touch" or id == "modem_message")  
  212. end
  213.  
  214.  
  215. local function buttonHandler(name)
  216.   buttonHighlight(name)
  217.   os.sleep(1)
  218.   local err = goToFloorByName(name)
  219.   if(err) then
  220.     button.label(1,34,err)
  221.   else
  222.     note.play(72, 0.3)
  223.     note.play(67, 0.3)
  224.   end
  225. end
  226.  
  227. local function getClickOrUpdate()
  228.   local e, _, x, y, _, floorName = event.pullFiltered(1,eventFilter)  
  229.   if (e == "touch") then
  230.     if x == nil or y == nil then
  231.       local h, w = gpu.getResolution()
  232.       gpu.set(h,w,".")
  233.       gpu.set(h,w," ")
  234.     else
  235.       button.checkxy(x,y)
  236.     end
  237.   elseif (e == "modem_message") then
  238.     buttonHandler(floorName)
  239.   end
  240. end
  241.  
  242. local function displayAvailableFloors()
  243.   term.setCursorBlink(false)
  244.   button.clear()
  245.  
  246.   local minX = 8
  247.   local minY  = 3
  248.   local buttonHeight = 2
  249.   local buttonWidth = 24
  250.   local buttonSpacing = 2
  251.  
  252.   for k,v in pairs(floors) do
  253.     button.setTable(v.label, buttonHandler, minX, minX + buttonWidth, minY, minY + buttonHeight)
  254.     minY = minY + buttonHeight+ buttonSpacing
  255.   end
  256.  
  257.   button.screen()
  258.  
  259. end
  260.  
  261. function mainTouch()
  262.   gpu.setResolution(40, 40)
  263.  
  264.   displayAvailableFloors()
  265.  
  266.   init()
  267.  
  268.   button.heading("Please Choose A Floor:")
  269.  
  270.   while(true)  do
  271.     getClickOrUpdate()
  272.   end
  273.  
  274. end
  275.  
  276. if touch then
  277.   mainTouch()
  278. else
  279.   mainCLI()
  280. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement