Skip to content

Commit

Permalink
Added Flash (SWD) command
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Sep 9, 2024
1 parent d8ef548 commit b4aec6d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
},
{
"command": "raspberry-pi-pico.runProject",
"title": "Run Pico Project",
"title": "Run Pico Project (USB)",
"category": "Raspberry Pi Pico",
"enablement": "raspberry-pi-pico.isPicoProject"
},
Expand Down Expand Up @@ -180,6 +180,12 @@
"command": "raspberry-pi-pico.uninstallPicoSDK",
"title": "Uninstall Pico SDK",
"category": "Raspberry Pi Pico"
},
{
"command": "raspberry-pi-pico.flashProject",
"title": "Flash Pico Project (SWD)",
"category": "Raspberry Pi Pico",
"enablement": "raspberry-pi-pico.isPicoProject"
}
],
"configuration": {
Expand Down
4 changes: 3 additions & 1 deletion src/commands/clearGithubApiCache.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { window } from "vscode";
export default class ClearGithubApiCacheCommand extends Command {
private _logger: Logger = new Logger("ClearGithubApiCacheCommand");

public static readonly id = "clearGithubApiCache";

constructor() {
super("clearGithubApiCache");
super(ClearGithubApiCacheCommand.id);
}

async execute(): Promise<void> {
Expand Down
70 changes: 70 additions & 0 deletions src/commands/flashProjectSwd.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { CommandWithResult } from "./command.mjs";
import Logger from "../logger.mjs";
import { tasks, window } from "vscode";
import { EventEmitter } from "stream";

export default class FlashProjectSWDCommand extends CommandWithResult<boolean> {
private _logger: Logger = new Logger("FlashProjectSWDCommand");

public static readonly id = "flashProject";

constructor() {
super(FlashProjectSWDCommand.id);
}

async execute(): Promise<boolean> {
this._logger.info("Executing flash task...");

// Get the task with the specified name
const task = (await tasks.fetchTasks()).find(task => task.name === "Flash");

if (task) {
// Execute the task
const emitter = new EventEmitter();

// add callbacks for task completion
const end = tasks.onDidEndTaskProcess(e => {
if (e.execution.task === task) {
emitter.emit(
"terminated",
e.exitCode === undefined ? -1 : e.exitCode
);
}
});
const end2 = tasks.onDidEndTask(e => {
if (e.execution.task === task) {
emitter.emit("terminated", -1);
}
});

const codePromise = new Promise<number>(resolve => {
emitter.on("terminated", code => {
if (typeof code === "number") {
resolve(code);
} else {
resolve(-1);
}
});
});

let code = -1;
try {
await tasks.executeTask(task);

code = await codePromise;
} finally {
// dispose of callbacks
end.dispose();
end2.dispose();
}

return code === 0;
} else {
// Task not found
this._logger.error("Task 'Flash' not found.");
void window.showErrorMessage("Task 'Flash' not found.");

return false;
}
}
}
2 changes: 2 additions & 0 deletions src/extension.mts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import { pyenvInstallPython, setupPyenv } from "./utils/pyenvUtil.mjs";
import NewExampleProjectCommand from "./commands/newExampleProject.mjs";
import SwitchBoardCommand from "./commands/switchBoard.mjs";
import UninstallPicoSDKCommand from "./commands/uninstallPicoSDK.mjs";
import FlashProjectSWDCommand from "./commands/flashProjectSwd.mjs";

export async function activate(context: ExtensionContext): Promise<void> {
Logger.info(LoggerSource.extension, "Extension activation triggered");
Expand Down Expand Up @@ -105,6 +106,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
new GetTargetCommand(),
new CompileProjectCommand(),
new RunProjectCommand(),
new FlashProjectSWDCommand(),
new ClearGithubApiCacheCommand(),
new ConditionalDebuggingCommand(),
new DebugLayoutCommand(),
Expand Down
15 changes: 14 additions & 1 deletion src/webview/activityBar.mts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import ConfigureCmakeCommand from "../commands/configureCmake.mjs";
import ImportProjectCommand from "../commands/importProject.mjs";
import NewExampleProjectCommand from "../commands/newExampleProject.mjs";
import SwitchBoardCommand from "../commands/switchBoard.mjs";
import FlashProjectSWDCommand from "../commands/flashProjectSwd.mjs";

export class QuickAccessCommand extends TreeItem {
constructor(
Expand All @@ -44,7 +45,8 @@ const EXAMPLE_PROJECT_LABEL = "New Project From Example";
const SWITCH_SDK_LABEL = "Switch SDK";
const SWITCH_BOARD_LABEL = "Switch Board";
const COMPILE_PROJECT_LABEL = "Compile Project";
const RUN_PROJECT_LABEL = "Run Project";
const RUN_PROJECT_LABEL = "Run Project (USB)";
const FLASH_PROJECT_LABEL = "Flash Project (SWD)";
const CONFIGURE_CMAKE_PROJECT_LABEL = "Configure CMake";
const DEBUG_PROJECT_LABEL = "Debug Project";
const DEBUG_LAYOUT_PROJECT_LABEL = "Debug Layout";
Expand Down Expand Up @@ -99,6 +101,9 @@ export class PicoProjectActivityBar
case RUN_PROJECT_LABEL:
element.iconPath = new ThemeIcon("run");
break;
case FLASH_PROJECT_LABEL:
element.iconPath = new ThemeIcon("desktop-download");
break;
case CONFIGURE_CMAKE_PROJECT_LABEL:
// alt. "gather"
element.iconPath = new ThemeIcon("beaker");
Expand Down Expand Up @@ -204,6 +209,14 @@ export class PicoProjectActivityBar
title: RUN_PROJECT_LABEL,
}
),
new QuickAccessCommand(
FLASH_PROJECT_LABEL,
TreeItemCollapsibleState.None,
{
command: `${extensionName}.${FlashProjectSWDCommand.id}`,
title: FLASH_PROJECT_LABEL,
}
),
new QuickAccessCommand(
CONFIGURE_CMAKE_PROJECT_LABEL,
TreeItemCollapsibleState.None,
Expand Down

0 comments on commit b4aec6d

Please sign in to comment.