2022-03-10 19:09:21 +00:00
|
|
|
-- #REQUIRES log.lua
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Protected Peripheral Manager
|
|
|
|
--
|
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
local self = {
|
|
|
|
mounts = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
-- wrap peripheral calls with lua protected call
|
|
|
|
-- ex. reason: we don't want a disconnect to crash the program before a SCRAM
|
|
|
|
local peri_init = function (device)
|
|
|
|
for key, func in pairs(device) do
|
|
|
|
device[key] = function (...)
|
|
|
|
local status, result = pcall(func, ...)
|
|
|
|
|
|
|
|
if status then
|
2022-03-10 19:21:03 +00:00
|
|
|
-- assume nil is only for functions with no return, so return status
|
|
|
|
if result == nil then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return result
|
|
|
|
end
|
2022-03-10 19:12:07 +00:00
|
|
|
else
|
|
|
|
-- function failed
|
2022-03-10 19:21:03 +00:00
|
|
|
log._error("PPM: protected " .. key .. "() -> " .. result)
|
2022-03-10 19:12:07 +00:00
|
|
|
return nil
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-03-10 19:12:07 +00:00
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- mount all available peripherals (clears mounts first)
|
|
|
|
function mount_all()
|
|
|
|
local ifaces = peripheral.getNames()
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
self.mounts = {}
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
for i = 1, #ifaces do
|
|
|
|
local pm_dev = peripheral.wrap(ifaces[i])
|
|
|
|
peri_init(pm_dev)
|
|
|
|
self.mounts[ifaces[i]] = { peripheral.getType(ifaces[i]), pm_dev }
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
2022-03-10 19:21:03 +00:00
|
|
|
|
|
|
|
if #ifaces == 0 then
|
|
|
|
log._warning("PPM: mount_all() -> no devices found")
|
|
|
|
end
|
2022-03-10 19:12:07 +00:00
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- mount a particular device
|
|
|
|
function mount(name)
|
|
|
|
local ifaces = peripheral.getNames()
|
|
|
|
local pm_dev = nil
|
|
|
|
|
|
|
|
for i = 1, #ifaces do
|
|
|
|
if name == peripheral.getType(ifaces[i]) then
|
|
|
|
pm_dev = peripheral.wrap(ifaces[i])
|
|
|
|
peri_init(pm_dev)
|
|
|
|
|
|
|
|
self.mounts[ifaces[i]] = {
|
|
|
|
type = peripheral.getType(ifaces[i]),
|
|
|
|
device = pm_dev
|
|
|
|
}
|
|
|
|
break
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
return pm_dev
|
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- handle peripheral_detach event
|
2022-03-10 19:21:03 +00:00
|
|
|
function handle_unmount(iface)
|
2022-03-10 19:12:07 +00:00
|
|
|
-- what got disconnected?
|
|
|
|
local lost_dev = self.mounts[iface]
|
|
|
|
local type = lost_dev.type
|
|
|
|
|
2022-03-10 19:21:03 +00:00
|
|
|
log._warning("PPM: lost device " .. type .. " mounted to " .. iface)
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
return self.mounts[iface]
|
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- list all available peripherals
|
|
|
|
function list_avail()
|
|
|
|
return peripheral.getNames()
|
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- list mounted peripherals
|
|
|
|
function list_mounts()
|
|
|
|
return self.mounts
|
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- get a mounted peripheral by side/interface
|
|
|
|
function get_periph(iface)
|
|
|
|
return self.mounts[iface].device
|
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- get a mounted peripheral by type
|
|
|
|
function get_device(name)
|
|
|
|
local device = nil
|
|
|
|
|
|
|
|
for side, data in pairs(self.mounts) do
|
|
|
|
if data.type == name then
|
|
|
|
device = data.device
|
|
|
|
break
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
|
|
|
end
|
2022-03-10 19:12:07 +00:00
|
|
|
|
|
|
|
return device
|
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
-- list all connected monitors
|
|
|
|
function list_monitors()
|
|
|
|
local monitors = {}
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
for side, data in pairs(self.mounts) do
|
|
|
|
if data.type == "monitor" then
|
|
|
|
monitors[side] = data.device
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-10 19:12:07 +00:00
|
|
|
return monitors
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|