]> Git Repo - pico-vscode.git/commitdiff
Added Flash (SWD) command
authorpaulober <[email protected]>
Mon, 9 Sep 2024 13:15:34 +0000 (14:15 +0100)
committerpaulober <[email protected]>
Mon, 9 Sep 2024 13:15:34 +0000 (14:15 +0100)
Signed-off-by: paulober <[email protected]>
package.json
src/commands/clearGithubApiCache.mts
src/commands/flashProjectSwd.mts [new file with mode: 0644]
src/extension.mts
src/webview/activityBar.mts

index f361cbe8e1c33a91ab0ba5ba7ca51c4a79328293..fd26241811ad0c5b37351a11ebeff58cd7a13203 100644 (file)
       },
       {
         "command": "raspberry-pi-pico.runProject",
-        "title": "Run Pico Project",
+        "title": "Run Pico Project (USB)",
         "category": "Raspberry Pi Pico",
         "enablement": "raspberry-pi-pico.isPicoProject"
       },
         "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": {
index 573dc2897212aeea936eb9701255f5f4aa991121..efe499bbb4f0af581da2d0f0c169a7714e5e3698 100644 (file)
@@ -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> {
diff --git a/src/commands/flashProjectSwd.mts b/src/commands/flashProjectSwd.mts
new file mode 100644 (file)
index 0000000..4b52eae
--- /dev/null
@@ -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;
+    }
+  }
+}
index 8e544c7d4d89c65e2732e2b79f516e5c19972956..709ffb9e50cd90f2a1c8e4648797121cd9d8b280 100644 (file)
@@ -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");
@@ -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(),
index 0b936daedcc1ea07633611b8d764c86fec2dfb30..c2c330b0a673f6ad9f93877d2493b4b7e949edca 100644 (file)
@@ -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(
@@ -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";
@@ -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");
@@ -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,
This page took 0.038922 seconds and 4 git commands to generate.