|
1 | 1 | #!/usr/bin/python |
2 | 2 |
|
3 | 3 | import argparse |
| 4 | +import re |
4 | 5 | import json |
5 | 6 | import struct |
6 | 7 | import sys |
@@ -74,7 +75,40 @@ def appendToFirmware(firmware_file, product_name, lua_name, defines, config, lay |
74 | 75 | firmware_file.write(config['prior_target_name'].upper().encode()) |
75 | 76 | firmware_file.write(b'\0') |
76 | 77 |
|
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): |
78 | 112 | product_name = "Unified" |
79 | 113 | lua_name = "Unified" |
80 | 114 | layout = None |
@@ -103,15 +137,27 @@ def doConfiguration(file, defines, config, moduletype, frequency, platform, devi |
103 | 137 | products.append(k) |
104 | 138 | # Sort the list by product name, case insensitive, and print the list |
105 | 139 | products = sorted(products, key=lambda p: p['product_name'].casefold()) |
| 140 | + print(f'0) Leave bare (no configuration)') |
106 | 141 | for i, p in enumerate(products): |
107 | 142 | 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 | + |
109 | 149 | 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] |
112 | 157 |
|
113 | 158 | if config is not None: |
114 | 159 | product_name = config['product_name'] |
| 160 | + setDefaultProductForTarget(target_name, product_name) |
115 | 161 | lua_name = config['lua_name'] |
116 | 162 | dir = 'TX' if moduletype == 'tx' else 'RX' |
117 | 163 | layout = f"hardware/{dir}/{config['layout_file']}" |
@@ -147,7 +193,7 @@ def appendConfiguration(source, target, env): |
147 | 193 | defines = json.JSONEncoder().encode(env['OPTIONS_JSON']) |
148 | 194 |
|
149 | 195 | 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) |
151 | 197 |
|
152 | 198 | if __name__ == '__main__': |
153 | 199 | parser = argparse.ArgumentParser(description="Configure Unified Firmware") |
|
0 commit comments