Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Args
- local args = { ... }
- local pollDelay = tonumber(args[1]) or 30
- local channel = tonumber(args[2]) or 40
- -- Libraries
- local setup = require("/lua/lib/setupUtils")
- local monUtils = require("/lua/lib/monitorUtils")
- local write = monUtils.write
- local drawBox = monUtils.drawBox
- local render = require("/lua/lib/render")
- -- Peripherals
- local wrappedPers = setup.getPers({
- "monitor", "modem"
- })
- local monitor = setup.setupMonitor(
- wrappedPers.monitor[1], 0.5
- )
- local modem = wrappedPers.modem[1]
- local speaker = peripheral.find("speaker")
- -- Setup
- local data = {}
- local fullData = {}
- local buttons = {}
- local lastUpdated = 0
- -- Windows
- local winHeader = setup.setupWindow(
- monitor, 1, 1, monitor.x, 5
- )
- local winMain = setup.setupWindow(
- monitor, 1, 6, monitor.x, (monitor.y - (5 + 5))
- )
- local windows = render.setupWindows(winMain)
- local winFooter = setup.setupWindow(
- monitor, 1, (monitor.y - 4), monitor.x, 5
- )
- -- Main
- function start()
- print("# Program Started")
- modem.open(channel)
- drawHeader()
- drawMain()
- updateRender()
- parallel.waitForAny(await, drawFooter,
- function()
- while(true) do
- poll()
- sleep(pollDelay)
- end
- end
- )
- end
- function await()
- while(true) do
- local event, p1, p2, p3, p4, p5 = os.pullEvent()
- local isTouch = (event == "monitor_touch")
- local isModemMessage = (event == "modem_message")
- if(isTouch) then
- local x = p2
- local y = p3 - winHeader.y
- for i, slave in pairs(fullData) do
- local window = slave.output
- local controlWindow = slave.controlOutput
- local slaveData = slave.data
- local relX = x - (window.posX + controlWindow.posX)
- local relY = y - (window.posY + controlWindow.posY) + 2
- if(relX >= 0 and relX <= 12) then
- for i, button in pairs(buttons) do
- if(relY == button.y) then
- if(speaker) then
- speaker.playSound(
- "minecraft:block.lever.click",
- 1, 0.8
- )
- end
- modifySlave(slaveData.num,
- {action = button.action}
- )
- end
- end
- end
- end
- elseif(isModemMessage) then
- local body = p4
- if(body.type == "pollRes") then
- local slaveData = body.slaveData
- local index = #data + 1
- table.insert(data, slaveData)
- table.sort(data,
- function(a, b)
- return a.num < b.num
- end
- )
- updateRender()
- elseif(body.type == "updatePending") then
- poll()
- end
- end
- end
- end
- function modifySlave(num, changeData)
- modem.transmit(channel, channel,
- {
- type = "modify",
- slaveNum = num,
- change = changeData
- }
- )
- end
- function poll()
- data = {}
- lastUpdated = 0
- write(winFooter,
- "Loading...", 2, 3, "right"
- )
- modem.transmit(channel, channel,
- {
- type = "poll"
- }
- )
- end
- function updateRender()
- fullData = {}
- for i, slaveData in pairs(data) do
- table.insert(fullData, {
- data = slaveData,
- output = windows[i].window,
- controlOutput = windows[i].controlWindow
- })
- end
- winMain.clear()
- drawHeader()
- buttons = render.ender(fullData)
- end
- function drawHeader()
- winHeader.bg = colors.blue
- winHeader.setBackgroundColor(winHeader.bg)
- drawBox(winHeader,
- 1, 1, winHeader.x, winHeader.y,
- true
- )
- write(winHeader,
- "Endergenic Resonation Monitor",
- 0, 2, "centre"
- )
- write(winHeader,
- "Monitoring " .. #data .. " units",
- 0, 4, "centre"
- )
- end
- function drawFooter()
- winFooter.bg = colors.blue
- winFooter.setBackgroundColor(winFooter.bg)
- drawBox(winFooter,
- 1, 1, winFooter.x, winFooter.y,
- true
- )
- while(true) do
- winFooter.clear()
- drawBox(winFooter,
- 1, 1, winFooter.x, 1,
- true, colors.white
- )
- local msg = "Last Updated: "
- if(lastUpdated == 0) then
- msg = msg .. "Just now"
- else
- msg = msg .. lastUpdated .. " seconds ago"
- end
- write(winFooter,
- msg, 0, 3, "centre"
- )
- lastUpdated = lastUpdated + 1
- sleep(1)
- end
- end
- function drawMain()
- winMain.bg = colors.white
- winMain.setBackgroundColor(winMain.bg)
- drawBox(winMain,
- 1, 1, winMain.x, winMain.y,
- true
- )
- for i, window in pairs(windows) do
- windows[i].window.bg = colors.purple
- windows[i].window.setBackgroundColor(
- colors.purple
- )
- local window = windows[i].window
- drawBox(window,
- 1, 1, window.x, window.y,
- true
- )
- end
- end
- setup.utilsWrapper(start, modem, channel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement