-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder-test.lua
124 lines (100 loc) · 2.12 KB
/
encoder-test.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
-- twist knobs
-- draw waveforms
-- see code for hysteresis control
-- TODO: make configurable (need UI or something, UGH)
hyst_ms = {50,50,50}
function init()
print("INIT")
samp = {{},{},{}}
start = {1,1,1}
last_time = {}
last_sign = {0,0,0}
--last_abs = {0,0,0}
-- silly, but simplifies housekeeping
for e=1,3 do
for i=1,128 do
samp[e][i] = 0
last_time[e] = util.time()
end
end
key_state = 0
redraw_clock_id = clock.run(function ()
while true do
redraw()
clock.sleep(1/15)
end
end)
end
function enc(n, d)
-- circular buffer
--print('samp['..n..']['..start[n]..'] = '..d)
now = util.time()
delta_time = now - last_time[n]
last_time[n] = now
sign = d < 0 and -1 or 1
-- cold -> hot
if delta_time > 0.1 then
end
-- looks like we caught a JANK
if sign ~= last_sign[n] and delta_time < hyst_ms[n]/1000 then
print("JANK DETECTED")
--d = 0 - d
--sign = 0 - sign
return -- just trash it
end
print(delta_time, last_sign[n], sign)
last_sign[n] = sign
--last_abs[n] = math.abs(d)
samp[n][start[n]] = d
start[n] = start[n] % 128 + 1
end
function key(n,v)
bit = 8 >> n
if v == 1 then
key_state = key_state | bit
else
key_state = key_state & (bit ~ 7)
end
-- three-finger salute
if key_state == 7 then rerun() end
end
function redraw()
screen.clear()
line = {{},{},{}}
for e=1,3 do
for l=1,7 do
line[e][l] = ""
end
end
for i=0,127 do
for e=1,3 do
for l=1,7 do
thresh = l - 4
pixel = 0
index = (i + start[e]) % 128 + 1
if thresh < 0 then
if samp[e][index] <= thresh then
pixel = 1
end
elseif thresh > 0 then
if samp[e][index] >= thresh then
pixel = 1
end
end
line[e][l] = line[e][l] .. string.char(pixel)
end
end
end
for e=1,3 do
for l=1,7 do
screen.poke(1, e*19+l-9, 128, 1, line[e][l])
end
end
screen.update()
end
function rerun()
norns.script.load(norns.state.script)
end
function cleanup()
clock.cancel(redraw_clock_id)
end