Advertisement
Column01

Open Computers redstone signal toggle latch

May 26th, 2020
1,427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local sides = require("sides")
  4. local term = require("term")
  5.  
  6. local gpu = term.gpu()
  7. local rs = component.redstone
  8.  
  9. -- SETUP
  10. -- Place the computer down and configure it with a redstone card and an Internet card (to download this program)
  11. -- 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)
  12. -- 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)
  13. -- Download and run this program
  14.  
  15. -- Interrupt state. Changed when the program is closed using Ctrl+C
  16. interrupted = false
  17.  
  18. -- Global state
  19. state = false
  20. online = false
  21.  
  22. -- Screen size
  23. initialWidth, initialHeight = gpu.getViewport()
  24.  
  25. -- Write to the centre of the screen
  26. function writeScreen(text)
  27.     if term.isAvailable() then
  28.         local width, height = gpu.getResolution()
  29.         term.clear()
  30.         gpu.set(width / 2 - #text / 2, height / 2, text)
  31.     else
  32.         print(text)
  33.     end
  34. end
  35.  
  36. -- Initializes the screen
  37. function initializeScreen()
  38.     print("Initializing screen...")
  39.     -- Act like we are doing something :P
  40.     os.sleep(1)
  41.     if term.isAvailable() then
  42.         term.clear()
  43.         gpu.setResolution(16, 8)
  44.         writeScreen("Standby")
  45.     end
  46. end
  47.  
  48. function interruptRecieved()
  49.     print("Interrupt recieved... Exiting...")
  50.     -- Change the interrupted state value to close the program
  51.     interrupted = true
  52.     return false
  53. end
  54.  
  55. function getState()
  56.     if online == false then
  57.         return false
  58.     end
  59.     if state == false then
  60.         return true
  61.     elseif state == true then
  62.         return false
  63.     end
  64. end
  65.  
  66. -- Listen for interruption event
  67. event.listen("interrupted", interruptRecieved)
  68.  
  69. -- Initialize the screen
  70. initializeScreen()
  71.  
  72. -- Main loop
  73. while interrupted == false do
  74.     local pulseValue = rs.getInput(sides.east)
  75.     local onlineValue = rs.getInput(sides.west)
  76.     local backValue = rs.getInput(sides.back)
  77.     if onlineValue == 15 then
  78.         online = true
  79.     else
  80.         online = false
  81.     end
  82.     state = getState()
  83.     -- Player is not online and setup is active so we disable it.
  84.     if online == false and backValue > 0 then
  85.         rs.setOutput(sides.back, 0)
  86.         writeScreen("Disabled")
  87.         os.sleep(1)
  88.     else
  89.         if pulseValue == 15 then
  90.             if state == true then
  91.                 if backValue < 15 then
  92.                     rs.setOutput(sides.back, 15)
  93.                 end
  94.                     writeScreen("Enabled")
  95.                     os.sleep(1)
  96.             elseif state == false then
  97.                 if backValue > 0 then
  98.                     rs.setOutput(sides.back, 0)
  99.                 end
  100.                 writeScreen("Disabled")
  101.                 os.sleep(1)
  102.             -- If anything funny happened, we want to fail safe and set the output to disabled.
  103.             else
  104.                 rs.setOutput(sides.back, 0)
  105.                 writeScreen("What happened.")
  106.                 os.sleep(1)
  107.             end
  108.         end
  109.     end
  110.     os.sleep(0.1)
  111. end
  112.  
  113. -- Cleanup tasks
  114. rs.setOutput(sides.back, 0)
  115. gpu.setResolution(initialWidth, initialHeight)
  116. term.clear()
  117. os.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement