@@ -38,6 +38,9 @@ const firmware_flasher = {
3838 config : { } ,
3939 developmentFirmwareLoaded : false , // Is the firmware to be flashed from the development branch?
4040 cancelBuild : false ,
41+ // Properties to preserve firmware state during flashing
42+ preFlashingMessage : null ,
43+ preFlashingMessageType : null ,
4144} ;
4245
4346firmware_flasher . initialize = async function ( callback ) {
@@ -782,20 +785,14 @@ firmware_flasher.initialize = async function (callback) {
782785
783786 // Common function to reset flashing state on errors
784787 const resetFlashingState = ( ) => {
785- self . isFlashing = false ;
786- self . enableFlashButton ( true ) ;
787- self . enableDfuExitButton ( PortHandler . dfuAvailable ) ;
788- self . enableLoadRemoteFileButton ( true ) ;
789- self . enableLoadFileButton ( true ) ;
790- self . flashingMessage ( i18n . getMessage ( "firmwareFlasherFirmwareNotLoaded" ) , self . FLASH_MESSAGE_TYPES . NEUTRAL ) ;
791- GUI . interval_resume ( "sponsor" ) ;
788+ self . resetFlashingState ( ) ;
792789 } ;
793790
794791 if ( isDFU ) {
795792 tracking . sendEvent ( tracking . EVENT_CATEGORIES . FLASHING , "DFU Flashing" , {
796793 filename : self . filename || null ,
797794 } ) ;
798- DFU . connect ( port , firmware , options ) . catch ( error => {
795+ DFU . connect ( port , firmware , options ) . catch ( ( error ) => {
799796 console . error ( `${ self . logHead } DFU connection failed:` , error ) ;
800797 resetFlashingState ( ) ;
801798 } ) ;
@@ -813,24 +810,23 @@ firmware_flasher.initialize = async function (callback) {
813810
814811 tracking . sendEvent ( tracking . EVENT_CATEGORIES . FLASHING , "Flashing" , { filename : self . filename || null } ) ;
815812
816- STM32 . connect ( port , baud , firmware , options ) . catch ( error => {
817- console . error ( `${ self . logHead } STM32 connection failed:` , error ) ;
818- resetFlashingState ( ) ;
819- } ) ;
813+ STM32 . connect ( port , baud , firmware , options ) ;
820814 } else {
821815 // Maybe the board is in DFU mode, but it does not have permissions. Ask for them.
822816 console . log ( `${ self . logHead } No valid port detected, asking for permissions` ) ;
823817
824- DFU . requestPermission ( ) . then ( ( device ) => {
825- DFU . connect ( device . path , firmware , options ) . catch ( error => {
826- console . error ( `${ self . logHead } DFU permission connection failed:` , error ) ;
818+ DFU . requestPermission ( )
819+ . then ( ( device ) => {
820+ DFU . connect ( device . path , firmware , options ) . catch ( ( error ) => {
821+ console . error ( `${ self . logHead } DFU permission connection failed:` , error ) ;
822+ resetFlashingState ( ) ;
823+ } ) ;
824+ } )
825+ . catch ( ( error ) => {
826+ // Error or user cancelled: reset flashing state and re-enable button
827+ console . error ( `${ self . logHead } DFU permission request failed:` , error ) ;
827828 resetFlashingState ( ) ;
828829 } ) ;
829- } ) . catch ( ( error ) => {
830- // Error or user cancelled: reset flashing state and re-enable button
831- console . error ( `${ self . logHead } DFU permission request failed:` , error ) ;
832- resetFlashingState ( ) ;
833- } ) ;
834830 }
835831 }
836832
@@ -1403,6 +1399,9 @@ firmware_flasher.initialize = async function (callback) {
14031399 return ;
14041400 }
14051401
1402+ // Preserve current firmware message state before flashing
1403+ self . preservePreFlashingState ( ) ;
1404+
14061405 self . isFlashing = true ;
14071406 GUI . interval_pause ( "sponsor" ) ;
14081407
@@ -1522,6 +1521,43 @@ firmware_flasher.enableDfuExitButton = function (enabled) {
15221521 $ ( "a.exit_dfu" ) . toggleClass ( "disabled" , ! enabled ) ;
15231522} ;
15241523
1524+ firmware_flasher . resetFlashingState = function ( ) {
1525+ console . log ( `${ this . logHead } Resetting flashing state` ) ;
1526+ this . isFlashing = false ;
1527+ this . enableFlashButton ( this . parsed_hex || this . uf2_binary ? true : false ) ; // Only enable if firmware is loaded
1528+ this . enableDfuExitButton ( PortHandler . dfuAvailable ) ;
1529+ this . enableLoadRemoteFileButton ( true ) ;
1530+ this . enableLoadFileButton ( true ) ;
1531+
1532+ // Restore pre-flashing message if firmware is still loaded, otherwise show "not loaded"
1533+ if ( this . parsed_hex || this . uf2_binary ) {
1534+ if ( this . preFlashingMessage && this . preFlashingMessageType ) {
1535+ this . flashingMessage ( this . preFlashingMessage , this . preFlashingMessageType ) ;
1536+ }
1537+ } else {
1538+ this . flashingMessage ( i18n . getMessage ( "firmwareFlasherFirmwareNotLoaded" ) , this . FLASH_MESSAGE_TYPES . NEUTRAL ) ;
1539+ }
1540+
1541+ GUI . interval_resume ( "sponsor" ) ;
1542+ } ;
1543+
1544+ firmware_flasher . preservePreFlashingState = function ( ) {
1545+ // Preserve the current firmware message and type before flashing starts
1546+ const progressLabel = $ ( "span.progressLabel" ) ;
1547+ this . preFlashingMessage = progressLabel . html ( ) ;
1548+
1549+ // Determine the current message type based on CSS classes
1550+ if ( progressLabel . hasClass ( "valid" ) ) {
1551+ this . preFlashingMessageType = this . FLASH_MESSAGE_TYPES . VALID ;
1552+ } else if ( progressLabel . hasClass ( "invalid" ) ) {
1553+ this . preFlashingMessageType = this . FLASH_MESSAGE_TYPES . INVALID ;
1554+ } else if ( progressLabel . hasClass ( "actionRequired" ) ) {
1555+ this . preFlashingMessageType = this . FLASH_MESSAGE_TYPES . ACTION ;
1556+ } else {
1557+ this . preFlashingMessageType = this . FLASH_MESSAGE_TYPES . NEUTRAL ;
1558+ }
1559+ } ;
1560+
15251561firmware_flasher . refresh = function ( callback ) {
15261562 const self = this ;
15271563
0 commit comments