55'use strict' ;
66import { html , render } from 'https://cdn.jsdelivr.net/npm/lit-html/+esm' ;
77import { asyncAppend } from 'https://cdn.jsdelivr.net/npm/lit-html/directives/async-append/+esm' ;
8- import * as esptoolPackage from "https://cdn.jsdelivr.net/npm/esp-web-flasher@5.1.2/dist/web/index.js/+esm"
9-
8+ import { ESPLoader , Transport } from "https://unpkg.com/esptool-js@0.5.3/bundle.js" ;
109export const ESP_ROM_BAUD = 115200 ;
1110
1211export class InstallButton extends HTMLButtonElement {
@@ -15,12 +14,15 @@ export class InstallButton extends HTMLButtonElement {
1514
1615 constructor ( ) {
1716 super ( ) ;
17+ this . baudRate = ESP_ROM_BAUD ;
1818 this . dialogElements = { } ;
1919 this . currentFlow = null ;
2020 this . currentStep = 0 ;
2121 this . currentDialogElement = null ;
22- this . port = null ;
23- this . espStub = null ;
22+ this . device = null ;
23+ this . transport = null ;
24+ this . esploader = null ;
25+ this . chip = null ;
2426 this . dialogCssClass = "install-dialog" ;
2527 this . connected = this . connectionStates . DISCONNECTED ;
2628 this . menuTitle = "Installer Menu" ;
@@ -343,13 +345,17 @@ export class InstallButton extends HTMLButtonElement {
343345 return macAddr . map ( ( value ) => value . toString ( 16 ) . toUpperCase ( ) . padStart ( 2 , "0" ) ) . join ( ":" ) ;
344346 }
345347
346- async disconnect ( ) {
347- if ( this . espStub ) {
348- await espStub . disconnect ( ) ;
349- await espStub . port . close ( ) ;
350- this . updateUIConnected ( this . connectionStates . DISCONNECTED ) ;
351- this . espStub = null ;
348+ async espDisconnect ( ) {
349+ if ( this . transport ) {
350+ await this . transport . disconnect ( ) ;
351+ await this . transport . waitForUnlock ( 1500 ) ;
352+ this . updateEspConnected ( this . connectionStates . DISCONNECTED ) ;
353+ this . transport = null ;
354+ this . device = null ;
355+ this . chip = null ;
356+ return true ;
352357 }
358+ return false ;
353359 }
354360
355361 async runFlow ( flow ) {
@@ -390,6 +396,17 @@ export class InstallButton extends HTMLButtonElement {
390396 }
391397 }
392398
399+ async advanceSteps ( stepCount ) {
400+ if ( ! this . currentFlow ) {
401+ return ;
402+ }
403+
404+ if ( this . currentStep <= this . currentFlow . steps . length + stepCount ) {
405+ this . currentStep += stepCount ;
406+ await this . currentFlow . steps [ this . currentStep ] . bind ( this ) ( ) ;
407+ }
408+ }
409+
393410 async showMenu ( ) {
394411 // Display Menu
395412 this . showDialog ( this . dialogs . menu ) ;
@@ -405,38 +422,60 @@ export class InstallButton extends HTMLButtonElement {
405422 this . showDialog ( this . dialogs . error , { message : message } ) ;
406423 }
407424
408- async setBaudRateIfChipSupports ( chipType , baud ) {
409- if ( baud == ESP_ROM_BAUD ) { return } // already the default
410-
411- if ( chipType == esptoolPackage . CHIP_FAMILY_ESP32 ) { // only supports the default
412- this . logMsg ( `ESP32 Chip only works at 115200 instead of the preferred ${ baud } . Staying at 115200...` ) ;
413- return
414- }
425+ async setBaudRateIfChipSupports ( baud ) {
426+ if ( baud == this . baudRate ) { return } // already the current setting
415427
416428 await this . changeBaudRate ( baud ) ;
417429 }
418430
419431 async changeBaudRate ( baud ) {
420- if ( this . espStub && this . baudRates . includes ( baud ) ) {
421- await this . espStub . setBaudrate ( baud ) ;
432+ if ( this . baudRates . includes ( baud ) ) {
433+ if ( this . transport == null ) {
434+ this . baudRate = baud ;
435+ } else {
436+ this . errorMsg ( "Cannot change baud rate while connected." ) ;
437+ }
422438 }
423439 }
424440
425- async espHardReset ( bootloader = false ) {
426- if ( this . espStub ) {
427- await this . espStub . hardReset ( bootloader ) ;
441+ async espHardReset ( ) {
442+ if ( this . esploader ) {
443+ await this . esploader . hardReset ( ) ;
428444 }
429445 }
430446
431447 async espConnect ( logger ) {
432- // - Request a port and open a connection.
433- this . port = await navigator . serial . requestPort ( ) ;
434-
435448 logger . log ( "Connecting..." ) ;
436- await this . port . open ( { baudRate : ESP_ROM_BAUD } ) ;
449+
450+ if ( this . device === null ) {
451+ this . device = await navigator . serial . requestPort ( { } ) ;
452+ this . transport = new Transport ( this . device , true ) ;
453+ }
454+
455+ const espLoaderTerminal = {
456+ clean ( ) {
457+ // Clear the terminal
458+ } ,
459+ writeLine ( data ) {
460+ logger . log ( data ) ;
461+ } ,
462+ write ( data ) {
463+ logger . log ( data ) ;
464+ } ,
465+ } ;
466+
467+ const loaderOptions = {
468+ transport : this . transport ,
469+ baudrate : this . baudRate ,
470+ terminal : espLoaderTerminal ,
471+ debugLogging : false ,
472+ } ;
473+
474+ this . esploader = new ESPLoader ( loaderOptions ) ;
475+ this . chip = await this . esploader . main ( ) ;
437476
438477 logger . log ( "Connected successfully." ) ;
439478
440- return new esptoolPackage . ESPLoader ( this . port , logger ) ;
479+ return this . esploader ;
441480 } ;
442481}
0 commit comments