Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local sides = require("sides")
- local term = require("term")
- local gpu = term.gpu()
- local rs = component.redstone
- -- SETUP
- -- Place the computer down and configure it with a redstone card and an Internet card (to download this program)
- -- Place a "Player Detector" down on the left side of the computer (can be hard wired using a lever if all you want is pulse toggling)
- -- Place a redstone pulse device on the right side of the computer (The signal to toggle the setup). Pulse duration should be less than one second but longer than a few ticks (5 ticks is usually good)
- -- Download and run this program
- -- Interrupt state. Changed when the program is closed using Ctrl+C
- interrupted = false
- -- Global state
- state = false
- online = false
- -- Screen size
- initialWidth, initialHeight = gpu.getViewport()
- -- Write to the centre of the screen
- function writeScreen(text)
- if term.isAvailable() then
- local width, height = gpu.getResolution()
- term.clear()
- gpu.set(width / 2 - #text / 2, height / 2, text)
- else
- print(text)
- end
- end
- -- Initializes the screen
- function initializeScreen()
- print("Initializing screen...")
- -- Act like we are doing something :P
- os.sleep(1)
- if term.isAvailable() then
- term.clear()
- gpu.setResolution(16, 8)
- writeScreen("Standby")
- end
- end
- function interruptRecieved()
- print("Interrupt recieved... Exiting...")
- -- Change the interrupted state value to close the program
- interrupted = true
- return false
- end
- function getState()
- if online == false then
- return false
- end
- if state == false then
- return true
- elseif state == true then
- return false
- end
- end
- -- Listen for interruption event
- event.listen("interrupted", interruptRecieved)
- -- Initialize the screen
- initializeScreen()
- -- Main loop
- while interrupted == false do
- local pulseValue = rs.getInput(sides.east)
- local onlineValue = rs.getInput(sides.west)
- local backValue = rs.getInput(sides.back)
- if onlineValue == 15 then
- online = true
- else
- online = false
- end
- state = getState()
- -- Player is not online and setup is active so we disable it.
- if online == false and backValue > 0 then
- rs.setOutput(sides.back, 0)
- writeScreen("Disabled")
- os.sleep(1)
- else
- if pulseValue == 15 then
- if state == true then
- if backValue < 15 then
- rs.setOutput(sides.back, 15)
- end
- writeScreen("Enabled")
- os.sleep(1)
- elseif state == false then
- if backValue > 0 then
- rs.setOutput(sides.back, 0)
- end
- writeScreen("Disabled")
- os.sleep(1)
- -- If anything funny happened, we want to fail safe and set the output to disabled.
- else
- rs.setOutput(sides.back, 0)
- writeScreen("What happened.")
- os.sleep(1)
- end
- end
- end
- os.sleep(0.1)
- end
- -- Cleanup tasks
- rs.setOutput(sides.back, 0)
- gpu.setResolution(initialWidth, initialHeight)
- term.clear()
- os.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement