-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.py
executable file
·280 lines (256 loc) · 7.98 KB
/
driver.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""
This script contains driver classes for perferials, include
- buzzer
- the PCB touch clickwheel (sometimes called the `ring` in the code)
"""
#%% buzzer
import pwmio
# define buzzer
class Buzzer:
"""
Driver class of the buzzer
"""
def __init__(self, pin):
self.buzzer = pwmio.PWMOut(pin, variable_frequency=True)
self.buzzer.duty_cycle = 0
def beep(self, freq):
"""
Turn the buzzer on at a certain sound wave frequency
freq=0 to turn off
"""
if freq == 0:
self.buzzer.duty_cycle = 0
return
else:
self.buzzer.frequency = freq
self.buzzer.duty_cycle = 32768
return
#%% clickwheel
import touchio
from math import sqrt, atan2, pi, exp, sin
import time
from timetrigger import Timer
# Relay to filter out shakes
class ThetaFilter:
def __init__(self):
self.thr = pi / 30
self.remain = 0
# on theta
def __call__(self, x):
# return (1 / (exp(-x) +1) - 0.5) * 2 * abs(x) * 2.5 + 0.5 * x
self.remain += x
if self.remain > self.thr:
y = self.remain - self.thr
elif self.remain < -self.thr:
y = self.remain + self.thr
else:
self.remain *= 0.95
y = 0
self.remain = self.remain - y
return y
theta_filter = ThetaFilter()
# # uncomment and ctrl enter for plot and testing
# print('startplot:', 'y1', 'y2')
# y1 = 0
# y2 = 0
# for i in range(0, 70):
# t = i / 100
# x = sin(t * 10)
# y1 += x
# y2 += theta_filter(x)
# print(y1, y2)
#%%
def theta_diff(a, b):
c = a - b
if c >= pi:
c -= 2 * pi
if c < - pi:
c += 2 * pi
return c
# define button
class Button:
"""
Driver class of a single button
"""
def __init__(self, pin):
self.touch = touchio.TouchIn(pin)
self.need_init = True
self.current = False
self.last = False
self.en = True
def get(self):
# init on the first check
if self.need_init:
time.sleep(0.1)
self.touch.threshold = self.touch.raw_value + 100
self.need_init = False
# compute output
self.current = self.touch.value
out = 0
if self.current and (not self.last):
out = 1 # press edge
elif (not self.current) and self.last:
if not self.en:
self.en = True
else:
out = -1 # release edge
elif self.current and self.last:
out = 2 # hold
self.last = self.current
if self.en:
return out
else:
return 0
# define ring
class Ring:
"""
Driver class of the touch clickwheel
"""
def __init__(self, pins, center, N=8):
# center button
self.center = center
# ring pins
self.ring = []
for i in range(4):
time.sleep(0.05)
self.ring.append(touchio.TouchIn(pins[i]))
# ring value range init
self.min = []
self.max = []
for i in range(4):
time.sleep(0.05)
self.min.append(self.ring[-1].raw_value)
self.max.append(self.min[-1] + 120)
# constants
self.alter_x = [-1, 0, 0, 1]
self.alter_y = [0, 1, -1, 0]
filtering_N = 2
self.filtering_alpha = 1 / filtering_N
self.dial_N = N
# states
self.pos_x = 0
self.pos_y = 0
self.r = 0
self.theta = 0
self.theta_last = 0
self.theta_d = 0
self.theta_residual = 0
self.touch = False
self.touch_last = False
self.dial_changed = False
# timer
self.hold_timer = Timer()
def get(self):
# read sensor
center_now = self.center.get()
ring_now = [r.raw_value for r in self.ring]
# conver sensor to weights
w = [
(ring_now[i] - self.min[i]) / (self.max[i] - self.min[i])
for i in range(4)
]
# computer vector sum
pos_x = sum([w[i] * self.alter_x[i] for i in range(4)])
pos_y = sum([w[i] * self.alter_y[i] for i in range(4)])
# 1st-order low pass filter
self.pos_x = pos_x * self.filtering_alpha \
+ self.pos_x * (1 - self.filtering_alpha)
self.pos_y = pos_y * self.filtering_alpha \
+ self.pos_y * (1 - self.filtering_alpha)
# covert xy to polar
self.r = sqrt(self.pos_x ** 2 + self.pos_y ** 2)
self.theta = atan2(self.pos_y, self.pos_x)
# covert r to touch
self.touch = self.r > 0.3
# init outputs
dial = 0
buttons = {
'left': 0,
'right': 0,
'up': 0,
'down': 0,
'center': 0,
'ring': 0,
}
# touch conditions
if self.touch and not self.touch_last: # ring touch edge
buttons['ring'] = 1
if self.pos_x > abs(self.pos_y): # right
buttons['right'] = 1
if self.pos_x < -abs(self.pos_y): # left
buttons['left'] = 1
if self.pos_y > abs(self.pos_x): # up
buttons['up'] = 1
if self.pos_y < -abs(self.pos_x): # down
buttons['down'] = 1
# init dial states
self.theta_residual = 0
self.theta_d = 0
self.theta_last = self.theta
self.dial_changed = False
elif self.touch and self.touch_last: # ring hold
buttons['ring'] = 2
self.theta_d = theta_filter(theta_diff(self.theta, self.theta_last))
self.theta_residual += self.theta_d
while self.theta_residual > pi / self.dial_N:
self.theta_residual -= 2 * pi / self.dial_N
dial += 1
while self.theta_residual < -pi / self.dial_N:
self.theta_residual += 2 * pi / self.dial_N
dial -= 1
if dial:
self.dial_changed = True
elif not self.touch and self.touch_last: # ring release edge
buttons['ring'] = -1
if not self.dial_changed:
if self.pos_x > abs(self.pos_y): # right
buttons['right'] = -1
if self.pos_x < -abs(self.pos_y): # left
buttons['left'] = -1
if self.pos_y > abs(self.pos_x): # up
buttons['up'] = -1
if self.pos_y < -abs(self.pos_x): # down
buttons['down'] = -1
else: # ring idle
# center button only works when ring is not touched
buttons['center'] = center_now
# hold detect
buttons_hold = {
'left': 0,
'right': 0,
'up': 0,
'down': 0,
'center': 0,
}
# long press
if buttons['ring'] == 1:
self.hold_timer.start(1)
if buttons['ring'] == 2 and self.hold_timer.over():
if not self.dial_changed:
self.dial_changed = True
if self.pos_x > abs(self.pos_y): # right
buttons_hold['right'] = 1
if self.pos_x < -abs(self.pos_y): # left
buttons_hold['left'] = 1
if self.pos_y > abs(self.pos_x): # up
buttons_hold['up'] = 1
if self.pos_y < -abs(self.pos_x): # down
buttons_hold['down'] = 1
if center_now == 1:
self.hold_timer.start(1)
if center_now == 2 and self.hold_timer.over():
buttons_hold['center'] = 1
self.center.en = False
# output
out = {
'dial': -dial,
'buttons': buttons,
'buttons_hold': buttons_hold,
'theta': self.theta,
'theta_d': -self.theta_d,
'r': self.r,
}
# update
self.touch_last = self.touch
self.theta_last = self.theta
return out