Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --//Redstoner - Modem Based Redstone Control
- function wrapOnePeripheral(pType, check)
- local wrap = nil
- local constructor = peripheral.getNames
- if pType == "modem" then
- constructor = redstone.getSides
- end
- for i,v in pairs(constructor()) do
- if peripheral.getType(v) == pType and (check == nil or check(v) == true) then
- wrap = {
- ["wrap"] = peripheral.wrap(v),
- ["side"] = v,
- }
- break
- end
- end
- return wrap
- end
- local modem = wrapOnePeripheral("modem")["wrap"]
- local localChannel = nil
- local filter = nil
- local tArgs = {...}
- if tArgs[1] == nil or type(tonumber(tArgs[1])) ~= "number" or tonumber(tArgs[1]) < 0 or tonumber(tArgs[1]) > 65535 then
- localChannel = math.random(0, 65535)
- else
- localChannel = tonumber(tArgs[1])
- end
- if tArgs[2] ~= nil then
- filter = tArgs[2]
- end
- function unwrapMessage(message, reply)
- message = textutils.unserialize(message)
- if message["filter"] == filter then
- print(message["request"])
- if message["request"] == "set" then
- setOutput(message["side"], message["value"])
- print("Side: "..message["side"])
- print("State: "..tostring(message["value"]))
- elseif message["request"] == "output" then
- getOutput(message["side"], message["type"], reply)
- elseif message["request"] == "input" then
- getInput(message["side"], message["type"], reply)
- end
- end
- end
- function setOutput(side, value)
- local method = nil
- if type(value) == "boolean" then
- redstone.setOutput(side, value)
- elseif type(value) == "number" then
- redstone.setBundledOutput(side, value)
- else
- print("Invalid redstone request attempted")
- end
- end
- function getOutput(side, methodType, reply)
- local value = nil
- if methodType == "bundled" then
- value = redstone.getBundledOutput(side)
- else
- value = redstone.getOutput(side)
- end
- local t = {
- ["redstoneReturn"] = true,
- ["reply"] = value,
- }
- t = textutils.serialize(t)
- modem.transmit(reply, localChannel, t)
- end
- function getInput(side, methodType, reply)
- local value = nil
- if methodType == "bundled" then
- value = redstone.getBundledInput(side)
- else
- value = redstone.getInput(side)
- end
- local t = {
- ["redstoneReturn"] = true,
- ["reply"] = value,
- }
- t = textutils.serialize(t)
- modem.transmit(reply, localChannel, t)
- end
- term.clear()
- term.setCursorPos(1,1)
- print("Local Channel: "..tostring(localChannel))
- print("Filter: "..tostring(filter))
- if modem ~= nil then
- modem.open(localChannel)
- while true do
- local event = {os.pullEvent("modem_message")}
- print("Got a message")
- unwrapMessage(event[5], event[4])
- end
- else
- print("No modem found")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement