Skip to content

Commit f127ab9

Browse files
committed
Automatic upload of default target, clean to remove default target
1 parent 9c2f386 commit f127ab9

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

src/python/UnifiedConfiguration.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
7880
def 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
9398
def 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+
111146
def 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)

src/python/build_flags.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def get_git_sha():
130130
def get_version():
131131
return string_to_ascii(env.get('GIT_VERSION'))
132132

133+
def cleanDefaultProductForTarget(target_name: str) -> None:
134+
from UnifiedConfiguration import clearDefaultProductForTarget
135+
clearDefaultProductForTarget(target_name)
136+
133137
json_flags['flash-discriminator'] = randint(1,2**32-1)
134138
json_flags['wifi-on-interval'] = -1
135139

@@ -176,6 +180,10 @@ def get_version():
176180
target_name != "NATIVE":
177181
build_flags = [f for f in build_flags if "Regulatory_Domain_ISM_2400" not in f]
178182

183+
# Remove the default product if this is a "clean" task
184+
if env.GetOption("clean"):
185+
cleanDefaultProductForTarget(target_name)
186+
179187
env['OPTIONS_JSON'] = json_flags
180188
env['BUILD_FLAGS'] = build_flags
181189
sys.stdout.write("\nbuild flags: %s\n\n" % build_flags)

0 commit comments

Comments
 (0)