-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
generateArduinoLibrary.py
150 lines (125 loc) · 4.73 KB
/
generateArduinoLibrary.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os, shutil, fnmatch, glob, fileinput
from datetime import datetime
def copyfiles(srcdir, dstdir, filepattern):
def failed(exc):
raise exc
for dirpath, dirs, files in os.walk(srcdir, topdown=True, onerror=failed):
for file in fnmatch.filter(files, filepattern):
if "test" not in dirpath and "examples" not in dirpath and "CMakeFiles" not in dirpath:
shutil.copy2(os.path.join(dirpath, file), dstdir)
print("Copied ", file)
def write_isobus_hpp(includefiles, f):
f.write(
f"""/*******************************************************************************
** @file AgIsoStack.hpp
** @author Automatic Code Generation
** @date {datetime.today().strftime("%B %d, %Y")} at {datetime.now().strftime("%H:%M:%S")}
** @brief Includes all important files in the AgIsoStack library.
**
** Copyright {datetime.today().strftime("%Y")} The AgIsoStack++ Developers
*******************************************************************************/
#ifndef AG_ISO_STACK_HPP
#define AG_ISO_STACK_HPP
""")
for includefile in includefiles:
f.write(f"#include <{includefile}>\n")
f.write(f"\n");
f.write(f"#endif // AG_ISO_STACK_HPP\n")
def write_library_properties(f):
f.write(
f"""name=AgIsoStack
version=0.1.0
license=MIT
author=Adrian Del Grosso <delgrossoengineering@protonmail.com>
maintainer=Adrian Del Grosso <delgrossoengineering@protonmail.com>
sentence=A free ISOBUS (ISO11783) and J1939 CAN Stack for Teensy.
paragraph=Includes ISOBUS virtual terminal client, task controller client, and transport layer functionality. Based on the CMake AgIsoStack++ at https://github.com/Open-Agriculture/AgIsoStack-plus-plus.
category=Communication
architectures=teensy
includes=AgIsoStack.hpp
url=https://github.com/Open-Agriculture/AgIsoStack-Arduino
""")
def fixup_header_paths(fileName):
f = open(fileName,'r')
filedata = f.read()
f.close()
newdata = filedata.replace("isobus/isobus/","")
newdata = newdata.replace("isobus/utility/","")
newdata = newdata.replace("isobus/hardware_integration/","")
f = open(fileName,'w')
f.write(newdata)
f.close()
arduinoLibPath = "arduino_library/"
sourceDir = "src"
sourcePath = arduinoLibPath + sourceDir
if os.path.exists(arduinoLibPath) and os.path.isdir(arduinoLibPath):
shutil.rmtree(arduinoLibPath)
print("Removed existing arduino library")
os.mkdir(arduinoLibPath)
print("Created directory ", arduinoLibPath)
os.mkdir(sourcePath)
print("Created directory ", sourcePath)
print("Copying source files to library")
copyfiles(".", sourcePath, "*.cpp")
copyfiles(".", sourcePath, "*.hpp")
copyfiles(".", sourcePath, "*.tpp")
print("Pruning unneeded files for Arduino platform")
filePruneList = [
"iop_file_interface.cpp",
"iop_file_interface.hpp",
"available_can_drivers.hpp",
"canal.h",
"canal_a.h",
"innomaker_usb2can_windows_plugin.hpp",
"InnoMakerUsb2CanLib.h",
"libusb.h",
"mac_can_pcan_plugin.hpp",
"mcp2515_can_interface.hpp",
"pcan_basic_windows_plugin.hpp",
"PCANBasic.h",
"PCBUSB.h",
"socket_can_interface.hpp",
"spi_hardware_plugin.hpp",
"spi_interface_esp.hpp",
"spi_transaction_frame.hpp",
"toucan_vscp_canal.hpp",
"twai_plugin.hpp",
"virtual_can_plugin.hpp",
"innomaker_usb2can_windows_plugin.cpp",
"mac_can_pcan_plugin.cpp",
"mcp2515_can_interface.cpp",
"pcan_basic_windows_plugin.cpp",
"socket_can_interface.cpp",
"spi_interface_esp.cpp",
"spi_transaction_frame.cpp",
"toucan_vscp_canal.cpp",
"twai_plugin.cpp",
"virtual_can_plugin.cpp",
"can_hardware_interface.hpp",
"can_hardware_interface.cpp",
"socketcand_windows_network_client.hpp",
"socketcand_windows_network_client.cpp",
"CMakeCXXCompilerId.cpp"]
for punableFile in filePruneList:
if os.path.exists(os.path.join(sourcePath, punableFile)) and os.path.isfile(os.path.join(sourcePath, punableFile)):
os.remove(os.path.join(sourcePath, punableFile))
print("Pruning file ", punableFile)
print("Generating isobus.hpp from files in " + "./" + sourcePath + "/*.hpp")
headers = [os.path.normpath(i) for i in glob.glob("./" + sourcePath + "/*.hpp")]
strippedHeaders = list(map(lambda x: x.replace('arduino_library\\src\\','').replace('arduino_library/src/',''),headers))
print("Headers ", strippedHeaders)
f = open(os.path.join(sourcePath, "AgIsoStack.hpp"), "w")
write_isobus_hpp(strippedHeaders, f)
f.close()
print("Generating library.properties")
f = open(os.path.join(arduinoLibPath, "library.properties"), "w")
write_library_properties(f)
f.close()
print("Patching header file paths")
for header in headers:
fixup_header_paths(header)
print("Patched ", header)
sources = [os.path.normpath(i) for i in glob.glob("./" + sourcePath + "/*.cpp")]
for source in sources:
fixup_header_paths(source)
print("Patched ", source)