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

Commit

Permalink
incorporated PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamah committed Feb 10, 2020
1 parent f97990e commit c20029b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 39 deletions.
13 changes: 13 additions & 0 deletions src/adafruit_circuitplayground/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,16 @@

EVENTS_BUTTON_PRESS = ["button_a", "button_b", "switch"]
EVENTS_SENSOR_CHANGED = ["temperature", "light", "motion_x", "motion_y", "motion_z"]

ALL_EXPECTED_INPUT_EVENTS = [
"button_a",
"button_b",
"switch",
"temperature",
"light",
"shake",
"motion_x",
"motion_y",
"motion_z",
"touch",
]
6 changes: 6 additions & 0 deletions src/adafruit_circuitplayground/express.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,11 @@ def stop_tone(self):
telemetry_py.send_telemetry("STOP_TONE")
raise NotImplementedError(CONSTANTS.NOT_IMPLEMENTED_ERROR)

def update_state(self, new_state):
for event in CONSTANTS.ALL_EXPECTED_INPUT_EVENTS:
self._Express__state[event] = new_state.get(
event, self._Express__state[event]
)


cpx = Express()
5 changes: 5 additions & 0 deletions src/microbit/__model/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,8 @@
SAME_SIZE_ERR = "images must be the same size"

TIME_DELAY = 0.03

EXPECTED_INPUT_BUTTONS = [
"button_a",
"button_b",
]
19 changes: 19 additions & 0 deletions src/microbit/__model/microbit_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .button import Button
from .display import Display
from . import constants as CONSTANTS


class MicrobitModel:
Expand All @@ -12,12 +13,30 @@ def __init__(self):
self.__start_time = time.time()
self.display = Display()

self.microbit_button_dict = {
"button_a": self.button_a,
"button_b": self.button_b,
}

def sleep(self, n):
time.sleep(n / 1000)

def running_time(self):
print(f"time. time: {time.time()}")
return time.time() - self.__start_time

def update_state(self, new_state):
for button_name in CONSTANTS.EXPECTED_INPUT_BUTTONS:
button = self.microbit_button_dict[button_name]

previous_pressed = button.is_pressed()
button_pressed = new_state.get(button_name, previous_pressed)

if button_pressed != previous_pressed:
if button_pressed:
button._Button__press_down()
else:
button._Button__release()


__mb = MicrobitModel()
25 changes: 3 additions & 22 deletions src/process_user_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(self):
threading.Thread.__init__(self)

def run(self):
device_dict = {CPX: cpx, MICROBIT: mb}
while True:
read_val = sys.stdin.readline()
sys.stdin.flush()
Expand All @@ -50,35 +51,15 @@ def run(self):
device = new_state_message.get(CONSTANTS.ACTIVE_DEVICE_FIELD)
new_state = new_state_message.get(CONSTANTS.STATE_FIELD, {})

if device == CPX:
update_cpx(new_state)
elif device == MICROBIT:
update_microbit(new_state)
if device in device_dict:
device_dict[device].update_state(new_state)
else:
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
17 changes: 0 additions & 17 deletions src/python_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@
DEVICE_NOT_IMPLEMENTED_ERROR = "Device not implemented."

ENABLE_TELEMETRY = "enable_telemetry"
EXPECTED_INPUT_EVENTS_CPX = [
"button_a",
"button_b",
"switch",
"temperature",
"light",
"shake",
"motion_x",
"motion_y",
"motion_z",
"touch",
]

EXPECTED_INPUT_BUTTONS_MICROBIT = [
"button_a",
"button_b",
]

EXEC_COMMAND = "exec"
ERROR_SENDING_EVENT = "Error trying to send event to the process : "
Expand Down

0 comments on commit c20029b

Please sign in to comment.