8000 WIP tools/mprepl: Add exit command. · micropython/micropython@b6a0d28 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit b6a0d28

Browse files
pi-anldpgeorge
authored andcommitted
WIP tools/mprepl: Add exit command.
On remote repl or in provided script, running exit() with or without exit code, eg exit(-1) will close the script on pc.
1 parent 7d80767 commit b6a0d28

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

tools/mprepl.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import pyboard
3737

3838

39+
CMD_EXIT = 0
3940
CMD_STAT = 1
4041
CMD_ILISTDIR_START = 2
4142
CMD_ILISTDIR_NEXT = 3
@@ -47,6 +48,7 @@
4748

4849
fs_hook_code = """\
4950
import os, io, select, ustruct as struct, micropython, pyb, sys
51+
CMD_EXIT = 0
5052
CMD_STAT = 1
5153
CMD_ILISTDIR_START = 2
5254
CMD_ILISTDIR_NEXT = 3
@@ -55,6 +57,13 @@
5557
CMD_READ = 6
5658
CMD_WRITE = 7
5759
CMD_SEEK = 8
60+
61+
def exit(code=0):
62+
cmd = RemoteCommand()
63+
cmd.begin(CMD_EXIT)
64+
cmd.wr_int32(code)
65+
cmd.end()
66+
5867
class RemoteCommand:
5968
use_second_port = False
6069
def __init__(self):
@@ -380,6 +389,10 @@ def wr_str(self, s):
380389
data_ilistdir = []
381390
data_files = []
382391

392+
def do_exit(cmd):
393+
exitcode = cmd.rd_int32()
394+
return exitcode
395+
383396
def do_stat(cmd):
384397
path = root + cmd.rd_str()
385398
try:
@@ -452,6 +465,7 @@ def do_write(cmd):
452465
cmd.wr_int32(n)
453466

454467
cmd_table = {
468+
CMD_EXIT: do_exit,
455469
CMD_STAT: do_stat,
456470
CMD_ILISTDIR_START: do_ilistdir_start,
457471
CMD_ILISTDIR_NEXT: do_ilistdir_next,
@@ -494,24 +508,31 @@ def main_loop(console, dev_in, dev_out, pyfiles):
494508

495509
console.write(bytes('Connected to MicroPython at %s\r\n' % dev_in, 'utf8'))
496510
console.write(bytes('Local directory %s is mounted at /remote\r\n' % root, 'utf8'))
497-
console.write(bytes('Use Ctrl-X to exit this shell\r\n', 'utf8'))
498-
511+
repl = True
499512
if pyfiles:
500513
for pyfile in pyfiles:
501514
script = Path(pyfile)
502515
if not script.exists():
503516
console.write(bytes('\r\nERROR: Provided script not found!\r\n', 'utf8'))
504517
else:
505-
pyb.exec_(script.read_bytes())
518+
ret = pyb.exec_(script.read_bytes())
519+
print(ret.strip(b'\x18\x00\r\n').decode())
520+
if ret.strip(b'\x00\r\n').endswith(b'\x18'):
521+
# Script ends with exit(), don't start repl.
522+
repl = False
506523

507524
pyb.exit_raw_repl()
508525

509-
while True:
526+
if repl:
527+
console.write(bytes('Use Ctrl-X to exit this shell\r\n', 'utf8'))
528+
529+
530+
while repl:
510531
if isinstance(console, ConsolePosix):
511532
select.select([console.infd, pyb.serial.fd], [], []) # TODO pyb.serial might not have fd
512533
else:
513534
while not (console.inWaiting() or pyb.serial.inWaiting()):
514-
time.sleep(0.1)
535+
time.sleep(0.01)
515536
c = console.readchar()
516537
if c:
517538
if c == b'\x18': # ctrl-X, quit
@@ -546,7 +567,9 @@ def main_loop(console, dev_in, dev_out, pyfiles):
546567
if c == b'\x18':
547568
# a special command
548569
c = pyb.serial.read(1)[0]
549-
cmd_table[c](cmd)
570+
exitcode = cmd_table[c](cmd)
571+
if exitcode is not None:
572+
return exitcode
550573

551574
elif not VT_ENABLED and c == b'\x1b':
552575
# ESC code, ignore these on windows
@@ -590,9 +613,10 @@ def main():
590613
console = Console()
591614
console.enter()
592615
try:
593-
main_loop(console, dev, args.port2, args.scripts)
616+
ret = main_loop(console, dev, args.port2, args.scripts)
594617
finally:
595618
console.exit()
619+
return ret
596620

597621
if __name__ == '__main__':
598-
main()
622+
sys.exit(main() or 0)

0 commit comments

Comments
 (0)
2A57
0