Experimenting with Arduino-Builder

Lately, I have been experimenting with Arduino-Builder. It was been available as a command line interface to compile sketches since version 1.6.6 of the Arduino IDE.  It is actually what the Arduino IDE invokes behind the scenes to compile the sketch.

It is written in the Go language. The source code is available HERE on github.com.

Arduino-Builder is easily invoked to compile all of your sketches with a script like this:

#!/bin/bash
cd PATH_TO_YOUR_ARDUINO_IDE
for sketch in `find ~/sketchbook/ -name '*.ino'`
do
  ./arduino-builder -hardware ./hardware -tools ./hardware/tools/avr -tools ./tools-builder -libraries ./libraries -libraries ~/sketchbook/libraries/ -fqbn arduino:avr:uno $sketch
done

To see a list of all the ways it can be customized, invoke it without any parameters.

I am not a fan of the Arduino IDE due to lack of an easy to use editor.  I’d prefer to use VI to edit Arduino sketches. However, the IDE does have an easy way to compile the sketches and load them via AVRDUDE.

Arduino IDE

 

 

 

 

 

Now that I’ve been experimenting with Arduino-Builder, I came up with a bash script to both compile and load a sketch.

The bash script:

#!/bin/bash
# From:  http://microcontrollerelectronics.com

dir="/opt/arduino/"
ver="arduino-1.6.13"
brd="arduino:avr:uno"
bpath="/temp/"
dev="/dev/ttyUSB0"
udir="/"$USER"/sketchbook/"
loc=$dir$ver"/"
libs="libraries"
mylibs="/"$USER"/sketchbook/libraries"
tools="hardware/tools"
bldr="tools-builder"
hw="hardware"
pgmr="arduino"
part="m328p"
sname=""
verbose="false"

while [ "$#" -gt 0 ]; do
  case "$1" in
    -v) ver="$2"; shift 2;;
    -l) dir="$2"; shift 2;;
    -b) brd="$2"; shift 2;;
    -d) dev="$2"; shift 2;;
    -p) bpath="$2"; shift 2;;
    -u) udir="$2"; shift 2;;

    --ver=*)     ver="${1#*=}";     shift 1;;
    --dir=*)     dir="${1#*=}";     shift 1;;
    --brd=*)     brd="${1#*=}";     shift 1;;
    --dev=*)     dev="${1#*=}";     shift 1;;
    --bpath=*)   bpath="${1#*=}";   shift 1;;
    --udir=*)    udir="${1#*=}";    shift 1;;
    --verbose=*) verbose="${1#*=}"; shift 1;;

    --ver|--verbose|--dir|--brd|--dev|--bpath|--udir) echo "$1 requires an argument" >&2; exit 1;;

    -*) echo "unknown option: $1" >&2; exit 1;;
     *) sname="$1"; shift 1;;
  esac
done

if [ -z "$sname" ]; then
 echo "No sketch specified!"
 exit 1
fi

sketch=${udir}${sname}"/"${sname}".ino"

if [ ! -f "$sketch" ]; then
 echo "Sketch not found!"
 exit 1
fi

set -e

${loc}arduino-builder -verbose=$verbose -fqbn=$brd -libraries=$loc$libs -libraries=$mylibs -hardware=$loc$hw -tools=$loc$tools -tools=$loc$bldr -build-path=$bpath $sketch

if [ ! -e "$dev" ]; then
 echo "avrdude Port $dev not found!"
 exit 1
fi

avrdude -p $part -c $pgmr -P $dev -U flash:w:${bpath}${sname}.ino.hex:i

Experimenting with Arduino-BuilderNow, I have the best of all worlds (so to speak). I can edit a sketch using VI and run the script containing Arduino-Builder to compile and load it.

Leave a Reply

Your email address will not be published.