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

Commit

Permalink
added listeners on buttons for backend
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamah committed Feb 10, 2020
1 parent 6cbdf5b commit a84f7fe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
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 a84f7fe

Please sign in to comment.