diff --git a/src/common/utils.py b/src/common/utils.py index 1af1c0eb5..d6d241b2f 100644 --- a/src/common/utils.py +++ b/src/common/utils.py @@ -24,8 +24,11 @@ def update_state_with_device_name(state, device_name): return updated_state -def create_message(state): - message = {"type": "state", "data": json.dumps(state)} +def create_message(msg, send_type="state"): + if isinstance(msg, dict): + msg = json.dumps(msg) + + message = {"type": send_type, "data": msg} return message @@ -41,6 +44,13 @@ def send_to_simulator(state, device_name): time.sleep(CONSTANTS.TIME_DELAY) +def send_print_to_simulator(raw_msg): + data_str = str(raw_msg) + message = create_message(data_str, "print") + print(json.dumps(message) + "\0", file=sys.__stdout__, flush=True) + time.sleep(CONSTANTS.TIME_DELAY) + + def remove_leading_slashes(string): string = string.lstrip("\\/") return string @@ -52,14 +62,6 @@ def escape_if_OSX(file_name): return file_name -def print_for_unimplemented_functions(function_name, one_more_call=False): - # Frame 0 is this function call - # Frame 1 is the call that calls this function, which is a microbit function - # Frame 2 is the call that calls the microbit function, which is in the user's file - # If one_more_call is True, then there is another frame between what was originally supposed to be frame 1 and 2. - frame_no = 2 if not one_more_call else 3 - line_number = sys._getframe(frame_no).f_lineno - user_file_name = sys._getframe(frame_no).f_code.co_filename - print( - f"'{function_name}' on line {line_number} in {user_file_name} is not implemented in the simulator but it will work on the actual device!" - ) +def print_for_unimplemented_functions(function_name): + msg = f"'{function_name}' is not implemented in the simulator but it will work on the actual device!\n" + send_print_to_simulator(msg) diff --git a/src/micropython/microbit/__model/microbit_model.py b/src/micropython/microbit/__model/microbit_model.py index b9149e721..1f78e35ae 100644 --- a/src/micropython/microbit/__model/microbit_model.py +++ b/src/micropython/microbit/__model/microbit_model.py @@ -30,15 +30,11 @@ def __init__(self): def panic(self, n): # Due to the shim, there is another call frame. - utils.print_for_unimplemented_functions( - MicrobitModel.panic.__name__, one_more_call=True - ) + utils.print_for_unimplemented_functions(MicrobitModel.panic.__name__) def reset(self): # Due to the shim, there is another call frame. - utils.print_for_unimplemented_functions( - MicrobitModel.reset.__name__, one_more_call=True - ) + utils.print_for_unimplemented_functions(MicrobitModel.reset.__name__) def sleep(self, n): time.sleep(n / 1000)