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

Commit

Permalink
button connections implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamah committed Feb 10, 2020
1 parent cda4e75 commit c876f4f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
43 changes: 24 additions & 19 deletions src/process_user_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,39 @@ def run(self):
read_val = sys.stdin.readline()
sys.stdin.flush()
try:
new_state = json.loads(read_val)
json_val = json.loads(read_val)
device = json_val.get(CONSTANTS.ACTIVE_DEVICE_FIELD)
new_state = json_val.get(CONSTANTS.STATE_KEYWORD, {})

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]
)
update_cpx(new_state)
elif device == MICROBIT:
new_state = new_state.get("state", {})
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"{button} is at {button_pressed}")
if button_pressed:
exec(f"mb.{button}._Button__press_down()")
else:
exec(f"mb.{button}._Button__release()")
update_microbit(new_state)
else:
raise Exception("Device not implemented.")
raise Exception(CONSTANTS.DEVICE_NOT_IMPLEMENTED_ERROR)

except Exception as e:
print(CONSTANTS.ERROR_SENDING_EVENT, e, file=sys.stderr, flush=True)


def update_cpx(new_state):
for event in CONSTANTS.EXPECTED_INPUT_EVENTS_CPX:
cpx._Express__state[event] = new_state.get(event, cpx._Express__state[event])


def update_microbit(new_state):
for button in CONSTANTS.EXPECTED_INPUT_BUTTONS_MICROBIT:
previous_pressed = None
exec(f"previous_pressed = mb.{button}.get_presses()")
button_pressed = new_state.get(button, previous_pressed)

if button_pressed != previous_pressed:
if button_pressed:
exec(f"mb.{button}._Button__press_down()")
else:
exec(f"mb.{button}._Button__release()")


user_input = UserInput()
threads.append(user_input)
user_input.start()
Expand Down
6 changes: 5 additions & 1 deletion src/python_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

CPX_DRIVE_NAME = "CIRCUITPY"

DEVICE_NOT_IMPLEMENTED_ERROR = "Device not implemented."

ENABLE_TELEMETRY = "enable_telemetry"
EXPECTED_INPUT_EVENTS_CPX = [
"button_a",
Expand All @@ -19,7 +21,7 @@
"touch",
]

EXPECTED_INPUT_EVENTS_BUTTONS_MICROBIT = [
EXPECTED_INPUT_BUTTONS_MICROBIT = [
"button_a",
"button_b",
]
Expand All @@ -44,6 +46,8 @@

PYTHON_LIBS_DIR = "python_libs"

STATE_KEYWORD = "state"

UTF_FORMAT = "utf-8"

WINDOWS_OS = "win32"
Expand Down

0 comments on commit c876f4f

Please sign in to comment.