opus/sys/apis/peripheral.lua

231 lines
4.7 KiB
Lua
Raw Normal View History

2018-01-06 11:07:49 +00:00
local Event = require('event')
local Socket = require('socket')
local Util = require('util')
2017-10-08 21:45:01 +00:00
local Peripheral = Util.shallowCopy(_G.peripheral)
2017-04-01 23:21:49 +00:00
2017-10-08 21:45:01 +00:00
function Peripheral.getList()
2017-04-01 23:21:49 +00:00
if _G.device then
return _G.device
end
local deviceList = { }
2017-10-08 21:45:01 +00:00
for _,side in pairs(Peripheral.getNames()) do
2017-04-01 23:21:49 +00:00
Peripheral.addDevice(deviceList, side)
end
return deviceList
end
function Peripheral.addDevice(deviceList, side)
2016-12-11 19:24:52 +00:00
local name = side
2017-10-08 21:45:01 +00:00
local ptype = Peripheral.getType(side)
2016-12-11 19:24:52 +00:00
if not ptype then
return
end
if ptype == 'modem' then
2017-10-08 21:45:01 +00:00
if Peripheral.call(name, 'isWireless') then
2016-12-11 19:24:52 +00:00
ptype = 'wireless_modem'
else
ptype = 'wired_modem'
end
end
local sides = {
front = true,
back = true,
top = true,
bottom = true,
left = true,
right = true
}
if sides[name] then
local i = 1
local uniqueName = ptype
2017-04-01 23:21:49 +00:00
while deviceList[uniqueName] do
2016-12-11 19:24:52 +00:00
uniqueName = ptype .. '_' .. i
i = i + 1
end
name = uniqueName
end
2017-10-08 21:45:01 +00:00
local s, m = pcall(function() deviceList[name] = Peripheral.wrap(side) end)
2017-07-24 02:37:07 +00:00
if not s and m then
2017-10-08 21:45:01 +00:00
_G.printError('wrap failed')
_G.printError(m)
2017-07-24 02:37:07 +00:00
end
2017-06-23 06:04:56 +00:00
if deviceList[name] then
Util.merge(deviceList[name], {
name = name,
type = ptype,
side = side,
})
return deviceList[name]
end
2016-12-11 19:24:52 +00:00
end
function Peripheral.getBySide(side)
2017-10-08 21:45:01 +00:00
return Util.find(Peripheral.getList(), 'side', side)
2016-12-11 19:24:52 +00:00
end
function Peripheral.getByType(typeName)
2017-10-08 21:45:01 +00:00
return Util.find(Peripheral.getList(), 'type', typeName)
2016-12-11 19:24:52 +00:00
end
function Peripheral.getByMethod(method)
2017-10-08 21:45:01 +00:00
for _,p in pairs(Peripheral.getList()) do
2016-12-11 19:24:52 +00:00
if p[method] then
return p
end
end
end
-- match any of the passed arguments
function Peripheral.get(args)
if type(args) == 'string' then
args = { type = args }
end
2018-01-07 03:25:33 +00:00
if args.name then
return _G.device[args.name]
2018-01-06 11:07:49 +00:00
end
2016-12-11 19:24:52 +00:00
if args.type then
local p = Peripheral.getByType(args.type)
if p then
return p
end
end
if args.method then
local p = Peripheral.getByMethod(args.method)
if p then
return p
end
end
if args.side then
local p = Peripheral.getBySide(args.side)
if p then
return p
end
end
end
2018-01-06 11:07:49 +00:00
local function getProxy(pi)
local socket = Socket.connect(pi.host, 189)
if not socket then
error("Timed out attaching peripheral: " .. pi.uri)
end
socket:write(pi.path)
local proxy = socket:read(3)
if not proxy then
error("Timed out attaching peripheral: " .. pi.uri)
end
local methods = proxy.methods
proxy.methods = nil
for _,method in pairs(methods) do
proxy[method] = function(...)
socket:write({ fn = method, args = { ... } })
local resp = socket:read()
if not resp then
error("Timed out communicating with peripheral: " .. pi.uri)
end
return table.unpack(resp)
end
end
if proxy.blit then
local methods = { 'clear', 'clearLine', 'setCursorPos', 'write', 'blit',
'setTextColor', 'setTextColour', 'setBackgroundColor',
'setBackgroundColour', 'scroll', 'setCursorBlink', }
local queue = nil
for _,method in pairs(methods) do
proxy[method] = function(...)
if not queue then
queue = { }
Event.onTimeout(0, function()
2018-01-07 03:25:33 +00:00
if not socket:write({ fn = 'fastBlit', args = { queue } }) then
error("Timed out communicating with peripheral: " .. pi.uri)
end
2018-01-06 11:07:49 +00:00
queue = nil
socket:read()
end)
end
table.insert(queue, {
fn = method,
args = { ... },
})
end
end
end
if proxy.type == 'monitor' then
Event.addRoutine(function()
while true do
local event = socket:read()
if not event then
break
end
if not Util.empty(event) then
os.queueEvent(table.unpack(event))
end
end
end)
end
return proxy
end
--[[
Parse a uri into it's components
Examples:
2018-01-07 03:25:33 +00:00
monitor = { name = 'monitor' }
side/top = { side = 'top' }
method/list = { method = 'list' }
12://name/monitor = { host = 12, name = 'monitor' }
2018-01-06 11:07:49 +00:00
]]--
local function parse(uri)
local pi = Util.split(uri:gsub('^%d*://', ''), '(.-)/')
if #pi == 1 then
pi = {
2018-01-07 03:25:33 +00:00
'name',
2018-01-06 11:07:49 +00:00
pi[1],
}
end
return {
host = uri:match('^(%d*)%:'), -- 12
2018-01-07 03:25:33 +00:00
uri = uri, -- 12://name/monitor
path = uri:gsub('^%d*://', ''), -- name/monitor
[ pi[1] ] = pi[2], -- name = 'monitor'
2018-01-06 11:07:49 +00:00
}
end
function Peripheral.lookup(uri)
local pi = parse(uri)
if pi.host then
return getProxy(pi)
end
return Peripheral.get(pi)
end
2016-12-11 19:24:52 +00:00
return Peripheral