Thursday, February 21, 2013

A First Sketch

After installing and moving the different directories (Cosa source/headers and examples) into place it is time to look at an example. Restart Arduino IDE and check that the examples are now available in your Sketchbook.


You should have the above menu structure with the Cosa examples. Select the CosaBlinkPeriodic example and let us look into an interesting twist of the classical Arduino blink example.

This example make use of Cosa's class for Periodic functions together with the Watchdog timer and the Event dispatcher. The twist is running three LEDs (RGB) with different frequencies to generate a palette of colors.

For this example we include the Periodic class, the Pins header file for the OutputPin class and the Board Pin definitions. We use the periodic class with its run() method to allow toggling of the output pin with a given time period. 

#include "Cosa/Periodic.hh"
#include "Cosa/Pins.hh"

#include "Cosa/Board.hh"

class LED : public Periodic {
private:
  OutputPin m_pin;
public:
  LED(Board::DigitalPin pin, uint16_t ms, uint8_t initial = 0) :
    Periodic(ms), m_pin(pin, initial) {}
  virtual void run() { m_pin.toggle(); }
};


The next step is to create three instances to handle the RGB LED connected to pins 5, 6 and 7. Here comes a big difference to standard Arduino/Wiring. Instead of a pin number, as in Arduino Wiring, Cosa uses symbols (see Board.hh) which allows the compiler to do some checking. The second constructor parameter is the timeout period in milliseconds. The last parameter is the initial state of the LED, default is zero(0), LED off. The green LED is initiated to one(1), LED on. 

LED redLedPin(Board::D5, 512);
LED greenLedPin(Board::D6, 1024, 1);
LED blueLedPin(Board::D7, 1024);


The red LED should be connected to the D5 pin. It will blink with a 1 Hz period (toggled on and off every 512 ms). The green LED to the D6 pin. It will blink with 0.5 Hz. Etc.

The setup() is simply to start the time out event generator. The Watchdog is used for this to allow low power mode in Cosa. The ATmega/ATtiny built-in Watchdog has its own clock circuit. The parameters to Watchdog::begin() are the timeout period in ms (16 is the shortest period), sleep mode and interrupt handler. The predefined interrupt handler that pushes timeout events is used.

void setup()
{
  Watchdog::begin(16, SLEEP_MODE_IDLE, Watchdog::push_timeout_events);
}


The main loop is a basic event dispatcher. The Watchdog will produce timeout events which are then dispatch to calls to the LED class run() functions.

void loop()
{
  Event event;
  Event::queue.await(&event);
  event.dispatch();
}


The event wait will put the processor in low power mode.

There are several advantages with the object-oriented nature of Cosa compared to Arduino/Wiring. The most important is speed. Cosa trades a small amount of memory to cache the pin special register reference and port bit mask. This gives between X2-X10 speedup compared to Arduino/Wiring. I will get back to this in a later posting. 

No comments:

Post a Comment