Files
airship-autopilot/files/cockpit/link.lua

34 lines
1.2 KiB
Lua
Raw Normal View History

-- link.lua -- lettvekts rednet-lenke mellom autopiloten (maskinrom) og piloten (pilot-hus).
-- Modem-agnostisk: virker over wired (nettverkskabel) ELLER wireless/ender modem. Graceful:
-- hvis ingen modem finnes, blir publish/send no-ops (autopiloten kjorer fint solo).
local M = {}
M.STATUS = "airship_status" -- autopilot -> pilot (status-kringkasting)
M.CMD = "airship_cmd" -- pilot -> autopilot (kommandoer)
local opened = false
-- aapne rednet paa ALLE tilkoblede modem (wired og/eller wireless)
function M.open()
for _, name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == "modem" and not rednet.isOpen(name) then
rednet.open(name); opened = true
end
end
-- ogsaa hvis et modem allerede var aapent
for _, name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == "modem" and rednet.isOpen(name) then opened = true end
end
return opened
end
function M.isOpen() return opened end
-- kringkast status (kalles hver tick av autopiloten)
function M.publishStatus(t) if opened then rednet.broadcast(t, M.STATUS) end end
-- send en kommando-tabell (kalles av piloten ved knappetrykk)
function M.sendCommand(t) if opened then rednet.broadcast(t, M.CMD) end end
return M