-
Notifications
You must be signed in to change notification settings - Fork 0
/
lgpio_encoder_test.py
executable file
·74 lines (55 loc) · 1.76 KB
/
lgpio_encoder_test.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
#!/usr/bin/env python3
import lgpio as sbc
import time
import signal
def callback(chip, gpio, level, tick):
print(f"{chip} {gpio} {level} {tick/1e9:.09f}")
def checkEncoder(solo, last, pins):
active_pin = None
if last is None:
last = pins
return solo, last, active_pin
if solo is None and len(pins) == 1:
solo = pins[0]
if solo is None and len(pins) == 2:
last = pins
return solo, last, active_pin
if last == pins:
return solo, last, active_pin
# Something is happening!
print('last', last, 'pins', pins, 'solo', solo)
if last == []:
#encoder activation
if len(pins) == 2:
print('solo', pins, pins[0], solo)
active_pin = [p for p in pins if p != solo][0]
else:
print('not solo', pins, pins[0])
active_pin = pins[0]
last = pins
else:
last = pins
if active_pin is not None:
print(f" Encoder activated on pin {active_pin}")
return solo, last, active_pin
# Define the GPIO pin to watch
chip = 0
PINS = (14, 15)
GPIO_PINS = (14, 15)
print(dir(sbc))
sbc.exceptions = False
handle = sbc.gpiochip_open(chip)
sbc.group_claim_input(handle, PINS, sbc.SET_BIAS_PULL_UP | sbc.SET_ACTIVE_LOW)
last = None
solo = None
foo, mask = None, None
while True:
foo, new_mask = sbc.group_read(handle, PINS[0])
if new_mask == mask and mask is not None:
continue
mask = new_mask
pins = [n for n in range(32) if mask & 2**n]
print(int(time.time_ns() / 1000000), pins)
solo, last, active_pin = checkEncoder(solo, last, pins)
if active_pin is not None:
print(f" Encoder activated on pin {active_pin}")