@@ -54,12 +54,12 @@ def check_mac(s):
5454 return True
5555
5656class Progress :
57- def __init__ (self ):
57+ def __init__ (self , txt = 'Flashing progress: 0.0%' ):
5858 layout = [
59- [sg .Text ('Flashing progress: 0.0%' , key = 'txt' )],
59+ [sg .Text (txt , key = 'txt' )],
6060 [sg .ProgressBar (1.0 , orientation = 'h' , size = (20 ,20 ), key = 'progress' )],
6161 ]
62- self .self . window = sg .Window ("Progress" , layout , modal = True , finalize = True )
62+ self .window = sg .Window ("Progress" , layout , modal = True , finalize = True )
6363 def update (self , val ):
6464 self .window ['progress' ].update (val )
6565 self .window ['txt' ].update (f'Flashing progress: { val * 100 :.1f} %' )
@@ -83,8 +83,11 @@ def __init__(self, options, text):
8383 def wait (self ):
8484 event , values = self .window .Read ()
8585 self .window .close ()
86- type = getattr (dai .DeviceBootloader .Type , values ['bootType' ])
87- return (str (event ) == "Submit" , type )
86+ if values is not None :
87+ type = getattr (dai .DeviceBootloader .Type , values ['bootType' ])
88+ return (str (event ) == "Submit" , type )
89+ else :
90+ return (False , None )
8891
8992class SelectIP :
9093 def __init__ (self ):
@@ -158,17 +161,13 @@ def wait(self) -> dai.DeviceInfo:
158161 self .window .close ()
159162 return deviceSelected
160163
161- def flashBootloader (device : dai .DeviceInfo ):
164+ def flashBootloader (device : dai .DeviceInfo , type : dai . DeviceBootloader . Type ):
162165 try :
163- sel = SelectBootloader (['AUTO' , 'USB' , 'NETWORK' ], "Select bootloader type to flash." )
164- ok , type = sel .wait ()
165- if not ok :
166- print ("Flashing bootloader canceled." )
167- return
166+
167+ pr = Progress ('Connecting...' )
168168
169169 bl = dai .DeviceBootloader (device , True )
170170
171- pr = Progress ()
172171 progress = lambda p : pr .update (p )
173172 if type == dai .DeviceBootloader .Type .AUTO :
174173 type = bl .getType ()
@@ -178,22 +177,19 @@ def flashBootloader(device: dai.DeviceInfo):
178177 PrintException ()
179178 sg .Popup (f'{ ex } ' )
180179
181- def factoryReset (bl : dai .DeviceBootloader ):
182- sel = SelectBootloader (['USB' , 'NETWORK' ], "Select bootloader type used for factory reset." )
183- ok , type = sel .wait ()
184- if not ok :
185- print ("Factory reset canceled." )
186- return
187-
180+ def factoryReset (device : dai .DeviceInfo , type : dai .DeviceBootloader .Type ):
188181 try :
182+ pr = Progress ('Preparing and connecting...' )
183+
189184 blBinary = dai .DeviceBootloader .getEmbeddedBootloaderBinary (type )
190185 # Clear 1 MiB for USB BL and 8 MiB for NETWORK BL
191186 mib = 1 if type == dai .DeviceBootloader .Type .USB else 8
192187 blBinary = blBinary + ([0xFF ] * ((mib * 1024 * 1024 + 512 ) - len (blBinary )))
193188 tmpBlFw = tempfile .NamedTemporaryFile (delete = False )
194189 tmpBlFw .write (bytes (blBinary ))
195190
196- pr = Progress ()
191+ bl = dai .DeviceBootloader (device , True )
192+
197193 progress = lambda p : pr .update (p )
198194 success , msg = bl .flashBootloader (progress , tmpBlFw .name )
199195 msg = "Factory reset was successful." if success else f"Factory reset failed. Error: { msg } "
@@ -216,9 +212,11 @@ def flashFromFile(file, bl: dai.DeviceBootloader):
216212def recoveryMode (bl : dai .DeviceBootloader ):
217213 try :
218214 bl .bootUsbRomBootloader ()
215+ return True
219216 except Exception as ex :
220217 PrintException ()
221218 sg .Popup (f'{ ex } ' )
219+ return False
222220
223221def connectToDevice (device : dai .DeviceInfo ) -> dai .DeviceBootloader :
224222 try :
@@ -342,9 +340,11 @@ def deviceStateTxt(state: dai.XLinkDeviceState) -> str:
342340 ],
343341 [sg .HSeparator ()],
344342 [
345- sg .Text ("" , size = (10 , 2 )),
343+ sg .Text ("" , size = (1 , 2 )),
346344 sg .Button ("Flash configuration" , size = (15 , 2 ), font = ('Arial' , 10 , 'bold' ), disabled = True ,
347345 button_color = '#FFA500' ),
346+ sg .Button ("Clear configuration" , size = (15 , 2 ), font = ('Arial' , 10 , 'bold' ), disabled = True ,
347+ button_color = '#FFA500' ),
348348 sg .Button ("Clear flash" , size = (15 , 2 ), font = ('Arial' , 10 , 'bold' ), disabled = True ,
349349 button_color = '#FFA500' ),
350350 sg .Button ("Flash DAP" , size = (15 , 2 ), font = ('Arial' , 10 , 'bold' ), disabled = True ,
@@ -443,9 +443,33 @@ def run(self) -> None:
443443 self .getConfigs ()
444444 self .unlockConfig ()
445445 elif event == "Flash newest Bootloader" :
446- self .closeDevice () # We will reconnect, as we need to set allowFlashingBootloader to True
447- flashBootloader (self .device )
448- self .window .Element ('currBoot' ).update (self .bl .getVersion ())
446+ sel = SelectBootloader (['AUTO' , 'USB' , 'NETWORK' ], "Select bootloader type to flash." )
447+ ok , type = sel .wait ()
448+ if ok :
449+ # We will reconnect, as we need to set allowFlashingBootloader to True
450+ self .closeDevice ()
451+ flashBootloader (self .device , type )
452+ # Device will reboot, close previous and reset GUI
453+ self .closeDevice ()
454+ self .resetGui ()
455+ self .getDevices ()
456+ else :
457+ print ("Flashing bootloader cancelled." )
458+
459+ elif event == "Factory reset" :
460+ sel = SelectBootloader (['NETWORK' , 'USB' ], "Select bootloader type used for factory reset." )
461+ ok , type = sel .wait ()
462+ if ok :
463+ # We will reconnect, as we need to set allowFlashingBootloader to True
464+ self .closeDevice ()
465+ factoryReset (self .device , type )
466+ # Device will reboot, close previous and reset GUI
467+ self .closeDevice ()
468+ self .resetGui ()
469+ self .getDevices ()
470+ else :
471+ print ("Factory reset cancelled." )
472+
449473 elif event == "Flash configuration" :
450474 self .flashConfig ()
451475 self .getConfigs ()
@@ -455,8 +479,16 @@ def run(self) -> None:
455479 else :
456480 self .devices .clear ()
457481 self .window .Element ('devices' ).update ("Search for devices" , values = [])
458- elif event == "Factory reset" :
459- factoryReset (self .bl )
482+ elif event == "Clear configuration" :
483+ self .clearConfig ()
484+ self .getConfigs ()
485+ self .resetGui ()
486+ if self .isUsb ():
487+ self .unlockConfig ()
488+ else :
489+ self .devices .clear ()
490+ self .window .Element ('devices' ).update ("Search for devices" , values = [])
491+
460492 elif event == "Flash DAP" :
461493 file = sg .popup_get_file ("Select .dap file" , file_types = (('DepthAI Application Package' , '*.dap' ), ('All Files' , '*.* *' )))
462494 flashFromFile (file , self .bl )
@@ -467,7 +499,12 @@ def run(self) -> None:
467499 self .window ['-COL2-' ].update (visible = False )
468500 self .window ['-COL1-' ].update (visible = True )
469501 elif event == "recoveryMode" :
470- recoveryMode (self .bl )
502+ if recoveryMode (self .bl ):
503+ sg .Popup (f'Device successfully put into USB recovery mode.' )
504+ # Device will reboot, close previous and reset GUI
505+ self .closeDevice ()
506+ self .resetGui ()
507+ self .getDevices ()
471508 self .window .close ()
472509
473510 @property
@@ -540,6 +577,7 @@ def unlockConfig(self):
540577
541578 self .window ['Flash newest Bootloader' ].update (disabled = False )
542579 self .window ['Flash configuration' ].update (disabled = False )
580+ self .window ['Clear configuration' ].update (disabled = False )
543581 self .window ['Factory reset' ].update (disabled = False )
544582 # self.window['Clear flash'].update(disabled=False)
545583 self .window ['Flash DAP' ].update (disabled = False )
@@ -559,6 +597,7 @@ def resetGui(self):
559597
560598 self .window ['Flash newest Bootloader' ].update (disabled = True )
561599 self .window ['Flash configuration' ].update (disabled = True )
600+ self .window ['Clear configuration' ].update (disabled = True )
562601 self .window ['Factory reset' ].update (disabled = True )
563602 self .window ['Clear flash' ].update (disabled = True )
564603 self .window ['Flash DAP' ].update (disabled = True )
@@ -591,7 +630,9 @@ def getDevices(self):
591630 deviceTxt = deviceInfo .getMxId ()
592631 listedDevices .append (deviceTxt )
593632 self .devices [deviceTxt ] = deviceInfo
594- self .window .Element ('devices' ).update ("Select device" , values = listedDevices )
633+
634+ # Update the list regardless
635+ self .window .Element ('devices' ).update ("Select device" , values = listedDevices )
595636 except Exception as ex :
596637 PrintException ()
597638 sg .Popup (f'{ ex } ' )
@@ -634,6 +675,17 @@ def flashConfig(self):
634675 except Exception as ex :
635676 PrintException ()
636677 sg .Popup (f'{ ex } ' )
678+ def clearConfig (self ):
679+ try :
680+ success , error = self .bl .flashConfigClear ()
681+ if not success :
682+ sg .Popup (f"Clearing configuration failed: { error } " )
683+ else :
684+ sg .Popup ("Successfully cleared configuration." )
685+ except Exception as ex :
686+ PrintException ()
687+ sg .Popup (f'{ ex } ' )
688+
637689
638690app = DeviceManager ()
639691app .run ()
0 commit comments