-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstream-sndh-fast.py
57 lines (48 loc) · 1.62 KB
/
stream-sndh-fast.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
import Queue
import serial # using pyserial package - https://pypi.org/project/pyserial/
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()
# Assume we fill the PC buffer faster than the ATmega buffer get consumed
self.__fd.write(''.join([self.__q.get() for _ in xrange(available)]))
def main():
fd = serial.Serial("/dev/ttyUSB0", 500000, timeout=2)
q = Queue.Queue()
um = UART_manager(fd, q)
um.start()
l = sys.stdin.readline()
prev_ts = 0
while l:
if q.qsize() < 3000:
_, ts, regs = l.split()
ts = int(ts, 16)
delta = ts - prev_ts
prev_ts = ts
q.put(chr(delta >> 8))
q.put(chr(delta & 0xff))
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') # End of sample
l = sys.stdin.readline()
else:
time.sleep(0.01)
q.put('\xfe') # End of song
um.join()
fd.close()
print ""
main()