Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Commit

Permalink
Merge branch 'users/t-anmah/button-integration' of https://github.com…
Browse files Browse the repository at this point in the history
…/microsoft/vscode-python-devicesimulator into users/t-anmah/button-integration
  • Loading branch information
xnkevinnguyen committed Feb 10, 2020
2 parents 767d14a + a84f7fe commit 1266d12
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@


def create_message(state, device_name):
state_copy = dict(state)
state_ext = {
"device_name": device_name,
}
state.update(state_ext)
state_copy.update(state_ext)

message = {"type": "state", "data": json.dumps(state)}
message = {"type": "state", "data": json.dumps(state_copy)}
return message


Expand Down
27 changes: 23 additions & 4 deletions src/process_user_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/python_constants.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 1266d12

Please sign in to comment.