📜 ⬆️ ⬇️

How to make a library for Arduino? (example attached)

Suppose you decide to arrange the functions / classes written by you for the Arduino into the library, so that you yourself can work with them easier, and the main thing is that someone else can easily solve these problems using your library, remembering you with a kind word, and not invent new bike.
I will describe a minimum of features, based on the reader, at least a little familiar with C / C ++ programming.
A library must have at least one .h file and, as a rule, one .cpp file. In the .h file, as usual, all types / functions / classes / constants are described, and in .cpp all of this is implemented.
The library will compile without additional transformations (more here ), so if you need any functions / objects / constants from the Arduino standard library, you need to include the appropriate header files (like "WConstants.h", "WProgram.h").
If I understand correctly, the rule of “good tone” is to design the code as a class, and if, according to the meaning of the tasks performed, an instance of this class can be only one, then at the same time the declaration of this instance in the .h file:
 extern MyClass MyClassObject;
and creating one global object in a .cpp file:
 MyClass MyClassObject;
For example, if we make a class for working with a hardware serial port, since such a port is physically only one, then it is desirable to create an instance of the class in the library itself. Actually, in the standard library the Serial object is declared.
The written library needs to be put in a folder with some suitable name in the “hardware \ libraries” directory, and restart the Arduino environment. At startup, the environment will try to compile your library, and if errors occur, they will be told about them.
It is not bad to create in the folder with the library a file “keywords.txt”, which describes the names of the types, methods, functions, constants used in the library, so that the editor “colored” with their respective colors. The syntax of the file is simple - here is an example (separator - TAB):
 --- keywords.txt ---
 MyType KEYWORD1
 MyFunction KEYWORD2
 MY_CONSTANT LITERAL1
 ---
In addition, you can create a folder “examples”, where to put one or more folders with projects examples of working with the library (already in the .pde format).
As an example I will give a simple little library that organizes the input of numbers from a serial port.
When experimenting with Arduino, it may be convenient to write a program in a dialogue form, and work with it through terminalku (the same HyperTerminal). Well, the task of entering numbers is constantly arising - this is what the library does. Work with the library like this:
 #include <SerialInput.h>

 void setup () {
   Serial.begin (9600);
 }

 void loop ()
 {
   long int Number;

   Serial.print ("Enter number:");
   Number = SerialInput.InputNumber ();  // Input is here

   Serial.print ("You entered:");
   Serial.println (Number, DEC);
 }
A more complete example comes with the library, which you can download here: SerialInput.zip Archive unzip to the folder "hardware \ libraries".

')

Source: https://habr.com/ru/post/55401/


All Articles