This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
preBuildConfig.py
57 lines (43 loc) · 1.81 KB
/
preBuildConfig.py
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
import json
import binascii
import inspect, os.path
def preBuildConfigFun():
filename = inspect.getframeinfo(inspect.currentframe()).filename
dir_path = os.path.dirname(os.path.abspath(filename))
filename = "config"
h = open(dir_path + "/../src/generated/" + filename + ".h", "w", encoding="utf8")
cpp = open(dir_path + "/../src/generated/" + filename + ".cpp", "w", encoding="utf8")
with open(dir_path + '/../gui/js/configuration.json') as f:
data = json.load(f)
# binascii.crc32(mes.encode('utf8'))
#headers
h.write("#ifndef CONFIG_H\n")
h.write("#define CONFIG_H\n\n")
h.write("struct configData\n{\n")
cpp.write("#include <Arduino.h>\n")
cpp.write("#include \"config.h\"\n\n")
cpp.write("uint32_t configVersion = " + str(binascii.crc32(json.dumps(data).encode())) + "; //generated identifier to compare config with EEPROM\n\n")
cpp.write("const configData defaults PROGMEM =\n{\n")
#loop through variables
first = True
for item in data:
if first==True:
first=False
else:
cpp.write(',\n')
if item['type'] == 'char':
cpp.write("\t\"" + item['value'] + "\"")
h.write("\tchar " + item['name'] + "[" + str(item['length']) + "];\n")
elif item['type'] == 'bool':
cpp.write("\t" + str(item['value']).lower())
h.write("\t" + item['type'] + " " + item['name'] +";\n")
else:
cpp.write("\t" + str(item['value']))
h.write("\t" + item['type'] + " " + item['name'] +";\n")
#footers
h.write("};\n\nextern uint32_t configVersion;\n")
h.write("extern const configData defaults;\n\n")
h.write("#endif")
cpp.write("\n};")
h.close()
cpp.close()