-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembleProject.py
396 lines (329 loc) · 13.4 KB
/
assembleProject.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python
# TODO
# prompt if roundRobintasks are desired? DONE
# added more macros for serial communication WAS DONE ALREADY
# change FQBN string so other than arduino:avr board may also be used DONE
# investigate if following can be done:
# change method of generating state machines so that assembleProject.py can be used as global executable.
# Recommended to prompt if state machines are desired
# or split the state machine script all togather so that it can be used anywhere locally
import time
import glob
import os
import shutil, errno
import platform
import subprocess
import serial.tools.list_ports
from datetime import datetime
projectName = ""
buildDir = ""
roundRobinTasks = 0
def clear():
time.sleep(1)
os.system('cls')
def getStateMachines() : # function tested!
Files = []
if platform.system() == "Windows":
slash = '\\'
else:
slash = '/'
type = "." + slash +"stateMachines"
for root, dirs, fileList in os.walk(".", topdown=False):
if root == type :
Files = fileList
string = []
for file in Files:
if file != ".gitkeep": # ignore this empty file
#string.append(file[:-8]) # adds files to list, removes the .graphml extension
string.append(file) # adds files to list, removes the .graphml extension
return string
def moveStateMachines(_src, _dest):
src = _src
dest = _dest
for src_dir, dirs, files in os.walk(src):
#print(dst_dir)
dst_dir = src_dir.replace(src, dest, 1)
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
for file_ in files:
src_file = os.path.join(src_dir, file_)
#print("moving " + src_file)
dst_file = os.path.join(dst_dir, file_)
if os.path.exists(dst_file):
os.remove(dst_file)
if ".gitkeep" in src_file:
pass
elif "graphml" in src_file:
#shutil.copy(src_file, dst_dir + "/stateDiagrams") #no longer copy stuff here, not really needed for this repository
shutil.move(src_file, dst_dir + "/stateDiagrams") # make me m ove instead of copy
else:
shutil.move(src_file, dst_dir)
def pickModules():
# modules = os.listdir("./modules/")
# print("\n\nSelect the modules you want to import\ntype 'done' when you are ready")
# for i, module in enumerate(modules):
# if i % 2 == 0:
# print("{:2d} {}".format(int(i/2), module[:-4]))
retValue = ""
while retValue != 'done':
modules = os.listdir("./modules/")
print("Select the modules you want to import\ntype 'done' when you are ready")
for i, module in enumerate(modules):
if i % 2 == 0:
print("{:2d} {}".format(int(i/2), module[:-4]))
retValue = input()
if retValue != 'done':
try:
i = int(retValue) * 2
print("\n\n" + modules[i] + " picked")
src = "modules/" + modules[i]
#print(src)
dest = folder + "/src/"
#print(dest)
shutil.copy(src, dest)
src = "modules/" + modules[i+1]
shutil.copy(src, dest)
time.sleep(1)
except:
print("invalid value!")
#clear()
def copyAllFiles():
#shutil.copy("updateTimers.py" , folder) // OBSOLETE DUE TO FAVOR OF DIFFERENT TIMING METHOD
#shutil.move("timers.tab" , folder)
#shutil.copy("src/updateIO.py" , folder + "/src/" ) # replaced by global executable
shutil.copy("src/io.tab" , folder)
#shutil.copy("serial.cpp" , folder) // OBSOLETE, may be moved to the module folder if still used...
#shutil.copy("serial.h" , folder)
shutil.copy("src/tasks.json" , folder + "/.vscode/" )
# shutil.copy("src/stateMachine.code-snippets", folder + "/.vscode/" ) # REDUNDANT one should use a global code-snippet instead, much better
shutil.copy("src/macros.h" , folder + "/src/" )
shutil.copy("src/stateMachineClass.h" , folder + "/src/" )
shutil.copy("src/stateMachineClass.cpp" , folder + "/src/" )
#shutil.copy("src/addDate.py" , folder + "/src/" ) # replaced by global executable
#shutil.copy("src/gitInit.py" , folder + "/src/" ) # replaced by global executable
#shutil.copy("src/release.py" , folder + "/src/" ) # replaced by global executable
shutil.copy("src/version.h" , folder + "/src/" )
#shutil.copy("src/changelog.py" , folder + "/src/" )
def assembleMain():
folder2 = folder[2:]
with open(folder + "/" + folder2 + ".ino", "w") as main: #main.c
#main.write('#include "src/timers.h"\n')
main.write('#include "src/io.h"\n')
main.write('#include "src/date.h"\n')
main.write('#include "src/version.h"\n')
main.write('#include "src/macros.h"\n')
if roundRobinTasks == 1 :
main.write('#include "roundRobinTasks.h"\n') # needed?
#main.write('#include " .h"\n') #fill in custom libraries
#main.write('#include " .h"\n')
for machine in stateMachines:
machine = machine[:-8]
main.write('#include "' + machine + '.h"\n\n')
main.write("void setup()\n")
main.write("{\n")
main.write(" initIO() ;\n")
main.write(" Serial.begin( 115200 ) ;\n")
main.write(" Serial.println( version ) ;\n")
main.write(" Serial.println( date ) ;\n")
for machine in stateMachines:
machine = machine[:-8]
main.write(" " + machine + "Init() ;\n")
main.write("}\n\n")
main.write("void loop()\n{\n")
if roundRobinTasks == 1 :
main.write(" processRoundRobinTasks() ;\n\n")
for machine in stateMachines:
machine = machine[:-8]
main.write("\t" + machine + "();\n")
main.write("}")
main.close()
def assembleRoundRobinTasks():
answer = input("\nDo you wish to include files for round robin task? [Y/n]\n")
if answer == 'Y' or answer == 'y':
print("GENERATING ROUND ROBIN FILES\n")
time.sleep( 1 )
roundRobinTasks = 1
with open(folder + "/roundRobinTasks.cpp", "w") as rr:
rr.write("""
#include "roundRobinTasks.h"
#include "src/io.h"
#include "src/macros.h"
extern void processRoundRobinTasks(void)
{
static unsigned char taskCounter = 0;
// HIGH PRIORITY ROUND ROBIN TASKS
// LOW PRIORITY ROUND ROBIN TASKS
switch( ++ taskCounter)
{
default:
taskCounter = 0;
/* fill in a task */
break;
case 1:
/* fill in a task */
break;
case 2:
/* fill in a task */
break;
}
}""")
rr.close()
with open(folder + "/roundRobinTasks.h", "w") as rr:
rr.write("#include <Arduino.h>\n\n")
rr.write("void processRoundRobinTasks() ;\n")
rr.close()
def getProjectName():
projectName = input("Type name of new project\n")
return projectName
def getBoardType():
subprocess.call( "arduino-cli.exe board listall" )
#subprocess.call( ['sh', './listAllBoards.sh'])
print("For what board will you be compiling?")
boardName = input("Fill in a FQBN of above list\n")
return boardName
#boardType = input("for what board will you be compiling\n")
def createFolders():
path = os.getcwd()
#print("Current Directory: " + path)
buildDir = os.path.abspath(os.path.join(path, os.pardir))
#print( buildDir )
folder = "../" + projectName
try:
#print(folder)
os.makedirs(folder)
os.makedirs(folder + "/src")
#os.makedirs(folder + "/src/modules") # OBSOLETE
#os.makedirs(folder + "/src/basics")
os.makedirs(folder + "/stateDiagrams")
os.makedirs(folder + "/.vscode")
return folder
except OSError:
print("ERROR FOLDER EXISTS")
exit()
pass
def assembleBuildScripts():
CPU = ""
CLOCK_FREQ = ""
CLOCK_SOURCE = ""
PROGRAMMER = ""
with open(folder + "/src/build.py", "w") as script:
if 'nano' in FQBN :
answer = input("NANO SELECTED, DO YOU USE THE OLD BOOTLOADER? [Y/n]\n\n")
if answer == 'Y' or answer == 'y':
CPU = ":cpu=atmega328old"
if 'ATTinyCore' in FQBN :
print("ATTINY CORE SELECTED\n")
CPU = ':chip=' + input("ENTER CHIP NUMBER \n1634\n2313/4313\n24/44/84\n441/841\n25/45/85\n261/461/861\n87/167\n48/88\n43\n828\n\n")
time.sleep(1)
clear()
CLOCK_FREQ = ",clock=" + input("ENTER CLOCK FREQUENCE IN MHz: 1,4,6,8,12,16,20\n\n")
time.sleep(1)
clear()
CLOCK_SOURCE = input("ENTER CLOCK SOURCE: 'internal' OR 'external'?\n\n")
time.sleep(1)
clear()
PROGRAMMER = " --programmer " + input("""WHAT PROGRAMMER WILL YOU BE USING?\n
usbasp USBasp (ATTinyCore)
micronucleusprog Micronucleus
parallel Parallel Programmer
arduinoasisp Arduino as ISP
diamexusbisp Diamex USB ISP
dragon AVR Dragon ISP mode (ATTinyCore)
usbtinyisp USBtinyISP (ATTinyCore) SLOW, for new or 1 MHz parts
ponyser Ponyser Programmer
stk500 Atmel STK500
arduinoasisp32u4 Arduino Leo/Micro as ISP (ATmega32U4)
usbtinyisp2 USBtinyISP (ATTinyCore) FAST, for parts running >=2 MHz
atmel_ice Atmel-ICE
avrispmkii AVRISP mkII
avrisp\n\n
""")
script.write( "#!/usr/bin/env python\n")
script.write( 'import os\n')
script.write('import sys\n')
script.write( "print('ASSEMBLING IO FILES')\n")
script.write( 'os.system("updateIO.py")\n')
script.write( "print('ADDING TIME STAMP')\n")
script.write( 'os.system("addDate.py")\n')
script.write( "print('BUILDING PROJECT')\n")
script.write( "x = os.system('arduino-cli compile -b " + FQBN + CPU + CLOCK_FREQ + CLOCK_SOURCE + buildDir + " -e')\n")
script.write( "if x == 1 :\n" )
script.write( " print('BUILD FAILED!!!')\n" )
script.write( " sys.exit(1)\n" )
script.write( "else :\n" )
script.write( " print('BUILD SUCCES!!!')\n" )
script.write( " sys.exit(0)\n" )
script.close()
ports = serial.tools.list_ports.comports()
for port, desc, hwid in sorted(ports):
lastPort = port
with open(folder + "/src/upload.py", "w") as script:
script.write("#!/usr/bin/env python\n")
script.write('import os\n')
script.write('import sys\n')
script.write('import serial.tools.list_ports\n\n')
script.write("""
def get_com_port():
ports = list(serial.tools.list_ports.comports())
for port in ports:
if 'Arduino' in port.description or 'CH340' in port.description:
return port.device
return None
\n""")
script.write("""
com_port = get_com_port()
if com_port is None:
print("No device on COM port found")
sys.exit(1)
\n""")
script.write('retCode = os.system("python src/build.py")\n')
script.write('if retCode == 0 :\n')
script.write(' print("UPLOADING")\n')
script.write(' retCode = os.system(f"arduino-cli upload -b ' + FQBN + CPU + CLOCK_FREQ + CLOCK_SOURCE + PROGRAMMER + ' -p ' + "{com_port}" + ' -i ' + buildDir + './build/')
for letter in FQBN:
if( letter == ':'):
script.write('.')
else:
script.write( letter )
script.write('/' + projectName + '.ino.hex")\n')
script.write(' if retCode == 1 :\n')
script.write(' print("UPLOADING FAILED!!! ")\n')
script.write(' else :\n')
script.write(' print("UPLOADING SUCCES!!! ")\n')
script.close()
dateTimeObj = datetime.now()
with open(folder + "/changelog.txt", "w") as log: # makes changelog
log.write(projectName + "\nv0.0.0 Date/time:" + str(dateTimeObj) + "\nProject assembled\n\n" )
log.close()
### BEGIN SCRIPT ###
clear()
projectName = getProjectName()
clear()
FQBN = getBoardType()
print (FQBN + " selected\n" )
clear()
folder = createFolders()
stateMachines = getStateMachines() #GENERATE ALL MAIN STATE MACHINES
#print("stateMachines: ")
#print( stateMachines)
for machine in stateMachines:
print("GENERATING STATE MACHINES")
print( machine )
os.system("python ./src/stateMachineGenerator.py " + "stateMachines/"+ machine )
for file in glob.glob('*.cpp'):
shutil.move(file, "stateMachines/")
for file in glob.glob('*.h'):
shutil.move(file, "stateMachines/")
#os.system("mv.exe *.cpp stateMachines/") // does not work outside vs code due to lack of git bash
#os.system("mv.exe *.h stateMachines/")
moveStateMachines("stateMachines", folder)
pickModules()
clear()
copyAllFiles()
assembleRoundRobinTasks()
clear()
assembleMain()
assembleBuildScripts()
clear()
input("FINISHED press <ENTER> to close the program")
### END SCRIPT ###