-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaddVehicleMessage.py
More file actions
95 lines (70 loc) · 3.09 KB
/
addVehicleMessage.py
File metadata and controls
95 lines (70 loc) · 3.09 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import fileinput
def addCode(fullFilePath, searchStr, addStr, addMode='prefix'):
newCppStr = ''
with open(fullFilePath, 'r') as cppFile:
iLine = 0
iSearchStrLine = 0
# Read all lines in the file one by one
for line in cppFile:
# For each line, check if line contains the string
iLine += 1
if searchStr in line:
# If yes, then return
iSearchStrLine = iLine
# add new Str
if addMode == 'prefix':
newCppStr = newCppStr + line.replace(line, addStr+'\n'+line)
elif addMode == 'ConfigHelper':
newCppStr = newCppStr + line.replace("};", addStr+"};")
else:
newCppStr = newCppStr + line
if iSearchStrLine == 0:
print('reach end of file, did not find the pattern!')
exit()
# update the file
with open(fullFilePath, 'w') as cppFile:
cppFile.write(newCppStr)
### begin adding code automatically
RealSimPath = os.path.abspath(os.getcwd())
vehicleMessageName = 'lightIndicators'
vehicleMessageDataType = 'uint16_t'
#############################################################################
###################### ADD to CPP
#
# will change MsgHelper.cpp and ConfigHelper.cpp
#
#############################################################################
fullFilePath = os.path.join(RealSimPath, 'CommonLib\MsgHelper.cpp')
# 1)
searchStr = '// printVehData: add new vehicle message field here'
addStr = '\tif (VehicleMessageField_set.find(\"{0}\") != VehicleMessageField_set.end()) {{\n\t\tprintf(\"\\t {0}: %d\\n\", VehData.{0});\n\t}}\n'.format(vehicleMessageName,vehicleMessageDataType)
addCode(fullFilePath, searchStr, addStr)
# # 2)
# searchStr = '// VehFullDataToSubData: add new vehicle message field here'
# addStr = '\tif (VehicleMessageField_set.find(\"{0}\") != VehicleMessageField_set.end()) {{\n\t\tVehData[\"{0}\"] = VehFullData.{0};\n\t}}\n'.format(vehicleMessageName)
# addCode(fullFilePath, searchStr, addStr)
# 3)
searchStr = '// packVehData: add new vehicle message field here'
addStr = '\tnumericVehDataToBuffer<{1}>(VehData.{0}, \"{0}\", buffer, iByte);\n'.format(vehicleMessageName, vehicleMessageDataType)
addCode(fullFilePath, searchStr, addStr)
# 4)
searchStr = '// depackVehData: add new vehicle message field here'
if (vehicleMessageDataType == 'float'):
pat = 'tempFloat'
if (vehicleMessageDataType == 'uint16_t'):
pat = 'tempUint16'
if (vehicleMessageDataType == 'int8_t'):
pat = 'tempInt8'
if (vehicleMessageDataType == 'int32_t'):
pat = 'tempInt32'
addStr = '\tbufferToNumericVehData<{1}>(buffer, &iByte, "{0}", {2}); VehData.{0} = {2};\n'.format(vehicleMessageName, vehicleMessageDataType, pat)
addCode(fullFilePath, searchStr, addStr)
##
# 5)
fullFilePath = os.path.join(RealSimPath, 'CommonLib\ConfigHelper.cpp')
searchStr = 'SimulationSetup.VehicleMessageField ='
addStr = ', \"{0}\"'. format(vehicleMessageName)
addCode(fullFilePath, searchStr, addStr, addMode='ConfigHelper')
# add to matlab
# TO BE DONE