Friday, February 15, 2013

Che Cosa?

Cosa is an object-oriented platform for Arduino. It replaces the Arduino and Wiring library with a large set of integrated classes that support the full range of AVR/ATmega/ATtiny internal hardware modules; all pin modes, Digital, and Analog Pins, External and Pin Change Interrupts, Analog Comparator, PWM, Watchdog, Timer0 (RTC), Timer1 (Servo), UART, USI, SPI, TWI and EEPROM.
Cosa is implemented as an Arduino IDE core. The Cosa platform can be used with almost all Arduino boards and ATtiny/ATmega processors. All classes may be compiled for all variants.


Though object-oriented with optional operator overloading syntax, Cosa is between 2-10X faster than Arduino with regard to digital pin functions. This comes with a small price-tag; memory, 4 bytes per digital pin and 12 bytes per analog pin. Cosa analog pin objects holds the latest sample and allows an event handler. See the benchmarks in the examples directory for further details.

Cosa also contains a data streaming format (Ciao) for encoding of C/C++ language data types such as strings, integer and floating pointer numbers into a binary format. It may be used for a number of applications; tracing, remote procedure calls, data exchange between Arduino devices, etc. The format allows user data types to be defined and values exchanged without additional encoding. The stream header itself is a pre-defined serializable data type. Ciao is used to define an Arduino monitoring and control language (Cosa fai) which has much in common with Firmata.


The primary programming paradigm is object-oriented and state-machine/event driven with proto-threads or multi-tasking. There is a large number of device drivers available for SPI, I2C (TWI) and 1-Wire (OWI). A strict directory structure is used to organize the Cosa/driver source code. Sub-directories are used for each driver type. This allows a foundation for scaling and configuration.

To improve debugging and testing there is trace/syslog style support. The IOStream class allows output to both serial communication (UART) and small TFT displays (such as the ST7735R). The drawing Canvas class supports basic drawing operation and scripting to reduce program memory footprint. The Canvas class also supports drawing of icons and multiple fonts (GLCD and UTFT).

The popular VirtualWire library has been refactored to the object-oriented style of Cosa (VWI) and extended with three additional codecs; Manchester, 4B5B and Bitstuffing. This allows basic ultra cheap wireless nodes with RF315/433 receiver and transmitter. For more advanced wireless connections there is also a driver for the Nordic Semiconductor NRF24L01+ chip, which allows low-power wireless communication of up to 2 Mbps in the 2.4GHz band, and the TI CC1101 Low-Power Sub-1 GHz RF Transceiver.

The goal of this project is to provide an efficient programming platform for rapid prototyping of "Internet-of-things"-devices. There is an Ethernet/Socket with W5100 Ethernet controller device driver. This implementation allows streaming direct to the device buffers. Cosa also implements a number of IP protocols; DNS, DHCP, NTP, HTTP, SNMP and MQTT.

Unfortunately Cosa is not a beginners entry level programming platform, though following some of the design patterns in Cosa will help beginners build more complex small scale embedded systems with richer concurrency and low power consumption. So much for the sales pitch. Now lets see whats Cosa all about when programming the Arduino.

1 comment:

  1. Hi Mikael, I recently discovered your Cosa project and was amazed to find that it was pretty much exactly what I was looking for - a mature, efficient, object-oriented approach to Arduino. I was just starting to write my own, but happily now I don't have to! (Plus, yours is much better than anything I would have hacked together). I teach Product Design and Interaction and have been using Arduino for years, and I'd like to adapt Cosa to teaching beginners (and non-programmers) the basics of interaction. I've started putting together some easy boilerplate examples that my students can learn from, using Cosa, at https://github.com/pixelpusher/evlib. I'd love to hear from you about it, and perhaps you're interested in more of a collaboration, or have some ideas about teaching Arduino you'd like to share. I didn't see your email address on this blog, so please get it touch with me at e.raskob@rave.ac.uk when you have a chance. And thanks for Cosa!

    ReplyDelete