forked from ac2cz/FoxTelem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_header.py
59 lines (52 loc) · 1.67 KB
/
gen_header.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
# Generate a C header files needed for spacecraft from the FoxTelem payload file
import sys
#
# Main
#
if len(sys.argv) < 3:
print ('Usage: gen_header <name> <fileName.csv>')
print ('Generate a spacecraft c header file from the FoxTelem payload file')
sys.exit(1)
name = sys.argv[1]
fileName = sys.argv[2]
line = ""
fields = []
firstLine = True
num = 0
print("typedef struct __attribute__((__packed__)) {");
# open the infile and read all the content in as a set of lines
try:
with open(fileName) as infile:
for line in infile:
if (firstLine) :
firstLine = False
else :
fields = line.split(',')
print(' unsigned int ' + fields[2] + ' : ' + fields[3] + ';')
print('} ' + name + '_t;\n')
#
# Now generate the function that prints the layout
#
firstLine = True
print('void print_'+name+'('+ name + '_t payload) {')
print(' printf("RAW PAYLOAD: '+name+r'\n");') # use a raw string where we want the \n
with open(fileName) as infile:
for line in infile:
if (firstLine) :
firstLine = False
else :
fields = line.split(',')
print(' printf("'+fields[10]+': %d ",payload.'+fields[2]+');')
num += 1
if (num % 4 == 0) :
print(r' printf("\n");') # print as a raw string to allow \n
if (num %4 != 0) :
print(r' printf("\n");')
print('}\n')
except UnicodeDecodeError as e:
print ("ERROR: Binary data found in the file. Is it a CSV file?")
print (e)
print (line)
print (fields)
exit(1)