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
33 changes: 18 additions & 15 deletions docs/how-to-use.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Commands are accessible through :

- **The command palette** (`Ctrl+shift+P` or `View->Command Palette`) and type 'Pacifica : `command_name`'
- **The extension buttons** available on the top right of the Text Editor Panel when you have a Python file open\*
- **The extension buttons** available on the top right of the Text Editor Panel when you have a Python file open (1)

## Available commands

Expand All @@ -18,15 +18,15 @@ Commands are accessible through :
- **Deploy to Device** : saves the code to a Circuit Playground Express.
_(**Note :** the board needs to be correctly formatted to a `CIRCUITPY` drive first if it's not the case : [Installing CircuitPython](https://learn.adafruit.com/welcome-to-circuitpython/installing-circuitpython))_.

- **Select Serial Port** : selects the serial port of the board you want the serial monitor to interact with.
- **Select Serial Port** : selects the serial port of the board you want the serial monitor to interact with. (2)
_(**Note :** USB detection must be enabled in the extension settings.)_

- **Open Serial Monitor** : opens the serial monitor.
- **Open Serial Monitor** : opens the serial monitor. (2)
_(**Note :** A serial port must have been selected already)_.

- **Change Baud Rate** : changes the baud rate of the serial monitor.
- **Change Baud Rate** : changes the baud rate of the serial monitor. (2)

- **Close Serial Montitor** : closes the serial monitor.
- **Close Serial Montitor** : closes the serial monitor. (2)

## Available features

Expand Down Expand Up @@ -56,24 +56,26 @@ Commands are accessible through :
- Device's features
- Sound sensor
- Tones
- Sound detection\*\*
- IR transmitter\*\*
- Sound detection (3)
- IR transmitter (3)
- Motion sensors
- Tap detection

## Pacifica configuration

Here are the settings you can change in the Pacifica configuration:

- **Debugger Server Port:** allows you to change the port used to communicate with the debugger. Default value is _5577_.
- **Debugger Server Port:** allows you to change the port used to communicate with the debugger. Default value is _5577_. (4)

- **Enable USBDetection:** when disabled, prevents the serial monitor from listening to messages from the serial port.

- **Show Device Icon In Editor Title Menu:** allows you to chose whether the _`Deploy to Device`_ button should be in the editor title.
- **Show Device Icon In Editor Title Menu:** allows you to choose whether the _`Deploy to Device`_ button should be in the editor title.

- **Show Open Icon In Editor Title Menu:** allows you to chose whether the _`Open Simulator`_ button should be in the editor title.
- **Show Open Icon In Editor Title Menu:** allows you to choose whether the _`Open Simulator`_ button should be in the editor title.

- **Show Simulator Icon In Editor Title Menu:** allows you to chose whether the _`Run Simulator`_ button should be in the editor title.
- **Show Simulator Icon In Editor Title Menu:** allows you to choose whether the _`Run Simulator`_ button should be in the editor title.

- **Show Dependency Install:** allows you to choose whether you want to be prompted to install the Python dependencies.

## Troubleshooting Tips

Expand All @@ -84,8 +86,9 @@ Here are the settings you can change in the Pacifica configuration:
- 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
### Notes

\* Can be changed in settings.
\*\* 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 Python process. But when you debug your code, it will communicate over sockets on port 5577. This is the default port that you can change in your `Settings` : `'Pacifica: Debugger Server Port'`.
(1) Can be changed in settings.
(2) To use the Serial Monitor commands, you'll need to open a folder because this saves the configuration file for the serial communication. You can still use the rest of the extension without opening a folder.
(3) Sensors currently not supported by the official adafruit_circuit_playground.express library (v2.1.2).
(4) The regular communication is using the stdout and stdin of the Python process. But when you debug your code, it will communicate over sockets on port 5577. This is the default port that you can change in your `Settings` : `'Pacifica: Debugger Server Port'`.
1 change: 1 addition & 0 deletions locales/en/out/constants.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"error.invalidFileNameDebug": "The file you tried to debug isn\\'t named \"code.py\" or \"main.py\\. Rename your file if you want your code to work on your actual device.",
"error.noDevice": "No plugged in boards detected. Please double check if your board is connected and/or properly formatted",
"error.noFileToRun": "\n[ERROR] We can't find the .py file to run on simulator. Open up a new .py file, or browse through some examples\n",
"error.noFolderCreated": "In order to use the Serial Monitor, you need to open a folder and reload VS Code.",
"error.noProgramFoundDebug": "Cannot find a program to debug.",
"error.noPythonPath": "We found that you don't have Python 3 installed on your computer, please install the latest version, add it to your PATH and try again.",
"error.stderr": "\n[ERROR] {0} \n",
Expand Down
6 changes: 5 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export const CONSTANTS = {
"error.noFileToRun",
'[ERROR] We can\'t find a Python file to run. Please make sure you select or open a new ".py" code file, or use the "New File" command to get started and see useful links.\n'
),
NO_FOLDER_OPENED: localize(
"error.noFolderCreated",
"In order to use the Serial Monitor, you need to open a folder and reload VS Code."
),
NO_PROGRAM_FOUND_DEBUG: localize(
"error.noProgramFoundDebug",
"Cannot find a program to debug."
Expand Down Expand Up @@ -218,7 +222,7 @@ export const CONSTANTS = {
),
SERIAL_PORT_NOT_STARTED: localize(
"warning.serialPortNotStarted",
"Serial port has not been started."
"Serial port has not been started.\n"
)
}
};
Expand Down
139 changes: 102 additions & 37 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let currentFileAbsPath: string = "";
let currentTextDocument: vscode.TextDocument;
let telemetryAI: TelemetryAI;
let pythonExecutableName: string = "python";
let configFileCreated: boolean = false;
let inDebugMode: boolean = false;
let debuggerCommunicationHandler: DebuggerCommunicationServer;
// Notification booleans
Expand All @@ -41,13 +42,16 @@ function loadScript(context: vscode.ExtensionContext, scriptPath: string) {
.toString()}"></script>`;
}

const setPathAndSendMessage = (currentPanel: vscode.WebviewPanel, newFilePath: string) => {
const setPathAndSendMessage = (
currentPanel: vscode.WebviewPanel,
newFilePath: string
) => {
currentFileAbsPath = newFilePath;
currentPanel.webview.postMessage({
command: "current-file",
state: { running_file: newFilePath }
});
}
};

// Extension activation
export async function activate(context: vscode.ExtensionContext) {
Expand All @@ -63,7 +67,14 @@ export async function activate(context: vscode.ExtensionContext) {
updatePythonExtraPaths();

// Generate cpx.json
utils.generateCPXConfig();
try {
utils.generateCPXConfig();
configFileCreated = true;
} catch (err) {
console.info("Failed to create the CPX config file.");
configFileCreated = false;
}

pythonExecutableName = await utils.setPythonExectuableName();

if (pythonExecutableName === "") {
Expand All @@ -75,9 +86,11 @@ export async function activate(context: vscode.ExtensionContext) {
utils.logToOutputChannel(outChannel, CONSTANTS.INFO.WELCOME_OUTPUT_TAB);
}

vscode.workspace.onDidSaveTextDocument(async (document: vscode.TextDocument) => {
await updateCurrentFileIfPython(document, currentPanel);
});
vscode.workspace.onDidSaveTextDocument(
async (document: vscode.TextDocument) => {
await updateCurrentFileIfPython(document, currentPanel);
}
);

const openWebview = () => {
if (currentPanel) {
Expand Down Expand Up @@ -133,14 +146,21 @@ export async function activate(context: vscode.ExtensionContext) {
case WebviewMessages.PLAY_SIMULATOR:
console.log(`Play button ${messageJson} \n`);
if (message.text.state as boolean) {
setPathAndSendMessage(currentPanel, message.text.selected_file);
setPathAndSendMessage(
currentPanel,
message.text.selected_file
);
if (currentFileAbsPath) {
const foundDocument = utils.getActiveEditorFromPath(currentFileAbsPath);
const foundDocument = utils.getActiveEditorFromPath(
currentFileAbsPath
);
if (foundDocument !== undefined) {
currentTextDocument = foundDocument;
}
}
telemetryAI.trackFeatureUsage(TelemetryEventName.COMMAND_RUN_SIMULATOR_BUTTON);
telemetryAI.trackFeatureUsage(
TelemetryEventName.COMMAND_RUN_SIMULATOR_BUTTON
);
runSimulatorCommand();
} else {
killProcessIfRunning();
Expand Down Expand Up @@ -173,14 +193,19 @@ export async function activate(context: vscode.ExtensionContext) {
context.subscriptions
);

activeEditorListener = utils.addVisibleTextEditorCallback(currentPanel, context);
activeEditorListener = utils.addVisibleTextEditorCallback(
currentPanel,
context
);
console.log("sent");
}

currentPanel.onDidDispose(
() => {
currentPanel = undefined;
debuggerCommunicationHandler.setWebview(undefined);
if (debuggerCommunicationHandler) {
debuggerCommunicationHandler.setWebview(undefined);
}
killProcessIfRunning();
if (firstTimeClosed) {
vscode.window.showInformationMessage(
Expand Down Expand Up @@ -324,15 +349,21 @@ export async function activate(context: vscode.ExtensionContext) {

killProcessIfRunning();

await updateCurrentFileIfPython(vscode.window.activeTextEditor!.document, currentPanel);
await updateCurrentFileIfPython(
vscode.window.activeTextEditor!.document,
currentPanel
);

if (currentFileAbsPath === "") {
utils.logToOutputChannel(outChannel, CONSTANTS.ERROR.NO_FILE_TO_RUN, true);
vscode.window
.showErrorMessage(
CONSTANTS.ERROR.NO_FILE_TO_RUN,
DialogResponses.MESSAGE_UNDERSTOOD
)
utils.logToOutputChannel(
outChannel,
CONSTANTS.ERROR.NO_FILE_TO_RUN,
true
);
vscode.window.showErrorMessage(
CONSTANTS.ERROR.NO_FILE_TO_RUN,
DialogResponses.MESSAGE_UNDERSTOOD
);
} else {
// Save on run
await currentTextDocument.save();
Expand Down Expand Up @@ -409,7 +440,7 @@ export async function activate(context: vscode.ExtensionContext) {
case "print":
console.log(
`Process print statement output = ${
messageToWebview.data
messageToWebview.data
}`
);
utils.logToOutputChannel(
Expand Down Expand Up @@ -476,15 +507,21 @@ export async function activate(context: vscode.ExtensionContext) {

utils.logToOutputChannel(outChannel, CONSTANTS.INFO.DEPLOY_DEVICE, true);

await updateCurrentFileIfPython(vscode.window.activeTextEditor!.document, currentPanel);
await updateCurrentFileIfPython(
vscode.window.activeTextEditor!.document,
currentPanel
);

if (currentFileAbsPath === "") {
utils.logToOutputChannel(outChannel, CONSTANTS.ERROR.NO_FILE_TO_RUN, true);
vscode.window
.showErrorMessage(
CONSTANTS.ERROR.NO_FILE_TO_RUN,
DialogResponses.MESSAGE_UNDERSTOOD
);
utils.logToOutputChannel(
outChannel,
CONSTANTS.ERROR.NO_FILE_TO_RUN,
true
);
vscode.window.showErrorMessage(
CONSTANTS.ERROR.NO_FILE_TO_RUN,
DialogResponses.MESSAGE_UNDERSTOOD
);
} else if (!utils.validCodeFileName(currentFileAbsPath)) {
// Save on run
await currentTextDocument.save();
Expand Down Expand Up @@ -595,34 +632,57 @@ export async function activate(context: vscode.ExtensionContext) {
}
);

const serialMonitor: SerialMonitor = SerialMonitor.getInstance();
context.subscriptions.push(serialMonitor);
let serialMonitor: SerialMonitor | undefined;
if (configFileCreated) {
serialMonitor = SerialMonitor.getInstance();
context.subscriptions.push(serialMonitor);
}

const selectSerialPort: vscode.Disposable = vscode.commands.registerCommand(
"pacifica.selectSerialPort",
() => {
// todo add telemetry after
serialMonitor.selectSerialPort(null, null);
if (serialMonitor) {
serialMonitor.selectSerialPort(null, null);
} else {
vscode.window.showErrorMessage(CONSTANTS.ERROR.NO_FOLDER_OPENED);
console.info("Serial monitor is not defined.");
}
}
);

const openSerialMonitor: vscode.Disposable = vscode.commands.registerCommand(
"pacifica.openSerialMonitor",
() => {
serialMonitor.openSerialMonitor();
if (serialMonitor) {
serialMonitor.openSerialMonitor();
} else {
vscode.window.showErrorMessage(CONSTANTS.ERROR.NO_FOLDER_OPENED);
console.info("Serial monitor is not defined.");
}
}
);

const changeBaudRate: vscode.Disposable = vscode.commands.registerCommand(
"pacifica.changeBaudRate",
() => {
serialMonitor.changeBaudRate();
if (serialMonitor) {
serialMonitor.changeBaudRate();
} else {
vscode.window.showErrorMessage(CONSTANTS.ERROR.NO_FOLDER_OPENED);
console.info("Serial monitor is not defined.");
}
}
);

const closeSerialMonitor: vscode.Disposable = vscode.commands.registerCommand(
"pacifica.closeSerialMonitor",
(port, showWarning = true) => {
serialMonitor.closeSerialMonitor(port, showWarning);
if (serialMonitor) {
serialMonitor.closeSerialMonitor(port, showWarning);
} else {
vscode.window.showErrorMessage(CONSTANTS.ERROR.NO_FOLDER_OPENED);
console.info("Serial monitor is not defined.");
}
}
);

Expand Down Expand Up @@ -739,11 +799,16 @@ const updateCurrentFileIfPython = async (
setPathAndSendMessage(currentPanel, activeTextDocument.fileName);
currentTextDocument = activeTextDocument;
} else if (currentFileAbsPath === "") {
setPathAndSendMessage(currentPanel,
getActivePythonFile() || "");
setPathAndSendMessage(currentPanel, getActivePythonFile() || "");
}
if (currentTextDocument && utils.getActiveEditorFromPath(currentTextDocument.fileName) === undefined) {
await vscode.window.showTextDocument(currentTextDocument, vscode.ViewColumn.One);
if (
currentTextDocument &&
utils.getActiveEditorFromPath(currentTextDocument.fileName) === undefined
) {
await vscode.window.showTextDocument(
currentTextDocument,
vscode.ViewColumn.One
);
}
};

Expand Down