Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sSide = "back" -- change this if you want to emit redstone signal on another side
- local sRednetHost = os.getComputerLabel()
- local sProtocol = "rn_redstone"
- local sStateFilePath = "latch_state"
- local fOldPullEvent = os.pullEvent
- os.pullEvent = os.pullEventRaw
- local function detectModem()
- local tPeripherals = {}
- for _,p in pairs(peripheral.getNames()) do
- local sType = peripheral.getType(p)
- if sType == "modem" then
- tPeripherals[#tPeripherals+1] = p
- end
- end
- if #tPeripherals ~= 1 then
- return
- else
- return tPeripherals[1]
- end
- end
- local function listener()
- repeat
- rednet.receive(sProtocol)
- print("Change output")
- redstone.setOutput(sSide, not redstone.getOutput(sSide))
- local tStateFile = fs.open(sStateFilePath, "w")
- local bState = redstone.getOutput(sSide)
- if bState then
- tStateFile.write("t")
- else
- tStateFile.write("f")
- end
- tStateFile.close()
- until false
- end
- local function initialState(sStateFilePath)
- local tStateFile
- if not fs.exists(sStateFilePath) then
- tStateFile = fs.open(sStateFilePath, "w")
- tStateFile.write("f")
- tStateFile.close()
- end
- tStateFile = fs.open(sStateFilePath, "r")
- local sState = tStateFile.readAll()
- tStateFile.close()
- return sState == "t"
- end
- local sModemSide = detectModem()
- if not sModemSide then
- print("Many modems detected, please place only one modem on the computer.")
- return
- end
- local modem = peripheral.wrap(sModemSide)
- if not modem.getNameLocal() then
- print("Configuration error, the modem should be connected.")
- end
- -- set initial state
- local bInitialState = initialState(sStateFilePath)
- redstone.setOutput(sSide, bInitialState)
- -- start rednet listener
- rednet.open(sModemSide)
- rednet.host(sProtocol, sRednetHost)
- term.clear()
- term.setCursorPos(1, 1)
- print("Emitting redstone on the "..sSide)
- print("Hosting "..sProtocol.." for host "..sRednetHost)
- print("Now listening to redstone messages...")
- parallel.waitForAny(
- listener,
- function()
- local event = os.pullEventRaw("terminate")
- os.pullEvent = fOldPullEvent
- rednet.unhost(sProtocol)
- rednet.close(sModemSide)
- end
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement