-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal-slider.lua
100 lines (91 loc) · 2.23 KB
/
signal-slider.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
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
-- 7
local ws = require('websocket')
local confFilename = 'var/signal.conf'
local conf = {}
function setSides(count)
if count > table.getn(conf.sides) then
count = table.getn(conf.sides)
end
if count < 0 then
count = 0
end
conf.level = count
for i = 1, table.getn(conf.sides) do
redstone.setOutput(conf.sides[i], i <= count)
end
sendMethods()
display()
end
function init()
if fs.exists(confFilename) then
local file = fs.open(confFilename, 'r')
conf = textutils.unserialise(file.readAll())
file.close()
else
conf = {
sides = {'back'},
computerName = 'Signal slider',
methodKey = 'Level',
level = 0
}
saveConf()
end
if conf.level == nil then
conf.level = 0
end
setSides(conf.level)
display()
ws.connect(conf.computerName, true)
sendMethods()
end
function saveConf()
local file = fs.open(confFilename, 'w')
file.write(textutils.serialise(conf))
file.close()
end
function sendMethods()
ws.methods({{
type = "slider",
key = conf.methodKey,
value = conf.level,
min = 0,
max = table.getn(conf.sides),
fn = function(level)
setSides(tonumber(level))
end
}})
end
function keyListener()
local running = true
while running do
local evt, key = os.pullEvent()
if evt == 'key' and key == keys.up then
setSides(conf.level + 1)
end
if evt == 'key' and key == keys.down then
setSides(conf.level - 1)
end
if evt == 'key' and key == keys.q then
running = false
end
if evt == 'mouse_scroll' then
setSides(conf.level + key)
end
saveConf()
end
ws.disconnect()
term.clear()
end
function display()
term.clear()
term.setCursorPos(2, 2)
term.write('Signal Slider');
term.setCursorPos(2, 4);
term.write(string.format('Level: %d/%d', conf.level, table.getn(conf.sides)))
local w, h = term.getSize()
term.setCursorPos(2, h - 1)
term.blit('q', 'f', '0')
term.write('uit')
end
init()
parallel.waitForAny(ws.runtime, keyListener)