-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
68 lines (61 loc) · 1.57 KB
/
code.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
import rotaryio
import board
import digitalio
encoders = [
{
"reference": "encoder1",
"instance": rotaryio.IncrementalEncoder(board.GP15, board.GP14),
"previous": 0
},
{
"reference": "encoder2",
"instance": rotaryio.IncrementalEncoder(board.GP17, board.GP16),
"previous": 0
},
]
buttons = [
{
"reference": "up",
"instance": digitalio.DigitalInOut(board.GP9),
"previous": 0
},
{
"reference": "right",
"instance": digitalio.DigitalInOut(board.GP10),
"previous": 0
},
{
"reference": "down",
"instance": digitalio.DigitalInOut(board.GP11),
"previous": 0
},
{
"reference": "left",
"instance": digitalio.DigitalInOut(board.GP12),
"previous": 0
},
{
"reference": "centre",
"instance": digitalio.DigitalInOut(board.GP13),
"previous": 0
},
]
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
led.value = True
for but in buttons:
but["instance"].switch_to_input(pull=digitalio.Pull.UP)
while True:
for but in buttons:
curr = but["instance"].value
if not curr and but["previous"]:
print(f"push-{but['reference']}")
but["previous"] = curr
for enc in encoders:
curr = enc["instance"].position
change = curr - enc["previous"]
if change > 0:
print(f"{enc['reference']}-inc")
elif change < 0:
print(f"{enc['reference']}-dec")
enc["previous"] = curr