Sunday, July 28, 2013

Tip: External editor for Arduino

I love Arduino: it is the perfect microcontroller platform. It is easy to program on Linux, cheaply available, there is a library for most anything you can think of. And the community is great for when you get stuck.

My only grudge: The Arduino editor doesn't match up to Emacs.

Yes, the IDE is great, but for large programs I find myself yearning for a C mode, proper indenting, incremental search, buffers. You know, a real editor.

Makefile for Arduino

So I experimented with using Emacs and compiling from the console. After experimenting with a few Makefiles, OpenXC works great for me. Clone their project, and set up environment variables according to your setup. I had to change exactly zero variables for their examples to work out of the box on Ubuntu. The Makefile is well documented. You can upload the program to an arduino by doing 'make upload'. This works with simple sketches and for doing everything through a shell script or cron.

It has limitations. It won't do pre-processing: so it will claim that methods or data structures are not defined. It won't work with multiple .ino files. Also, the list of boards is limited, and it doesn't have an easy way to specify Arduino Mini with 8Mhz, which is the device I'm currently hacking on.

External editor for Arduino

Navigate to:
File -> Preferences -> Use external editor.

This should gray out your editor window. Now, your Arduino IDE is only for compiling, uploading, and for the serial monitor, which it excels at. To save screen real estate, you can make the window short, though it wants to be a minimum height. Now edit it in your favorite editor.

When you are ready to compile, switch to Arduino (Alt-Tab), and then compile (Ctrl-R) or upload (Ctrl-U).

I'm waiting for Arduino to tie in with inotify on Linux and automatically compile when the code changes.  But in a pinch, this workflow gets over most of the annoyances of the Arduino IDE while retaining most of its advantages.

Makefile + External Editor

The best approach might be to combine the two above. Switch to an external editor in Arduino, and use a Makefile for automated builds on the command-line. The automated builds on a console will quickly let you know if your incremental edits introduced a compilation bug. And when you want to upload it to your boards, switch over to Arduino IDE.