changed ppm to not wrap under ppm() function

This commit is contained in:
Mikayla Fischler 2022-03-10 14:12:07 -05:00
parent ea84563bb4
commit a0b2c1f3e2

View File

@ -4,124 +4,111 @@
-- Protected Peripheral Manager -- Protected Peripheral Manager
-- --
function ppm() local self = {
local self = { mounts = {}
mounts = {} }
}
-- wrap peripheral calls with lua protected call -- wrap peripheral calls with lua protected call
-- ex. reason: we don't want a disconnect to crash the program before a SCRAM -- ex. reason: we don't want a disconnect to crash the program before a SCRAM
local peri_init = function (device) local peri_init = function (device)
for key, func in pairs(device) do for key, func in pairs(device) do
device[key] = function (...) device[key] = function (...)
local status, result = pcall(func, ...) local status, result = pcall(func, ...)
if status then if status then
return result return result
else else
-- function failed -- function failed
log._error("protected " .. key .. "() -> " .. result) log._error("protected " .. key .. "() -> " .. result)
return nil return nil
end
end end
end end
end end
end
-- mount all available peripherals (clears mounts first)
local mount_all = function () -- mount all available peripherals (clears mounts first)
local ifaces = peripheral.getNames() function mount_all()
local ifaces = peripheral.getNames()
self.mounts = {}
self.mounts = {}
for i = 1, #ifaces do
local pm_dev = peripheral.wrap(ifaces[i]) for i = 1, #ifaces do
peri_init(pm_dev) local pm_dev = peripheral.wrap(ifaces[i])
self.mounts[ifaces[i]] = { peripheral.getType(ifaces[i]), pm_dev } peri_init(pm_dev)
end self.mounts[ifaces[i]] = { peripheral.getType(ifaces[i]), pm_dev }
end end
end
-- mount a particular device
local mount = function (name) -- mount a particular device
local ifaces = peripheral.getNames() function mount(name)
local pm_dev = nil local ifaces = peripheral.getNames()
local pm_dev = nil
for i = 1, #ifaces do
if name == peripheral.getType(ifaces[i]) then for i = 1, #ifaces do
pm_dev = peripheral.wrap(ifaces[i]) if name == peripheral.getType(ifaces[i]) then
peri_init(pm_dev) pm_dev = peripheral.wrap(ifaces[i])
peri_init(pm_dev)
self.mounts[ifaces[i]] = {
type = peripheral.getType(ifaces[i]), self.mounts[ifaces[i]] = {
device = pm_dev type = peripheral.getType(ifaces[i]),
} device = pm_dev
break }
end break
end end
end
return pm_dev
end return pm_dev
end
-- handle peripheral_detach event
local unmount_handler = function (iface) -- handle peripheral_detach event
-- what got disconnected? function unmount_handler(iface)
local lost_dev = self.mounts[iface] -- what got disconnected?
local type = lost_dev.type local lost_dev = self.mounts[iface]
local type = lost_dev.type
log._warning("PMGR: lost device " .. type .. " mounted to " .. iface)
log._warning("PMGR: lost device " .. type .. " mounted to " .. iface)
return self.mounts[iface]
end return self.mounts[iface]
end
-- list all available peripherals
local list_avail = function () -- list all available peripherals
return peripheral.getNames() function list_avail()
end return peripheral.getNames()
end
-- list mounted peripherals
local list_mounts = function () -- list mounted peripherals
return self.mounts function list_mounts()
end return self.mounts
end
-- get a mounted peripheral by side/interface
local get_periph = function (iface) -- get a mounted peripheral by side/interface
return self.mounts[iface].device function get_periph(iface)
end return self.mounts[iface].device
end
-- get a mounted peripheral by type
local get_device = function (name) -- get a mounted peripheral by type
local device = nil function get_device(name)
local device = nil
for side, data in pairs(self.mounts) do
if data.type == name then for side, data in pairs(self.mounts) do
device = data.device if data.type == name then
break device = data.device
end break
end end
end
return device
end return device
end
-- list all connected monitors
local list_monitors = function () -- list all connected monitors
local monitors = {} function list_monitors()
local monitors = {}
for side, data in pairs(self.mounts) do
if data.type == "monitor" then for side, data in pairs(self.mounts) do
monitors[side] = data.device if data.type == "monitor" then
end monitors[side] = data.device
end end
end
return monitors
end return monitors
return {
mount_all = mount_all,
mount = mount,
umount = unmount_handler,
list_avail = list_avail,
list_mounts = list_mounts,
get_periph = get_periph,
get_device = get_device,
list_monitors = list_monitors
}
end end