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

Pause UsbDetector during uploading #372

Merged
merged 6 commits into from
Jul 20, 2017
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
3 changes: 3 additions & 0 deletions src/arduino/arduino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { VscodeSettings } from "./vscodeSettings";

import { arduinoChannel } from "../common/outputChannel";
import { SerialMonitor } from "../serialmonitor/serialMonitor";
import { UsbDetector } from "../serialmonitor/usbDetector";

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

const needRestore = await serialMonitor.closeSerialMonitor(dc.port);
UsbDetector.getInstance().pauseListening();
await vscode.workspace.saveAll(false);

const appPath = path.join(vscode.workspace.rootPath, dc.sketch);
Expand All @@ -122,6 +124,7 @@ export class ArduinoApp {
args.push("--verbose");
}
await util.spawn(this._settings.commandPath, arduinoChannel.channel, args).then(async () => {
UsbDetector.getInstance().resumeListening();
if (needRestore) {
await serialMonitor.openSerialMonitor();
}
Expand Down
9 changes: 3 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import * as Logger from "./logger/logger";
import { SerialMonitor } from "./serialmonitor/serialMonitor";
import { UsbDetector } from "./serialmonitor/usbDetector";

let usbDetector: UsbDetector = null;
const status: any = {};

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

usbDetector = new UsbDetector(context.extensionPath);
usbDetector.startListening();
UsbDetector.getInstance().initialize(context.extensionPath);
UsbDetector.getInstance().startListening();

if (vscode.workspace.rootPath && (
util.fileExistsSync(path.join(vscode.workspace.rootPath, ARDUINO_CONFIG_FILE))
Expand Down Expand Up @@ -222,8 +221,6 @@ export async function activate(context: vscode.ExtensionContext) {
export async function deactivate() {
const monitor = SerialMonitor.getInstance();
await monitor.closeSerialMonitor(null, false);
if (usbDetector) {
usbDetector.stopListening();
}
UsbDetector.getInstance().stopListening();
Logger.traceUserData("deactivate-extension");
}
36 changes: 34 additions & 2 deletions src/serialmonitor/usbDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,26 @@ import { SerialMonitor } from "./serialMonitor";

export class UsbDetector {

public static getInstance(): UsbDetector {
if (!UsbDetector._instance) {
UsbDetector._instance = new UsbDetector();
}
return UsbDetector._instance;
}

private static _instance: UsbDetector;

private _usbDetector;

private _boardDescriptors = null;

constructor(
private _extensionRoot: string) {
private _extensionRoot = null;

private constructor() {
}

public initialize(extensionRoot: string) {
this._extensionRoot = extensionRoot;
}

public async startListening() {
Expand All @@ -32,6 +46,10 @@ export class UsbDetector {
return;
}

if (this._extensionRoot === null) {
throw new Error("UsbDetector should be initialized before using.");
}

this._usbDetector.on("add", async (device) => {
if (device.vendorId && device.productId) {
const deviceDescriptor = this.getUsbDeviceDescriptor(
Expand Down Expand Up @@ -106,6 +124,20 @@ export class UsbDetector {
}
}

public pauseListening() {
if (this._usbDetector) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we resume the existing stopListening/startListening? The two new APIs seems duplicate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, it is possible but the startListening() method needs to be modified. The current startListening() just sets the add event listener and does not explicitly start listening. It works previously just because the detector is default to be listening after initialization. startListening() cannot switch back the status of detector correctly yet.

My concern is that it may be unnecessary to set the add event listener again and again.

this._usbDetector.stopMonitoring();
}
}

public resumeListening() {
if (this._usbDetector) {
this._usbDetector.startMonitoring();
} else {
this.startListening();
}
}

private switchBoard(bd: IBoard, vid: string, pid: string) {
ArduinoContext.boardManager.doChangeBoardType(bd);
const monitor = SerialMonitor.getInstance();
Expand Down