The slave program is meant to be run on a normal computer with a wireless modem attached. It can be called with the api, and will also generate a spoof event. This means the program will generate events that happened to the slave on the master program. WARNING: This has not been tested with other programs that overwrite os.pullEventRaw! It takes 3 command line arguments:
wpslave <channela> <channelb> <modem_side>
The api is meant to be run on a (wireless) pocket computer, but it will also run just fine on a computer (if they wireless modem is placed on the back). It contains just two functions:
<variable> = wp.wrap(<side> <channela> <channelb>)
wp.setModem(<side>) --#note that by default, the modem is set on the back. I have included this override if you wish to place the modem on a different side.
Note that if you want, you can wrap a peripheral on multiple slaves. For instance, you could have a computer for your door and a computer for your monitor, and be handling both with a third computer, or a (wireless) pocket computer.
wp
local modem = peripheral.wrap("back")
local oldos = {}
for k, v in pairs(os) do
oldos[k] = v
end
function os.pullEventRaw(...)
local args = {...}
local event = {oldos.pullEventRaw(unpack(args))}
if event[1] == "modem_message" and type(event[5]) == "table" and event[5][1] == "wpe" then
return unpack(event[5][2])
else
return unpack(event)
end
end
function setModem(side)
modem = peripheral.wrap(side)
end
function wrap(side, channela, channelb)
local per, call = {
side = side,
channela = channela,
channelb = channelb,
}, {}
modem.transmit(channela, channelb, side)
modem.open(channela)
msg = {os.pullEvent("modem_message")}
for k, v in pairs(msg[5]) do
call[v] = function(...)
modem.transmit(channela, channelb, {side = side, call = v, params = {...}})
return(unpack({os.pullEvent("modem_message")})[5])
end
end
setmetatable(per, {__index = call})
return per
end
wpslave
local args = {...}
if #args < 3 then
error("Incomplete arguments", 2)
elseif #args > 3 then
error("Too many arguments", 2)
end
assert(tonumber(args[1]), args[1].." isn't a number!")
assert(tonumber(args[2]), args[2].." isn't a number!")
args[1] = tonumber(args[1])
args[2] = tonumber(args[2])
local valid = false
for i = 1, 6 do
if args[3] == (rs.getSides())[i] then
valid = true
end
end
if not valid then
error("Side is not valid")
end
local modem = peripheral.wrap(args[3])
modem.open(args[2])
local function reply(info)
modem.transmit(args[1], args[2], info)
end
while true do
event = {os.pullEvent()}
if event[1] == "modem_message" and type(event[5]) == "table" then
local response = {peripheral.call(event[5].side, event[5].call, unpack(event[5].params))}
reply(response)
print("peripheral response sent")
elseif event[1] == "modem_message" and type(event[5]) == "string" then
local pm = peripheral.getMethods(event[5])
reply(pm)
print("peripheral table sent")
else
reply({"wpe", event})
print("peripheral event sent")
end
end
pastebin get TgHFHwkH slave
pastebin get eQkJhzG2 wp
Note: if you attempt to catch a peripheral event such as monitor_touch, the program will never return a value. If anyone knows how I could fix this, I would appreciate the help. Instead, use something similar to this code:
local event
while true do
event = {os.pullEvent()}
if event[1] == "monitor_touch" then
break
end
end