Skip to content

Commit

Permalink
Fix #153 #176, Added executeOnConnect setting
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Dec 27, 2023
1 parent d912ba4 commit 61391b2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ This extension contributes the following settings:
* `micropico.statusbarButtons`: Select which buttons to show in the statusbar (DO NOT CHANGE, unless you know what you are doing)
* `micropico.gcBeforeUpload`: Run garbage collection before uploading files to the board. This will free up some memory usefull when uploading large files but adds about a second or two to the upload process.
* `micropico.softResetAfterUpload`: Soft-resets your board after any upload action. Usefull if you are developing with `main.py` or `boot.py`.
* `micropico.executeOnConnect`: Path to a MicroPython script on the Pico to execute on connect. Leave empty to disable. (must be relative to the root of the Pico's filesystem; doesn't need to begin with a slash; overrides `micropico.openOnStart` setting)

## Extension Context Keys

Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,14 @@
"title": "Soft-reset after upload",
"description": "Soft-resets your board after any upload action which also reruns main.py and boot.py. Usefull when working with main.py and boot.py.",
"order": 12
},
"micropico.executeOnConnect": {
"type": "string",
"default": "",
"scope": "resource",
"title": "Script to execute on connect",
"description": "Path to a MicroPython script on the Pico to execute on connect. Leave empty to disable.",
"order": 13
}
}
},
Expand Down
24 changes: 21 additions & 3 deletions src/activator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export default class Activator {

disposable = vscode.commands.registerCommand(
commandPrefix + "remote.run",
async () => {
async (fileOverride?: string | vscode.Uri) => {
if (!this.pyb?.isPipeConnected()) {
void vscode.window.showWarningMessage(
"Please connect to the Pico first."
Expand All @@ -388,7 +388,10 @@ export default class Activator {
return;
}

const file = await getFocusedFile(true);
const file =
(fileOverride !== undefined && typeof fileOverride === "string"
? fileOverride
: undefined) ?? (await getFocusedFile(true));

if (file === undefined) {
void vscode.window.showWarningMessage(
Expand All @@ -404,7 +407,7 @@ export default class Activator {
"import uos as _pico_uos; " +
"__pico_dir=_pico_uos.getcwd(); " +
`_pico_uos.chdir('${dirname(file)}'); ` +
`execfile('${file}'); ` +
`execfile('${basename(file)}'); ` +
"_pico_uos.chdir(__pico_dir); " +
"del __pico_dir; " +
"del _pico_uos",
Expand Down Expand Up @@ -1195,6 +1198,21 @@ export default class Activator {
// the pyboard wrapper and mark the Pico as disconnected
await this.pyb?.checkStatus();
if (this.pyb?.isPipeConnected()) {
// ensure that the script is only executed once
if (this.ui?.getState() === false) {
const scriptToExecute = settings.getString(
SettingsKey.executeOnConnect
);
if (
scriptToExecute !== undefined &&
scriptToExecute.trim() !== ""
) {
void vscode.commands.executeCommand(
commandPrefix + "remote.run",
scriptToExecute
);
}
}
this.ui?.refreshState(true);

return;
Expand Down
1 change: 1 addition & 0 deletions src/settings.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum SettingsKey {
statusbarButtons = "statusbarButtons",
gcBeforeUpload = "gcBeforeUpload",
softResetAfterUpload = "softResetAfterUpload",
executeOnConnect = "executeOnConnect",
}

export type Setting = string | boolean | string[] | null | undefined;
Expand Down
6 changes: 6 additions & 0 deletions src/ui.mts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class UI {
private visible = false;
private initialized = false;
private userOperationOngoing = false;
private lastState = false;

private items: { [key: string]: StatusBarItem } = {};

Expand Down Expand Up @@ -96,13 +97,18 @@ export default class UI {
}

private setState(connected: boolean): void {
this.lastState = connected;
this.setButton(
"status",
connected ? "check" : "chrome-close",
connected ? "Pico Connected" : "Pico Disconnected"
);
}

public getState(): boolean {
return this.lastState;
}

public refreshState(force: boolean): void {
this.setState(force);

Expand Down

0 comments on commit 61391b2

Please sign in to comment.