1+ import sys
2+ from typing import Callable , Tuple
3+ from enum import Enum
4+ import depthai
5+
6+ FLASH = depthai .DeviceBootloader .Memory .FLASH
7+
8+ def main ():
9+ if len (sys .argv ) < 2 :
10+ print (f"Usage: { sys .argv [0 ]} [GPIO_MODE/USB_RECOVERY/NORMAL/FAST] [params]" )
11+ print ("\t Options:" )
12+ print (f"\t \t { sys .argv [0 ]} GPIO_MODE gpioModeNum" )
13+ print (f"\t \t { sys .argv [0 ]} USB_RECOVERY" )
14+ print (f"\t \t { sys .argv [0 ]} NORMAL [frequency] [location] [dummyCycles] [offset]" )
15+ print (f"\t \t { sys .argv [0 ]} FAST [frequency] [location] [dummyCycles] [offset]" )
16+ return 0
17+
18+ mode = sys .argv [1 ].lower ()
19+ flash_func = None
20+
21+ if mode == "gpio_mode" :
22+ # gpio mode header
23+ if len (sys .argv ) < 3 :
24+ print (f"Usage: { sys .argv [0 ]} GPIO_MODE gpioModeNum" )
25+ return 0
26+
27+ gpio_mode = int (sys .argv [2 ])
28+ def flash_gpio (bl : 'depthai.DeviceBootloader' ) -> Tuple [bool , str ]:
29+ return bl .flashGpioModeBootHeader (FLASH , gpio_mode )
30+ flash_func = flash_gpio
31+
32+ elif mode == "usb_recovery" :
33+ # usb recovery header
34+ def flash_usb (bl : 'depthai.DeviceBootloader' ) -> Tuple [bool , str ]:
35+ return bl .flashUsbRecoveryBootHeader (FLASH )
36+ flash_func = flash_usb
37+
38+ elif mode in ("normal" , "fast" ):
39+ if len (sys .argv ) != 2 and len (sys .argv ) != 3 and len (sys .argv ) <= 3 :
40+ print (f"Usage: { sys .argv [0 ]} NORMAL/FAST [frequency] [location] [dummyCycles] [offset]" )
41+ print (f"Usage: { sys .argv [0 ]} NORMAL/FAST [frequency]" )
42+ return 0
43+
44+ offset = - 1
45+ location = - 1
46+ dummy_cycles = - 1
47+ frequency = - 1
48+
49+ if len (sys .argv ) > 3 :
50+ if len (sys .argv ) >= 3 :
51+ offset = int (sys .argv [2 ])
52+ if len (sys .argv ) >= 4 :
53+ location = int (sys .argv [3 ])
54+ if len (sys .argv ) >= 5 :
55+ dummy_cycles = int (sys .argv [4 ])
56+ if len (sys .argv ) >= 6 :
57+ frequency = int (sys .argv [5 ])
58+ elif len (sys .argv ) == 3 :
59+ frequency = int (sys .argv [2 ])
60+
61+ if mode == "normal" :
62+ def flash_normal (bl : 'depthai.DeviceBootloader' ) -> Tuple [bool , str ]:
63+ return bl .flashBootHeader (FLASH , frequency , location , dummy_cycles , offset )
64+ flash_func = flash_normal
65+ elif mode == "fast" :
66+ def flash_fast (bl : 'depthai.DeviceBootloader' ) -> Tuple [bool , str ]:
67+ return bl .flashFastBootHeader (FLASH , frequency , location , dummy_cycles , offset )
68+ flash_func = flash_fast
69+
70+ # Find device and flash specified boot header
71+ success , info = depthai .DeviceBootloader .getFirstAvailableDevice ()
72+
73+ if success :
74+ print (f"Found device with name: { info .name } " )
75+ bl = depthai .DeviceBootloader (info )
76+
77+ # Flash the specified boot header
78+ if flash_func :
79+ success , error_msg = flash_func (bl )
80+ if success :
81+ print ("Successfully flashed boot header!" )
82+ else :
83+ print (f"Couldn't flash boot header: { error_msg } " )
84+ else :
85+ print ("Invalid boot option header specified" )
86+ else :
87+ print ("No devices found" )
88+
89+ return 0
90+
91+ if __name__ == "__main__" :
92+ main ()
0 commit comments