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

Commit

Permalink
initial work on sensor listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamah committed Feb 10, 2020
1 parent f97990e commit 0525df8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/process_user_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def update_cpx(new_state):


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()")
Expand All @@ -78,6 +79,28 @@ def update_microbit(new_state):
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 = None
exec(f"previous_motion_val = mb.accelerometer.get_{direction}()")
new_motion_val = new_state.get(name, previous_motion_val)
if new_motion_val != previous_motion_val:
print("change motion val")

# set temperature
previous_temp = mb.temperature()
new_temp = new_state.get(CONSTANTS.EXPECTED_INPUT_TEMP_MICROBIT, previous_temp)
if new_temp != new_temp:
print("set temp value")

# 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 != new_light_level:
print("set light value")


user_input = UserInput()
threads.append(user_input)
Expand Down
11 changes: 11 additions & 0 deletions src/python_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
DEVICE_NOT_IMPLEMENTED_ERROR = "Device not implemented."

ENABLE_TELEMETRY = "enable_telemetry"

EXPECTED_INPUT_EVENTS_CPX = [
"button_a",
"button_b",
Expand All @@ -21,11 +22,21 @@
"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 = [
"temperature",
"light",
]
EXPECTED_INPUT_TEMP_MICROBIT = "temperature"

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 0525df8

Please sign in to comment.