Guest User

Untitled

a guest
Dec 15th, 2012
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1. rednet.open("bottom")
  2. local config = dofile("elevator.config")
  3. local mainId = nil
  4.  
  5.  
  6. local function handleMain(senderId, message)   
  7.     if message.command == "announce" then
  8.         if message.id == config.id then
  9.             rednet.send(senderId, textutils.serialize{type="elevator", sender = "dispenser", id=config.id, command="ack"})
  10.             mainId = senderId
  11.         end
  12.     elseif message.command == "ack" then
  13.         mainId = senderId
  14.     elseif message.command == "dispense" and message.id == config.id then      
  15.         rs.setOutput("left", true)
  16.         sleep(.2)
  17.         rs.setOutput("left", false)
  18.     elseif message.command == "launch" and message.id == config.id then
  19.         rs.setOutput("top", true)
  20.     elseif message.command == "cancel" then
  21.         rs.setOutput("top", false)
  22.     elseif message.command == "sendCart" and message.id == config.id then
  23.         rs.setOutput("left", true)
  24.         sleep(.2)
  25.         rs.setOutput("left", false)
  26.         sleep(.2)
  27.         rs.setOutput("top", true)  
  28.         sleep(.2)
  29.         rs.setOutput("top", false)
  30.     end
  31. end
  32.  
  33. local function doNetwork()
  34.     print("Starting network...")
  35.     rednet.broadcast(textutils.serialize{type="elevator", sender = "dispenser", id=config.id, command="announce"})
  36.     while true do
  37.         local event, senderId, data, distance = os.pullEvent("rednet_message")
  38.         local message = textutils.unserialize(data)
  39.         if message and message.type == "elevator" then
  40.             if message.sender == "main" then
  41.                 handleMain(senderId, message)          
  42.             end
  43.         end
  44.     end
  45. end
  46.  
  47. local function displayMenu()
  48.     term.clear()
  49.     term.setCursorPos(1,1)
  50.     print(string.format("Floor Id: %s", tostring(mainId)))
  51.     print("Press Q ")
  52. end
  53.  
  54. local function doMenu()
  55.     print("Displaying menu...")
  56.     while true do
  57.         sleep(1)
  58.         displayMenu()
  59.     end
  60. end
  61.  
  62. local function doKeyboard()
  63.     print("Starting keyboard handler...")
  64.     while true do
  65.         local event, key = os.pullEvent()
  66.         if event == "char" then
  67.             if string.lower(key) == "q" then
  68.                 break
  69.             end
  70.         end
  71.     end
  72. end
  73.  
  74.  
  75. local function startup()
  76.     term.clear()
  77.     term.setCursorPos(1,1)
  78.     return parallel.waitForAny(doNetwork, doKeyboard, doMenu)
  79. end
  80.  
  81.  
  82. local rtn, error = pcall(startup)
  83. if not rtn then
  84.     print("Dispenser failed: " .. error)
  85. end
  86. print("Exiting.")
Advertisement
Add Comment
Please, Sign In to add comment