Skip to content

Commit 21e3835

Browse files
committed
Added autoConnect feature
1 parent 8fe14b4 commit 21e3835

File tree

4 files changed

+94
-29
lines changed

4 files changed

+94
-29
lines changed

src/activator.mts

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export default class Activator {
3939
private stubs?: Stubs;
4040
private picoFs?: PicoWFs;
4141

42+
private autoConnectTimer?: NodeJS.Timer;
43+
private comDevice?: string;
44+
4245
constructor() {
4346
this.logger = new Logger("Activator");
4447
}
@@ -110,10 +113,10 @@ export default class Activator {
110113
this.stubs = new Stubs();
111114
await this.stubs.update();
112115

113-
let comDevice = await settings.getComDevice();
116+
this.comDevice = await settings.getComDevice();
114117

115-
if (comDevice === undefined || comDevice === "") {
116-
comDevice = undefined;
118+
if (this.comDevice === undefined || this.comDevice == "") {
119+
this.comDevice = undefined;
117120

118121
vscode.window
119122
.showErrorMessage(
@@ -131,12 +134,14 @@ export default class Activator {
131134
this.ui.init();
132135

133136
this.pyb = new PyboardRunner(
134-
comDevice ?? "default",
137+
this.comDevice ?? "default",
135138
this.pyboardOnError.bind(this),
136139
this.pyboardOnExit.bind(this),
137140
pyCommand
138141
);
139142

143+
this.setupAutoConnect(settings);
144+
140145
const terminal = new Terminal(async () => {
141146
if (this.pyb?.isPipeConnected()) {
142147
const result = await this.pyb?.executeCommand(
@@ -239,7 +244,7 @@ export default class Activator {
239244

240245
if (
241246
settings.getBoolean(SettingsKey.openOnStart) &&
242-
comDevice !== undefined
247+
this.comDevice !== undefined
243248
) {
244249
await focusTerminal(terminalOptions);
245250
}
@@ -273,10 +278,11 @@ export default class Activator {
273278
disposable = vscode.commands.registerCommand(
274279
"picowgo.connect",
275280
async () => {
276-
comDevice = await settings.getComDevice();
277-
if (comDevice !== undefined) {
281+
this.comDevice = await settings.getComDevice();
282+
if (this.comDevice !== undefined) {
278283
await this.ui?.init();
279-
this.pyb?.switchDevice(comDevice);
284+
this.pyb?.switchDevice(this.comDevice);
285+
this.setupAutoConnect(settings);
280286
}
281287
}
282288
);
@@ -286,6 +292,7 @@ export default class Activator {
286292
disposable = vscode.commands.registerCommand(
287293
"picowgo.disconnect",
288294
async () => {
295+
clearInterval(this.autoConnectTimer);
289296
await this.pyb?.disconnect();
290297
}
291298
);
@@ -317,7 +324,8 @@ export default class Activator {
317324
const data = await this.pyb.runFile(file, (data: string) => {
318325
// only freeze after operation has started
319326
if (!frozen) {
320-
terminal?.freeze();
327+
commandExecuting = true;
328+
terminal?.clean(true);
321329
terminal?.write("\r\n");
322330
this.ui?.userOperationStarted();
323331
frozen = true;
@@ -329,6 +337,7 @@ export default class Activator {
329337
const result = data as PyOutCommandResult;
330338
// TODO: reflect result.result somehow
331339
}
340+
commandExecuting = false;
332341
terminal?.melt();
333342
terminal?.write("\r\n");
334343
terminal?.prompt();
@@ -362,7 +371,8 @@ export default class Activator {
362371
(data: string) => {
363372
// only freeze after operation has started
364373
if (!frozen) {
365-
terminal?.freeze();
374+
commandExecuting = true;
375+
terminal?.clean(true);
366376
terminal?.write("\r\n");
367377
this.ui?.userOperationStarted();
368378
frozen = true;
@@ -372,6 +382,7 @@ export default class Activator {
372382
true
373383
);
374384
this.ui?.userOperationStopped();
385+
commandExecuting = false;
375386
terminal?.melt();
376387
terminal?.write("\r\n");
377388
terminal?.prompt();
@@ -401,7 +412,8 @@ export default class Activator {
401412
(data: string) => {
402413
// only freeze after operation has started
403414
if (!frozen) {
404-
terminal?.freeze();
415+
commandExecuting = true;
416+
terminal?.clean(true);
405417
terminal?.write("\r\n");
406418
this.ui?.userOperationStarted();
407419
frozen = true;
@@ -410,6 +422,7 @@ export default class Activator {
410422
},
411423
true
412424
);
425+
commandExecuting = false;
413426
this.ui?.userOperationStopped();
414427
if (data.type === PyOutType.commandResult) {
415428
const result = data as PyOutCommandResult;
@@ -639,14 +652,16 @@ export default class Activator {
639652
"picowgo.toggleConnect",
640653
async () => {
641654
if (this.pyb?.isPipeConnected()) {
655+
clearInterval(this.autoConnectTimer);
642656
await this.pyb?.disconnect();
643657
} else {
644-
comDevice = await settings.getComDevice();
645-
if (comDevice === undefined) {
658+
this.comDevice = await settings.getComDevice();
659+
if (this.comDevice == undefined) {
646660
vscode.window.showErrorMessage("No COM device found!");
647661
} else {
648662
await this.ui?.init();
649-
this.pyb?.switchDevice(comDevice);
663+
this.pyb?.switchDevice(this.comDevice);
664+
this.setupAutoConnect(settings);
650665
}
651666
}
652667
}
@@ -772,8 +787,8 @@ export default class Activator {
772787
});
773788

774789
if (port !== undefined) {
775-
comDevice = port;
776-
this.pyb?.switchDevice(comDevice);
790+
this.comDevice = port;
791+
this.pyb?.switchDevice(this.comDevice);
777792
}
778793
}
779794
);
@@ -856,14 +871,54 @@ export default class Activator {
856871
return this.ui;
857872
}
858873

874+
private setupAutoConnect(settings: Settings): void {
875+
this.autoConnectTimer = setInterval(async () => {
876+
await this.pyb?.checkStatus();
877+
if (this.pyb?.isPipeConnected()) {
878+
this.ui?.refreshState(true);
879+
return;
880+
}
881+
this.ui?.refreshState(false);
882+
const autoPort = settings.getBoolean(SettingsKey.autoConnect);
883+
884+
const ports = await PyboardRunner.getPorts(settings.pythonExecutable);
885+
if (ports.ports.length === 0) {
886+
return;
887+
}
888+
889+
// try to connect to previously connected device first
890+
if (this.comDevice && ports.ports.includes(this.comDevice)) {
891+
// try to reconnect
892+
this.pyb?.switchDevice(this.comDevice);
893+
await new Promise(resolve => setTimeout(resolve, 1000));
894+
if (this.pyb?.isPipeConnected()) {
895+
return;
896+
}
897+
}
898+
899+
if (autoPort) {
900+
const port = ports.ports[0];
901+
this.comDevice = port;
902+
this.pyb?.switchDevice(port);
903+
}
904+
}, 2500);
905+
}
906+
859907
private pyboardOnError(data: Buffer | undefined) {
860-
if (data === undefined) {
861-
this.ui?.refreshState(true);
862-
this.logger.info("Connection to Pico successfully established");
908+
if (
909+
data === undefined &&
910+
this.comDevice !== undefined &&
911+
this.comDevice !== "" &&
912+
this.comDevice !== "default"
913+
) {
914+
//this.ui?.refreshState(true);
915+
this.logger.info("Connection to wrapper successfully established");
863916

864917
return;
865918
} else {
866-
vscode.window.showErrorMessage(data.toString("utf-8"));
919+
if (data) {
920+
vscode.window.showErrorMessage(data.toString("utf-8"));
921+
}
867922
}
868923
}
869924

src/osHelper.mts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ export async function getPythonCommand(): Promise<string | undefined> {
1919
const pythonCommand: string | undefined = pythonCommands[currentPlatform];
2020

2121
return new Promise(resolve => {
22-
exec(`${pythonCommand} --version`, (error, stdout, stderr) => {
23-
if (error) {
24-
console.error(`Error executing ${pythonCommand}: ${error.message}`);
25-
resolve(undefined);
26-
} else {
27-
console.debug(`Python version: ${stdout || stderr}`);
28-
resolve(pythonCommand);
22+
exec(
23+
`${pythonCommand} --version`,
24+
{ timeout: 1000 },
25+
(error, stdout, stderr) => {
26+
if (error) {
27+
console.error(`Error executing ${pythonCommand}: ${error.message}`);
28+
resolve(undefined);
29+
} else {
30+
console.debug(`Python version: ${stdout || stderr}`);
31+
resolve(pythonCommand);
32+
}
2933
}
30-
});
34+
);
3135
});
3236
}
3337

src/terminal.mts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,16 @@ export class Terminal implements Pseudoterminal {
5858
public freeze(): void {
5959
this.isForzen = true;
6060

61+
this.clean(false);
62+
}
63+
64+
public clean(waitingForPrompt?: boolean): void {
65+
this.waitingForPrompt! != waitingForPrompt;
66+
6167
// TODO: maybe restore current state
6268
this.buffer = "";
6369
this.multilineMode = false;
6470
this.indentation = 0;
65-
this.waitingForPrompt = false;
6671
this.xCursor = 0;
6772
}
6873

src/ui.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export default class UI {
8181

8282
private setButton(name: string, icon: string, text: string): void {
8383
this.items[name].text = `$(${icon}) ${text}`;
84+
this.items[name].show();
8485
}
8586

8687
private setState(connected: boolean): void {

0 commit comments

Comments
 (0)