@@ -740,10 +740,60 @@ firmware_flasher.initialize = async function (callback) {
740740 STM32 . rebootMode = 0 ;
741741 GUI . connect_lock = false ;
742742 startFlashing ( ) ;
743+ } else {
744+ // Auto-detect board when firmware flasher tab is active and no flash-on-connect
745+ console . log ( `${ self . logHead } Auto-detecting board for connected USB device` ) ;
746+ AutoDetect . verifyBoard ( ) ;
747+ }
748+ }
749+
750+ function detectedSerialDevice ( device ) {
751+ const isFlashOnConnect = $ ( "input.flash_on_connect" ) . is ( ":checked" ) ;
752+
753+ console . log ( `${ self . logHead } Detected serial device:` , device ) ;
754+ console . log ( `${ self . logHead } Reboot mode: %s, flash on connect` , STM32 . rebootMode , isFlashOnConnect ) ;
755+
756+ if ( STM32 . rebootMode || isFlashOnConnect ) {
757+ STM32 . rebootMode = 0 ;
758+ GUI . connect_lock = false ;
759+ startFlashing ( ) ;
760+ } else {
761+ // Auto-detect board when firmware flasher tab is active and no flash-on-connect
762+ console . log ( `${ self . logHead } Auto-detecting board for connected serial device` ) ;
763+ AutoDetect . verifyBoard ( ) ;
743764 }
744765 }
745766
746767 EventBus . $on ( "port-handler:auto-select-usb-device" , detectedUsbDevice ) ;
768+ EventBus . $on ( "port-handler:auto-select-serial-device" , detectedSerialDevice ) ;
769+
770+ // Also listen for port changes to catch reconnections
771+ function onPortChange ( port ) {
772+ console . log ( `${ self . logHead } Port changed to:` , port ) ;
773+ // Auto-detect board when port changes and we're on firmware flasher tab
774+ if ( port && port !== "0" && ! $ ( "input.flash_on_connect" ) . is ( ":checked" ) && ! STM32 . rebootMode ) {
775+ console . log ( `${ self . logHead } Auto-detecting board for port change` ) ;
776+ setTimeout ( ( ) => {
777+ AutoDetect . verifyBoard ( ) ;
778+ } , 500 ) ; // Small delay to ensure port is ready
779+ } else if ( ! port || port === "0" ) {
780+ // Clear board selection when no port is selected
781+ console . log ( `${ self . logHead } Clearing board selection - no port selected` ) ;
782+ $ ( 'select[name="board"]' ) . val ( "0" ) . trigger ( "change" ) ;
783+ }
784+ }
785+
786+ // Listen for device removal to clear board selection
787+ function onDeviceRemoved ( devicePath ) {
788+ console . log ( `${ self . logHead } Device removed:` , devicePath ) ;
789+ // Clear board selection when device is removed
790+ $ ( 'select[name="board"]' ) . val ( "0" ) . trigger ( "change" ) ;
791+ // Also clear any loaded firmware
792+ clearBufferedFirmware ( ) ;
793+ }
794+
795+ EventBus . $on ( "ports-input:change" , onPortChange ) ;
796+ EventBus . $on ( "port-handler:device-removed" , onDeviceRemoved ) ;
747797
748798 async function saveFirmware ( ) {
749799 const fileType = self . firmware_type ;
@@ -1458,6 +1508,17 @@ firmware_flasher.initialize = async function (callback) {
14581508 $ ( "a.exit_dfu" ) . removeClass ( "disabled" ) ;
14591509 }
14601510
1511+ // Auto-detect board if a drone is already connected when tab becomes active
1512+ if (
1513+ ( PortHandler . portAvailable && ! $ ( 'select[name="board"]' ) . val ( ) ) ||
1514+ $ ( 'select[name="board"]' ) . val ( ) === "0"
1515+ ) {
1516+ console . log ( `${ self . logHead } Auto-detecting board for already connected device` ) ;
1517+ setTimeout ( ( ) => {
1518+ AutoDetect . verifyBoard ( ) ;
1519+ } , 1000 ) ; // Small delay to ensure tab is fully loaded
1520+ }
1521+
14611522 GUI . content_ready ( callback ) ;
14621523 }
14631524
@@ -1476,6 +1537,12 @@ firmware_flasher.cleanup = function (callback) {
14761537 $ ( document ) . unbind ( "keypress" ) ;
14771538 $ ( document ) . off ( "click" , "span.progressLabel a" ) ;
14781539
1540+ // Clean up EventBus listeners
1541+ EventBus . $off ( "port-handler:auto-select-usb-device" ) ;
1542+ EventBus . $off ( "port-handler:auto-select-serial-device" ) ;
1543+ EventBus . $off ( "ports-input:change" ) ;
1544+ EventBus . $off ( "port-handler:device-removed" ) ;
1545+
14791546 if ( callback ) callback ( ) ;
14801547} ;
14811548
0 commit comments