diff --git a/tools/run.py b/tools/run.py index 27ea7879b52e9c86533ed45d2194a3d2eb9b9773..3ad81103ebdd27c7cc66796534a35be83b1c825a 100755 --- a/tools/run.py +++ b/tools/run.py @@ -9,6 +9,11 @@ import base64 import argparse import serial +from signal import signal, SIGINT +from sys import exit + + + parser = argparse.ArgumentParser(description="Executes python scripts on card10") parser.add_argument( "script", type=argparse.FileType("r"), help="micropython file to execute" @@ -23,16 +28,52 @@ args = parser.parse_args() code = base64.b64encode(bytes(args.script.read(), "utf-8")) +n=100 +codechunks = [code[i:i+n] for i in range(0, len(code), n)] + with serial.Serial(port=args.device, baudrate=115200) as s: - s.write(b"\x03import ubinascii; exec(ubinascii.a2b_base64('" + code + b"'))\r\n") + + def handler(signal_received, frame): + # Handle any cleanup here + print('SIGINT or CTRL-C detected. Exiting gracefully') + s.write(b"\x03") + exit(0) + + signal(SIGINT, handler) + s.write(b"\x03") # ctrl-c + s.readline() + s.write(b"import ubinascii; \r\n") s.readline() + s.write(b"code=[\r\n") s.readline() + for chunk in codechunks: + s.write(b"'"+chunk+b"',\r\n") + s.readline() #dont echo this line + s.write(b"'']\r\n") + s.readline() + + s.write(b"exec(ubinascii.a2b_base64(''.join(code)))\r\n") + s.readline() + + + + + + line = "" + buf = b"" #s.read() + while True: +# while not line.startswith(">>>"): + char="" + + buf+=s.read() + + try: + char = buf.decode("utf-8") + print (char, end="") + buf = b"" - line = "" - while not line.startswith(">>>"): - char = s.read().decode("utf-8") - if char == "\n": - print(line) - line = "" - else: - line += char + except: + if(len(buf)>3): + print("ENCODING ERROR") + buf="" +