Skip to content

Commit 4865a22

Browse files
committed
tools/pyboard.py: Add "--follow" option to wait for output indefinitely.
Also flush stdout so you can see output as it comes.
1 parent ff987cc commit 4865a22

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

tools/pyboard.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
def stdout_write_bytes(b):
3737
sys.stdout.buffer.write(b)
38+
sys.stdout.buffer.flush()
3839

3940
class PyboardError(BaseException):
4041
pass
@@ -207,17 +208,22 @@ def main():
207208
import argparse
208209
cmd_parser = argparse.ArgumentParser(description='Run scripts on the pyboard.')
209210
cmd_parser.add_argument('--device', default='/dev/ttyACM0', help='the serial device of the pyboard')
211+
cmd_parser.add_argument('--follow', action='store_true', help='follow the output after running the scripts [default if no scripts given]')
210212
cmd_parser.add_argument('--test', action='store_true', help='run a small test suite on the pyboard')
211213
cmd_parser.add_argument('files', nargs='*', help='input files')
212214
args = cmd_parser.parse_args()
213215

214216
if args.test:
215217
run_test(device=args.device)
216218

217-
if len(args.files) == 0:
219+
for filename in args.files:
218220
try:
219221
pyb = Pyboard(args.device)
220-
ret, ret_err = pyb.follow(timeout=None, data_consumer=stdout_write_bytes)
222+
pyb.enter_raw_repl()
223+
with open(filename, 'rb') as f:
224+
pyfile = f.read()
225+
ret, ret_err = pyb.exec_raw(pyfile, timeout=None, data_consumer=stdout_write_bytes)
226+
pyb.exit_raw_repl()
221227
pyb.close()
222228
except PyboardError as er:
223229
print(er)
@@ -228,14 +234,10 @@ def main():
228234
stdout_write_bytes(ret_err)
229235
sys.exit(1)
230236

231-
for filename in args.files:
237+
if args.follow or len(args.files) == 0:
232238
try:
233239
pyb = Pyboard(args.device)
234-
pyb.enter_raw_repl()
235-
with open(filename, 'rb') as f:
236-
pyfile = f.read()
237-
ret, ret_err = pyb.exec_raw(pyfile, timeout=None, data_consumer=stdout_write_bytes)
238-
pyb.exit_raw_repl()
240+
ret, ret_err = pyb.follow(timeout=None, data_consumer=stdout_write_bytes)
239241
pyb.close()
240242
except PyboardError as er:
241243
print(er)

0 commit comments

Comments
 (0)