How to create Arduino Library from Arduino Sketch

how to create arduino library

On previous article, I wrote an Arduino sketch (i.e. program) for interfacing LCD 5110 display module with Arduino. Technically speaking the Arduino sketch that I developed is not an Arduino library yet. In this article, I will discuss on how to create an Arduino Library, or more precisely, how to convert the Arduino sketch into an Arduino library.

The sketch that we are going to use to convert to Arduino library is available here for download.

An Arduino sketch is a program often consists of a few user-defined functions together with two special functions that are core part of every Arduino sketch: setup() and loop(). The setup() is called once when the sketch started, it is used to initialising the status of the program. The loop() function is where the main program logic and flow that utilises all the functions.

An Arduino library take an object-oriental programming approach to separate the reusable functions from its application program, it provides a set of APIs (or interfaces) for programmers to use the library without knowing the detail implementation of the library.

Step 1 – Create a directory for your Arduino library

Step 1 of creating an Arduino library is to create a directory named after the functionality of the library within the Libraries directory of your Arduino Sketchbook directory. Since our library is related to LCD 5110 so we will name our library as LCD5110.

The Sketchbook directory and Library directory is where the Arduino IDE stores the sketches. The Library directory is automatically created by the IDE when Arduino IDE is installed. On Linux machines, the directory is called Sketchbook and it is typically located in /home/username/. On Windows and Macintosh machines, the default name of the directory is Arduino and is located in your Documents directory.

arduino-library-directory-structure
Arduino library directory structure

Step 2 – Add Arduino library header and source files

Step 2 is to create an Arduino library header file and source file. An Arduino library needs at least two files for a library: a header file with file extension of .h and the source file with extension of .cpp.

Go ahead to create two empty files named LCD5110.h and LCD5110.cpp under our newly created directory LCD5110 now. You will need a programming text editor to do so as Arduino IDE editor does not works for .h and .cpp file extension. Once you created the file. If you launch your Arduino IDE and navigate to Sketch –> Include Library, you should see our library LCD5110 show up under the Contributed libraries section.

access-arduino-library-via-sketch-include-library menu
Access Arduino library via Sketch -> Include Library menu

Although the Arduino IDE recognised our newly created Arduino library now, but the LCD5110.h and LCD5110.cpp currently contains no functional code yet, so let’s add codes into those files.

Setp 2.a – Arduino library header file (.h)

The header file provides the definitions of the library with a few preprocessor directives and a class prototype or class interface.

The #ifndef LCD5110_h ... #endif preprocessor directives basically avoid the file to be loaded multiple times.

The #include "Arduino.h" provides the access to the standard types and constants of the Arduino language.

Wrapping within those directives is the class prototype which is a declaration of the class and all methods’ (functions) within the class with the method’s name and type signature, but without the function body. Methods and properties (variables) can be classified as public which provide a set of APIs for accessing the class methods, or private for those can only be access within the class.

In addition to all the methods, there is a class construct, the class construct will be the first code to run when a class instance is instantiated. The .h header file use C++ language notation, so the construct has the same name as the class, but no return type.

All the methods within our Arduino sketch are public accessible except the write() which only called within the class. Most of the constants and variable(s) need not be accessed outside of the library.

It is a good practice to add a few lines of comments on top of the .h header file to describe our library.

/*
  LCD5110.h - A LCD5110 (PCD8544) display library
  Created by Henry Cheung, December 1, 2017.
  License: MIT
*/
#ifndef LCD5110_h
#define LCD5110_h

#include "Arduino.h"

class LCD5110
{
  public:
    LCD5110(void);
    void clear(void);
    void cursor(unsigned char row, unsigned char col);
    void backlight(boolean state);
    void inverse(boolean inv);
    void printStr(unsigned char str[]);
    void printImage(unsigned char image[]);
  private:
    void write(unsigned int mode, unsigned char data);
    boolean _inverse;

    const int CLK = 13; // Must connect to Arduino D13 (SPI SCLK) when using SPI library
    const int DN = 11;  // Must connect to Arduino D11 (SPI MOSI) when using SPI library
    const int DC = 10;
    const int RST = 9;
    const int SCE = 8;
    const int LED = 7;

    const int LCD_WIDTH = 84;
    const int LCD_HEIGHT = 48;
    const int CMD = LOW;
    const int DATA = HIGH;
    const int ON = HIGH;
    const int OFF = LOW;
};

#endif

Step 2.b – Arduino library source file (.cpp)

The Arduino library source file starts with two basic preprocessor directives #include "Arduino.h" and the library header file (i.e. #include "LCD5110.h". In our case, we also need to add #include "SPI.h" because our code uses the SPI library. Please noted the subtle differences between C++ notation which use double quotes “SPI.h” and Arduino notation which use pointy brackets <SPI.h>.

The class construct replaced most (if not all) of the codes in Arduino sketch setup() function.

All the code in our sketch does not need further modification except that the construct and all methods are prefixed with class name for name spacing purpose.

#include "Arduino.h"
#include "LCD5110.h"
#include "SPI.h"

LCD5110::LCD5110(void)
{
  pinMode(CLK, OUTPUT);
  pinMode(DN, OUTPUT);
  pinMode(DC, OUTPUT);
  pinMode(SCE, OUTPUT);
  pinMode(RST, OUTPUT);
  pinMode(LED, OUTPUT);

  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);

  digitalWrite(RST, HIGH);
  digitalWrite(SCE, HIGH);
  write(CMD, 0x21);  // Set Extended Command set
  write(CMD, 0xb2);  // Set Vlcd to 6v (LCD Contrast)
  write(CMD, 0x13);  // Set voltage bias system 1:48 (Viewing Angle)
  write(CMD, 0x20);  // Set Normal Command set
  clear(); // Clear all display memory and set cursor to 1,1
  write(CMD, 0x09);  // Set all pixels ON
  write(CMD, 0x0c);  // Set display mode to Normal

  backlight(OFF);
}

void LCD5110::clear(void) {
  int pixel;
  cursor(0,0);
  for (pixel=(LCD_WIDTH * LCD_HEIGHT / 8); pixel > 0; pixel--) {
    write(DATA, 0x00);
  }
}

void LCD5110::cursor(unsigned char row, unsigned char col) {
  if ( (row < 1 | row > LCD_HEIGHT / 8) | (col < 1 | col > LCD_WIDTH / 6)) {
    return;
  }
  write(CMD, 0x40 | ( row - 1) );
  write(CMD, 0x80 | ( col - 1)*6 );
}

void LCD5110::write(unsigned int mode, unsigned char data) {
  digitalWrite(SCE, LOW);
  digitalWrite(DC, mode);  //HIGH = Data mode, LOW = Command mode
  if (mode == HIGH & _inverse == true) {
      data = ~ data;
  }
  SPI.transfer(data);
  digitalWrite(SCE, HIGH);
}

void LCD5110::backlight(boolean state) {
  digitalWrite(LED, state);
}

void LCD5110::inverse(boolean inv) {
  _inverse = inv;
}

void LCD5110::printImage(unsigned char image[]) {
  unsigned int i;
  cursor(1,1);
  for (i = 0; i < (LCD_WIDTH * LCD_HEIGHT / 8); i++) {
    write(DATA, image[i]);
  }
}

void LCD5110::printStr(unsigned char str[]) {
  unsigned int p, i;
  unsigned char b;
  const unsigned char FONT_TABLE [][5] = {
    { 0x00, 0x00, 0x00, 0x00, 0x00 }, // 0x20, space
    { 0x00, 0x00, 0x5f, 0x00, 0x00 }, // 0x21, !
    { 0x00, 0x07, 0x00, 0x07, 0x00 }, // 0x22, "
    { 0x14, 0x7f, 0x14, 0x7f, 0x14 }, // 0x23, #
    { 0x24, 0x2a, 0x7f, 0x2a, 0x12 }, // 0x24, $
    { 0x23, 0x12, 0x08, 0x64, 0x62 }, // 0x25, %
    { 0x36, 0x49, 0x55, 0x22, 0x50 }, // 0x26, &
    { 0x00, 0x05, 0x03, 0x00, 0x00 }, // 0x27, '
    { 0x00, 0x1c, 0x22, 0x41, 0x00 }, // 0x28, (
    { 0x00, 0x41, 0x22, 0x1c, 0x00 }, // 0x29, )
    { 0x14, 0x08, 0x3E, 0x08, 0x14 }, // 0x2a, *
    { 0x08, 0x08, 0x3E, 0x08, 0x08 }, // 0x2b, +
    { 0x00, 0x50, 0x30, 0x00, 0x00 }, // 0x2c, ,
    { 0x08, 0x08, 0x08, 0x08, 0x08 }, // 0x2d, -
    { 0x00, 0x60, 0x60, 0x00, 0x00 }, // 0x2e, .
    { 0x20, 0x10, 0x08, 0x04, 0x02 }, // 0x2f, /
    { 0x3E, 0x51, 0x49, 0x45, 0x3E }, // 0x30, 0
    { 0x00, 0x42, 0x7F, 0x40, 0x00 }, // 0x31, 1
    { 0x42, 0x61, 0x51, 0x49, 0x46 }, // 0x32, 2
    { 0x21, 0x41, 0x45, 0x4B, 0x31 }, // 0x33, 3
    { 0x18, 0x14, 0x12, 0x7F, 0x10 }, // 0x34, 4
    { 0x27, 0x45, 0x45, 0x45, 0x39 }, // 0x35, 5
    { 0x3C, 0x4A, 0x49, 0x49, 0x30 }, // 0x36, 6
    { 0x01, 0x71, 0x09, 0x05, 0x03 }, // 0x37, 7
    { 0x36, 0x49, 0x49, 0x49, 0x36 }, // 0x38, 8
    { 0x06, 0x49, 0x49, 0x29, 0x1E }, // 0x39, 9
    { 0x00, 0x36, 0x36, 0x00, 0x00 }, // 0x3a, :
    { 0x00, 0x56, 0x36, 0x00, 0x00 }, // 0x3b, ;
    { 0x08, 0x14, 0x22, 0x41, 0x00 }, // 0x3c, <
    { 0x14, 0x14, 0x14, 0x14, 0x14 }, // 0x3d, =
    { 0x00, 0x41, 0x22, 0x14, 0x08 }, // 0x3e, >
    { 0x02, 0x01, 0x51, 0x09, 0x06 }, // 0x3f, ?
    { 0x32, 0x49, 0x59, 0x51, 0x3E }, // 0x40, @
    { 0x7E, 0x11, 0x11, 0x11, 0x7E }, // 0x41, A
    { 0x7F, 0x49, 0x49, 0x49, 0x36 }, // 0x42, B
    { 0x3E, 0x41, 0x41, 0x41, 0x22 }, // 0x43, C
    { 0x7F, 0x41, 0x41, 0x22, 0x1C }, // 0x44, D
    { 0x7F, 0x49, 0x49, 0x49, 0x41 }, // 0x45, E
    { 0x7F, 0x09, 0x09, 0x09, 0x01 }, // 0x46, F
    { 0x3E, 0x41, 0x49, 0x49, 0x7A }, // 0x47, G
    { 0x7F, 0x08, 0x08, 0x08, 0x7F }, // 0x48, H
    { 0x00, 0x41, 0x7F, 0x41, 0x00 }, // 0x49, I
    { 0x20, 0x40, 0x41, 0x3F, 0x01 }, // 0x4a, J
    { 0x7F, 0x08, 0x14, 0x22, 0x41 }, // 0x4b, K
    { 0x7F, 0x40, 0x40, 0x40, 0x40 }, // 0x4c, L
    { 0x7F, 0x02, 0x0C, 0x02, 0x7F }, // 0x4d, M
    { 0x7F, 0x04, 0x08, 0x10, 0x7F }, // 0x4e, N
    { 0x3E, 0x41, 0x41, 0x41, 0x3E }, // 0x4f, O
    { 0x7F, 0x09, 0x09, 0x09, 0x06 }, // 0x50, P
    { 0x3E, 0x41, 0x51, 0x21, 0x5E }, // 0x51, Q
    { 0x7F, 0x09, 0x19, 0x29, 0x46 }, // 0x52, R
    { 0x46, 0x49, 0x49, 0x49, 0x31 }, // 0x53, S
    { 0x01, 0x01, 0x7F, 0x01, 0x01 }, // 0x54, T
    { 0x3F, 0x40, 0x40, 0x40, 0x3F }, // 0x55, U
    { 0x1F, 0x20, 0x40, 0x20, 0x1F }, // 0x56, V
    { 0x3F, 0x40, 0x38, 0x40, 0x3F }, // 0x57, W
    { 0x63, 0x14, 0x08, 0x14, 0x63 }, // 0x58, X
    { 0x07, 0x08, 0x70, 0x08, 0x07 }, // 0x59, Y
    { 0x61, 0x51, 0x49, 0x45, 0x43 }, // 0x5a, Z
    { 0x00, 0x7F, 0x41, 0x41, 0x00 }, // 0x5b, [
    { 0x55, 0x2A, 0x55, 0x2A, 0x55 }, // 0x5c, back slash
    { 0x00, 0x41, 0x41, 0x7F, 0x00 }, // 0x5d, ]
    { 0x04, 0x02, 0x01, 0x02, 0x04 }, // 0x5e, ^
    { 0x40, 0x40, 0x40, 0x40, 0x40 }, // 0x5f, _
    { 0x00, 0x01, 0x02, 0x04, 0x00 }, // 0x60, `
    { 0x20, 0x54, 0x54, 0x54, 0x78 }, // 0x61, a
    { 0x7F, 0x48, 0x44, 0x44, 0x38 }, // 0x62, b
    { 0x38, 0x44, 0x44, 0x44, 0x20 }, // 0x63, c
    { 0x38, 0x44, 0x44, 0x48, 0x7F }, // 0x64, d
    { 0x38, 0x54, 0x54, 0x54, 0x18 }, // 0x65, e
    { 0x08, 0x7E, 0x09, 0x01, 0x02 }, // 0x66, f
    { 0x0C, 0x52, 0x52, 0x52, 0x3E }, // 0x67, g
    { 0x7F, 0x08, 0x04, 0x04, 0x78 }, // 0x68, h
    { 0x00, 0x44, 0x7D, 0x40, 0x00 }, // 0x69, i
    { 0x20, 0x40, 0x44, 0x3D, 0x00 }, // 0x6a, j
    { 0x7F, 0x10, 0x28, 0x44, 0x00 }, // 0x6b, k
    { 0x00, 0x41, 0x7F, 0x40, 0x00 }, // 0x6c, l
    { 0x7C, 0x04, 0x18, 0x04, 0x78 }, // 0x6d, m
    { 0x7C, 0x08, 0x04, 0x04, 0x78 }, // 0x6e, n
    { 0x38, 0x44, 0x44, 0x44, 0x38 }, // 0x6f, o
    { 0x7C, 0x14, 0x14, 0x14, 0x08 }, // 0x70, p
    { 0x08, 0x14, 0x14, 0x18, 0x7C }, // 0x71, q
    { 0x7C, 0x08, 0x04, 0x04, 0x08 }, // 0x72, r
    { 0x48, 0x54, 0x54, 0x54, 0x20 }, // 0x73, s
    { 0x04, 0x3F, 0x44, 0x40, 0x20 }, // 0x74, t
    { 0x3C, 0x40, 0x40, 0x20, 0x7C }, // 0x75, u
    { 0x1C, 0x20, 0x40, 0x20, 0x1C }, // 0x76, v
    { 0x3C, 0x40, 0x30, 0x40, 0x3C }, // 0x77, w
    { 0x44, 0x28, 0x10, 0x28, 0x44 }, // 0x78, x
    { 0x0C, 0x50, 0x50, 0x50, 0x3C }, // 0x79, y
    { 0x44, 0x64, 0x54, 0x4C, 0x44 }, // 0x7a, z
    { 0x00, 0x08, 0x36, 0x41, 0x00 }, // 0x7b, {
    { 0x00, 0x00, 0x7f, 0x00, 0x00 }, // 0x7c, |
    { 0x00, 0x41, 0x36, 0x08, 0x00 }, // 0x7d, }
    { 0x10, 0x08, 0x08, 0x10, 0x08 }, // 0x7e, ~
    { 0x78, 0x46, 0x41, 0x46, 0x78 }  // 0x7f, DEL
  };
  p = 0;
  while (str[p]!='\0') {
    if ( (str[p] >= 0x20) & (str[p] <= 0x7f) ) {
      for (i = 0; i < 5; i++) {
        b = FONT_TABLE[str[p] - 32][i];
        write(DATA, b);
      }
      write(DATA, 0x00);
    }
    p++;
  }
}

We are now have a minimum but fully functional Arduino library. Launch Arduino IDE and open a new sketch, navigate to Sketch --> Include Library, we should see our library LCD5110 under Contributed Libraries.

Selecting our newly created library from the Sketch --> Include Library --> LCD5110 would result in adding a line #include <LCD5110.h> into our sketch. This is how we can use our library.

Click on the Verify button (the first button with "tick" sign on the left top of the Arduino IDE) to compile the code, there should be no error message generated from the verify process.

Step 3 - Create example on using your Arduino library

Step 3 is to create an example sketch to show how to use the Arduino library.

Arduino library requires you to create an Examples directory within the Arduino library directory, and for each example sketch requires its own directory too. In our case, I would name the example sketch as LCD5110_demo.pde (please noted that it is not .ino file extension as normal sketch) and put it under LCD5110/Examples/LCD5110_demo/ directory.

The example is not really your API documentation, but it provide a quick reference on how to utilise the library. It is therefore a good practice to add a few line of comments at the beginning the example sketch to explain how to use the library.

The example program starts with preprocessor to include the library, follow by creating an instance of the library class. There is nothing for the setup() function because it all set at the library. The loop() function of our example demo program is very much copy from the loop() of our original Arduino sketch.

/*

 This example sketch demonstrates on how to use the LCD5110 Arduino library

 Hardware interface: The following pins for interfacing with the LCD module
  LCD     Arduino I/O pin
  CLK     D13 (SCLK)
  DN      D11 (MOSI)
  DC      D10
  RST     D9
  SCE     D8
  LED     D7
  GND     GND
  VCC     3v3
  * D7 connects to LED pin via a 220-ohm resistor to restrict the LED current.
  
 API for LCD5110 library

 LCD5110 lcd - create an instance called lcd of class LCD5110. The class construct will:
     1. initialise GPIO and SPI ports;
     2. clear the screen and internal display memory
     3. set cursor to col 1 and rol 1
     4. set LCD backlight = Off
     5. set LCD display mode = Normal
  void LCD5110.clear(void) - clear LCD screen and set cursor to row 1 and col 1
  void LCD5110.cursor(unsigned char row, unsigned char col) - set LCD cursor to row, col
  void LCD5110.backlight(boolean true|false) - set LCD backlight on (true) or off (false)
  void LCD5110.inverse(boolean true/false) - Set display mode to Inverse (ture) or Normal (false)
  void LCD5110.printStr(unsigned char str[]) - print str[] array
  void LCD5110.printImage(unsigned char image[]) - print image[], image is an array[504] of pixels data

*/


#include "LCD5110.h"

LCD5110 lcd;

void setup() {

}

void loop()
  {
  unsigned char eTinkersLogo[504] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xf0, 0xf0, 0xf8, 0xfc, 0xfc, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0xfc, 0xfc, 0xf8, 0xf0, 0xf0, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0xf8, 0x10, 0x10, 0xa0, 0x20, 0x40, 0x40, 0x80, 0xfc, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x82, 0x82, 0x44, 0x44, 0x28, 0x28, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x3f, 0x10, 0x11, 0x09, 0x09, 0x04, 0x04, 0x02, 0x7e, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xf0, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0,
    0x00, 0x01, 0x07, 0x0f, 0x0f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x0f, 0x0f, 0x07, 0x01, 0x00
  };
  lcd.cursor(2, 1);
  lcd.printStr(" Hello World! ");
  lcd.cursor(4, 2);
  lcd.printStr("e-tinkers.com");
  lcd.inverse(true);
  lcd.cursor(6,1);
  lcd.printStr("** Nov 2017 **");
  lcd.inverse(false);
  delay(5000);

  lcd.backlight(true);
  lcd.printImage(eTinkersLogo);
  delay(5000);
  lcd.backlight(false);
  lcd.clear();
}

Step 4 - Create Keywords.txt (optional)

Step 4 is optional, it provides Arduino IDE syntax highlighting for the newly created Arduino library using Keywords.txt file.

If you try to write a sketch using our library's class and methods, none of our library's class and methods are highlighted in color as other Arduino functions and other libraries shown, but we can gives it a little help by creating a list of keywords.

Each line has the name of the keyword, followed by a tab (not spaces), followed by the kind of keyword. Class and datatypes (if any) should be classify as "KEYWORD1", and all methods and functions should be categories as "KEYWORD2", Constants and variables should be categories as "LITERAL1". Store the Keywords.txt in the Library/LCD5110/ directory. Relaunch Arduino IDE to see the effect.

# Syntax Highlighting For LCD5110 library

# Class and Datatypes (KEYWORD1)
LCD5110		KEYWORD1

# Methods and Functions (KEYWORD2)
lcdClear	KEYWORD2
lcdCursor	KEYWORD2
lcdWrite	KEYWORD2
lcdBacklight	KEYWORD2
lcdInverse	KEYWORD2
printStr	KEYWORD2
printImage	KEYWORD2

# Constants (LITERAL1)

We have successfully converted our Arduino sketch to an Arduino library. The complete LCD5110 library can be download from my github repository.

Leave a Reply

Your email address will not be published. Required fields are marked *