Initial commit
This commit is contained in:
39
sys/services/device.lua
Normal file
39
sys/services/device.lua
Normal file
@@ -0,0 +1,39 @@
|
||||
require = requireInjector(getfenv(1))
|
||||
local Event = require('event')
|
||||
local Peripheral = require('peripheral')
|
||||
|
||||
multishell.setTitle(multishell.getCurrent(), 'Devices')
|
||||
|
||||
local attachColor = colors.green
|
||||
local detachColor = colors.red
|
||||
|
||||
if not term.isColor() then
|
||||
attachColor = colors.white
|
||||
detachColor = colors.lightGray
|
||||
end
|
||||
|
||||
Event.addHandler('peripheral', function(event, side)
|
||||
if side then
|
||||
local dev = Peripheral.addDevice(side)
|
||||
if dev then
|
||||
term.setTextColor(attachColor)
|
||||
Util.print('[%s] %s attached', dev.side, dev.name)
|
||||
os.queueEvent('device_attach', dev.name)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
Event.addHandler('peripheral_detach', function(event, side)
|
||||
if side then
|
||||
local dev = Util.find(device, 'side', side)
|
||||
if dev then
|
||||
term.setTextColor(detachColor)
|
||||
Util.print('[%s] %s detached', dev.side, dev.name)
|
||||
os.queueEvent('device_detach', dev.name)
|
||||
device[dev.name] = nil
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
print('waiting for peripheral changes')
|
||||
Event.pullEvents()
|
||||
Reference in New Issue
Block a user