-
Notifications
You must be signed in to change notification settings - Fork 16
/
jsonToHeader.py
executable file
·84 lines (59 loc) · 2.03 KB
/
jsonToHeader.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/python
import sys
import json
names = []
maxLength = 0
def writeObject(name, obj, objName):
global output
global maxLength
string = json.dumps(obj, indent=4)
output += "const char " + name + "[] PROGMEM =\n"
tmpStr = "{\"" + objName + "\":\n" + string + "}"
output += "R\"RAWSTR(" + tmpStr + ")RAWSTR\";\n\n"
maxLength = max(maxLength, len(tmpStr) + 2)
names.append(name)
if len(sys.argv) != 3 and len(sys.argv) != 1:
print("Usage: ./jsonToHeader.py [input_json_file] [output_header_file]")
exit()
input_filename = None
output_filename = None
stdio = False
if len(sys.argv) == 3:
input_filename = sys.argv[1]
output_filename = sys.argv[2]
else:
input_filename = "stdin"
output_filename = "stdout"
stdio = True
json_raw = None
if stdio:
json_raw = sys.stdin.read()
else:
json_raw = open(input_filename, 'r').read()
json_dict = json.loads(json_raw)
output = "#ifndef OMNI_ARDUINOJSONCONFIG_H\n#define OMNI_ARDUINOJSONCONFIG_H\n\n#include <Arduino.h>\n\n"
output += "namespace omni\n{\n"
if "NetworkReceiver" in json_dict:
writeObject("NetworkReceiver", json_dict["NetworkReceiver"], "NetworkReceiver")
if "NetworkSender" in json_dict:
writeObject("NetworkSender", json_dict["NetworkSender"], "NetworkSender")
if "CompositePeriphs" in json_dict:
writeObject("CompositePeriphs", json_dict["CompositePeriphs"], "CompositePeriphs")
if "Devices" in json_dict:
count = 0
for dev in json_dict["Devices"]:
writeObject("Device_"+str(count), [dev], "Devices")
count += 1
num_strings = len(names)
output += "\n\nconst char* const Config_Json_Strings[] PROGMEM = {\n"
for name in names:
output += " " + name + ",\n"
output = output[:-2] + "\n"
output += "};\n\n"
output += "const unsigned int Num_Json_Strings = " + str(num_strings) + ";\n\n"
output += "const unsigned int Max_Json_String_Length = " + str(maxLength) + ";\n\n"
output += "}\n\n#endif\n"
if stdio:
sys.stdout.write(output)
else:
open(output_filename, "w").write(output)