Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("bottom")
- local config = dofile("elevator.config")
- local mainId = nil
- local function handleMain(senderId, message)
- if message.command == "announce" then
- if message.id == config.id then
- rednet.send(senderId, textutils.serialize{type="elevator", sender = "dispenser", id=config.id, command="ack"})
- mainId = senderId
- end
- elseif message.command == "ack" then
- mainId = senderId
- elseif message.command == "dispense" and message.id == config.id then
- rs.setOutput("left", true)
- sleep(.2)
- rs.setOutput("left", false)
- elseif message.command == "launch" and message.id == config.id then
- rs.setOutput("top", true)
- elseif message.command == "cancel" then
- rs.setOutput("top", false)
- elseif message.command == "sendCart" and message.id == config.id then
- rs.setOutput("left", true)
- sleep(.2)
- rs.setOutput("left", false)
- sleep(.2)
- rs.setOutput("top", true)
- sleep(.2)
- rs.setOutput("top", false)
- end
- end
- local function doNetwork()
- print("Starting network...")
- rednet.broadcast(textutils.serialize{type="elevator", sender = "dispenser", id=config.id, command="announce"})
- while true do
- local event, senderId, data, distance = os.pullEvent("rednet_message")
- local message = textutils.unserialize(data)
- if message and message.type == "elevator" then
- if message.sender == "main" then
- handleMain(senderId, message)
- end
- end
- end
- end
- local function displayMenu()
- term.clear()
- term.setCursorPos(1,1)
- print(string.format("Floor Id: %s", tostring(mainId)))
- print("Press Q ")
- end
- local function doMenu()
- print("Displaying menu...")
- while true do
- sleep(1)
- displayMenu()
- end
- end
- local function doKeyboard()
- print("Starting keyboard handler...")
- while true do
- local event, key = os.pullEvent()
- if event == "char" then
- if string.lower(key) == "q" then
- break
- end
- end
- end
- end
- local function startup()
- term.clear()
- term.setCursorPos(1,1)
- return parallel.waitForAny(doNetwork, doKeyboard, doMenu)
- end
- local rtn, error = pcall(startup)
- if not rtn then
- print("Dispenser failed: " .. error)
- end
- print("Exiting.")
Advertisement
Add Comment
Please, Sign In to add comment