serLCD - Sparkfun serLCD Library

This is my attempt at porting LiquidCrystal library for use with serial displays. It implements most all functions from LiquidCrystal and added a few functions of my own. This library subclasses NewSoftSerial.

Since version 1.5 the library no longer subclasses NewSoftSerial as it is Arduino 1.0 compatible. Please download v1.5 from:

Download Here: https://github.com/nemith/serLCD

Functions

Examples

CODE

serLCD.h

  1. /*
  2.  serLCD.h - Library for controlling a SparkFun serLCD module
  3.             Code written for firmware version 2.5
  4.  
  5.  Created by Cody B. Null, September 9, 2011
  6.  Modified by Jordan Hochenbaum, Dec 5, 2011. (Version 1.5)
  7.  Modified by Johan Korten, March 23, 2013. (Version 1.6)
  8.  
  9.  
  10.  Version 1.4 - This version wrote specifically for 16x2
  11.                Display.
  12.                More display support coming in later version.
  13.  
  14.  Version 1.5 - Since mid 2011 the Arduino IDE has implemented the NewSoftSerial
  15.                library updates in the standard SoftwareSerial library and so
  16.                using NewSoftSerial is no longer needed. serLCD has been updated
  17.                to use the Standard SoftwareSerial library. Also, Arduino 1.0
  18.                support has been added. Backwards compatible with previous versions.
  19.  
  20.  Version 1.6 - Added support for the setType special command.
  21.                                 This will help using different LCD's (2x16, 2x20, 4x16, 4x20).  
  22.                       Added support for scrollLeft and scrollRight commands.
  23.  
  24.  Note - This library requires NewSoftSerial library
  25.         The latest version of NewSoftSerial library can
  26.         always be found at http://arduiniana.org. -> NO LONGER NECESSARY. See 1.5 notes above.-Jordan Hochenbaum
  27. */
  28. #ifndef serLCD_h
  29. #define serLCD_h
  30.  
  31. #if ARDUINO >= 100
  32. #include "Arduino.h"       // for delayMicroseconds,digitalPinToBitMask, etc
  33. #else
  34. #include "WProgram.h"      // for delayMicroseconds
  35. #include "pins_arduino.h"  // for digitalPinToBitMask, etc
  36. #endif
  37. #include "SoftwareSerial.h"
  38.  
  39. // Commands
  40. #define LCD_BACKLIGHT           0x80
  41. #define LCD_CLEARDISPLAY        0x01
  42. #define LCD_CURSORSHIFT         0x10
  43. #define LCD_DISPLAYCONTROL      0x08
  44. #define LCD_ENTRYMODESET        0x04
  45. #define LCD_FUNCTIONSET         0x20
  46. #define LCD_SETCGRAMADDR        0x40
  47. #define LCD_SETDDRAMADDR        0x80
  48. #define LCD_SETSPLASHSCREEN     0x0A
  49. #define LCD_SPLASHTOGGLE        0x09
  50. #define LCD_RETURNHOME          0x02
  51.  
  52. // Flags for display entry mode
  53. #define LCD_ENTRYRIGHT          0x00
  54. #define LCD_ENTRYLEFT           0x02
  55.  
  56. // Flags for display on/off control
  57. #define LCD_BLINKON             0x01
  58. #define LCD_CURSORON            0x02
  59. #define LCD_DISPLAYON           0x04
  60.  
  61. // Flags for display size
  62. #define LCD_2LINE               0x02
  63. #define LCD_4LINE               0x04
  64. #define LCD_16CHAR              0x10
  65. #define LCD_20CHAR              0x14
  66.  
  67. //  Flags for setting display size
  68. #define LCD_SET2LINE            0x06
  69. #define LCD_SET4LINE            0x05
  70. #define LCD_SET16CHAR           0x04
  71. #define LCD_SET20CHAR           0x03
  72.  
  73. class serLCD : public SoftwareSerial {
  74. public:
  75.         serLCD (int pin);
  76.  
  77.         void clear();
  78.         void clearLine(int);
  79.         void home();
  80.         void setBrightness(int);
  81.  
  82.         void setSplash();
  83.         void setType(int);  // new in 1.6
  84.         void scrollLeft();  // new in 1.6
  85.         void scrollRight(); // new in 1.6
  86.         void toggleSplash();
  87.  
  88.         void blink();
  89.         void noBlink();
  90.         void cursor();
  91.         void noCursor();
  92.         void display();
  93.         void noDisplay();
  94.  
  95.         void setCursor(int, int);
  96.         void selectLine(int);
  97.  
  98.         void leftToRight();
  99.         void rightToLeft();
  100.         void autoscroll();
  101.         void noAutoscroll();
  102.  
  103.         void createChar(int, uint8_t[]);
  104.         void printCustomChar(int);
  105.  
  106. private:
  107.         void command(uint8_t);
  108.         void specialCommand(uint8_t);
  109.  
  110.         uint8_t _displayfunction;
  111.         uint8_t _displaycontrol;
  112.         uint8_t _displaymode;
  113.         uint8_t _numlines;
  114.         uint8_t _numchars;
  115.         uint8_t _rowoffset;
  116. };
  117.  
  118. #endif

serLCD.cpp

  1. /*
  2.  serLCD.cpp - Library for controlling a SparkFun serLCD
  3.               module.
  4.               Code written for firmware version 2.5
  5.  
  6.  Created by Cody B. Null, September 9, 2011
  7.  Modified by Jordan Hochenbaum, Dec 5, 2011. (Version 1.5)
  8.  Modified by Johan Korten, March 23, 2013. (Version 1.6)
  9.  
  10.  
  11.  Version 1.4 - This version wrote specifically for 16x2
  12.                Display.
  13.                More display support coming in later version.
  14.  
  15.  Version 1.5 - Since mid 2011 the Arduino IDE has implemented the NewSoftSerial
  16.                 library updates in the standard SoftwareSerial library and so
  17.                 using NewSoftSerial is no longer needed. serLCD has been updated
  18.                 to use the Standard SoftwareSerial library. Also, Arduino 1.0
  19.                 support has been added. Backwards compatible with previous versions.
  20.  
  21.  Version 1.6 - Added support for the setType special command.
  22.                                 This will help using different LCD's (2x16, 2x20, 4x16, 4x20).  
  23.                       Added support for scrollLeft and scrollRight commands.
  24.  
  25.  Note - This library requires NewSoftSerial library
  26.  The latest version of NewSoftSerial library can
  27.  always be found at http://arduiniana.org. -> NO LONGER NECESSARY. See V1.5 notes above
  28. */
  29.  
  30. //#include <../NewSoftSerial/NewSoftSerial.h>
  31.  
  32. #include <SoftwareSerial.h>
  33. #include "serLCD.h"
  34.  
  35. //      PUBLIC FUNCTIONS
  36.  
  37. // Contstructor
  38. // defaults to 16x2 display
  39. serLCD::serLCD(int pin) : SoftwareSerial(pin, pin){
  40.         pinMode(pin, OUTPUT);
  41.         begin(9600);
  42.         _numlines = LCD_2LINE;
  43.         _numchars = LCD_16CHAR;
  44.         _rowoffset = 0;
  45. }
  46.  
  47. /* Initialize.. not used trying to implement all display sizes
  48. void serLCD::init(int pin, int rows, int cols){
  49.         pinMode(pin, OUTPUT);
  50.         delay(4);
  51.         begin(9600);
  52.         if(cols == LCD_20CHAR){
  53.                 _numchars = LCD_20CHAR;
  54.                 specialCommand(LCD_SET20CHAR);
  55.         }else{ // default to 16 char display
  56.                 _numchars = LCD_16CHAR;
  57.                 specialCommand(LCD_SET16CHAR);
  58.         }      
  59.         if(rows == LCD_4LINE){
  60.                 _rowoffset = 1;
  61.                 _numlines = LCD_4LINE;
  62.                 specialCommand(LCD_SET4LINE);
  63.         }else{ // default to 2 line if input was invalid
  64.                 _rowoffset = 0;
  65.                 _numlines = LCD_2LINE;
  66.                 specialCommand(LCD_SET2LINE);
  67.         }
  68.         // clear the display
  69.         clear();
  70.         // set brightness to full
  71.         setBrightness(30);
  72. }
  73. */
  74.  
  75. // Set brightness value range 1-30 1=OFF 30=FULL
  76. void serLCD::setBrightness(int val){
  77.         if(val >= 1 && val <= 30){
  78.                 specialCommand(LCD_BACKLIGHT | (val - 1));
  79.         }
  80. }
  81.  
  82. // Clears screen and returns cursor to home position
  83. void serLCD::clear(){
  84.         command(LCD_CLEARDISPLAY);
  85. }
  86.  
  87. // Clears a single line by writing blank spaces then returning
  88. // cursor to beginning of line
  89. void serLCD::clearLine(int num){
  90.         if(num > 0 && num <= _numlines){
  91.                 setCursor(num, 1);
  92.                 print("                ");
  93.                 setCursor(num, 1);
  94.         }
  95. }
  96.  
  97. // Moves cursor to the beginning of selected line
  98. void serLCD::selectLine(int num){
  99.         if(num > 0 && num <= _numlines){
  100.                 setCursor(num, 1);
  101.         }
  102. }
  103.  
  104. // returns cursor to home position
  105. void serLCD::home(){
  106.         command(LCD_RETURNHOME);
  107. }
  108.  
  109. // Saves first 2 lines of txt to splash screen memory
  110. void serLCD::setSplash(){
  111.         specialCommand(LCD_SETSPLASHSCREEN);
  112. }
  113.  
  114. // Toggles splashscreen on and off
  115. void serLCD::toggleSplash(){
  116.         specialCommand(LCD_SPLASHTOGGLE);
  117. }
  118.  
  119. //  This is for text that flows Left to Right
  120. void serLCD::leftToRight(){
  121.         _displaymode |= LCD_ENTRYLEFT;
  122.         command(LCD_ENTRYMODESET | _displaymode);
  123. }
  124.  
  125. // This is for text that flows Right to Left
  126. void serLCD::rightToLeft() {
  127.         _displaymode &= ~LCD_ENTRYLEFT;
  128.         command(LCD_ENTRYMODESET | _displaymode);
  129. }
  130.  
  131. // Blinking cursor on/off
  132. void serLCD::blink(){
  133.         _displaycontrol |= LCD_BLINKON;
  134.         command(LCD_DISPLAYCONTROL | _displaycontrol);
  135. }
  136. void serLCD::noBlink(){
  137.         _displaycontrol &= ~LCD_BLINKON;
  138.         command(LCD_DISPLAYCONTROL | _displaycontrol);
  139. }
  140.  
  141. // Underline cursor on/off
  142. void serLCD::cursor(){
  143.         _displaycontrol |= LCD_CURSORON;
  144.         command(LCD_DISPLAYCONTROL | _displaycontrol);
  145. }
  146. void serLCD::noCursor(){
  147.         _displaycontrol &= ~LCD_CURSORON;
  148.         command(LCD_DISPLAYCONTROL | _displaycontrol);
  149. }
  150.  
  151. // Display on/off
  152. void serLCD::display(){
  153.         _displaycontrol |= LCD_DISPLAYON;
  154.         command(LCD_DISPLAYCONTROL | _displaycontrol);
  155. }
  156. void serLCD::noDisplay(){
  157.         _displaycontrol &= ~LCD_DISPLAYON;
  158.         command(LCD_DISPLAYCONTROL | _displaycontrol);
  159. }
  160.  
  161. // Set cursor to specific row and col values start at 1 not 0
  162. void serLCD::setCursor(int row, int col){
  163.         int row_offsets[2][4] = {
  164.                 { 0x00, 0x40, 0x10, 0x50 },
  165.                 { 0x00, 0x40, 0x14, 0x54 }
  166.         };
  167.         if((row > 0 && row < 3) && (col > 0 && col < 17)){
  168.            command(LCD_SETDDRAMADDR | ((col - 1) + row_offsets[_rowoffset][(row - 1)]));
  169.         }
  170. }
  171.  
  172. // Creates custom characters 8 char limit
  173. // Input values start with 1
  174. void serLCD::createChar(int location, uint8_t charmap[]){
  175.         location -= 1;
  176.         location &= 0x07;
  177.   for (int i=0; i<8; i++){
  178.     command(LCD_SETCGRAMADDR | (location << 3) | i);
  179.     write(charmap[i]);
  180.   }
  181. }
  182.  
  183. // Prints custom character
  184. // Input values start with 1
  185. void serLCD::printCustomChar(int num){
  186.         write((num - 1));
  187. }
  188.  
  189. // new in 1.6: sets the type of the LCD
  190. void serLCD::setType(int num){
  191. /*
  192.   3: type 2x16
  193.   4: type 2x20
  194.   5: type 4x16
  195.   6: type 4x20
  196. */
  197.         specialCommand(num);
  198. }
  199.  
  200. // new in 1.6: scrolls text to left with one position
  201. void serLCD::scrollLeft(){
  202.   command(0x18);
  203. }
  204.  
  205. // new in 1.6: scrolls text to right with one position
  206. void serLCD::scrollRight(){
  207.         command(0x1C);
  208. }
  209.  
  210.  
  211. // PRIVATE FUNCTIONS
  212.  
  213. // Functions for sending the special command values
  214. void serLCD::command(uint8_t value){
  215.         write(0xFE);
  216.         write(value);
  217.         delay(5);
  218. }
  219. void serLCD::specialCommand(uint8_t value){
  220.         write(0x7C);
  221.         write(value);
  222.         delay(5);
  223. }

Share