Compare commits
2 Commits
ed0f7f47f9
...
b9562c04e0
Author | SHA1 | Date | |
---|---|---|---|
b9562c04e0 | |||
b3399e6134 |
|
@ -13,6 +13,20 @@ color_lookup = { "red": 31, "green": 32, "cyan": 36, "yellow": 33 }
|
|||
def color(name, text):
|
||||
return "\033[%dm%s\033[0m" % (color_lookup[name], text);
|
||||
|
||||
try:
|
||||
# On Unix systems, print an additional marker in the output stream
|
||||
# when SIGUSR1 or SIGUSR2 is received. This is just to help clarify
|
||||
# output when the chip is externally reset, etc.
|
||||
import signal
|
||||
def sigusr1_handler(signum, frame):
|
||||
print(color("yellow", "--- mark ---"))
|
||||
def sigusr2_handler(signum, frame):
|
||||
print(color("yellow", "--- reset ---"))
|
||||
signal.signal(signal.SIGUSR1, sigusr1_handler)
|
||||
signal.signal(signal.SIGUSR2, sigusr2_handler)
|
||||
except AttributeError as e:
|
||||
pass
|
||||
|
||||
class ResyncException(Exception):
|
||||
pass
|
||||
|
||||
|
@ -69,6 +83,7 @@ class ITMParser:
|
|||
except TimeoutException as e:
|
||||
# Timeout inside a parser should be reported.
|
||||
print(color("red", "Timeout"))
|
||||
break
|
||||
except ResyncException as e:
|
||||
print(color("red", "Resync"))
|
||||
|
||||
|
@ -153,15 +168,32 @@ def main(argv):
|
|||
args = parser.parse_args()
|
||||
|
||||
ser = serial.Serial(args.device, args.baudrate)
|
||||
ser.timeout = 1
|
||||
|
||||
print(color('green', 'ready'))
|
||||
|
||||
def input_stream():
|
||||
while True:
|
||||
# Read one byte with a 1s timeout.
|
||||
ser.timeout = 1
|
||||
data = ser.read(1)
|
||||
if len(data) == 0:
|
||||
# Timeout
|
||||
yield None
|
||||
else:
|
||||
yield data[0]
|
||||
continue
|
||||
yield data[0]
|
||||
|
||||
# Then read as many more as there are immediately
|
||||
# available, and send them. This is more efficient than
|
||||
# reading each individual byte, when they're coming in
|
||||
# fast. Once they stop, we'll go back to the normal
|
||||
# 1 byte read with timeout.
|
||||
ser.timeout = 0
|
||||
while True:
|
||||
data = ser.read(65536)
|
||||
if len(data) == 0:
|
||||
break
|
||||
for c in data:
|
||||
yield c
|
||||
|
||||
try:
|
||||
ITMParser(input_stream()).process()
|
||||
|
|
Loading…
Reference in New Issue
Block a user