Skip to content

Instantly share code, notes, and snippets.

@Forecaster
Created May 7, 2020 19:53
Show Gist options
  • Save Forecaster/2dfee9f4d31ee4f2d76a03988f816a1a to your computer and use it in GitHub Desktop.
Save Forecaster/2dfee9f4d31ee4f2d76a03988f816a1a to your computer and use it in GitHub Desktop.
broadcast-to-linked-card-relay for microcontroller
--Uses line-separated key=value pairs for configuration in the eeprom data section
--Available settings: `inputOnly`:boolean, `outputOnly`:boolean, ports:comma-separated-list
local function splitter(str, sep)
if sep == nil then sep = "%s" end
local t={}
for string in string.gmatch(str, "([^"..sep.."]+)") do table.insert(t, string) end
return t
end
local tunnel = component.list("tunnel")()
if tunnel then
tunnel = component.proxy(tunnel)
else
error("No tunnel component found. Install linked card.")
end
local modem = component.list("modem")()
if modem then
modem = component.proxy(modem)
else
error("No modem component found. Install network card.")
end
local redstone = component.list("redstone")()
if redstone then
redstone = component.proxy(redstone)
end
local settings_str = component.proxy(component.list("eeprom")()).getData()
local settings = {}
if settings_str then
for _,v in ipairs(splitter(settings_str, "\n")) do
local set = splitter(v, "=")
settings[set[1]] = set[2]
end
end
if settings.ports then
for _,port in ipairs(splitter(settings.ports,",")) do
modem.open(tonumber(port))
end
end
computer.beep("..")
while true do
local signal = {computer.pullSignal(1)}
if signal[1] == "modem_message" then
local event, receiver, sender, port, distance = table.unpack(signal)
local data = {select(6, table.unpack(signal))}
if not settings.outputOnly and port ~= 0 then
tunnel.send(port, table.unpack(data))
elseif not settings.inputOnly then
modem.broadcast(data[1], select(2, table.unpack(data)))
else
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment