Monday, June 30, 2014

Object Oriented Arduino

As we all noticed, Arduino is not more than a bunch of (Pretty nice organized/integrated though) libraries (Arduino, Wiring, some LCDs, etc), bootloader, and the AVR gcc compiler.

But as i was programing some sketches for arduino, i noticed that using a lot of functions to try not repeat myself on the setup() loop() of the ".ino" kind of sketches, i was building a big pile of "spaguetti code"... it was harder to follow the program execution as the code was growing.

Then just out of curiosity, intrigued by how those 2 functions and the others functions and loose variables written on a ".ino" sketch file ended into a C/C++ source, i opened my arduino installation directory, and started searching for some kind of template or something where my sketches were included/inserted.

Arduino cores source files

Here, i said "hey! 'main.cpp', let's have a look at this". this is the content of this main.cpp:


#include <Arduino.h>

int main(void)
{
init();


#if defined(USBCON)
USBDevice.attach();
#endif

setup();
 
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
     
return 0;
}


Then, if you have a look to the Arduino.h file, there is some of the avr libs, arduino constants, functions and other definitions.

This means that you can actually write plain C/C++ code and include it, just like any other C++ program you have written before.

Though that is not a secret (that you can write objects), the small code examples we get from internet (which are never OO based examples) and the way the IDE is designed, aren't of any help when you want/need to work with a big (nor medium) number of classes and files.

Now, the arduino IDE interface is kind of ugly when you try to add more files to your project, i can't remember exactly why, but you can try and shure you'll say "mmmh, isn't another ide for arduino out there?". Yes, there are.

I'm now using SublimeText with the Stino plugin:

Stino and SublimeText
Stino and SublimeText


In the last image i show you the setup() and loop() functions of a sketch that sends through XBee the value of a Sensor and the values obtained from a GPS if the data is obtained successfully from it.

In this case, every sensor implements a common interface, so i can add more sensors or exchange them without much trouble.
In the first line of loop(), the object sh calls a method named read() which returns a string of data. This public method was inherited from the implementation of this code:

/*
* sensors.h
* This is a class definition for the Sensors interface.
* The list of virtual methods are common to all sensors
* to perform basic tasks.
*
* (C) Ricardo Carrasco Cursach 2014
*
* last modified: 16-03-14
* by: rccursach
*
*/

#ifndef   SENSORS_H
#define   SENSORS_H

#include <Arduino.h>

class Sensors{
  public:
    virtual void begin() = 0;
    virtual String read() = 0;
    virtual int getRequiredPins() = 0;
    virtual int getReqSerialSpeed() = 0;
    virtual ~Sensors() = 0;
};

#endif

If you like to do the same this is what you need:

Sublime text: http://www.sublimetext.com/2
Package Control for Sublime: https://sublime.wbond.net/installation
Arduino IDE http://arduino.cc/en/Main/Software
Install Stino plugin from Package Control.

I don't need to mention how this is useful to keep your code readability, maintainability, and all the other benefits of an object oriented approach in your arduino code.