#!/usr/bin/env python3 # Copied from https://github.com/opendata-stuttgart/sensors-software/blob/master/airrohr-firmware/airrohr-cfg.h.py configshape_in = """ String wifi_ssid Password wifi_password UInt wifi_timeout Bool auto_calibrate_sensor """ with open("ampel-firmware/save_config.h", "w") as h: print(""" // This file is generated, please do not edit. // Change generate_ampel_config_h.py instead. enum ConfigEntryType : unsigned short { Config_Type_Bool, Config_Type_UInt, Config_Type_Time, Config_Type_String, Config_Type_Password }; struct ConfigShapeEntry { enum ConfigEntryType cfg_type; unsigned short cfg_len; const char* _cfg_key; union { void* as_void; bool* as_bool; unsigned int* as_uint; char* as_str; } cfg_val; const __FlashStringHelper* cfg_key() const { return FPSTR(_cfg_key); } }; enum ConfigShapeId {""", file=h) entries = [row.strip().split()[:2] for row in configshape_in.strip().splitlines()] for _, cfgkey in entries: print(" Config_", cfgkey, ",", sep='', file=h) print("};", file=h) for _, cfgkey in entries: print("static constexpr char CFG_KEY_", cfgkey.upper(), "[] PROGMEM = \"", cfgkey, "\";", sep='', file=h) print("static constexpr ConfigShapeEntry configShape[] PROGMEM = {", file=h) for cfgtype, cfgkey in entries: print(" { Config_Type_", cfgtype, ", sizeof(cfg::" + cfgkey + ")-1" if cfgtype in ('String', 'Password') else ", 0", ", CFG_KEY_", cfgkey.upper(), ", ", "" if cfgtype in ('String', 'Password') else "&", "cfg::", cfgkey, " },", sep='', file=h) print("};", file=h)