@@ -49,6 +49,7 @@ const term = new Terminal({ cols: 120, rows: 40 });
4949term . open ( terminal ) ;
5050
5151let device = null ;
52+ let deviceInfo = null ;
5253let transport : Transport ;
5354let chip : string = null ;
5455let esploader : ESPLoader ;
@@ -105,6 +106,7 @@ connectButton.onclick = async () => {
105106 try {
106107 if ( device === null ) {
107108 device = await serialLib . requestPort ( { } ) ;
109+ deviceInfo = device . getInfo ( ) ;
108110 transport = new Transport ( device , true ) ;
109111 }
110112 const flashOptions = {
@@ -228,6 +230,7 @@ function removeRow(row: HTMLTableRowElement) {
228230 */
229231function cleanUp ( ) {
230232 device = null ;
233+ deviceInfo = null ;
231234 transport = null ;
232235 chip = null ;
233236 initializePartitionTable ( ) ;
@@ -461,11 +464,50 @@ disconnectButton.onclick = async () => {
461464} ;
462465
463466let isConsoleClosed = false ;
467+ let isReconnecting = false ;
468+
469+ const sleep = async ( ms : number ) => {
470+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
471+ } ;
472+
464473consoleStartButton . onclick = async ( ) => {
465474 if ( device === null ) {
466475 device = await serialLib . requestPort ( { } ) ;
467476 transport = new Transport ( device , true ) ;
477+ deviceInfo = device . getInfo ( ) ;
478+
479+ // Set up device lost callback
480+ transport . setDeviceLostCallback ( async ( ) => {
481+ if ( ! isConsoleClosed && ! isReconnecting ) {
482+ term . writeln ( "\n[DEVICE LOST] Device disconnected. Click 'Reconnect' to restore connection..." ) ;
483+ await sleep ( 1000 ) ;
484+ isReconnecting = true ;
485+ term . writeln ( "\n[RECONNECT] Attempting to reconnect..." ) ;
486+ if ( serialLib && serialLib . getPorts ) {
487+ const ports = await serialLib . getPorts ( ) ;
488+ if ( ports . length > 0 ) {
489+ const newDevice = ports . find (
490+ ( port ) =>
491+ port . getInfo ( ) . usbVendorId === deviceInfo . usbVendorId &&
492+ port . getInfo ( ) . usbProductId === deviceInfo . usbProductId ,
493+ ) ;
494+ device = newDevice ;
495+ transport . updateDevice ( device ) ;
496+ term . writeln ( "[RECONNECT] Found previously authorized device, connecting..." ) ;
497+ await transport . connect ( parseInt ( consoleBaudrates . value ) ) ;
498+ term . writeln ( "[RECONNECT] Successfully reconnected!" ) ;
499+ consoleStopButton . style . display = "initial" ;
500+ resetButton . style . display = "initial" ;
501+ isReconnecting = false ;
502+
503+ startConsoleReading ( ) ;
504+ return ;
505+ }
506+ }
507+ }
508+ } ) ;
468509 }
510+
469511 lblConsoleFor . style . display = "block" ;
470512 lblConsoleBaudrate . style . display = "none" ;
471513 consoleBaudrates . style . display = "none" ;
@@ -476,21 +518,45 @@ consoleStartButton.onclick = async () => {
476518
477519 await transport . connect ( parseInt ( consoleBaudrates . value ) ) ;
478520 isConsoleClosed = false ;
521+ isReconnecting = false ;
522+
523+ startConsoleReading ( ) ;
524+ } ;
525+
526+ /**
527+ * Start the console reading loop
528+ */
529+ async function startConsoleReading ( ) {
530+ if ( isConsoleClosed || ! transport ) return ;
479531
480- while ( true && ! isConsoleClosed ) {
532+ try {
481533 const readLoop = transport . rawRead ( ) ;
482- const { value, done } = await readLoop . next ( ) ;
483534
484- if ( done || ! value ) {
485- break ;
535+ while ( true && ! isConsoleClosed ) {
536+ const { value, done } = await readLoop . next ( ) ;
537+
538+ if ( done || ! value ) {
539+ break ;
540+ }
541+
542+ if ( value ) {
543+ term . write ( value ) ;
544+ }
545+ }
546+ } catch ( error ) {
547+ if ( ! isConsoleClosed ) {
548+ term . writeln ( `\n[CONSOLE ERROR] ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
486549 }
487- term . write ( value ) ;
488550 }
489- console . log ( "quitting console" ) ;
490- } ;
551+
552+ if ( ! isConsoleClosed ) {
553+ term . writeln ( "\n[CONSOLE] Connection lost, waiting for reconnection..." ) ;
554+ }
555+ }
491556
492557consoleStopButton . onclick = async ( ) => {
493558 isConsoleClosed = true ;
559+ isReconnecting = false ;
494560 if ( transport ) {
495561 await transport . disconnect ( ) ;
496562 await transport . waitForUnlock ( 1500 ) ;
0 commit comments