-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperipheral.lua
More file actions
106 lines (95 loc) · 3.44 KB
/
peripheral.lua
File metadata and controls
106 lines (95 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
local expect = require "cc.expect"
local syscall = require "_syscall"
local peripheral = {}
function peripheral.getNames()
return syscall.devchildren("/")
end
function peripheral.isPresent(name)
expect(1, name, "string")
return syscall.devinfo(name) ~= nil
end
function peripheral.getType(obj)
expect(1, obj, "string", "table")
if type(obj) == "string" then
local info = syscall.devinfo(obj)
if not info then return nil end
local retval = {}
for k in pairs(info.types) do retval[#retval+1] = k end
return table.unpack(retval)
else
local mt = getmetatable(obj)
if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then
error("bad argument #1 (table is not a peripheral)", 2)
end
return table.unpack(mt.types)
end
end
function peripheral.hasType(obj, typ)
expect(1, obj, "string", "table")
expect(2, typ, "string")
if type(obj) == "string" then
local info = syscall.devinfo(obj)
if not info then return nil end
return info.types[typ] ~= nil
else
local mt = getmetatable(obj)
if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then
error("bad argument #1 (table is not a peripheral)", 2)
end
return mt.types[typ] ~= nil
end
end
function peripheral.getMethods(name)
expect(1, name, "string")
local ok, retval = pcall(syscall.devmethods, name)
return ok and retval or nil
end
function peripheral.getName(obj)
expect(1, obj, "table")
local mt = getmetatable(obj)
if not mt or mt.__name ~= "peripheral" or type(mt.name) ~= "string" then
error("bad argument #1 (table is not a peripheral)", 2)
end
return mt.name
end
function peripheral.call(name, method, ...)
expect(1, name, "string")
expect(2, method, "string")
return syscall.devcall(name, method, ...)
end
function peripheral.wrap(name)
expect(1, name, "string")
local info = syscall.devinfo(name)
if not info then return nil end
local methods, properties = syscall.devmethods(name), syscall.devproperties(name)
for _, v in ipairs(properties) do properties[v] = true end
for k in pairs(info.types) do if type(k) == "string" then info.types[#info.types+1] = k end end
local retval = {}
for _, v in ipairs(methods) do retval[v] = function(self, ...) return syscall.devcall(name, v, ...) end end
return setmetatable(retval, {
__name = "peripheral",
name = name,
type = info.types[1],
types = info.types,
__index = function(self, idx)
if type(idx) == "string" and properties[idx] then return syscall.devcall(name, "get" .. idx:gsub("^.", string.upper)) end
end,
__newindex = function(self, idx, val)
if type(idx) == "string" and properties[idx] and methods["set" .. idx:gsub("^.", string.upper)] then return syscall.devcall(name, "set" .. idx:gsub("^.", string.upper), val) end
end,
__tostring = function(self)
return "wrapped device: " .. (info.displayName or info.uuid)
end
})
end
function peripheral.find(typ, filter)
expect(1, typ, "string")
expect(2, filter, "function", "nil")
local retval = {}
for _, v in ipairs{syscall.devfind(typ)} do
local p = peripheral.wrap(v)
if not filter or filter(v, p) then retval[#retval+1] = p end
end
return table.unpack(retval)
end
return peripheral