tools: Make pyboard.py have infinite timeout when running script.

This makes pyboard.py much more useful for long running scripts.  When
running a script via pyboard.py, it now waits until the script finishes,
with no timeout.  CTRL-C can be used to break out of the waiting if
needed.
This commit is contained in:
Damien George 2014-11-30 21:30:53 +00:00
parent 1960475ed7
commit 17c5ce3727

View file

@ -60,7 +60,7 @@ class Pyboard:
timeout_count = 0 timeout_count = 0
else: else:
timeout_count += 1 timeout_count += 1
if timeout_count >= 10 * timeout: if timeout is not None and timeout_count >= 10 * timeout:
break break
time.sleep(0.1) time.sleep(0.1)
return data return data
@ -81,15 +81,15 @@ class Pyboard:
def exit_raw_repl(self): def exit_raw_repl(self):
self.serial.write(b'\r\x02') # ctrl-B: enter friendly REPL self.serial.write(b'\r\x02') # ctrl-B: enter friendly REPL
def follow(self, data_consumer=False): def follow(self, timeout, data_consumer=None):
# wait for normal output # wait for normal output
data = self.read_until(1, b'\x04', data_consumer=data_consumer) data = self.read_until(1, b'\x04', timeout=timeout, data_consumer=data_consumer)
if not data.endswith(b'\x04'): if not data.endswith(b'\x04'):
raise PyboardError('timeout waiting for first EOF reception') raise PyboardError('timeout waiting for first EOF reception')
data = data[:-1] data = data[:-1]
# wait for error output # wait for error output
data_err = self.read_until(2, b'\x04>') data_err = self.read_until(2, b'\x04>', timeout=timeout)
if not data_err.endswith(b'\x04>'): if not data_err.endswith(b'\x04>'):
raise PyboardError('timeout waiting for second EOF reception') raise PyboardError('timeout waiting for second EOF reception')
data_err = data_err[:-2] data_err = data_err[:-2]
@ -97,7 +97,7 @@ class Pyboard:
# return normal and error output # return normal and error output
return data, data_err return data, data_err
def exec_raw(self, command, data_consumer=False): def exec_raw(self, command, timeout=10, data_consumer=None):
if isinstance(command, bytes): if isinstance(command, bytes):
command_bytes = command command_bytes = command
else: else:
@ -114,7 +114,7 @@ class Pyboard:
if data != b'OK': if data != b'OK':
raise PyboardError('could not exec command') raise PyboardError('could not exec command')
return self.follow(data_consumer) return self.follow(timeout, data_consumer)
def eval(self, expression): def eval(self, expression):
ret = self.exec('print({})'.format(expression)) ret = self.exec('print({})'.format(expression))
@ -214,7 +214,7 @@ def main():
if len(args.files) == 0: if len(args.files) == 0:
try: try:
pyb = Pyboard(args.device) pyb = Pyboard(args.device)
ret, ret_err = pyb.follow(data_consumer=lambda d:print(str(d, encoding='ascii'), end='')) ret, ret_err = pyb.follow(timeout=None, data_consumer=lambda d:print(str(d, encoding='ascii'), end=''))
pyb.close() pyb.close()
except PyboardError as er: except PyboardError as er:
print(er) print(er)
@ -231,7 +231,7 @@ def main():
pyb.enter_raw_repl() pyb.enter_raw_repl()
with open(filename) as f: with open(filename) as f:
pyfile = f.read() pyfile = f.read()
ret, ret_err = pyb.exec_raw(pyfile, data_consumer=lambda d:print(str(d, encoding='ascii'), end='')) ret, ret_err = pyb.exec_raw(pyfile, timeout=None, data_consumer=lambda d:print(str(d, encoding='ascii'), end=''))
pyb.exit_raw_repl() pyb.exit_raw_repl()
pyb.close() pyb.close()
except PyboardError as er: except PyboardError as er: