|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | +import serial |
| 5 | +import os |
| 6 | +import argparse |
| 7 | +from clint.textui import prompt, validators, colored, puts |
| 8 | + |
| 9 | +## Global Vars |
| 10 | +VERSION = "0.1" |
| 11 | +directory = "" |
| 12 | +ard_template = "\n\ |
| 13 | +#include <Arduino.h>\n\ |
| 14 | +#include <Wire.h>\n\ |
| 15 | +\n\ |
| 16 | +\n\ |
| 17 | +void setup() {\n\ |
| 18 | +}\n\ |
| 19 | +\n\ |
| 20 | +void loop() {\n\ |
| 21 | +}\n\ |
| 22 | +" |
| 23 | + |
| 24 | +## Command Parser |
| 25 | +parser = argparse.ArgumentParser(description='Arduino Makefile and boilerplate project generator. For use with Ard-Makefile') |
| 26 | +parser.add_argument('-v', '--verbose', action='store_true', help="Print file contents during creation") |
| 27 | +parser.add_argument('-d', '--directory', default='.', help='Directory to run generator') |
| 28 | +args = parser.parse_args() |
| 29 | + |
| 30 | +directory = args.directory |
| 31 | + |
| 32 | +def generateMakefile(): |
| 33 | + src_dir = "" |
| 34 | + |
| 35 | + puts(colored.cyan("Generating Arduino Makefile...")) |
| 36 | + puts(colored.red("Any existing Makefile in current directory will be overwritten!!")) |
| 37 | + # Header |
| 38 | + fileContents = "# Generated by ard-make version " + VERSION + "\n\n" |
| 39 | + btag = prompt.query('Board tag?', default='uno') |
| 40 | + if not btag == 'uno': |
| 41 | + bsub = prompt.query('Board sub micro?', default='atmega328') |
| 42 | + f_cpu = prompt.query('Board frequency', default='16000000L') |
| 43 | + else: |
| 44 | + bsub = 'AUTO' |
| 45 | + f_cpu = 'AUTO' |
| 46 | + monitor_port = prompt.query('Arduino port?', default='AUTO') |
| 47 | + |
| 48 | + fileContents += checkDefine('BOARD_TAG', btag) |
| 49 | + fileContents += checkDefine('BOARD_SUB', bsub) |
| 50 | + fileContents += checkDefine('F_CPU', f_cpu) |
| 51 | + fileContents += checkDefine('MONITOR_PORT', monitor_port) |
| 52 | + |
| 53 | + if not prompt.yn('Extended options?', default='n'): |
| 54 | + if not prompt.yn('Define local folders?', default='n'): |
| 55 | + src_dir = prompt.query('Sources folder (Makefile will be created here)?', default='', validators=[]) |
| 56 | + userlibs = prompt.query('Library folder (will create if does not exist) - AUTO is Sketchbook directory?', default='AUTO', validators=[]) |
| 57 | + obj_dir = prompt.query('Output directory?', default='AUTO', validators=[]) |
| 58 | + boards_txt = prompt.query('Boards file?', default='AUTO') |
| 59 | + isp_prog = prompt.query('ISP programmer?', default='atmelice_isp') |
| 60 | + isp_port = prompt.query('ISP port?', default='AUTO') |
| 61 | + if not prompt.yn('Quiet make?', default='n'): |
| 62 | + fileContents += "ARDUINO_QUIET = 1\n" |
| 63 | + |
| 64 | + fileContents += checkDefine('ISP_PROG', isp_prog) |
| 65 | + fileContents += checkDefine('ISP_PORT', isp_port) |
| 66 | + fileContents += checkDefine('BOARDS_TXT', boards_txt) |
| 67 | + |
| 68 | + # Check andd create folders |
| 69 | + checkCreateFolder(src_dir) |
| 70 | + checkCreateFolder(userlibs) |
| 71 | + checkCreateFolder(obj_dir) |
| 72 | + |
| 73 | + # Makefile will be in src_dir so lib and bin must be relative |
| 74 | + if src_dir: |
| 75 | + userlibs = "../" + userlibs |
| 76 | + obj_dir = "../" + obj_dir |
| 77 | + |
| 78 | + fileContents += checkDefine('USER_LIB_PATH', userlibs) |
| 79 | + fileContents += checkDefine('OBJDIR', obj_dir) |
| 80 | + |
| 81 | + if not prompt.yn('Create template Arduino source?', default='n'): |
| 82 | + sourceFilename = prompt.query('Name of project?', default='') |
| 83 | + if src_dir: |
| 84 | + writeTemplate(src_dir + "/" + sourceFilename) |
| 85 | + else: |
| 86 | + writeTemplate(sourceFilename) |
| 87 | + fileContents += checkDefine('TARGET', sourceFilename) |
| 88 | + |
| 89 | + if not "ARDMK_DIR" in os.environ: |
| 90 | + ardmk = prompt.query('Arduino Makefile path?', default='/usr/share/arduino', validators=[validators.PathValidator()]) |
| 91 | + ardmk = "ARDMK_DIR := " + ardmk + "\n" |
| 92 | + |
| 93 | + |
| 94 | + fileContents += "\ninclude $(ARDMK_DIR)/Arduino.mk" |
| 95 | + |
| 96 | + # Add forward slash if source directory exists |
| 97 | + if src_dir: |
| 98 | + writeToMakefile(fileContents, (src_dir + "/")) |
| 99 | + else: |
| 100 | + writeToMakefile(fileContents, "") |
| 101 | + |
| 102 | + return fileContents |
| 103 | + |
| 104 | +def writeToMakefile(fileContents, path): |
| 105 | + makefile = open(path + "Makefile", 'w') |
| 106 | + puts(colored.cyan("Writing Makefile...")) |
| 107 | + if args.verbose: |
| 108 | + puts(colored.yellow(fileContents)) |
| 109 | + makefile.write(fileContents) |
| 110 | + makefile.close() |
| 111 | + |
| 112 | +def writeTemplate(filename): |
| 113 | + src = open((filename + ".ino"),'w') |
| 114 | + puts(colored.cyan("Writing " + filename + ".ino...")) |
| 115 | + if args.verbose: |
| 116 | + puts(colored.yellow(ard_template)) |
| 117 | + src.write("/* Project: " + filename + " */\n" + ard_template) |
| 118 | + src.close() |
| 119 | + |
| 120 | +def checkCreateFolder(folder): |
| 121 | + if folder and not folder == 'AUTO': |
| 122 | + if not os.path.exists(folder): |
| 123 | + puts(colored.cyan(("Creating " + folder + " folder"))) |
| 124 | + os.makedirs(folder) |
| 125 | + |
| 126 | +def checkDefine(define, user): |
| 127 | + if user and not user == 'AUTO': |
| 128 | + return define + " = " + user + "\n" |
| 129 | + else: |
| 130 | + return "" |
| 131 | + |
| 132 | +def main(): |
| 133 | + # Create directory if not exist |
| 134 | + checkCreateFolder(directory) |
| 135 | + # Change to dir so all commands are run relative |
| 136 | + os.chdir(directory) |
| 137 | + generateMakefile(); |
| 138 | + |
| 139 | +main() |
0 commit comments