diff --git a/src/debug_user_code.py b/src/debug_user_code.py index 7e534f479..8be4792fb 100644 --- a/src/debug_user_code.py +++ b/src/debug_user_code.py @@ -32,9 +32,15 @@ # Insert absolute path to Circuitpython libraries for CLUE into sys.path sys.path.insert(0, os.path.join(abs_path_to_parent_dir, CONSTANTS.CIRCUITPYTHON)) +# get board so we can get terminal handle +import board + # This import must happen after the sys.path is modified from common import debugger_communication_client +# get handle to terminal for clue +curr_terminal = board.DISPLAY.terminal + ## Execute User Code ## # Get user's code path @@ -56,12 +62,26 @@ utils.abs_path_to_user_file = abs_path_to_code_file utils.debug_mode = True +# overriding print function so that it shows on clue terminal +def print_decorator(func): + global curr_terminal + + def wrapped_func(*args, **kwargs): + curr_terminal.add_str_to_terminal("".join(str(e) for e in args)) + return func(*args, **kwargs) + + return wrapped_func + + +print = print_decorator(print) + # Execute the user's code file with open(abs_path_to_code_file, encoding="utf8") as user_code_file: + curr_terminal.add_str_to_terminal(CONSTANTS.CODE_START_MSG_CLUE) user_code = user_code_file.read() try: codeObj = compile(user_code, abs_path_to_code_file, CONSTANTS.EXEC_COMMAND) - exec(codeObj, {}) + exec(codeObj, {"print": print}) sys.stdout.flush() except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() @@ -71,3 +91,5 @@ for frameIndex in range(2, len(stackTrace) - 1): errorMessage += "\t" + str(stackTrace[frameIndex]) print(e, errorMessage, file=sys.stderr, flush=True) + curr_terminal.add_str_to_terminal(CONSTANTS.CODE_FINISHED_MSG_CLUE) + board.DISPLAY.show(None)