IronicPickle

powerControlSlave.lua

Feb 27th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 KB | None | 0 0
  1. -- Args
  2. local args = { ... }
  3. local energyType = args[1] or "RF"
  4. local redstoneOutput = args[2] or "bottom"
  5. local channel = tonumber(args[3]) or 50
  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 render = require("/lua/lib/render")
  13. local state = require("/lua/lib/stateHandler")
  14.  
  15. -- Peripherals
  16. local wrappedPers = setup.getPers({
  17.     "monitor", "modem"
  18. })
  19. local monitor = setup.setupMonitor(
  20.     wrappedPers.monitor[1], 0.5
  21. )
  22. local modem = wrappedPers.modem[1]
  23. local speaker = peripheral.find("speaker")
  24.  
  25. -- Setup
  26. local stateData = state.getState("powerControlSlave")
  27. local defaultData = {
  28.     state = "on", energyType = energyType,
  29.     auto = "off", capData = {}
  30. }
  31. local slaveData = stateData or defaultData
  32. local buttons = {}
  33.  
  34. -- Windows
  35. local winHeader = setup.setupWindow(
  36.     monitor, 1, 1, monitor.x, 6
  37. )
  38. local winMain = setup.setupWindow(
  39.     monitor, 1, 7, monitor.x, (monitor.y - (6 + 4))
  40. )
  41. local winMainControl = setup.setupWindow(
  42.     winMain, (winMain.x - 13), 1,
  43.     14, winMain.y
  44. )
  45. local winFooter = setup.setupWindow(
  46.     monitor, 1, (monitor.y - 3), monitor.x, 4
  47. )
  48.  
  49. -- Actions
  50. local actions = {}
  51. actions.on = function()
  52.     rs.setAnalogOutput(redstoneOutput, 15)
  53.     if(speaker) then
  54.         speaker.playSound(
  55.             "minecraft:block.lever.click",
  56.             1, 1
  57.         )
  58.     end
  59.     slaveData.state = "on"
  60.     drawHeader()
  61.     drawFooter()
  62.     updateRender()
  63. end
  64. actions.off = function()
  65.     rs.setAnalogOutput(redstoneOutput, 0)
  66.     if(speaker) then
  67.         speaker.playSound(
  68.             "minecraft:block.lever.click",
  69.             1, 0.8
  70.         )
  71.     end
  72.     slaveData.state = "off"
  73.     drawHeader()
  74.     drawFooter()
  75.     updateRender()
  76. end
  77. actions.toggle = function()
  78.     if(slaveData.state == "off") then
  79.         actions.on()
  80.     else
  81.         actions.off()
  82.     end
  83. end
  84. actions.toggleAuto = function()
  85.     if(slaveData.auto == "off") then
  86.         slaveData.auto = "on"
  87.     else
  88.         slaveData.auto = "off"
  89.     end
  90.     updateRender()
  91. end
  92.  
  93. -- Main
  94. function start()
  95.     print("# Program Started")
  96.     modem.open(channel)
  97.    
  98.     drawHeader()
  99.     drawFooter()
  100.     drawMain()
  101.     updateRender()
  102.    
  103.     await()
  104. end
  105.  
  106. function await()
  107.     while(true) do
  108.    
  109.         local event, p1, p2, p3, p4, p5 = os.pullEvent()
  110.        
  111.         local isTouch = (event == "monitor_touch")
  112.        
  113.         local isModemMessage = (event == "modem_message")
  114.        
  115.         if(isTouch) then
  116.             local x = p2 - (winMain.x - winMainControl.x) - 1
  117.             local y = p3 - winHeader.y
  118.             if(x > 0) then
  119.                 for i, button in pairs(buttons) do
  120.                     if(y == button.y) then
  121.                         actions[button.action]()
  122.                         modem.transmit(channel, channel,
  123.                             {type = "updatePending"}
  124.                         )
  125.                         updateState()
  126.                     end
  127.                 end
  128.             end
  129.         elseif(isModemMessage) then
  130.             local body = p4
  131.             if(body.type == "modify") then
  132.                 local isSelf = (body.slaveNum == slaveNum)
  133.                 if(isSelf) then
  134.                     local change = body.change
  135.                     actions[change.action](change)
  136.                     modem.transmit(channel, channel,
  137.                         {type = "updatePending"}
  138.                     )
  139.                     updateState()
  140.                 end
  141.             elseif(body.type == "poll") then
  142.                 modem.transmit(channel, channel, {
  143.                     type = "pollRes",
  144.                     slaveData = slaveData
  145.                 })
  146.             end
  147.         end
  148.     end
  149. end
  150.  
  151. function updateRender()
  152.     buttons = render.power({{
  153.         output = winMain,
  154.         controlOutput = winMainControl,
  155.         data = slaveData
  156.     }})
  157. end
  158.  
  159. function updateState()
  160.     state.updateState("powerControlSlave", slaveData)
  161. end
  162.  
  163. function drawHeader()
  164.     winHeader.bg = colors.orange
  165.     winHeader.setBackgroundColor(winHeader.bg)
  166.    
  167.     drawBox(winHeader,
  168.         1, 1, winHeader.x, winHeader.y,
  169.         true
  170.     )
  171.     drawBox(winHeader,
  172.         1, winHeader.y, winHeader.x, winHeader.y,
  173.         true, colors.white
  174.     )
  175.    
  176.     write(winHeader,
  177.         "Power Control",
  178.         0, 2, "centre"
  179.     )
  180.     write(winHeader,
  181.         energyType .. " Power Unit",
  182.         0, 4, "centre"
  183.     )
  184. end
  185.  
  186. function drawFooter()
  187.     winFooter.bg = colors.orange
  188.     winFooter.setBackgroundColor(winFooter.bg)
  189.    
  190.     drawBox(winFooter,
  191.         1, 1, winFooter.x, winFooter.y,
  192.         true
  193.     )
  194.     drawBox(winFooter,
  195.         1, 1, winFooter.x, 1,
  196.         true, colors.white
  197.     )
  198. end
  199.  
  200. function drawMain()
  201.     winMain.bg = colors.yellow
  202.     winMain.setBackgroundColor(winMain.bg)
  203.    
  204.     drawBox(winMain,
  205.         1, 1, winMain.x, winMain.y,
  206.         true
  207.     )
  208. end
  209.  
  210. setup.utilsWrapper(start, modem, channel)
Add Comment
Please, Sign In to add comment