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

Deploy code to micro:bit and removed need to rename file to deploy to CPX #233

Merged
merged 20 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ In Device Simulator Express, you can use keyboard to interact with the device:
- IntelliSense and syntax highlighting for micro:bit code
- Template file generation
- Integrated Python Debugging for the Simulator
- Deploy MicroPython code to the physical device.
- Simulation of the micro:bit device, including:
- 25 LEDs
- Light sensor
Expand Down
3 changes: 2 additions & 1 deletion locales/en/package.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"deviceSimulatorExpressExtension.commands.cpx.newFile": "[Circuit Playground Express] New File",
"deviceSimulatorExpressExtension.commands.cpx.deployToDevice": "[Circuit Playground Express] Deploy to Device",
"deviceSimulatorExpressExtension.commands.cpx.selectSerialPort": "[Circuit Playground Express] Select Serial Port",
"deviceSimulatorExpressExtension.commands.microbit.deployToDevice": "[micro:bit] Deploy to Device",
"deviceSimulatorExpressExtension.commands.microbit.openSimulator": "[micro:bit] Open Simulator",
"deviceSimulatorExpressExtension.commands.microbit.newFile": "[micro:bit] New File",
"deviceSimulatorExpressExtension.configuration.title": "Device Simulator Express configuration",
"deviceSimulatorExpressExtension.configuration.properties.configEnvOnChange": "When you change the Python interpreter, the Device Simulator Express will automatically configure itself for the required dependencies.",
"deviceSimulatorExpressExtension.configuration.properties.debuggerPort": "The port the Server will listen on for communication with the debugger.",
"deviceSimulatorExpressExtension.configuration.properties.dependencyChecker": "Whether or not to ask if we can download dependencies. If unchecked, the extension will default to never download dependencies, except when automatically creating a virtual environment in the extension files."
"deviceSimulatorExpressExtension.configuration.properties.dependencyChecker": "Whether or not to ask if we can download dependencies. If unchecked, the extension will default to never download dependencies, except when automatically creating a virtual environment in the extension files.",
"deviceSimulatorExpressExtension.configuration.properties.previewMode": "Enable this to test out and play with the new micro:bit simulator!"
}
16 changes: 4 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"onCommand:deviceSimulatorExpress.cpx.openSerialMonitor",
"onCommand:deviceSimulatorExpress.cpx.openSimulator",
"onCommand:deviceSimulatorExpress.cpx.selectSerialPort",
"onCommand:deviceSimulatorExpress.microbit.deployToDevice",
"onCommand:deviceSimulatorExpress.microbit.newFile",
"onCommand:deviceSimulatorExpress.microbit.openSimulator",
"onDebug"
Expand Down Expand Up @@ -89,6 +90,11 @@
"title": "%deviceSimulatorExpressExtension.commands.cpx.selectSerialPort%",
"category": "%deviceSimulatorExpressExtension.commands.common.label%"
},
{
"command": "deviceSimulatorExpress.microbit.deployToDevice",
"title": "%deviceSimulatorExpressExtension.commands.microbit.deployToDevice%",
"category": "%deviceSimulatorExpressExtension.commands.common.label%"
},
{
"command": "deviceSimulatorExpress.microbit.openSimulator",
"title": "%deviceSimulatorExpressExtension.commands.microbit.openSimulator%",
Expand All @@ -102,6 +108,12 @@
],
"menus": {
"commandPalette": [
{
"command": "deviceSimulatorExpress.microbit.deployToDevice",
"title": "%deviceSimulatorExpressExtension.commands.microbit.deployToDevice%",
"category": "%deviceSimulatorExpressExtension.commands.common.label%",
"when": "config.deviceSimulatorExpress.previewMode"
},
{
"command": "deviceSimulatorExpress.microbit.openSimulator",
"title": "%deviceSimulatorExpressExtension.commands.microbit.openSimulator%",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"deviceSimulatorExpressExtension.commands.cpx.newFile": "[Circuit Playground Express] New File",
"deviceSimulatorExpressExtension.commands.cpx.deployToDevice": "[Circuit Playground Express] Deploy to Device",
"deviceSimulatorExpressExtension.commands.cpx.selectSerialPort": "[Circuit Playground Express] Select Serial Port",
"deviceSimulatorExpressExtension.commands.microbit.deployToDevice": "[micro:bit] Deploy to Device",
"deviceSimulatorExpressExtension.commands.microbit.openSimulator": "[micro:bit] Open Simulator",
"deviceSimulatorExpressExtension.commands.microbit.newFile": "[micro:bit] New File",
"deviceSimulatorExpressExtension.configuration.title": "Device Simulator Express configuration",
Expand Down
13 changes: 11 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ export const CONSTANTS = {
"error.unexpectedMessage",
"Webview sent an unexpected message"
),
WRONG_PYTHON_VERSION_FOR_MICROBIT_DEPLOYMENT: localize(
"error.wrongPythonVersionForMicrobitDeployment",
"To deploy your code to the micro:bit, you must be using Python 3.3+"
),
},
FILESYSTEM: {
OUTPUT_DIRECTORY: "out",
Expand Down Expand Up @@ -155,11 +159,11 @@ export const CONSTANTS = {
),
DEPLOY_SUCCESS: localize(
"info.deploySuccess",
"\n[INFO] Code successfully copied! Your Circuit Playground Express should be loading and ready to go shortly.\n"
"\n[INFO] Code successfully copied! Your device should be loading and ready to go shortly.\n"
),
EXTENSION_ACTIVATED: localize(
"info.extensionActivated",
"Congratulations, your extension Adafruit_Simulator is now active!"
"Congratulations, your extension Device Simulator Express is now active!"
),
FILE_SELECTED: (filePath: string) => {
return localize(
Expand Down Expand Up @@ -336,6 +340,7 @@ export enum TelemetryEventName {
CPX_COMMAND_SERIAL_MONITOR_BAUD_RATE = "CPX.COMMAND.SERIAL_MONITOR.BAUD_RATE",
CPX_COMMAND_SERIAL_MONITOR_CLOSE = "CPX.COMMAND.SERIAL_MONITOR.CLOSE",

MICROBIT_COMMAND_DEPLOY_DEVICE = "MICROBIT.COMMAND.DEPLOY.DEVICE",
MICROBIT_COMMAND_NEW_FILE = "MICROBIT.COMMAND.NEW.FILE",
MICROBIT_COMMAND_OPEN_SIMULATOR = "MICROBIT.COMMAND.OPEN.SIMULATOR",

Expand Down Expand Up @@ -373,12 +378,16 @@ export enum TelemetryEventName {
CPX_SUCCESS_COMMAND_DEPLOY_DEVICE = "CPX.SUCCESS.COMMAND.DEPLOY.DEVICE",

MICROBIT_ERROR_COMMAND_NEW_FILE = "MICROBIT.ERROR.COMMAND.NEW.FILE",
MICROBIT_ERROR_DEPLOY_WITHOUT_DEVICE = "MICROBIT.ERROR.DEPLOY.WITHOUT.DEVICE",
MICROBIT_ERROR_PYTHON_DEVICE_PROCESS = "MICROBIT.ERROR.PYTHON.DEVICE.PROCESS",
MICROBIT_SUCCESS_COMMAND_DEPLOY_DEVICE = "MICROBIT.SUCCESS.COMMAND.DEPLOY.DEVICE",

// Performance
CPX_PERFORMANCE_DEPLOY_DEVICE = "CPX.PERFORMANCE.DEPLOY.DEVICE",
CPX_PERFORMANCE_NEW_FILE = "CPX.PERFORMANCE.NEW.FILE",
CPX_PERFORMANCE_OPEN_SIMULATOR = "CPX.PERFORMANCE.OPEN.SIMULATOR",

MICROBIT_PERFORMANCE_DEPLOY_DEVICE = "MICROBIT.PERFORMANCE.DEPLOY.DEVICE",
MICROBIT_PERFORMANCE_NEW_FILE = "MICROBIT.PERFORMANCE.NEW.FILE",
MICROBIT_PERFORMANCE_OPEN_SIMULATOR = "MICROBIT.PERFORMANCE.OPEN.SIMULATOR",
}
Expand Down
69 changes: 52 additions & 17 deletions src/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import string
import os
import sys
import shutil
import json
import uflash
import python_constants as CONSTANTS

if sys.platform == "win32":
Expand All @@ -14,7 +16,9 @@


class Device:
def __init__(self):
def __init__(self, name, file_path):
self.name = name
self.file_path = file_path
self.connected = False
self.error_message = None

Expand Down Expand Up @@ -61,22 +65,53 @@ def find_device_directory(self):
self.error_message = None
return found_directory

def deployToCPX(self):
device_directory = self.find_device_directory()
if self.error_message:
print(
"{}:\t{}".format(self.error_message[0], self.error_message[1]),
file=sys.stderr,
flush=True,
)
if self.connected:
dest_path = os.path.join(
device_directory, self.file_path.rsplit(os.sep, 1)[-1]
)
shutil.copyfile(self.file_path, dest_path)
message = {"type": "complete"}
else:
message = {"type": "no-device"}
return message

if __name__ == "__main__":
import shutil
def deployToMicrobit(self):
try:
# Temporarily redirecting stdout because there are some print statements in uflash library
fake_stdout = open(os.devnull, "w")
_stdout = sys.stdout
sys.stdout = fake_stdout
uflash.flash(path_to_python=self.file_path)
sys.stdout = _stdout
message = {"type": "complete"}
except RuntimeError:
message = {"type": "wrong-python-version"}
except IOError:
self.error_message = CONSTANTS.NO_MICROBIT_DETECTED_ERROR_TITLE
print(
self.error_message, file=sys.stderr, flush=True,
)
message = {"type": "no-device"}
return message

cpx = Device()
device_directory = cpx.find_device_directory()
if cpx.error_message:
print(
"{}:\t{}".format(cpx.error_message[0], cpx.error_message[1]),
file=sys.stderr,
flush=True,
)
if cpx.connected:
dest_path = os.path.join(device_directory, sys.argv[1].rsplit(os.sep, 1)[-1])
shutil.copyfile(sys.argv[1], dest_path)
message = {"type": "complete"}
else:
message = {"type": "no-device"}
def deploy(self):
if self.name == CONSTANTS.MICROBIT:
return self.deployToMicrobit()
elif self.name == CONSTANTS.CPX:
return self.deployToCPX()
else:
return {"type": "no-device"}


if __name__ == "__main__":
device = Device(sys.argv[1], sys.argv[2])
message = device.deploy()
print(json.dumps(message), flush=True)
Loading