-
Notifications
You must be signed in to change notification settings - Fork 6
/
stream-sndh.py
73 lines (59 loc) · 2.03 KB
/
stream-sndh.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
import Queue
import serial # using pyserial package - https://pypi.org/project/pyserial/
import signal
import sys
import threading
import time
class UART_manager(threading.Thread):
def __init__(self, fd, q):
super(UART_manager, self).__init__()
self.__fd = fd
self.__q = q
def run(self):
time.sleep(1) # Wait for buffer to fill up
while not self.__q.empty():
# Read number of bytes to send
available = ord(self.__fd.read())
available = (available<<8) + ord(self.__fd.read())
sys.stdout.write("\x1b[2K\rYM_empty: {}\tPC_buffer: {}".format(available, self.__q.qsize()))
sys.stdout.flush()
chunk_size = min(available, self.__q.qsize())
self.__fd.write(chr(chunk_size>>8 & 0xff) + chr(chunk_size & 0xff))
self.__fd.read() # Ack
self.__fd.write(''.join([self.__q.get() for _ in xrange(chunk_size)]))
# Stolen from
# https://stackoverflow.com/questions/18499497/how-to-process-sigterm-signal-gracefully
class GracefulKiller:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
def exit_gracefully(self, signum, frame):
self.kill_now = True
def main():
fd = serial.Serial("/dev/ttyUSB0", 1000000, timeout=2)
q = Queue.Queue()
um = UART_manager(fd, q)
killer = GracefulKiller()
um.start()
l = sys.stdin.readline()
while l:
if q.qsize() < 3000:
_, ts, regs = l.split()
q.put(chr(int(ts[-4:-2], 16)))
q.put(chr(int(ts[-2:], 16)))
en_regs = filter(lambda x:x[1] != '..', enumerate(regs.split('-')))
for n,r in en_regs:
q.put(chr(n))
q.put(chr(int(r,16)))
q.put('\xff')
l = sys.stdin.readline()
else:
time.sleep(0.01)
if killer.kill_now:
break
for c in '\x00\x00\x08\x00\x09\x00\x0a\x00\xff': # Turn off channels
q.put(c)
um.join()
fd.close()
print ""
main()