Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion docs/how-to-use.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ Commands are accessible through :
- To open the output panel again after closing it go to VS Code menu : `View->Output`.
- If you have pylint enabled, it might underline the import of the adafruit_circuitplayground library, but it will work correctly.
- If you try to deploy to the device while it's plugged in but you still get an error saying it cannot find the board, make sure your Circuit Playground Express is formatted correctly and that its name matches `CIRCUITPY`.
- If you can't get the Simulator communication working while debugging, try to open you `Settings` and check the port used under `'Pacifica: Debugger Server Port'`. You can either change it (usually ports above 5000 could work) or try to free it, then start debugging again.

### Note

\* Sensors currently not supported by the official adafruit_circuit_playground Express library (v2.1.2).
\** The regular communication is using the stdout and stdin of the Pyhton process. But when you debug your code, it will use a communication over sockets on the port 5678.
\*\* The regular communication is using the stdout and stdin of the Pyhton process. But when you debug your code, it will use a communication over sockets on the port 5577. This is the default port that you can change in your `Settings` : `'Pacifica: Debugger Server Port'`.
3 changes: 2 additions & 1 deletion locales/en/out/constants.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dialogResponses.help": "I need help",
"dialogResponses.installPython": "Install from python.org",
"dialogResponses.tutorials": "Tutorials on Adafruit",
"error.debuggerServerInitFailed": "Warning : The Debugger Server cannot be opened. Please try to free the port {0} if it's already in use or select another one in your Settings 'Pacifica: Debugger Server Port' and start another debug session.\n You can still debug your code but you won't be able to use the Simulator.",
"error.debuggingSessionInProgress": "[ERROR] A debugging session is currently in progress, please stop it before running your code. \n",
"error.incorrectFileNameForDevice": "[ERROR] Can\\'t deploy to your Circuit Playground Express device, please rename your file to \"code.py\" or \"main.py\". \n",
"error.incorrectFileNameForDevicePopup": "Seems like you have a different file name than what the CPX requires, please rename it to \"code.py\" or \"main.py\".",
Expand All @@ -33,4 +34,4 @@
"label.webviewPanel": "Adafruit CPX",
"name": "Pacifica Simulator",
"warning.agreeAndRun": "By selecting ‘Agree and Run’, you understand the extension executes Python code on your local computer, which may be a potential security risk."
}
}
5 changes: 3 additions & 2 deletions locales/en/package.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"pacificaExtension.configuration.title": "Pacfica configuration",
"pacificaExtension.configuration.properties.open": "Whether to show 'Open Simulator' icon in editor title menu.",
"pacificaExtension.configuration.properties.device": "Whether to show 'Run Device' icon in editor title menu.",
"pacificaExtension.configuration.properties.simulator": "Whether to show 'Run Simulator' icon in editor title menu."
}
"pacificaExtension.configuration.properties.simulator": "Whether to show 'Run Simulator' icon in editor title menu.",
"pacificaExtension.configuration.properties.debuggerPort": "The port the Server will listen on for communication with the debugger."
}
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@
"default": true,
"description": "%pacificaExtension.configuration.properties.device%",
"scope": "resource"
},
"pacifica.debuggerServerPort": {
"type": "number",
"default": 5577,
"description": "%pacificaExtension.configuration.properties.debuggerPort%",
"scope": "resource"
}
}
},
Expand Down Expand Up @@ -200,7 +206,8 @@
"description": "Command line arguments passed to the program.",
"default": [],
"items": {
"type": "string"
"filePath": "string",
"serverPort": "string"
}
},
"rules": {
Expand Down
5 changes: 3 additions & 2 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"pacificaExtension.configuration.title": "Pacfica configuration",
"pacificaExtension.configuration.properties.open": "Whether to show 'Open Simulator' icon in editor title menu.",
"pacificaExtension.configuration.properties.device": "Whether to show 'Run Device' icon in editor title menu.",
"pacificaExtension.configuration.properties.simulator": "Whether to show 'Run Simulator' icon in editor title menu."
}
"pacificaExtension.configuration.properties.simulator": "Whether to show 'Run Simulator' icon in editor title menu.",
"pacificaExtension.configuration.properties.debuggerPort": "The port the Server will listen on for communication with the debugger."
}
3 changes: 2 additions & 1 deletion src/adafruit_circuitplayground/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

TIME_DELAY = 0.03

DEFAULT_PORT = 5678
DEFAULT_PORT = "5577"

EVENTS_BUTTON_PRESS = ['button_a', 'button_b', 'switch']
EVENTS_SENSOR_CHANGED = ['temperature',
'light', 'motion_x', 'motion_y', 'motion_z']
26 changes: 19 additions & 7 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import * as nls from "vscode-nls";
import * as path from "path";
import { MessageItem } from "vscode";

export const DEFAULT_SERVER_PORT: number = 5678;
// Debugger Server
export const SERVER_INFO = {
DEFAULT_SERVER_PORT: 5577,
ERROR_CODE_INIT_SERVER: "ERROR_INIT_SERVER",
SERVER_PORT_CONFIGURATION: "pacifica.debuggerServerPort"
};

const localize: nls.LocalizeFunc = nls.config({
messageFormat: nls.MessageFormat.file
Expand All @@ -19,11 +24,18 @@ export const CONSTANTS = {
PYTHON_LAUNCHER: "py -3"
},
ERROR: {
COMPORT_UNKNOWN_ERROR: "Writing to COM port (GetOverlappedResult): Unknown error code 121",
COMPORT_UNKNOWN_ERROR:
"Writing to COM port (GetOverlappedResult): Unknown error code 121",
CPX_FILE_ERROR: localize(
"error.cpxFileFormat",
"The cpx.json file format is not correct."
),
DEBUGGER_SERVER_INIT_FAILED: (port: number) => {
return localize(
"error.debuggerServerInitFailed",
`Warning : The Debugger Server cannot be opened. Please try to free the port ${port} if it's already in use or select another one in your Settings 'Pacifica: Debugger Server Port' and start another debug session.\n You can still debug your code but you won't be able to use the Simulator.`
);
},
DEBUGGING_SESSION_IN_PROGESS: localize(
"error.debuggingSessionInProgress",
"[ERROR] A debugging session is currently in progress, please stop it before running your code. \n"
Expand All @@ -32,13 +44,13 @@ export const CONSTANTS = {
return localize(
"error.failedToOpenSerialPort",
`[ERROR] Failed to open serial port ${port}.`
)
);
},
FAILED_TO_OPEN_SERIAL_PORT_DUE_TO: (port: string, error: any) => {
return localize(
"error.failedToOpenSerialPortDueTo",
`[ERROR] Failed to open serial port ${port} due to error: ${error}. \n`
)
);
},
INCORRECT_FILE_NAME_FOR_DEVICE: localize(
"error.incorrectFileNameForDevice",
Expand Down Expand Up @@ -146,7 +158,7 @@ export const CONSTANTS = {
RUNNING_CODE: localize("info.runningCode", "Running user code"),
THIRD_PARTY_WEBSITE: localize(
"info.thirdPartyWebsite",
"By clicking \"Agree and Proceed\" you will be redirected to adafruit.com, a third party website not managed by Microsoft. Please note that your activity on adafruit.com is subject to Adafruit's privacy policy",
'By clicking "Agree and Proceed" you will be redirected to adafruit.com, a third party website not managed by Microsoft. Please note that your activity on adafruit.com is subject to Adafruit\'s privacy policy'
),
WELCOME_OUTPUT_TAB: localize(
"info.welcomeOutputTab",
Expand Down Expand Up @@ -198,7 +210,7 @@ export const CONSTANTS = {
return localize(
"warning.serialMonitorAlreadyOpened",
`Serial monitor is already opened for ${port} \n`
)
);
},
SERIAL_MONITOR_NOT_STARTED: localize(
"warning.serialMonitorNotStarted",
Expand Down Expand Up @@ -308,7 +320,7 @@ export const USER_CODE_NAMES = {
export const STATUS_BAR_PRIORITY = {
PORT: 20,
OPEN_PORT: 30,
BAUD_RATE: 40,
BAUD_RATE: 40
};

export default CONSTANTS;
17 changes: 13 additions & 4 deletions src/debug_user_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,31 @@
from adafruit_circuitplayground import debugger_communication_client


# Init Communication
debugger_communication_client.init_connection()

# Insert absolute path to Adafruit library into sys.path
abs_path_to_parent_dir = os.path.dirname(os.path.abspath(__file__))
abs_path_to_lib = os.path.join(abs_path_to_parent_dir, CONSTANTS.LIBRARY_NAME)
sys.path.insert(0, abs_path_to_lib)


# Execute User Code
## Execute User Code ##


# Get user's code path
abs_path_to_code_file = ''
if len(sys.argv) > 1 and sys.argv[1]:
abs_path_to_code_file = sys.argv[1]
else:
raise FileNotFoundError(CONSTANTS.ERROR_NO_FILE)

# Get Debugger Server Port
server_port = CONSTANTS.DEFAULT_PORT
if len(sys.argv) > 2:
server_port = sys.argv[2]

# Init Communication
debugger_communication_client.init_connection(server_port)

# Init API variables
cpx._Express__abs_path_to_code_file = abs_path_to_code_file
cpx._Express__debug_mode = True
cpx.pixels._Pixel__set_debug_mode(True)
Expand Down
21 changes: 17 additions & 4 deletions src/debuggerCommunicationServer.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as http from "http";
import * as socketio from "socket.io";
import { WebviewPanel } from "vscode";
import { DEFAULT_SERVER_PORT } from "./constants";
import { SERVER_INFO } from "./constants";

export class DebuggerCommunicationServer {
// TODO: Port as a constants + user setting
private port: number;
private serverHttp: http.Server;
private serverIo: socketio.Server;
private simulatorWebview: WebviewPanel | undefined;

constructor(
webviewPanel: WebviewPanel | undefined,
port = DEFAULT_SERVER_PORT
port = SERVER_INFO.DEFAULT_SERVER_PORT
) {
this.port = port;
this.serverIo = socketio(this.port);
this.serverHttp = new http.Server();
this.initHttpServer();

this.serverIo = socketio(this.serverHttp);
this.simulatorWebview = webviewPanel;
this.initEventsHandlers();
console.info(`Server running on port ${this.port}`);
}

public closeConnection(): void {
this.serverHttp.close();
this.serverIo.close();
}

Expand All @@ -41,6 +47,13 @@ export class DebuggerCommunicationServer {
this.serverIo.emit("sensor_changed", newState);
}

private initHttpServer(): void {
this.serverHttp.listen(this.port);
if (!this.serverHttp.listening) {
throw new Error(SERVER_INFO.ERROR_CODE_INIT_SERVER);
}
}

private initEventsHandlers(): void {
this.serverIo.on("connection", (socket: any) => {
console.log("Connection received");
Expand Down
Loading