Sharpduino Library: Testing

Warning: This post is mostly about Unit testing and less about Sharpduino as a library.

It has been almost two months now that I have been developing the next version of the sharpduino library. I feel that the new implementation is far better in terms of understanding what each part of the code does, with as few byte constants as possible, so anyone who looks at the code can understand what is happening without having the firmata.org page or (shameless plug) my blog open. I tried to make various parts of the library as loosely coupled as possible, so it could be extensible and easier to code against.

In the previous version the library was tested by a console application trying to make sure that everything worked for the end users by telling them what to do (connect LEDS, etc.) and then what to expect as the library was used (Led turns on/off, etc).

For this version much of the functionality was pre-tested with unit tests before even hooking up an arduino. This was done with the help of the NUnit and Moq libraries. I won’t be going into detail about the choice of Unit testing and Mocking frameworks. Let’s just say that the NUnit – Moq combination worked for me without much hustle. If you are interested in finding alternative frameworks, the following is a small list which I made while searching for my library. This is not an exhaustive list, but a few of the most prevalent frameworks that I could find.

Unit Testing Frameworks: NUnit, XUnit.net,MSTest, MbUnit
Moq Frameworks: NMock,RhinoMocks,Moq,NSubstitute

Continue reading

Firmata Messages : Outgoing

Following my previous post about incoming messages of the firmata protocol now it is time to present you with the outgoing messages of the protocol. As you can see in the following picture the number of messages is quite big.

The new 2.2 protocol has a lot of new functionality added. For example we have the new Pin State Query, Analog Mapping Query, Capability Query and Extended Analog messages. These make it very easy to create programs on the host (PC,Phone) side that can talk to arbitrary devices and have a complete overview of the device capabilities and current state.

Firmata Outgoing Messages

All the outgoing messages of Firmata Protocol 2.2

Now that I have finished with the implementation of the message handling I will be posting a few things about the Sharpduino library and its new architecture. Within this month I am expecting to have a new version published on the google code page. It will be fully functional with an ArduinoUno by using the higher level library. You can also use the lower level library to communicate with every Firmata enabled device but you will have to do some more coding.

Firmata Messages : Incoming

As you already know I am rewriting the sharpduino library. The new implementation will handle all known firmata features to date (version 2.2 of the protocol). Trying to decipher the available messages you can get lost, as the text representation offered by the firmata.org page is not really indicative of the message structure. There is also the problem of the fragmentation of the information on the wiki pages.

This is why I made the following image. It is a round-up of all the messages, that can be received on the PC host side as of version 2.2

Firmata Messages

We can separate the messages into 2 categories. Those that are 3 bytes -7-bits, but let’s just say it’s bytes- long and those that are longer and are called Sysex Messages.

The sysex messages always start with 0xF0, they have a second byte that is a sysex command and a final byte 0xF7 which tells us that the message is finished. The second byte tells us what the rest of the message will be. Right now there are 6 kinds of sysex messages, but I expect that they will multiply as more functionality is embedded to the Firmata protocol.

Beware that the messages received are almost always a result of a message sent earlier. This image does not show the proper order of the message exchange, but only analyzes the structure of the incoming messages.

This should be helpful to people implementing their own solution for communication using the Firmata protocol, or people wanting to know the ins and outs of the underlying protocol which they are using, if they use a library such as sharpduino.

Sharpduino Library: Reboot

A little background (Yes you can skip this)

A few years ago I bought an arduino to ease myself into electronic project prototyping. I tried a couple of times with pic microcontrollers, but the promise of using USB to program and debug an electronics board was too good to pass. The arduino, as everyone knows by now, is very easy to use and I had no problem creating from the simplest programs, to more complicated ones.

My goal was to create a program that would communicate with the pc and offer functionality equivalent to USB data acquisition modules from NI that we were using in our laboratory. Although the capabilities of the Arduino weren’t an exact match, the price (about 1/10th) was right. One of the requirements that I had, was that I could use this from the Labview environment. Back then there was no library for arduino for labview so I had to make one. There were actually two ways to accomplish such a task. A full native Labview library or a .net library that could be imported to Labview and used.

Given my background on .net technologies I went for the second solution. This was the birth of the Sharpduino Library. It was based on the Firmata protocol and implemented some basic functionality (Analog Input/Output, Digital Input, Digital Output). It had the functionality that I wanted, so I put it on google code and never really updated it since. I had even forgot that I had written it…

Fast forward to December 2011. I received two emails from people who had seen my library and were interested in the Firmata protocol. One was Noriaki Mitsunaga who not only used the library, but also had made a more complete implementation of the Firmata protocol. You can find his useful applications using this library here. The other was my friend Anton Smirnov who was making an equivalent library in pure Java (find his library here). It rekindled my interest in the library and I decided to do some coding.

The first step was to integrate Noriaki’s work (with his permission) and make it available. This begun with the switch of the repository from svn to mercurial. I have become a huge fan of DVCS and have found that it suits my any style of development if you spend a little time to make the switch from server-based systems. You can find the current code from the source tab of the sharpduino page.

Then I started refactoring a lot of the code, so it would make more sense. At some point I decided that the library needed a more thorough refresh,hence the reboot of the project.

Reboot

For this new implementation I have decided to make a library that will be useful to two kinds of consumers. There will be a higher level API that will be useful to people in need of the previously available functionality. For example, using the ArduinoUno will be as easy as:

Of course the actual names might change, but the general direction will be this.

What is more interesting (for me) is the lower level API. It will be mostly Message Handlers, Message Senders, Messages and a few base classes to coordinate all these. The previously mentioned implementation will be based on this and will serve as an example on how to put things together to make other Firmata Implementations.

Keep an eye on this blog and/or the sharpduino page for more updates as the new implementation matures and I will have a few things to share.

PS. I would also like to mention that my goal is to create a nuget-distributable library.

Links