Files
rich 40412223a4 palm-client: shared tables must not be const (prc-tools puts const in code)
The transcript wrapped after a few characters on the Visor: gSmallFontWidths
was const, so prc-tools placed it in .text, but text.c reached it through an
extern declaration A5-relative (lea %a5@(0)) as if it were data, and read
garbage widths. The same applied to gPresets. Both generators now emit
non-const tables, with a note why. Host tests can't see this.
2026-11-01 15:34:13 -05:00

77 lines
2.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""Generates src/presets.c from tools/local-presets.json (local meshes, listed
first) and tools/meshcore-presets.json (a snapshot of MeshCore's suggested
radio presets; the Visor has no internet to fetch them).
tools/gen_presets.py # regenerate from the snapshot
tools/gen_presets.py --fetch # refresh the snapshot first
Frequencies become kHz and bandwidths Hz, converted from the decimal strings
exactly (no floating point).
"""
import json
import os
import sys
import urllib.request
from decimal import Decimal
HERE = os.path.dirname(os.path.abspath(__file__))
SNAPSHOT = os.path.join(HERE, 'meshcore-presets.json')
LOCAL = os.path.join(HERE, 'local-presets.json')
OUT = os.path.join(HERE, '..', 'src', 'presets.c')
URL = 'https://api.meshcore.nz/api/v1/config'
def fetch():
req = urllib.request.Request(URL, headers={'User-Agent': 'VisorMesh (Palm client preset snapshot)'})
with urllib.request.urlopen(req, timeout=30) as r:
data = json.load(r)
import datetime
snap = {'source': URL, 'fetched': datetime.date.today().isoformat(),
'entries': data['config']['suggested_radio_settings']['entries']}
with open(SNAPSHOT, 'w') as f:
json.dump(snap, f, indent=2)
f.write('\n')
def c_string(s):
return '"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"'
def main():
if '--fetch' in sys.argv:
fetch()
snap = json.load(open(SNAPSHOT))
local = json.load(open(LOCAL))['entries'] if os.path.exists(LOCAL) else []
rows = []
for e in local + snap['entries']:
title = e['title'].encode('ascii', 'replace').decode()
khz = int(Decimal(e['frequency']) * 1000)
hz = int(Decimal(e['bandwidth']) * 1000)
sf, cr = int(e['spreading_factor']), int(e['coding_rate'])
hash_size = int(e.get('network_settings', {}).get('path_hash_size', 0))
rows.append(f'\t{{ {c_string(title)}, {khz}UL, {hz}UL, {sf}, {cr}, {hash_size} }},')
with open(OUT, 'w') as f:
f.write(f'''/*
* Radio presets: tools/local-presets.json ({len(local)} local) then MeshCore's
* suggested presets from tools/meshcore-presets.json ({snap["source"]},
* fetched {snap["fetched"]}). GENERATED by tools/gen_presets.py.
* Don't edit by hand.
*/
#include "presets.h"
/* Not const: prc-tools puts const data in the code section, but other files reach an
* extern table A5-relative as if it were data, so a const table reads as garbage on the Visor. */
VmPreset gPresets[] = {{
''' + '\n'.join(rows) + f'''
}};
UInt16 gPresetCount = sizeof(gPresets) / sizeof(gPresets[0]);
''')
print(f'{len(rows)} presets -> {os.path.relpath(OUT)}')
if __name__ == '__main__':
main()