Advertisement
IronicPickle

toggle.lua

Feb 27th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.41 KB | None | 0 0
  1. -- Args
  2. local args = { ... }
  3. local redstoneOutput = args[1] or "right"
  4. local name = args[2] or "Unnamed"
  5. local channel = tonumber(args[3]) or 30
  6.  
  7. -- Libraries
  8. local setup = require("/lua/lib/setupUtils")
  9. local monUtils = require("/lua/lib/monitorUtils")
  10. local write = monUtils.write
  11. local drawBox = monUtils.drawBox
  12. local stateHandler = require("/lua/lib/stateHandler")
  13.  
  14. -- Peripherals
  15. local wrappedPers = setup.getPers({
  16.     "monitor", "modem"
  17. })
  18. local monitor = setup.setupMonitor(
  19.     wrappedPers.monitor[1], 0.5
  20. )
  21. local modem = wrappedPers.modem[1]
  22. local speaker = peripheral.find("speaker")
  23.  
  24. -- Setup
  25. local stateData = stateHandler.getState("toggle")
  26. local defaultData = "off"
  27. local state = stateData or defaultData
  28.  
  29. -- Windows
  30. local winHeader = setup.setupWindow(
  31.     monitor, 1, 1, monitor.x, 4
  32. )
  33. local winFooter = setup.setupWindow(
  34.     monitor, 1, 5, monitor.x, (monitor.y - 4)
  35. )
  36.  
  37. -- Main
  38. function start()
  39.     print("# Program Started")
  40.     modem.open(channel)
  41.    
  42.     drawHeader()
  43.     drawFooter()
  44.    
  45.     await()
  46. end
  47.  
  48. function await()
  49.     while(true) do
  50.         local event, p1, p2, p3, p4, p5 = os.pullEvent()
  51.        
  52.         local isTouch = (event == "monitor_touch")
  53.        
  54.         local isModemMessage = (event == "modem_message")
  55.        
  56.         if(isTouch) then
  57.             modem.transmit(channel, channel,
  58.                 { type = state }
  59.             )
  60.             if(state == "on") then
  61.                 off()
  62.             elseif(state == "off") then
  63.                 on()
  64.             end
  65.         elseif(isModemMessage) then
  66.             local body = p4
  67.             if(body.type == "on") then
  68.                 off()
  69.             elseif(body.type == "off") then
  70.                 on()
  71.             end
  72.         end
  73.     end
  74. end
  75.  
  76. function on()
  77.     rs.setAnalogOutput(redstoneOutput, 15)
  78.     if(speaker) then
  79.         speaker.playSound(
  80.             "minecraft:block.lever.click",
  81.             1, 1
  82.         )
  83.     end
  84.     state = "on"
  85.     updateState()
  86.     drawHeader()
  87.     drawFooter()
  88. end
  89.  
  90. function off()
  91.     rs.setAnalogOutput(redstoneOutput, 0)
  92.     if(speaker) then
  93.         speaker.playSound(
  94.             "minecraft:block.lever.click",
  95.             1, 0.8
  96.         )
  97.     end
  98.     state = "off"
  99.     updateState()
  100.     drawHeader()
  101.     drawFooter()
  102. end
  103.  
  104. function updateState()
  105.     stateHandler.updateState("toggle", state)
  106. end
  107.  
  108. function drawHeader()
  109.     local bgColors = {
  110.         on = colors.green,
  111.         off = colors.red
  112.     }
  113.     winHeader.bg = bgColors[state]
  114.     winHeader.setBackgroundColor(winHeader.bg)
  115.    
  116.     drawBox(winHeader,
  117.         1, 1, winHeader.x, winHeader.y,
  118.         true
  119.     )
  120.     drawBox(winHeader,
  121.         1, winHeader.y, winHeader.x, winHeader.y,
  122.         true, colors.white
  123.     )
  124.    
  125.     write(winHeader, name, 0, 2, "centre")
  126. end
  127.  
  128. function drawFooter()
  129.     local bgColors = {
  130.         on = colors.green,
  131.         off = colors.red
  132.     }
  133.     winFooter.bg = bgColors[state]
  134.     winFooter.setBackgroundColor(winFooter.bg)
  135.    
  136.     drawBox(winFooter,
  137.         1, 1, winFooter.x, winFooter.y,
  138.         true
  139.     )
  140.    
  141.     local msgs = {
  142.         on = "Enabled",
  143.         off = "Disabled"
  144.     }
  145.    
  146.     write(winFooter,
  147.         msgs[state],
  148.         0, 2, "centre"
  149.     )
  150.    
  151.     write(winFooter,
  152.         "Click to Toggle",
  153.         0, (winFooter.y - 1), "centre"
  154.     )
  155. end
  156.  
  157. setup.utilsWrapper(start, modem, channel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement