@@ -75,6 +75,8 @@ def appendToFirmware(firmware_file, product_name, lua_name, defines, config, lay
7575 firmware_file .write (config ['prior_target_name' ].upper ().encode ())
7676 firmware_file .write (b'\0 ' )
7777
78+ # Return the product name for the last hardware.json that was appended for the given PIO env target
79+ # e.g. Unified_ESP32_LR1121_via_WIFI -> "RadioMaster Nomad 2.4/900 TX"
7880def getDefaultProductForTarget (target_name : str ) -> str :
7981 if target_name is None or target_name == '' :
8082 return ''
@@ -90,6 +92,9 @@ def getDefaultProductForTarget(target_name: str) -> str:
9092 # No file or json.JSONDecodeError
9193 return ''
9294
95+ # Save the product name of a hardware configuration appended to a target, for a future default
96+ # target_name: e.g. Unified_ESP32_LR1121_via_WIFI
97+ # product_name: e.g. RadioMaster Nomad 2.4/900 TX
9398def setDefaultProductForTarget (target_name : str , product_name : str ) -> None :
9499 if target_name is None or target_name == '' :
95100 return
@@ -108,6 +113,36 @@ def setDefaultProductForTarget(target_name: str, product_name: str) -> None:
108113 with open ('.pio/default_target_config.json' , 'w' ) as f :
109114 json .dump (data , f , indent = 4 )
110115
116+ # Remove the default product name for a given target (e.g. Unified_ESP32_LR1121_via_WIFI)
117+ # so there will not be a default on future runs. Used in "clean" operation
118+ def clearDefaultProductForTarget (target_name : str ) -> None :
119+ # remove the "_via_WIFI" etc method
120+ target_wo_method = re .sub ('_VIA_.*' , '' , target_name .upper ())
121+ data = {}
122+ try :
123+ with open ('.pio/default_target_config.json' , 'r' ) as f :
124+ data = json .load (f )
125+ except :
126+ # No file or json.JSONDecodeError
127+ pass
128+
129+ data .pop (target_wo_method , None )
130+ with open ('.pio/default_target_config.json' , 'w' ) as f :
131+ json .dump (data , f , indent = 4 )
132+
133+ def is_pio_upload ():
134+ # SCons.Script is only available when run via 'pio run'?
135+ scons_module = sys .modules .get ("SCons.Script" )
136+ if not scons_module :
137+ return False
138+
139+ try :
140+ # COMMAND_LINE_TARGETS = ['upload'] when this is an upload build
141+ targets = getattr (scons_module , "COMMAND_LINE_TARGETS" , [])
142+ return "upload" in targets
143+ except Exception :
144+ return False
145+
111146def interactiveProductSelect (targets : dict , target_name : str , moduletype : str , frequency : str , platform : str ) -> dict :
112147 products = []
113148 for k in jmespath .search (f'[*."{ moduletype } _{ frequency } ".*][][?platform==`{ platform } `][]' , targets ):
@@ -125,20 +160,26 @@ def interactiveProductSelect(targets: dict, target_name: str, moduletype: str, f
125160
126161 # Sort the list by product name, case insensitive, and print the list
127162 products = sorted (products , key = lambda p : p ['product_name' ].casefold ())
128- print (f'0) Leave bare (no configuration)' )
129- for i , p in enumerate (products ):
130- print (f"{ i + 1 } ) { p ['product_name' ]} " )
163+ # Find a default if this target has been build before
131164 default_prod = getDefaultProductForTarget (target_name )
132165 # Make sure default_conf is a valid product name, set to '0' if not or default_prod is blank
133166 default_prod = default_prod if any (p ['product_name' ] == default_prod for p in products ) else '0'
134- print (f'default) { default_prod } ' )
135- print ('Choose a configuration to load into the firmware file' )
136167
137- choice = input ()
138- if choice == '' :
168+ if ( default_prod != '0' ) and is_pio_upload ():
169+ print ( f'Upload using default product " { default_prod } " (use Clean to clear default)' )
139170 choice = default_prod
140- if choice == '0' :
141- return None
171+ else :
172+ print (f'0) Leave bare (no configuration)' )
173+ for i , p in enumerate (products ):
174+ print (f"{ i + 1 } ) { p ['product_name' ]} " )
175+ print (f'default) { default_prod } ' )
176+ print ('Choose a configuration to load into the firmware file' )
177+
178+ choice = input ()
179+ if choice == '' :
180+ choice = default_prod
181+ if choice == '0' :
182+ return None
142183
143184 # First see if choice is a valid product name from the list
144185 config = next ((p for p in products if p ['product_name' ] == choice ), None )
0 commit comments