Hardware Reference
In-Depth Information
Listing 12-5. Revised main sketch code
/*
*
* Hello Library Example
*
*/
#include "HelloLibrary.h"
void setup()
{
}
void loop()
{
printHelloLibrary();
startBlink(1000);
}
Listing 12-5 outlines the pattern that all libraries follow. Include the library at the top of the file, and the compiler
will process the library, and then you can access the functions according to C/C++ principles. In libraries, there is a
common pattern for adding enumerations ( enum ) and structures ( struct ) to your code. You can use these as types in
your code, but only if you write a function that has them as a return type or parameter. Because of the preprocessing,
you cannot put them in your main sketch, but you will need to add them to a header file. For example, you may want
to keep track of the part of the day—morning, afternoon, evening, or night. It is possible to do this in one of two ways.
#define :
Using
#define MORNING 0
#define AFTERNOON 1
#define EVENING 2
#define NIGHT 3
Using an enumeration:
enum {MORNING, AFTERNOON, EVENING, NIGHT};
There is an automatic assigning of values starting from 0 and growing by one, until the final one is reached. This
can be overridden, and each can be initialized to a specific value.
enum {
MORNING = 1,
AFTERNOON = 3,
EVENING = 5,
NIGHT = 7
};
For this reason enum sequences are not typically iterated. You should use a different data type for values that you
want to iterate.
The other common C feature is structures. Structures are referred to as struct s and similarly must be
implemented in a header file in order to be used as parameters or return types for functions.
 
Search WWH ::




Custom Search