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

Commit 389a2b2

Browse files
Merge pull request #216 from microsoft/users/t-anmah/debugger
Basic functionality for microbit Debugging and fixes
2 parents 72b9f1e + 78821fb commit 389a2b2

13 files changed

+360
-161
lines changed

src/adafruit_circuitplayground/constants.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@
2727

2828
VALID_PIXEL_ASSIGN_ERROR = "The pixel color value should be a tuple with three values between 0 and 255 or a hexadecimal color between 0x000000 and 0xFFFFFF."
2929

30+
TELEMETRY_EVENT_NAMES = {
31+
"TAPPED": "API.TAPPED",
32+
"PLAY_FILE": "API.PLAY.FILE",
33+
"PLAY_TONE": "API.PLAY.TONE",
34+
"START_TONE": "API.START.TONE",
35+
"STOP_TONE": "API.STOP.TONE",
36+
"DETECT_TAPS": "API.DETECT.TAPS",
37+
"ADJUST_THRESHOLD": "API.ADJUST.THRESHOLD",
38+
"RED_LED": "API.RED.LED",
39+
"PIXELS": "API.PIXELS",
40+
}
3041
ERROR_SENDING_EVENT = "Error trying to send event to the process : "
3142

3243
TIME_DELAY = 0.03
3344

34-
DEFAULT_PORT = "5577"
35-
3645

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

src/adafruit_circuitplayground/debugger_communication_client.py

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/adafruit_circuitplayground/express.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from .pixel import Pixel
1313
from . import constants as CONSTANTS
1414
from collections import namedtuple
15-
from . import debugger_communication_client
15+
from applicationinsights import TelemetryClient
16+
import common
1617

1718
Acceleration = namedtuple("acceleration", ["x", "y", "z"])
1819

@@ -115,7 +116,9 @@ def light(self):
115116

116117
def __show(self):
117118
if self.__debug_mode:
118-
debugger_communication_client.debug_send_to_simulator(self.__state)
119+
common.debugger_communication_client.debug_send_to_simulator(
120+
self.__state, CONSTANTS.CPX
121+
)
119122
else:
120123
utils.send_to_simulator(self.__state, CONSTANTS.CPX)
121124

src/adafruit_circuitplayground/pixel.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
import json
55
import sys
6+
import common
67

78
from common import utils
89
from common.telemetry import telemetry_py
910
from common.telemetry_events import TelemetryEvent
1011
from . import constants as CONSTANTS
11-
from . import debugger_communication_client
1212

1313

1414
class Pixel:
@@ -22,9 +22,11 @@ def show(self):
2222
# Send the state to the extension so that React re-renders the Webview
2323
# or send the state to the debugger (within this library)
2424
if self.__debug_mode:
25-
debugger_communication_client.debug_send_to_simulator(self.__state)
25+
common.debugger_communication_client.debug_send_to_simulator(
26+
self.__state, CONSTANTS.CPX
27+
)
2628
else:
27-
utils.send_to_simulator(self.__state, CONSTANTS.CPX)
29+
common.utils.send_to_simulator(self.__state, CONSTANTS.CPX)
2830

2931
def __show_if_auto_write(self):
3032
if self.auto_write:

src/adafruit_circuitplayground/test/test_debugger_communication_client.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/common/constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
MAC_OS = "darwin"
22

3+
ERROR_SENDING_EVENT = "Error trying to send event to the process : "
4+
5+
ACTIVE_DEVICE_FIELD = "active_device"
6+
STATE_FIELD = "state"
7+
8+
CONNECTION_ATTEMPTS = 10
39
TIME_DELAY = 0.03
10+
DEFAULT_PORT = "5577"
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT license.
3+
4+
import sys
5+
import json
6+
import socketio
7+
import copy
8+
9+
from . import constants as CONSTANTS
10+
from . import utils
11+
import threading
12+
13+
14+
from adafruit_circuitplayground.express import cpx
15+
from adafruit_circuitplayground.constants import CPX
16+
17+
from microbit.__model.microbit_model import __mb as mb
18+
from microbit.__model.constants import MICROBIT
19+
20+
21+
device_dict = {CPX: cpx, MICROBIT: mb}
22+
processing_state_event = threading.Event()
23+
previous_state = {}
24+
25+
# similar to utils.send_to_simulator, but for debugging
26+
# (needs handle to device-specific debugger)
27+
def debug_send_to_simulator(state, active_device):
28+
global previous_state
29+
if state != previous_state:
30+
previous_state = copy.deepcopy(state)
31+
32+
updated_state = utils.update_state_with_device_name(state, active_device)
33+
message = utils.create_message(updated_state)
34+
35+
update_state(json.dumps(message))
36+
37+
38+
# Create Socket Client
39+
sio = socketio.Client(reconnection_attempts=CONSTANTS.CONNECTION_ATTEMPTS)
40+
41+
# TODO: Get port from process_user_code.py via childprocess communication
42+
43+
44+
# Initialize connection
45+
def init_connection(port=CONSTANTS.DEFAULT_PORT):
46+
sio.connect("http://localhost:{}".format(port))
47+
48+
49+
# Transfer the user's inputs to the API
50+
def __update_api_state(data):
51+
try:
52+
event_state = json.loads(data)
53+
active_device_string = event_state.get(CONSTANTS.ACTIVE_DEVICE_FIELD)
54+
55+
if active_device_string is not None:
56+
active_device = device_dict.get(active_device_string)
57+
if active_device is not None:
58+
active_device.update_state(event_state.get(CONSTANTS.STATE_FIELD))
59+
60+
except Exception as e:
61+
print(CONSTANTS.ERROR_SENDING_EVENT, e, file=sys.stderr, flush=True)
62+
63+
64+
# Method : Update State
65+
def update_state(state):
66+
processing_state_event.clear()
67+
sio.emit("updateState", state)
68+
processing_state_event.wait()
69+
70+
71+
# Event : Button pressed (A, B, A+B, Switch)
72+
# or Sensor changed (Temperature, light, Motion)
73+
@sio.on("input_changed")
74+
def input_changed(data):
75+
sio.emit("receivedState", data)
76+
__update_api_state(data)
77+
78+
79+
@sio.on("received_state")
80+
def received_state(data):
81+
processing_state_event.set()

0 commit comments

Comments
 (0)