Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- APIs
- os.loadAPI("api/Protect")
- os.loadAPI("api/Logger")
- os.loadAPI("api/Touchpoint")
- -- Globals
- screen_main = "monitor_2"
- screen_logger = "monitor_3"
- sensor = "openperipheral_sensor_1"
- bundled_cable_side = "front"
- bundled_cable_event_functions = {}
- bundled_cable = 0
- monitor = nil
- page1 = nil
- players = {}
- -- Functions
- -- Util
- function activeToText(active)
- if active then
- return "on"
- else
- return "off"
- end
- end
- function setBundledCableColor(side, color, value)
- local bus = rs.getBundledOutput(side)
- if value then
- bus = bit.bor(bus, color)
- else
- bus = bit.band(bus, bit.bnot(color))
- end
- rs.setBundledOutput(side, bus)
- end
- function getBundledCableColor(side, color)
- local bus = rs.getBundledInput(side)
- return bit.band(bus, color) ~= 0
- end
- function toggleBundledCableColor(side, color)
- local bus = rs.getBundledOutput(side)
- bus = bit.bxor(bus, color)
- rs.setBundledOutput(side, bus)
- end
- function savePage(page, file_name)
- local save_table = {}
- for label, data in pairs(page.buttonList) do
- save_table[label] = data.active
- end
- local handle = fs.open(file_name, "w")
- if handle then
- handle.write(textutils.serialize(save_table))
- handle.close()
- end
- end
- function loadPage(page, file_name)
- local handle = fs.open(file_name, "r")
- if handle then
- local load_table = textutils.unserialize(handle.readAll())
- handle.close()
- for label, state in pairs(load_table) do
- local button = page.buttonList[label]
- if button then
- button.active = state
- if button.func then
- button.func(label, state, true)
- end
- end
- end
- end
- end
- -- Handler
- function toggleManOverride(button, state, loading)
- if loading then
- state = not state
- end
- setBundledCableColor(bundled_cable_side, colors.white, not state)
- end
- function updateAKWState(state)
- page1.buttonList["Status"].active = not state
- end
- function toggleMeImport(button, state, loading)
- if loading then
- state = not state
- end
- setBundledCableColor(bundled_cable_side, colors.magenta, state)
- end
- -- Init
- function initTouchpoint()
- monitor = peripheral.wrap(screen_main)
- page1 = Touchpoint.new(screen_main)
- end
- function addButtons()
- page1:add("Atomkraftwerk", nil, 1, 1, 25, 3, colors.black, colors.black)
- page1:add("Status", nil, 2, 4, 9, 6, colors.red, colors.lime)
- page1:add("Man.Override", toggleManOverride, 11, 4, 24, 6, colors.red, colors.orange)
- page1:add("Sonstige", nil, 27, 1, 37, 3, colors.black, colors.black)
- page1:add("ME Import", toggleMeImport, 27, 4, 37, 6, colors.red, colors.lime)
- end
- function loadSettings()
- loadPage(page1, "save/page1.txt")
- end
- function addBundledCableEvents()
- bundled_cable_event_functions = {
- [colors.orange] = { updateAKWState }
- }
- end
- function updateAKW()
- toggleBundledCableColor(bundled_cable_side, colors.white)
- sleep(0.1)
- toggleBundledCableColor(bundled_cable_side, colors.white)
- end
- -- Loops
- function renderLoop()
- while not Protect.isTerminated() do
- page1:draw()
- coroutine.yield()
- end
- end
- function touchScreenLoop()
- local event, button
- while true do
- event = {page1:handleEvents(os.pullEvent("monitor_touch"))}
- if event[1] == "button_click" then
- button = page1.buttonList[event[2]]
- if button.func then
- button.active = button.func(event[2], button.active, false) or (not button.active)
- Logger.message(event[2] .. " turned " .. activeToText(button.active) .. " (" .. table.concat(players, ", ") .. ")", "ok")
- end
- savePage(page1, "save/page1.txt")
- end
- end
- end
- function sensorLoop()
- local sensor_obj = peripheral.wrap(sensor)
- local next_players
- while true do
- next_players = {}
- for _, v in ipairs(sensor_obj.getPlayers()) do
- next_players[#next_players + 1] = v.name
- end
- players = next_players
- sleep(0.1)
- end
- end
- function redstoneEventLoop()
- local first_run = true
- while true do
- local current_bundled_cable = rs.getBundledInput(bundled_cable_side)
- local changes = bit.bxor(current_bundled_cable, bundled_cable)
- for color, functions in pairs(bundled_cable_event_functions) do
- if (bit.band(changes, color) ~= 0) or first_run then
- local new_state = (bit.band(current_bundled_cable, color) ~= 0)
- for _, func in ipairs(functions) do
- func(new_state)
- end
- end
- end
- bundled_cable = current_bundled_cable
- first_run = false
- os.pullEvent("redstone")
- end
- end
- -- Main
- function main()
- Logger.init(screen_logger)
- Logger.log("Initializing Touchscreen", initTouchpoint)
- Logger.log("Adding Buttons", addButtons)
- Logger.log("Loading Settings", loadSettings)
- Logger.log("Adding BundledCable Events", addBundledCableEvents)
- Logger.log("Refreshing AKW redstone cable", updateAKW)
- Logger.message("Starting main loopa", "ok")
- parallel.waitForAny(renderLoop, touchScreenLoop, sensorLoop, redstoneEventLoop)
- end
- -- Code
- Protect.runNotify("TmOnlineMapper95", main)
- Logger.close()
Add Comment
Please, Sign In to add comment