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,14 +4,13 @@
-- 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, ...)
@ -25,10 +24,10 @@ function ppm()
end end
end end
end end
end end
-- mount all available peripherals (clears mounts first) -- mount all available peripherals (clears mounts first)
local mount_all = function () function mount_all()
local ifaces = peripheral.getNames() local ifaces = peripheral.getNames()
self.mounts = {} self.mounts = {}
@ -38,10 +37,10 @@ function ppm()
peri_init(pm_dev) peri_init(pm_dev)
self.mounts[ifaces[i]] = { peripheral.getType(ifaces[i]), pm_dev } self.mounts[ifaces[i]] = { peripheral.getType(ifaces[i]), pm_dev }
end end
end end
-- mount a particular device -- mount a particular device
local mount = function (name) function mount(name)
local ifaces = peripheral.getNames() local ifaces = peripheral.getNames()
local pm_dev = nil local pm_dev = nil
@ -59,10 +58,10 @@ function ppm()
end end
return pm_dev return pm_dev
end end
-- handle peripheral_detach event -- handle peripheral_detach event
local unmount_handler = function (iface) function unmount_handler(iface)
-- what got disconnected? -- what got disconnected?
local lost_dev = self.mounts[iface] local lost_dev = self.mounts[iface]
local type = lost_dev.type local type = lost_dev.type
@ -70,25 +69,25 @@ function ppm()
log._warning("PMGR: lost device " .. type .. " mounted to " .. iface) log._warning("PMGR: lost device " .. type .. " mounted to " .. iface)
return self.mounts[iface] return self.mounts[iface]
end end
-- list all available peripherals -- list all available peripherals
local list_avail = function () function list_avail()
return peripheral.getNames() return peripheral.getNames()
end end
-- list mounted peripherals -- list mounted peripherals
local list_mounts = function () function list_mounts()
return self.mounts return self.mounts
end end
-- get a mounted peripheral by side/interface -- get a mounted peripheral by side/interface
local get_periph = function (iface) function get_periph(iface)
return self.mounts[iface].device return self.mounts[iface].device
end end
-- get a mounted peripheral by type -- get a mounted peripheral by type
local get_device = function (name) function get_device(name)
local device = nil local device = nil
for side, data in pairs(self.mounts) do for side, data in pairs(self.mounts) do
@ -99,10 +98,10 @@ function ppm()
end end
return device return device
end end
-- list all connected monitors -- list all connected monitors
local list_monitors = function () function list_monitors()
local monitors = {} local monitors = {}
for side, data in pairs(self.mounts) do for side, data in pairs(self.mounts) do
@ -112,16 +111,4 @@ function ppm()
end end
return monitors return monitors
end
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