|
32 | 32 | # Insert absolute path to Circuitpython libraries for CLUE into sys.path
|
33 | 33 | sys.path.insert(0, os.path.join(abs_path_to_parent_dir, CONSTANTS.CIRCUITPYTHON))
|
34 | 34 |
|
| 35 | +# get board so we can get terminal handle |
| 36 | +import board |
| 37 | + |
35 | 38 | # This import must happen after the sys.path is modified
|
36 | 39 | from common import debugger_communication_client
|
37 | 40 |
|
| 41 | +# get handle to terminal for clue |
| 42 | +curr_terminal = board.DISPLAY.terminal |
| 43 | + |
38 | 44 | ## Execute User Code ##
|
39 | 45 |
|
40 | 46 | # Get user's code path
|
|
56 | 62 | utils.abs_path_to_user_file = abs_path_to_code_file
|
57 | 63 | utils.debug_mode = True
|
58 | 64 |
|
| 65 | +# overriding print function so that it shows on clue terminal |
| 66 | +def print_decorator(func): |
| 67 | + global curr_terminal |
| 68 | + |
| 69 | + def wrapped_func(*args, **kwargs): |
| 70 | + curr_terminal.add_str_to_terminal("".join(str(e) for e in args)) |
| 71 | + return func(*args, **kwargs) |
| 72 | + |
| 73 | + return wrapped_func |
| 74 | + |
| 75 | + |
| 76 | +print = print_decorator(print) |
| 77 | + |
59 | 78 | # Execute the user's code file
|
60 | 79 | with open(abs_path_to_code_file, encoding="utf8") as user_code_file:
|
| 80 | + curr_terminal.add_str_to_terminal(CONSTANTS.CODE_START_MSG_CLUE) |
61 | 81 | user_code = user_code_file.read()
|
62 | 82 | try:
|
63 | 83 | codeObj = compile(user_code, abs_path_to_code_file, CONSTANTS.EXEC_COMMAND)
|
64 |
| - exec(codeObj, {}) |
| 84 | + exec(codeObj, {"print": print}) |
65 | 85 | sys.stdout.flush()
|
66 | 86 | except Exception as e:
|
67 | 87 | exc_type, exc_value, exc_traceback = sys.exc_info()
|
|
71 | 91 | for frameIndex in range(2, len(stackTrace) - 1):
|
72 | 92 | errorMessage += "\t" + str(stackTrace[frameIndex])
|
73 | 93 | print(e, errorMessage, file=sys.stderr, flush=True)
|
| 94 | + curr_terminal.add_str_to_terminal(CONSTANTS.CODE_FINISHED_MSG_CLUE) |
| 95 | + board.DISPLAY.show(None) |
0 commit comments