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

Commit

Permalink
merged with button integration and resolved conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamah committed Feb 11, 2020
2 parents 5f73a16 + c20029b commit 7d6b4c8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 64 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 @@ -147,3 +147,8 @@
INVALID_GESTURE_ERR = "invalid gesture"

TIME_DELAY = 0.03

EXPECTED_INPUT_BUTTONS = [
"button_a",
"button_b",
]
44 changes: 44 additions & 0 deletions src/microbit/__model/microbit_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def __init__(self):

self.__start_time = time.time()
self.__temperature = 0
self.microbit_button_dict = {
"button_a": self.button_a,
"button_b": self.button_b,
}

def sleep(self, n):
time.sleep(n / 1000)
Expand All @@ -35,5 +39,45 @@ def __set_temperature(self, temperature):
else:
self.__temperature = temperature

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()

# set motion_x, motion_y, motion_z
for name, direction in CONSTANTS.EXPECTED_INPUT_ACCEL_MICROBIT:
previous_motion_val = self.accelerometer._Accelerometer__get_accel(
direction
)
new_motion_val = new_state.get(name, previous_motion_val)
if new_motion_val != previous_motion_val:
self.accelerometer._Accelerometer__set_accel(
direction, new_motion_val
)

# set temperature
previous_temp = self.temperature()
new_temp = new_state.get(
CONSTANTS.EXPECTED_INPUT_TEMP_MICROBIT, previous_temp
)
if new_temp != previous_temp:
self._MicrobitModel__set_temperature(new_temp)

# set light level
previous_light_level = self.display.read_light_level()
new_light_level = new_state.get(
CONSTANTS.EXPECTED_INPUT_LIGHT_MICROBIT, previous_light_level
)
if new_light_level != previous_light_level:
self.display._Display__set_light_level(new_light_level)


__mb = MicrobitModel()
47 changes: 3 additions & 44 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,57 +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):
# set button values
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()")

# set motion_x, motion_y, motion_z
for name, direction in CONSTANTS.EXPECTED_INPUT_ACCEL_MICROBIT:
previous_motion_val = mb.accelerometer._Accelerometer__get_accel(direction)
new_motion_val = new_state.get(name, previous_motion_val)
if new_motion_val != previous_motion_val:
mb.accelerometer._Accelerometer__set_accel(direction, new_motion_val)

# set temperature
previous_temp = mb.temperature()
new_temp = new_state.get(CONSTANTS.EXPECTED_INPUT_TEMP_MICROBIT, previous_temp)
if new_temp != previous_temp:
mb._MicrobitModel__set_temperature(new_temp)

# set light level
previous_light_level = mb.display.read_light_level()
new_light_level = new_state.get(
CONSTANTS.EXPECTED_INPUT_LIGHT_MICROBIT, previous_light_level
)
if new_light_level != previous_light_level:
mb.display._Display__set_light_level(new_light_level)


user_input = UserInput()
threads.append(user_input)
user_input.start()
Expand Down
20 changes: 0 additions & 20 deletions src/python_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,6 @@

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_ACCEL_MICROBIT = {"motion_x": "x", "motion_y": "y", "motion_z": "z"}

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

EXPECTED_INPUT_LIGHT_MICROBIT = "light"

EXPECTED_INPUT_SENSORS_MICROBIT = [
Expand Down

0 comments on commit 7d6b4c8

Please sign in to comment.