Skip to content
This repository was 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 3 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.extensionRoot = context.extensionPath;
Copy link
Contributor

Choose a reason for hiding this comment

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

You can add Initialize(extensionRoot : string) method to UsbDector instance for passing value but not use the module to pass object value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good advice.

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");
}
27 changes: 26 additions & 1 deletion src/serialmonitor/usbDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,22 @@ import { SerialMonitor } from "./serialMonitor";

export class UsbDetector {

public static extensionRoot: string;

public static getInstance(): UsbDetector {
if (!UsbDetector._instance) {
UsbDetector._instance = new UsbDetector(UsbDetector.extensionRoot);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should check the extensionRoot value carefully since there is no any explicit of the sequence of the calling order

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure what behavior should be if the assumption is violated. Can you give me some advice?

}
return UsbDetector._instance;
}

private static _instance: UsbDetector;

private _usbDetector;

private _boardDescriptors = null;

constructor(
private constructor(
private _extensionRoot: string) {
}

Expand Down Expand Up @@ -106,6 +117,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