@@ -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 ;
@@ -627,7 +640,7 @@ export default class Activator {
627640
628641 // [Command] Global settings
629642 disposable = vscode . commands . registerCommand (
630- "picowgo.globalsettings " ,
643+ "picowgo.globalSettings " ,
631644 async ( ) => {
632645 openSettings ( ) ;
633646 }
@@ -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
0 commit comments