Building a solution from the command line

The Arduino’s IDE is just awful. Period. To write sketches is awful as well. Scketches make me sick. Hopefully we can build our solutions from the command line, directly from the main() function, even using our favorite code editor, VIM for me. A more-in-depth tutorial on this topic can be found here.

Installing Arduino

We need to download the latest Arduino‘s release, as for this writing, 1.8.5. Decompress it wherever you want. Then enter into the uncompressed folder and install it with:

./install.sh

Note: Uninstall any previous installation. Older releases will work,  but they won’t compile my tweakings.

Installing Arduino.mk

In order for me to work I’d rather install this tool from the Linux Mint repositories:

sudo apt install Arduino.mk

I guess that’s all for the needed tools. If any missing I’ll update this section.

Makefile for our solution

We need to write a Makefile for each of our projects:

ARDUINO_DIR = /your/home/dir/arduino-1.8.5
BOARD_TAG = uno
ARDUINO_PORT = /dev/ttyUSB*
ARDUINO_LIBS =
CFLAGS_STD = -std=c99 -Wall
CXXFLAGS_STD = -std=gnu++11 -Wall -DUNO
include /usr/share/arduino/Arduino.mk

The flag ‘-DUNO’ isn’t needed at all, but as I’m using the Leonardo and Uno boards, I need it to make the difference between them as easy as possible.

Test it!

Before we can upload our executable we must add our user to the dialout group so the serial port is available to us as humans:

$sudo adduser <your_user> dialout

Then log out and log in into your session again.


Last step is to write a test program. Let’s call it main.cpp:

#include <Arduino.h>

int main(void)
{
 init();
 // initialize the Arduino's environment

 pinMode(LED_BUILTIN, OUTPUT);

 while( 1 )
 {
   digitalWrite( LED_BUILTIN, LOW );
   delay( 100 );
   digitalWrite( LED_BUILTIN, HIGH );
   delay( 100 );
 }

 return 0;
}

For compiling write in the console:

make

and for uploading it:

make upload

And the serial monitor?

We have two options:

  • The arduino.mk includes one: make monitor (to exit ctrl-a + ctrl-k).
  • But if you don’t like it, then you might use one of the many serial monitors available in Linux. I like GtkTerm because you can close and open the USB port on demand (for when you’re going to upload the code, for example) without leaving it.

Greetings!

Anuncios

2 Replies to “Building a solution from the command line”

  1. For non devs or simple lazy devs, there is a tool named amake that hides the complicated part of the Arduino IDE command line and has some nice features:

    * Code is Full compatible with the Arduino IDE platform, so no mo tweaking the code to share it with users of Arduino IDE.
    * It uses de Arduino IDE, so you have no problem adding support for libraries/boards/programmers: just fire the Arduino IDE and use the default tools to manage libraries, boards and programmers.
    * It’s a smart wrapper of the Arduino IDE Command line interface with some nice features.

    For example to verify/compile a sketch is just this:

    amake -v [board] [file]

    Yes, the parameters are opional, once you compiled it and it worked, you can keep doing just
    amake -v

    The same for uploading:

    amake -u [board] [file] [serial_device]

    Even so, it has a auto detect feature for the serial device so most of the time you don’t need to put the /dev/ttyUSB0 argument.

    Take a peek at: https://github.com/pavelmc/amake

    You can integrate it with other IDEs, fo example I use it with Geany.

    Cheers.

    Me gusta

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s