generate_ampel_config_h.py 1.67 KB
Newer Older
Eric Duminil's avatar
Eric Duminil committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/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)