Skip to content

Instantly share code, notes, and snippets.

@atomgomba
Last active February 1, 2024 19:41
Show Gist options
  • Save atomgomba/45c524089ec0831c42b693bafbc38ff2 to your computer and use it in GitHub Desktop.
Save atomgomba/45c524089ec0831c42b693bafbc38ff2 to your computer and use it in GitHub Desktop.
Generate C code for Betaflight build flags
import requests
JSON_URL = "https://build.betaflight.com/api/options/4.5.0-zulu"
def get_gates_options() -> tuple:
data = requests.get(JSON_URL, timeout=2).json()
gates = []
options = []
groups = list(data.keys())
for group_index, option_list in enumerate(data.values()):
for i, option in enumerate(option_list):
define = option.get("value")
if define:
gates.append(define)
number = option.get("key")
name = define.replace("USE_", "BUILD_OPTION_")
options.append((name, number, groups[group_index]))
return gates, options
def main():
gates, options = get_gates_options()
with open("enum.out", "w") as f:
maxlen = max(map(lambda x: len(x[0]), options)) + 4
lines = []
last_group = None
for k, v, g in options:
if g != last_group:
lines.append(f"// {g}\n")
last_group = g
lines.append("#define {:<{width}}{}\n".format(k, v, width=maxlen))
f.writelines(lines)
with open("write.out", "w+") as f:
lines = []
indent = " " * 4
for i, define in enumerate(gates):
option_name, _, _ = options[i]
lines.append(f"#ifdef {define}\n")
lines.append(f"{indent}{option_name},\n")
lines.append("#endif\n")
f.writelines(lines)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment