-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialreader.py
79 lines (69 loc) · 2.47 KB
/
serialreader.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
import logging
from serial import Serial
from abstractreader import AbstractReader, INPUT_VOLUME_ID, INPUT_BUTTON_ID, INPUT_LOG_ID
from buttoncommand import ButtonCommand
from volumecommand import VolumeCommand
# defined in arduino!
FRAME_START = 254
FRAME_STOP = 253
class SerialReader(AbstractReader):
def __init__(self, ser: Serial):
# call the thread class
super().__init__()
self.__serial = ser
self.__serial.flushInput()
def stop(self):
super().stop()
if self.__serial.isOpen():
self.__serial.flushInput()
self.__serial.close()
def readFrame(self):
frameStart = self.__serial.read(1)
if frameStart[0] == FRAME_START:
frameType = self.__serial.read(1)
if frameType == INPUT_VOLUME_ID:
self.__readVolume()
if frameType == INPUT_BUTTON_ID:
self.__readButton()
if frameType == INPUT_LOG_ID:
self.__readLog()
else:
logging.warning("Skipping unknown FRAME_START: " + str(frameStart[0]))
def __readVolume(self):
volume = self.__serial.read(1)
frameStop = self.__serial.read(1)
if frameStop[0] == FRAME_STOP:
# OK
self.receiveQ.put(VolumeCommand(volume[0]))
else:
logging.warning("Skipping unknown FRAME_STOP: " + str(frameStop[0]))
def __readButton(self):
button = self.__serial.read(1)
frameStop = self.__serial.read(1)
if frameStop[0] == FRAME_STOP:
# OK
self.receiveQ.put(ButtonCommand(button[0]))
else:
logging.warning("Skipping unknown FRAME_STOP: " + str(frameStop[0]))
def __readLog(self):
count = 0
error = False
msg = ''
while True:
character = self.__serial.read(1)
if character[0] == FRAME_STOP:
break
count += 1
if count > 100:
logging.warning("Skipping - no FRAME_STOP read while reading log contents")
error = True
break
try:
msg += character.decode('utf-8')
except Exception:
logging.error("Received non-ascii character " + str(character) + "in log message " + msg)
error = True
break
if not error:
# not using the queue, logging directly
logging.debug("Arduino log: " + msg)