Wednesday, July 22, 2009

Welcome

Welcome to the hard-wired blog. This is where I want to tell the world what I've been working on in the world of physical computing.

In this first post I want to tell you how I write code for the Arduino platform (or the ATMega platform but I've been doing this on the Arduino specifically). I started out by downloading the general Arduino sofware and that worked well up to a point, but I'm a software engineer at heart and wanted full control over the build/release/version control process. My development machine is a Mac BTW.

IDE

I use Eclipse as an IDE - with the C++ plug-in. I have a new project for each "program".

Makefile

I copied the standard Makefile from the Arduino software download and "made it work" for me. This involved making the following changes:

############################################################################
# Below here nothing should be changed...

ARDUINO = $(INSTALL_DIR)/hardware/cores/arduino
AVR_TOOLS_PATH = $(INSTALL_DIR)/hardware/tools/avr/bin
SRC = $(ARDUINO)/pins_arduino.c $(ARDUINO)/wiring.c \
$(ARDUINO)/wiring_analog.c $(ARDUINO)/wiring_digital.c \
$(ARDUINO)/wiring_pulse.c \
$(ARDUINO)/wiring_shift.c $(ARDUINO)/WInterrupts.c twi.c
CXXSRC = $(ARDUINO)/HardwareSerial.cpp $(ARDUINO)/WMath.cpp \
$(ARDUINO)/Print.cpp SerialLED4.cpp \
TSens.cpp \
DLApp.cpp

(Famous last words on the "nothing should change below this point"). I simply add my new source code to the dependencies above.

I then make sure that the "builder" for the project is external and make sure that the build directory is the root directory of my project.

(Images to come)

Application

I create one main application class with setup() and loop() methods and instantiate that in my main sketch, delegating the setup() and loop() methods of the sketch to the application. I could of course alter the makefile to do this myself but it works ok. I also include the following in my sketch:

extern "C" void __cxa_pure_virtual() {};

because without it most programs will not link.

Programming using C++

I have the following rules for my C++ classes:

Always include the following at the top

#include <WProgram.h>
#include <WConstants.h>
#include <inttypes.h>

Never use new and delete

Use #define, #ifdef etc. where needed to exclude functionality as desired. This is important if you need to reduce the size of the overall application to remove bloat if your particular program does not need a feature. I reuse a lot of my classes in my projects and this helps a lot while keeping your applications looking consistent. And that leads on to:

Have a standard application_config.h file which is included in all source files that contains those definitions (on or off) to customise your application. You could put it in your makefile but I try not to touch the makefile much other than to add source file dependencies.

Building

Once it's all setup I simply do

make

then

make upload

once the board is connected to the computer. Works a treat. In a future post I'll go through a typical C++ class design and how I approach that.

No comments:

Post a Comment