-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
37 lines (30 loc) · 1.25 KB
/
parser.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
from PyQt6.QtCore import QThread
from .signals import ControllerSignals, Button
class ButtonParser:
DIRECTION_MASK = 0b00000010 # this bit is 1 for CW and 0 for CCW
BUTTON_MASK = 0b000111 # after bit shift by 2 to the right, these bits contain info on the knob that is turned from 1 for the rightmost button to 7 for the leftmost
NUM_BUTTONS = 7
def __init__(self, response):
self.response = response
if self.response & self.DIRECTION_MASK > 0:
self.direction = 1
else:
self.direction = -1
self.button = Button(
self.direction,
self.NUM_BUTTONS - int(self.response >> 2 & self.BUTTON_MASK),
) # return button, leftmost button is 0 and rightmost button is 6
class ResponseReader(QThread):
def __init__(self, ser):
super(ResponseReader, self).__init__()
self.ser = ser
self.signals = ControllerSignals()
def run(self):
while self.ser.is_open:
try:
response = int.from_bytes(self.ser.read(1), "little")
self.signals.button_pressed.emit(ButtonParser(response).button)
except TypeError:
continue
except AttributeError:
continue