Skip to content

Commit 47ee2f3

Browse files
committed
Save last UnifiedConfiguration as default for target
1 parent 98d4219 commit 47ee2f3

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/python/UnifiedConfiguration.py

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22

33
import argparse
4+
import re
45
import json
56
import struct
67
import sys
@@ -74,7 +75,40 @@ def appendToFirmware(firmware_file, product_name, lua_name, defines, config, lay
7475
firmware_file.write(config['prior_target_name'].upper().encode())
7576
firmware_file.write(b'\0')
7677

77-
def doConfiguration(file, defines, config, moduletype, frequency, platform, device_name, rx_as_tx):
78+
def getDefaultProductForTarget(target_name: str) -> str:
79+
if target_name is None or target_name == '':
80+
return ''
81+
82+
# remove the "_via_WIFI" etc method
83+
target_wo_method = re.sub('_VIA_.*', '', target_name.upper())
84+
try:
85+
with open('.pio/default_target_config.json', 'r') as f:
86+
data = json.load(f)
87+
product_name = data.get(target_wo_method)
88+
return product_name
89+
except:
90+
# No file or json.JSONDecodeError
91+
return ''
92+
93+
def setDefaultProductForTarget(target_name: str, product_name: str) -> None:
94+
if target_name is None or target_name == '':
95+
return
96+
97+
# remove the "_via_WIFI" etc method
98+
target_wo_method = re.sub('_VIA_.*', '', target_name.upper())
99+
data = {}
100+
try:
101+
with open('.pio/default_target_config.json', 'r') as f:
102+
data = json.load(f)
103+
except:
104+
# No file or json.JSONDecodeError
105+
pass
106+
107+
data[target_wo_method] = product_name
108+
with open('.pio/default_target_config.json', 'w') as f:
109+
json.dump(data, f, indent=4)
110+
111+
def doConfiguration(file, defines, config, target_name, moduletype, frequency, platform, device_name, rx_as_tx):
78112
product_name = "Unified"
79113
lua_name = "Unified"
80114
layout = None
@@ -103,15 +137,27 @@ def doConfiguration(file, defines, config, moduletype, frequency, platform, devi
103137
products.append(k)
104138
# Sort the list by product name, case insensitive, and print the list
105139
products = sorted(products, key=lambda p: p['product_name'].casefold())
140+
print(f'0) Leave bare (no configuration)')
106141
for i, p in enumerate(products):
107142
print(f"{i+1}) {p['product_name']}")
108-
print('Choose a configuration to load into the firmware file (press enter to leave bare)')
143+
default_prod = getDefaultProductForTarget(target_name)
144+
# Make sure default_conf is a valid product name, set to '0' if not or default_prod is blank
145+
default_prod = default_prod if any(p['product_name'] == default_prod for p in products) else '0'
146+
print(f'default) {default_prod}')
147+
print('Choose a configuration to load into the firmware file')
148+
109149
choice = input()
110-
if choice != "":
111-
config = products[int(choice)-1]
150+
if choice == '':
151+
choice = default_prod
152+
if choice != '0':
153+
# First see if choice is a valid product name from the list
154+
config = next((p for p in products if p['product_name'] == choice), None)
155+
# else choice is an integer
156+
config = config if config else products[int(choice)-1]
112157

113158
if config is not None:
114159
product_name = config['product_name']
160+
setDefaultProductForTarget(target_name, product_name)
115161
lua_name = config['lua_name']
116162
dir = 'TX' if moduletype == 'tx' else 'RX'
117163
layout = f"hardware/{dir}/{config['layout_file']}"
@@ -147,7 +193,7 @@ def appendConfiguration(source, target, env):
147193
defines = json.JSONEncoder().encode(env['OPTIONS_JSON'])
148194

149195
with open(str(target[0]), "r+b") as firmware_file:
150-
doConfiguration(firmware_file, defines, config, moduletype, frequency, platform, device_name, None)
196+
doConfiguration(firmware_file, defines, config, target_name, moduletype, frequency, platform, device_name, None)
151197

152198
if __name__ == '__main__':
153199
parser = argparse.ArgumentParser(description="Configure Unified Firmware")

src/python/binary_configurator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def patch_unified(args, options):
104104
args.file,
105105
JSONEncoder().encode(json_flags),
106106
args.target,
107+
None,
107108
'tx' if options.deviceType is DeviceType.TX else 'rx',
108109
'2400' if options.radioChip is RadioType.SX1280 else '900' if options.radioChip is RadioType.SX127X else 'dual',
109110
'32' if options.mcuType is MCUType.ESP32 and options.deviceType is DeviceType.RX else '',

0 commit comments

Comments
 (0)