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

Commit

Permalink
Merge branch 'users/t-vali/microbit-template' of https://github.com/m…
Browse files Browse the repository at this point in the history
…icrosoft/vscode-python-devicesimulator into users/t-vali/microbit-template
  • Loading branch information
xnkevinnguyen committed Feb 12, 2020
2 parents 5e9c90d + 80cd19f commit 480002a
Show file tree
Hide file tree
Showing 30 changed files with 2,110 additions and 3,013 deletions.
33 changes: 17 additions & 16 deletions locales/en/package.i18n.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"deviceSimulatorExpressExtension.commands.changeBaudRate": "Change Baud Rate",
"deviceSimulatorExpressExtension.commands.closeSerialMonitor": "Close Serial Monitor",
"deviceSimulatorExpressExtension.commands.label": "Device Simulator Express",
"deviceSimulatorExpressExtension.commands.openSerialMonitor": "Open Serial Monitor",
"deviceSimulatorExpressExtension.commands.openSimulator": "Open Simulator",
"deviceSimulatorExpressExtension.commands.runSimulator": "Run Simulator",
"deviceSimulatorExpressExtension.commands.newFile": "New File",
"deviceSimulatorExpressExtension.commands.runDevice": "Deploy to Device",
"deviceSimulatorExpressExtension.commands.selectSerialPort": "Select Serial Port",
"deviceSimulatorExpressExtension.configuration.title": "Device Simulator Express configuration",
"deviceSimulatorExpressExtension.configuration.properties.open": "Whether to show 'Open Simulator' icon in editor title menu.",
"deviceSimulatorExpressExtension.configuration.properties.device": "Whether to show 'Run Device' icon in editor title menu.",
"deviceSimulatorExpressExtension.configuration.properties.simulator": "Whether to show 'Run Simulator' icon in editor title menu.",
"deviceSimulatorExpressExtension.configuration.properties.debuggerPort": "The port the Server will listen on for communication with the debugger."
}
{
"deviceSimulatorExpressExtension.commands.changeBaudRate": "Change Baud Rate",
"deviceSimulatorExpressExtension.commands.closeSerialMonitor": "Close Serial Monitor",
"deviceSimulatorExpressExtension.commands.installDependencies": "Install Extension Dependencies",
"deviceSimulatorExpressExtension.commands.label": "Device Simulator Express",
"deviceSimulatorExpressExtension.commands.openSerialMonitor": "Open Serial Monitor",
"deviceSimulatorExpressExtension.commands.openSimulator": "Open Simulator",
"deviceSimulatorExpressExtension.commands.runSimulator": "Run Simulator",
"deviceSimulatorExpressExtension.commands.newFile": "New File",
"deviceSimulatorExpressExtension.commands.runDevice": "Deploy to Device",
"deviceSimulatorExpressExtension.commands.selectSerialPort": "Select Serial Port",
"deviceSimulatorExpressExtension.configuration.title": "Device Simulator Express configuration",
"deviceSimulatorExpressExtension.configuration.properties.open": "Whether to show 'Open Simulator' icon in editor title menu.",
"deviceSimulatorExpressExtension.configuration.properties.device": "Whether to show 'Run Device' icon in editor title menu.",
"deviceSimulatorExpressExtension.configuration.properties.simulator": "Whether to show 'Run Simulator' icon in editor title menu.",
"deviceSimulatorExpressExtension.configuration.properties.debuggerPort": "The port the Server will listen on for communication with the debugger."
}
3,585 changes: 943 additions & 2,642 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"Adafruit"
],
"activationEvents": [
"onCommand:deviceSimulatorExpress.installDependencies",
"onCommand:deviceSimulatorExpress.openSerialMonitor",
"onCommand:deviceSimulatorExpress.openSimulator",
"onCommand:deviceSimulatorExpress.runSimulator",
Expand Down Expand Up @@ -56,6 +57,11 @@
"title": "%deviceSimulatorExpressExtension.commands.openSerialMonitor%",
"category": "%deviceSimulatorExpressExtension.commands.label%"
},
{
"command": "deviceSimulatorExpress.installDependencies",
"title": "%deviceSimulatorExpressExtension.commands.installDependencies%",
"category": "%deviceSimulatorExpressExtension.commands.label%"
},
{
"command": "deviceSimulatorExpress.openSimulator",
"title": "%deviceSimulatorExpressExtension.commands.openSimulator%",
Expand Down Expand Up @@ -369,4 +375,4 @@
"extensionDependencies": [
"ms-python.python"
]
}
}
2 changes: 2 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"deviceSimulatorExpressExtension.commands.changeBaudRate": "Change Baud Rate",
"deviceSimulatorExpressExtension.commands.closeSerialMonitor": "Close Serial Monitor",
"deviceSimulatorExpressExtension.commands.installDependencies": "Install Extension Dependencies",
"deviceSimulatorExpressExtension.commands.label": "Device Simulator Express",
"deviceSimulatorExpressExtension.commands.openSerialMonitor": "Open Serial Monitor",
"deviceSimulatorExpressExtension.commands.openSimulator": "Open Simulator",
"deviceSimulatorExpressExtension.commands.runSimulator": "Run Simulator",
"deviceSimulatorExpressExtension.commands.newFileCPX": "New Circuit Playground Express File",
"deviceSimulatorExpressExtension.commands.newFileMicrobit": "New micro:bit File",
"deviceSimulatorExpressExtension.commands.newFile": "New File",
"deviceSimulatorExpressExtension.commands.runDevice": "Deploy to Device",
"deviceSimulatorExpressExtension.commands.selectSerialPort": "Select Serial Port",
"deviceSimulatorExpressExtension.configuration.title": "Device Simulator Express configuration",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# similar to utils.send_to_simulator, but for debugging
# (needs handle to device-specific debugger)
def debug_show(state):
def debug_send_to_simulator(state):
global previous_state

if state != previous_state:
Expand Down
14 changes: 9 additions & 5 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ export const CONFIG = {
};

export const CONSTANTS = {
DEVICE_NAME: {
CPX: "CPX",
MICROBIT: "micro:bit",
},
DEBUG_CONFIGURATION_TYPE: "deviceSimulatorExpress",
DEPENDENCY_CHECKER: {
PIP3: "pip3",
PYTHON: "python",
PYTHON3: "python3.7",
},
DEVICE_NAME: {
CPX: "CPX",
MICROBIT: "micro:bit",
},
ERROR: {
COMPORT_UNKNOWN_ERROR:
"Writing to COM port (GetOverlappedResult): Unknown error code 121",
Expand Down Expand Up @@ -114,7 +114,7 @@ export const CONSTANTS = {
INFO: {
ARE_YOU_SURE: localize(
"info.areYouSure",
"Are you sure you don't want to install the dependencies? The extension can't run without installing it"
"Are you sure you don't want to install the dependencies? The extension can't run without installing them."
),
CLOSED_SERIAL_PORT: (port: string) => {
return localize(
Expand Down Expand Up @@ -237,6 +237,10 @@ export const CONSTANTS = {
),
},
NAME: localize("name", "Device Simulator Express"),
TEMPLATE: {
CPX: "cpx_template.py",
MICROBIT: "microbit_template.py",
},
WARNING: {
ACCEPT_AND_RUN: localize(
"warning.agreeAndRun",
Expand Down
27 changes: 22 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,24 +298,24 @@ export async function activate(context: vscode.ExtensionContext) {

const openCPXTemplateFile = () => {
switchDevice(CONSTANTS.DEVICE_NAME.CPX);
openTemplateFile("cpx");
openTemplateFile(CONSTANTS.TEMPLATE.CPX);
};

const openMicrobitTemplateFile = () => {
switchDevice(CONSTANTS.DEVICE_NAME.MICROBIT);
openTemplateFile("microbit");
openTemplateFile(CONSTANTS.TEMPLATE.MICROBIT);
};

const openTemplateFile = (device: string) => {
const fileName = `${device}_template.py`;
const openTemplateFile = (template: string) => {
const fileName = template;
const filePath =
__dirname + path.sep + "templates" + path.sep + fileName;
const file = fs.readFileSync(filePath, "utf8");
const showNewFilePopup: boolean = vscode.workspace
.getConfiguration()
.get(CONFIG.SHOW_NEW_FILE_POPUP);

if (showNewFilePopup && device === "cpx") {
if (showNewFilePopup && template === CONSTANTS.TEMPLATE.CPX) {
vscode.window
.showInformationMessage(
CONSTANTS.INFO.NEW_FILE,
Expand Down Expand Up @@ -391,6 +391,22 @@ export async function activate(context: vscode.ExtensionContext) {
}
);

const installDependencies: vscode.Disposable = vscode.commands.registerCommand(
"deviceSimulatorExpress.installDependencies",
() => {
const pathToLibs: string = utils.getPathToScript(
context,
CONSTANTS.FILESYSTEM.OUTPUT_DIRECTORY,
CONSTANTS.FILESYSTEM.PYTHON_LIBS_DIR
);
return utils.installPythonDependencies(
context,
pythonExecutableName,
pathToLibs
);
}
);

const killProcessIfRunning = () => {
if (childProcess !== undefined) {
if (currentPanel) {
Expand Down Expand Up @@ -946,6 +962,7 @@ export async function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
changeBaudRate,
closeSerialMonitor,
installDependencies,
openSerialMonitor,
openSimulator,
newFileCPX,
Expand Down
2 changes: 1 addition & 1 deletion src/extension_utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export const promptInstallPythonDependencies = (
pythonExecutable,
pathToLibs
);
} else if (selection === DialogResponses.NO) {
} else {
return vscode.window
.showInformationMessage(
CONSTANTS.INFO.ARE_YOU_SURE,
Expand Down
10 changes: 9 additions & 1 deletion src/microbit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .__model.image import Image
from .__model.microbit_model import __mb

accelerometer = __mb.accelerometer
button_a = __mb.button_a
button_b = __mb.button_b
display = __mb.display
Expand All @@ -21,4 +22,11 @@ def running_time():
Return the number of milliseconds since the board was switched on or
restarted.
"""
__mb.running_time()
return __mb.running_time()


def temperature():
"""
Return the temperature of the micro:bit in degrees Celcius.
"""
return __mb.temperature()
120 changes: 120 additions & 0 deletions src/microbit/__model/accelerometer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from . import constants as CONSTANTS


class Accelerometer:
# The implementation is based off of https://microbit-micropython.readthedocs.io/en/v1.0.1/accelerometer.html.
def __init__(self):
self.__x = 0
self.__y = 0
self.__z = 0
self.__current_gesture = ""
self.__prev_gestures = set()
self.__gestures = []

def get_x(self):
"""
Get the acceleration measurement in the ``x`` axis, as a positive or
negative integer, depending on the direction. The measurement is given in
milli-g.
"""
return self.__x

def get_y(self):
"""
Get the acceleration measurement in the ``y`` axis, as a positive or
negative integer, depending on the direction. The measurement is given in
milli-g.
"""
return self.__y

def get_z(self):
"""
Get the acceleration measurement in the ``z`` axis, as a positive or
negative integer, depending on the direction. The measurement is given in
milli-g.
"""
return self.__z

def get_values(self):
"""
Get the acceleration measurements in all axes at once, as a three-element
tuple of integers ordered as X, Y, Z.
"""
return (self.__x, self.__y, self.__z)

def current_gesture(self):
"""
Return the name of the current gesture.
"""
self.__add_current_gesture_to_gesture_lists()
return self.__current_gesture

def is_gesture(self, name):
"""
Return True or False to indicate if the named gesture is currently active.
"""
self.__add_current_gesture_to_gesture_lists()
if name not in CONSTANTS.GESTURES:
raise ValueError(CONSTANTS.INVALID_GESTURE_ERR)
return name == self.__current_gesture

def was_gesture(self, name):
"""
Return True or False to indicate if the named gesture was active since the
last [was_gesture] call.
"""
self.__add_current_gesture_to_gesture_lists()
if name not in CONSTANTS.GESTURES:
raise ValueError(CONSTANTS.INVALID_GESTURE_ERR)
was_gesture = name in self.__prev_gestures
self.__prev_gestures.clear()
return was_gesture

def get_gestures(self):
"""
Return a tuple of the gesture history. The most recent is listed last.
Also clears the gesture history before returning.
"""
self.__add_current_gesture_to_gesture_lists()
gestures = tuple(self.__gestures)
self.__gestures.clear()
return gestures

# Helpers and Hidden Functions

def __get_accel(self, axis):
if axis == "x":
return self.get_x()
elif axis == "y":
return self.get_y()
elif axis == "z":
return self.get_z()

def __set_accel(self, axis, accel):
if accel < CONSTANTS.MIN_ACCELERATION or accel > CONSTANTS.MAX_ACCELERATION:
raise ValueError(CONSTANTS.INVALID_ACCEL_ERR)
if axis == "x":
self.__x = accel
elif axis == "y":
self.__y = accel
elif axis == "z":
self.__z = accel

def __set_gesture(self, gesture):
if gesture in CONSTANTS.GESTURES:
self.__current_gesture = gesture
elif gesture == "":
self.__current_gesture = ""
else:
raise ValueError(CONSTANTS.INVALID_GESTURE_ERR)

def __add_current_gesture_to_gesture_lists(self):
if self.__current_gesture in CONSTANTS.GESTURES:
self.__gestures.append(self.__current_gesture)
self.__prev_gestures.add(self.__current_gesture)

def __update(self, axis, accel):
if accel is not None:
previous_accel = self.__get_accel(axis)
if accel != previous_accel:
self.__set_accel(axis, accel)
10 changes: 10 additions & 0 deletions src/microbit/__model/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ def __press_down(self):

def __release(self):
self.__pressed = False

def __update(self, is_button_pressed):
if is_button_pressed is not None:
was_button_pressed = self.is_pressed()

if is_button_pressed != was_button_pressed:
if is_button_pressed:
self.__press_down()
else:
self.__release()
Loading

0 comments on commit 480002a

Please sign in to comment.