SerialLCD

Library for easy hardware serial interface with SparkFun Serial Ready 20x4 LCD screen. All credit goes to original author of SfLCD2 (keenethery).

Setup

Do the following:

  • Declare a global SerialLCD object.
  • Pass the desired serial by reference using the begin() function.

Example:

  1. ...
  2. SerialLCD LCD;
  3. ...
  4. setup(){
  5.    ...
  6.    LCD.begin(&Serial);
  7.    ...
  8. }

Functions

  • void begin(HardwareSerial *serial);
  • void print(char *theText);
  • void printLine(int lineNum, char *theText);
  • void printChar(int lineNum, int charNum, char theChar);
  • void clear();
  • void backlight(int thePercentage);

Header File (SerialLCD.h)

  1. /*
  2.         Serial LCD library for Sparkfun.com Serial Enabled 20x4 LCD
  3.         Revision:       1
  4.         Date:           2011-12-19
  5.         Author: A.Spiridonov
  6.  
  7.         Based on SfLCD2 code (Rev. 1 is essentially SfLCD2 modified into a library)
  8.  
  9.         Initialization:
  10.                 Initialize by creating a global object of the class,
  11.                 then in setup() pass the desired serial by reference
  12.                         Example:
  13.                                 SerialLCD LCD;
  14.                                 ...
  15.                                 void setup(){
  16.                                 ...
  17.                                         LCD.begin(&Serial1);
  18.                                 ...
  19.                                 }
  20.  
  21.         Functions:
  22.                 void print(char *theText);
  23.                         - prints the passed char array over 4 lines(if necessary).
  24.                           Automatically truncates to 80 characters (20x4)
  25.                         Example:
  26.                                 LCD.print("This text will appear over multiple lines.");
  27.                 void printLine(int lineNum, char *theText);
  28.                         - prints the passed char array on the desired line
  29.                           Automatically truncates to 20 characters
  30.                         Example:
  31.                                 LCD.printLine(1, "foo");
  32.                                 LCD.printLine(2, "bar");
  33.  
  34. */
  35.  
  36. #ifndef SerialLCD_h
  37. #define SerialLCD_h
  38.  
  39. #include <Arduino.h>
  40.  
  41. class SerialLCD {
  42. private:
  43.         HardwareSerial *SER;
  44. public:
  45.         //SerialLCD(HardwareSerial &ser) {SER = ser;};
  46. //      SerialLCD();
  47.  
  48.         void begin(HardwareSerial *serial);
  49.         void print(char *theText);
  50.         void printLine(int lineNum, char *theText);
  51.         void printChar(int lineNum, int charNum, char theChar);
  52.         void clear();
  53.         void backlight(int thePercentage);
  54. };
  55. #endif

Body File (SerialLCD.cpp)

  1. #include "SerialLCD.h"
  2.  
  3. void SerialLCD::begin(HardwareSerial *serial){
  4. SER = serial;
  5. SER->begin(9600);
  6.  
  7. }
  8.  
  9. void SerialLCD::print(char *theText){
  10.   int delayTime = 50;
  11.   SER->write(0xFE);   // command flag
  12.   delay(delayTime);
  13.   SER->write(128);    // start position for line 1
  14.   if (strlen(theText) < 80) {
  15.     // less than 80 characters, print then and then
  16.     SER->print(theText);
  17.     // pad the rest of the line with spaces
  18.     for (int i = strlen(theText); i < 80; i++) {
  19.       SER->print(" ");
  20.     }
  21.   }
  22.   else {  
  23.     // 80 or more characters, just print the first 80
  24.     for (int i = 0; i < 80; i++) {
  25.       SER->print(theText[i]);
  26.     }
  27.   }
  28.   delay(delayTime);
  29. }
  30.  
  31.  
  32. // displaySerLcdLine( line number, "text for that line")
  33. // writes to each line separately
  34. // lineNum is an integer for the line number. valid values 1 through 4
  35. // *theText is a string of text and it gets padded at the end with spaces
  36. // to overwrite whatever is already showing on that line. If you send more than
  37. // 20 characters, it truncates the text.
  38. void SerialLCD::printLine(int lineNum, char *theText){
  39.   int delayTime = 50;
  40.   int lcdPosition = 0;  // initialize lcdPosition and use to indicate value values
  41.  
  42.   // based upon the lineNum, set the position on the SER
  43.   if (lineNum==1){
  44.     lcdPosition = 128;
  45.   }
  46.   else if (lineNum==2){
  47.     lcdPosition = 192;
  48.   }
  49.   else if (lineNum==3){
  50.     lcdPosition = 148;
  51.   }
  52.   else if (lineNum==4){
  53.     lcdPosition = 212;
  54.   }
  55.  
  56.   // don't write to the SER if the lineNum value didn't generate a valid position
  57.   if (lcdPosition > 0){
  58.     SER->write(0xFE);   //command flag
  59.     delay(delayTime);
  60.     SER->write(lcdPosition);    //position
  61.  
  62.     if (strlen(theText) < 20) {
  63.       // less than 20 characters, print then and then
  64.       SER->print(theText);
  65.       // pad the rest of the line with spaces
  66.       for (int i = strlen(theText); i < 20; i++) {
  67.         SER->print(" ");
  68.       }
  69.     }
  70.     else {  
  71.       // 20 or more characters, just print the first 20
  72.       for (int i = 0; i < 20; i++) {
  73.         SER->print(theText[i]);
  74.       }
  75.     }
  76.     delay(delayTime);
  77.   }
  78. }
  79.  
  80.  
  81. // displaySerLcdChar(SER line, position on line, 'the character to display')
  82. // SER line: integer 1 through 4
  83. // position on line: integer 1 through 20
  84. // character to display: a single character in single quotes
  85. void SerialLCD::printChar(int lineNum, int charNum, char theChar){
  86.   int delayTime = 50;
  87.   int lcdPosition = 0;  // initialize lcdPosition and use to indicate value values
  88.  
  89.   // charNum has to be within 1 to 20,
  90.   // lineNum has to be within 1 to 4
  91.   if (charNum > 0 && charNum < 21) {
  92.     if (lineNum==1){
  93.       lcdPosition = 128;
  94.     }
  95.     else if (lineNum==2){
  96.       lcdPosition = 192;
  97.     }
  98.     else if (lineNum==3){
  99.       lcdPosition = 148;
  100.     }
  101.     else if (lineNum==4){
  102.       lcdPosition = 212;
  103.     }
  104.   }
  105.  
  106.   // don't write to the SER if the lineNum and charNum values were not within range
  107.   if (lcdPosition > 0){
  108.     // add to start of line position to get the position to write to
  109.     lcdPosition = lcdPosition + charNum - 1;
  110.  
  111.     SER->write(0xFE);   //command flag
  112.     delay(delayTime);
  113.     SER->write(lcdPosition);    //position
  114.     SER->print(theChar);
  115.     delay(delayTime);
  116.   }
  117. }
  118.  
  119.  
  120.  
  121. void SerialLCD::clear(){
  122.   SER->write(0xFE);   //command flag
  123.   SER->write(0x01);   //clear command.
  124.   delay(50);
  125. }
  126.  
  127. void SerialLCD::backlight(int thePercentage){  //turns on the backlight
  128.   SER->write(0x7C);   //command flag for backlight stuff
  129.   int theValue = map(thePercentage, 0,100,128,157); // maps percentage to what SerSER wants to see
  130.   SER->write(theValue);    //light level.
  131.   delay(50);  
  132. }

Syntax (keywords.txt)

  1. #######################################
  2. # Syntax Coloring Map For SerialLCD
  3. #######################################
  4.  
  5. #######################################
  6. # Datatypes (KEYWORD1)
  7. #######################################
  8.  
  9. SerialLCD       KEYWORD1
  10.  
  11. #######################################
  12. # Methods and Functions (KEYWORD2)
  13. #######################################
  14. print   KEYWORD2
  15. printLine       KEYWORD2
  16. printChar       KEYWORD2
  17. clear   KEYWORD2
  18. backlight       KEYWORD2

Share