generate_ampel_config_h.py 2.16 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/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
UInt     measurement_timestep
UInt     csv_interval
UInt     temperature_offset # Signed, actually. :-(
UInt     altitude_above_sea_level
UInt     atmospheric_co2_concentration
Bool     auto_calibrate_sensor
UInt     max_brightness
UInt     min_brightness
UInt     led_count
String   http_user
Password http_password
UInt     mqtt_sending_interval
String   mqtt_server
UInt     mqtt_port
String   mqtt_user
String   mqtt_password
String   ntp_server
UInt     utc_offset_in_seconds
UInt     bauds # Too large for unsigned int
"""

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)