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
|
#!/usr/bin/env python
from __future__ import print_function
import serial
import os
import argparse
from clint.textui import prompt, validators, colored, puts
## Global Vars
VERSION = "0.1"
directory = ""
ard_template = "\n\
#include <Arduino.h>\n\
#include <Wire.h>\n\
\n\
\n\
void setup() {\n\
}\n\
\n\
void loop() {\n\
}\n\
"
## Command Parser
parser = argparse.ArgumentParser(description='Arduino Makefile and boilerplate project generator. For use with Ard-Makefile')
parser.add_argument('-v', '--verbose', action='store_true', help="Print file contents during creation")
parser.add_argument('-d', '--directory', default='.', help='Directory to run generator')
args = parser.parse_args()
directory = args.directory
def generateMakefile():
src_dir = ""
puts(colored.cyan("Generating Arduino Makefile..."))
puts(colored.red("Any existing Makefile in current directory will be overwritten!!"))
# Header
fileContents = "# Generated by ard-make version " + VERSION + "\n\n"
btag = prompt.query('Board tag?', default='uno')
if not btag == 'uno':
bsub = prompt.query('Board sub micro?', default='atmega328')
f_cpu = prompt.query('Board frequency', default='16000000L')
else:
bsub = 'AUTO'
f_cpu = 'AUTO'
monitor_port = prompt.query('Arduino port?', default='AUTO')
fileContents += checkDefine('BOARD_TAG', btag)
fileContents += checkDefine('BOARD_SUB', bsub)
fileContents += checkDefine('F_CPU', f_cpu)
fileContents += checkDefine('MONITOR_PORT', monitor_port)
if not prompt.yn('Extended options?', default='n'):
if not prompt.yn('Define local folders?', default='n'):
src_dir = prompt.query('Sources folder (Makefile will be created here)?', default='', validators=[])
userlibs = prompt.query('Library folder (will create if does not exist) - AUTO is Sketchbook directory?', default='AUTO', validators=[])
obj_dir = prompt.query('Output directory?', default='AUTO', validators=[])
boards_txt = prompt.query('Boards file?', default='AUTO')
isp_prog = prompt.query('ISP programmer?', default='atmelice_isp')
isp_port = prompt.query('ISP port?', default='AUTO')
if not prompt.yn('Quiet make?', default='n'):
fileContents += "ARDUINO_QUIET = 1\n"
fileContents += checkDefine('ISP_PROG', isp_prog)
fileContents += checkDefine('ISP_PORT', isp_port)
fileContents += checkDefine('BOARDS_TXT', boards_txt)
# Check andd create folders
checkCreateFolder(src_dir)
checkCreateFolder(userlibs)
checkCreateFolder(obj_dir)
# Makefile will be in src_dir so lib and bin must be relative
if src_dir:
userlibs = "../" + userlibs
obj_dir = "../" + obj_dir
fileContents += checkDefine('USER_LIB_PATH', userlibs)
fileContents += checkDefine('OBJDIR', obj_dir)
if not prompt.yn('Create template Arduino source?', default='n'):
sourceFilename = prompt.query('Name of project?', default='')
if src_dir:
writeTemplate(src_dir + "/" + sourceFilename)
else:
writeTemplate(sourceFilename)
fileContents += checkDefine('TARGET', sourceFilename)
if not "ARDMK_DIR" in os.environ:
ardmk = prompt.query('Arduino Makefile path?', default='/usr/share/arduino', validators=[validators.PathValidator()])
ardmk = "ARDMK_DIR := " + ardmk + "\n"
fileContents += "\ninclude $(ARDMK_DIR)/Arduino.mk"
# Add forward slash if source directory exists
if src_dir:
writeToMakefile(fileContents, (src_dir + "/"))
else:
writeToMakefile(fileContents, "")
return fileContents
def writeToMakefile(fileContents, path):
makefile = open(path + "Makefile", 'w')
puts(colored.cyan("Writing Makefile..."))
if args.verbose:
puts(colored.yellow(fileContents))
makefile.write(fileContents)
makefile.close()
def writeTemplate(filename):
src = open((filename + ".ino"),'w')
puts(colored.cyan("Writing " + filename + ".ino..."))
if args.verbose:
puts(colored.yellow(ard_template))
src.write("/* Project: " + filename + " */\n" + ard_template)
src.close()
def checkCreateFolder(folder):
if folder and not folder == 'AUTO':
if not os.path.exists(folder):
puts(colored.cyan(("Creating " + folder + " folder")))
os.makedirs(folder)
def checkDefine(define, user):
if user and not user == 'AUTO':
return define + " = " + user + "\n"
else:
return ""
def main():
# Create directory if not exist
checkCreateFolder(directory)
# Change to dir so all commands are run relative
os.chdir(directory)
generateMakefile();
main()
|