From a84f7fe1eebaaec1d2e27a8e7117af9b26aa1ce2 Mon Sep 17 00:00:00 2001 From: andreamah Date: Mon, 10 Feb 2020 10:49:56 -0800 Subject: [PATCH] added listeners on buttons for backend --- src/process_user_code.py | 27 +++++++++++++++++++++++---- src/python_constants.py | 7 +++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/process_user_code.py b/src/process_user_code.py index 8bc283c7e..608f8f83a 100644 --- a/src/process_user_code.py +++ b/src/process_user_code.py @@ -30,7 +30,9 @@ # This import must happen after the sys.path is modified from adafruit_circuitplayground.express import cpx from adafruit_circuitplayground.telemetry import telemetry_py +from adafruit_circuitplayground.constants import CPX from microbit.model.microbit_model import mb +from microbit.model.constants import MICROBIT # Handle User Inputs Thread @@ -44,10 +46,27 @@ def run(self): sys.stdin.flush() try: new_state = json.loads(read_val) - for event in CONSTANTS.EXPECTED_INPUT_EVENTS_CPX: - cpx._Express__state[event] = new_state.get( - event, cpx._Express__state[event] - ) + + device = new_state.get(CONSTANTS.ACTIVE_DEVICE_FIELD) + if device == CPX: + for event in CONSTANTS.EXPECTED_INPUT_EVENTS_CPX: + cpx._Express__state[event] = new_state.get( + event, cpx._Express__state[event] + ) + elif device == MICROBIT: + for button in CONSTANTS.EXPECTED_INPUT_EVENTS_BUTTONS_MICROBIT: + previous_pressed = None + exec(f"previous_pressed = mb.{button}.get_presses()") + button_pressed = new_state.get(event, previous_pressed) + + if button_pressed != previous_pressed: + print(f"{event} is at {button_pressed}") + if button_pressed: + exec(f"mb.{button}._Button__press_down()") + else: + exec(f"mb.{button}._Button__release()") + else: + raise Exception("Device not implemented.") except Exception as e: print(CONSTANTS.ERROR_SENDING_EVENT, e, file=sys.stderr, flush=True) diff --git a/src/python_constants.py b/src/python_constants.py index 4b9d0338f..e2cea8345 100644 --- a/src/python_constants.py +++ b/src/python_constants.py @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT license. +ACTIVE_DEVICE_FIELD = "active_device" + CPX_DRIVE_NAME = "CIRCUITPY" ENABLE_TELEMETRY = "enable_telemetry" @@ -17,6 +19,11 @@ "touch", ] +EXPECTED_INPUT_EVENTS_BUTTONS_MICROBIT = [ + "button_a", + "button_b", +] + EXEC_COMMAND = "exec" ERROR_SENDING_EVENT = "Error trying to send event to the process : " ERROR_TRACEBACK = "\n\tTraceback of code execution : \n"