Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit f6e5343

Browse files
codeworm96yaohaizh
authored andcommitted
Pause UsbDetector during uploading (#372)
* Make UsbDetector a singleton * Add pause/resume listening in UsbDetector * Pause usbDetector listening during the upload * Refine code
1 parent dc7508f commit f6e5343

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/arduino/arduino.ts

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { VscodeSettings } from "./vscodeSettings";
2020

2121
import { arduinoChannel } from "../common/outputChannel";
2222
import { SerialMonitor } from "../serialmonitor/serialMonitor";
23+
import { UsbDetector } from "../serialmonitor/usbDetector";
2324

2425
/**
2526
* Represent an Arduino application based on the official Arduino IDE.
@@ -114,6 +115,7 @@ export class ArduinoApp {
114115
const serialMonitor = SerialMonitor.getInstance();
115116

116117
const needRestore = await serialMonitor.closeSerialMonitor(dc.port);
118+
UsbDetector.getInstance().pauseListening();
117119
await vscode.workspace.saveAll(false);
118120

119121
const appPath = path.join(vscode.workspace.rootPath, dc.sketch);
@@ -122,6 +124,7 @@ export class ArduinoApp {
122124
args.push("--verbose");
123125
}
124126
await util.spawn(this._settings.commandPath, arduinoChannel.channel, args).then(async () => {
127+
UsbDetector.getInstance().resumeListening();
125128
if (needRestore) {
126129
await serialMonitor.openSerialMonitor();
127130
}

src/extension.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import * as Logger from "./logger/logger";
1919
import { SerialMonitor } from "./serialmonitor/serialMonitor";
2020
import { UsbDetector } from "./serialmonitor/usbDetector";
2121

22-
let usbDetector: UsbDetector = null;
2322
const status: any = {};
2423

2524
export async function activate(context: vscode.ExtensionContext) {
@@ -184,8 +183,8 @@ export async function activate(context: vscode.ExtensionContext) {
184183
const completionProvider = new CompletionProvider();
185184
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(ARDUINO_MODE, completionProvider, "<", '"', "."));
186185

187-
usbDetector = new UsbDetector(context.extensionPath);
188-
usbDetector.startListening();
186+
UsbDetector.getInstance().initialize(context.extensionPath);
187+
UsbDetector.getInstance().startListening();
189188

190189
if (vscode.workspace.rootPath && (
191190
util.fileExistsSync(path.join(vscode.workspace.rootPath, ARDUINO_CONFIG_FILE))
@@ -222,8 +221,6 @@ export async function activate(context: vscode.ExtensionContext) {
222221
export async function deactivate() {
223222
const monitor = SerialMonitor.getInstance();
224223
await monitor.closeSerialMonitor(null, false);
225-
if (usbDetector) {
226-
usbDetector.stopListening();
227-
}
224+
UsbDetector.getInstance().stopListening();
228225
Logger.traceUserData("deactivate-extension");
229226
}

src/serialmonitor/usbDetector.ts

+34-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,26 @@ import { SerialMonitor } from "./serialMonitor";
1414

1515
export class UsbDetector {
1616

17+
public static getInstance(): UsbDetector {
18+
if (!UsbDetector._instance) {
19+
UsbDetector._instance = new UsbDetector();
20+
}
21+
return UsbDetector._instance;
22+
}
23+
24+
private static _instance: UsbDetector;
25+
1726
private _usbDetector;
1827

1928
private _boardDescriptors = null;
2029

21-
constructor(
22-
private _extensionRoot: string) {
30+
private _extensionRoot = null;
31+
32+
private constructor() {
33+
}
34+
35+
public initialize(extensionRoot: string) {
36+
this._extensionRoot = extensionRoot;
2337
}
2438

2539
public async startListening() {
@@ -32,6 +46,10 @@ export class UsbDetector {
3246
return;
3347
}
3448

49+
if (this._extensionRoot === null) {
50+
throw new Error("UsbDetector should be initialized before using.");
51+
}
52+
3553
this._usbDetector.on("add", async (device) => {
3654
if (device.vendorId && device.productId) {
3755
const deviceDescriptor = this.getUsbDeviceDescriptor(
@@ -106,6 +124,20 @@ export class UsbDetector {
106124
}
107125
}
108126

127+
public pauseListening() {
128+
if (this._usbDetector) {
129+
this._usbDetector.stopMonitoring();
130+
}
131+
}
132+
133+
public resumeListening() {
134+
if (this._usbDetector) {
135+
this._usbDetector.startMonitoring();
136+
} else {
137+
this.startListening();
138+
}
139+
}
140+
109141
private switchBoard(bd: IBoard, vid: string, pid: string) {
110142
ArduinoContext.boardManager.doChangeBoardType(bd);
111143
const monitor = SerialMonitor.getInstance();

0 commit comments

Comments
 (0)