One of the most popular library used largely in Arduino is wire.h library its actually easy to use. The wire library is created on Object-Oriented C ++ class library, most Arduino user they are not familiar with the implementation details library.
Arduinos Library is usually stored in the \arduino-1.6.X\ libraries directory. Each library usually has an example inside of the folder, there are some example codes that users can quickly understand how the wire.h code library works.
Below are the simple arithmetic library, have the following functions.
1 2 |
int sum (int a, int b); int minus (int a, int b); |
The first return two number and the second function returns the difference between the two numbers. To test this demonstration we need to create a library named calculator, create a folder inside of the Arduino libraries named calculator then create a header file “calculator.h”.
1 2 3 4 5 6 7 8 9 |
#ifndef __CALCULATOR_H__ #define __CALCULATOR_H__ class calculator { public: int sum (int a, int b); // return sum int minus (int a, int b); // return difference }; #endif |
Setup the source files calculator.cpp
1 2 3 4 5 6 7 8 9 10 11 |
#include "calculator.h" int calculator :: sum (int a, int b) { return (a + b); } int minus (int a, int b) { return (ab); } |
Facilitating and understanding of the use of the library we need to build our example file, which has a demo named calculator_test,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Demo of calculator library #include <calculator.h> void setup () { Serial.begin (9600); Serial.print ("10 + 15 ="); Serial.println (calculator.sum (10, 15)); Serial.print ("100-19 ="); Serial.println (calculator.minus (100, 19)); Serial.println ("Calculator Library test is done!"); } void loop () { } |
Using the Arduino IDE, you will see some functions library and will be displayed as red, while others is are not, apparently the function name and the header file name and some macros to make the syntax shown in red color, actually it is handy to check for errors.
In order for us to implement this library to the IDE, you need to add a keyboards.txt calculator file folder inside the contents keyboards.txt follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
####################################### # Syntax Coloring Map For calculator ####################################### ####################################### # Datatypes (KEYWORD1) ####################################### calculator KEYWORD1 calculator KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) ####################################### sum KEYWORD2 minus KEYWORD2 ####################################### # Constants (LITERAL1) ####################################### |
As you can see above example, the class name or the file name written in the front KEYWORD1, the function name is written before KEYWORD2. As LITERAL1, to highlight the name of the macro definition, which is not related to the library, so empty. It should be noted that there is one and only one tab between the name and KEYWORD1, you cannot used space.