-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrollers.lua
65 lines (55 loc) · 1.63 KB
/
controllers.lua
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
local component = require("component")
local controllerNames = {}
local controllerAdrs = {}
for address, name in component.list("controller", false) do
table.insert(controllerNames, component.proxy(component.get(address)).getControllerName())
table.insert(controllerAdrs, component.get(address))
end
function table_contains(tbl, x)
found = false
for _, v in pairs(tbl) do
if v == x then
found = true
end
end
return found
end
function findIndexInTable(table, x)
for i, value in ipairs(table) do
if value == x then
return i
end
end
return nil
end
local function getAddress(name)
if table_contains(controllerNames, name) then
for var = #controllerNames, 1, -1 do
if (controllerNames[var] == name) then
return controllerAdrs[var]
end
end
else
table.insert(controllerNames, name)
table.insert(controllerAdrs, "NotConnected")
return "NotConnected"
end
end
local controllers = {}
function controllers.isConnected(name)
local var = findIndexInTable(controllerNames, name)
if (controllerAdrs[var] == "NotConnected") then
return false
end
return true
end
function controllers.printTable()
for var = #controllerNames, 1, -1 do
print(controllerNames[var])
print(controllerAdrs[var])
end
end
controllers.Signals = component.proxy(getAddress("Signals")) -- Signals
controllers.Switches = component.proxy(getAddress("Switches")) -- Switches
controllers.Crossings = component.proxy(getAddress("Crossings")) -- Crossings
return controllers