Library Tutorial for Arduino Author: Alexander Brevig Contact: email@example.com
So you want to write a Library for the Arduino?
Here is a little tutorial on how to write one.
You will learn the basic syntax rules for c++ class declaration and definition and I will summarize the conventional Arduino function and variable naming.
For this tutorial we will implement a library called LED13 we will do some fun stuff with the LED @ pin 13
We will make it turn on, off and make it blink.
We will make this:
A Library is a collection of instructions that are designed to execute a specific task.
Good libraries describe the tasks it tries to solve by the name of the library itself, and the names of the functions associated to the library.
This is because the library should be intuitive to use, and all names should describe, in an efficient way, all questions regarding what, when, and where, but should conceal the how.
On the Arduino the libraries often serve as a wrapper around the Arduino API language to enhance code readability and user experience.
Arduino IDE places it's libraries in \hardware\libraries The Library for this tutorial has the URL C:\programming\arduino-0015\hardware\libraries\LED13 on my Windows XP machine.
For the Ubuntu operating system, the libraries may be found at /usr/share/arduino/libraries.
The Arduino libraries uses a UppercaseUppercase convention for Library names, and lowercaseUppercase naming convention for the functions.
What this means is that our library has/should get the name LED13 (not led13 for instance), and the functions will be named on() and off().
I could've selected turnOn() and turnOff(), but I've selected the shortest because it is as descriptive, but in addition it is shorter. [short code == less probability for typos]
It is important to remember that you do not need a Library. It only makes it simpler.
If you are interested in the inner mechanics of a specific library, you'll find the MyLibrary.cpp (if downloaded) in /hardware/libraries/MyLibrary/MyLibrary.cpp change MyLibrary to whatever the name of the library you want to investigate
For our Library we've decided on these functions:
This should be the premise for our library.
At this point, we do not care how the on() should work, only that it should exist.
Before implementing a new class I usually find it very useful to test the idea in a sketch. Try a few different implementations, see what uses the least amount of space, and what seems simplest in use. Select the solution that compromises these two in a suitable way.
An experience I've made is that if one has something slightly similar to work from, that is often beneficial.
For our Library, one immediatly thinks of the Blink right? The Hello World! of µCs. So, go ahead and start the Arduino IDE and open the File->Sketchbook->Examples->Digital->Blink.
The first thing we'll do, is to cut the code inside loop(), and paste it into a new function called void blink().
I suspect this is not surprising, being that we know this code will blink the LED connected to pin 13.
Now, write blink(); inside the loop, so that the code that once was inside the loop() and now is located inside blink, gets executed (because loop calls blink).
Now, you should have a sketch looking like this:
The next two steps will be to add the void on(); and the void off();.
These will be really simple functions, becuase they will actually just 'wrap' digitalWrite(13,HIGH) and digitalWrite(13,LOW) respectively.
Go ahead and try to write these functions.
You should end up with something like:
We have one more thing to fix.
As the code is now, the blink() will always use two seconds, but we wanted to pass the number of microseconds as an argument. Additionally we'll want to remove the redundant code that turns a led on and off. We'll replace those redundant calls with on() and off(). You probably know how to write functions that has parameters, but here it is:
Now you have enough code to implement all the desired functionality.
Download the source for this tutorial
The next step is to:
There are a lot of resources on this topic, but generally the class are split into two files.
You will see that this source will be very much like the concept sketch.
However, I've changed some datatypes, and the rest is just the c++ syntax.
Notice the :: , it is called the 'scope resolution operator'
A common 'rule of thumb' is to make all data as private as possible. For Arduino that will often result in all variables declared private, and most functions defined public.
Being the Arduino has limited resources I think selecting the 'smallest' variable is the way to go. An Arduino pin will (probably) never become above 255. So in that case using a byte is sufficient, and preferable.
The arduino API has established some cues for how to name a variable or function. Start lower case, and indicate new words with an upper case letter. 'myDescriptiveVariable'
Be sure to create examples that are as self describing as possible. Additionally it should be made an effort to demo most of the functions.
Remember to implement this file, as it makes using your library a lot easier.
Our library has four names that needs highlighting.
And they are implemented in keywords.txt as:
LED13 KEYWORD1 on KEYWORD2 off KEYWORD2 blink KEYWORD2
You will get a series of errors if you've forgot to include the WProgram, and trying to use some of the Arduino functions or datatypes.
It is only necessary to include the WProgram.h in you library .h, your .cpp will include it because it includes your header.
When changes are made in the header or the source, those changes are not automatically incorporated in the object file of you library. To be sure that your changes are compiled and used, delete the LED13.o and recompile a sketch in the Arduino IDE.
NOTE: After making library changes, before recompiling, you may need to delete the <LibName>.o file which can be found by searching, or in a file similar to: c:\Documents and Settings\Administrator\Local Settings\Temp\build3998508668703718780.tmp
It can be a timesaver to take an hour and causing error deliberately, so you know what the error messages are caused by for later reference.
One of the fastest ways to flush out bugs in a library is to let other people use the library. Often people not only find bugs but also contribute patches to fix those bugs.
|Part of AlphaBeta Tutorials and Resources.|
|Last Modified:||August 23, 2014, at 04:48 PM|