CPP   5

SerialLCD.cpp

Guest on 2nd August 2021 03:18:25 PM

  1. /*
  2.   SerialLCD.h - Serial LCD driver Library
  3.   2011 Copyright (c) Seeed Technology Inc.  All right reserved.
  4.   For Serial LCD v1.0b firmware.
  5.  
  6.   Original Author: Jimbo.We
  7.   Contribution: Visweswara R
  8.  
  9.   This library is free software; you can redistribute it and/or
  10.   modify it under the terms of the GNU Lesser General Public
  11.   License as published by the Free Software Foundation; either
  12.   version 2.1 of the License, or (at your option) any later version.
  13.  
  14.   This library is distributed in the hope that it will be useful,
  15.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.   Lesser General Public License for more details.
  18.  
  19.   You should have received a copy of the GNU Lesser General Public
  20.   License along with this library; if not, write to the Free Software
  21.   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <inttypes.h>
  27. #include "WProgram.h"
  28. #include <NewSoftSerial.h>
  29. #include "SerialLCD.h"
  30.  
  31. SerialLCD::SerialLCD(uint8_t rx, uint8_t tx):NewSoftSerial(rx,tx)
  32. {
  33.  
  34. }
  35.  
  36. /********** High level commands, for the user! **********/
  37.  
  38. // Initialize the Serial LCD Driver. SerialLCD Module initiates the communication.
  39. void SerialLCD::begin()
  40. {
  41.     NewSoftSerial::begin(9600);
  42.     delay(2);
  43.     noPower();
  44.     delay(1);
  45.     power();
  46.  //    backlight();
  47.    delay(1);
  48. //    NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);  
  49.     NewSoftSerial::print(SLCD_INIT_ACK,BYTE);
  50.     while(1)
  51.     {
  52.         if (NewSoftSerial::available() > 0 &&NewSoftSerial::read()==SLCD_INIT_DONE)
  53.             break;
  54.     }
  55.     delay(2);
  56. }
  57.  
  58. //Turn off the back light
  59. void SerialLCD::noBacklight()
  60. {
  61.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);  
  62.     NewSoftSerial::print(SLCD_BACKLIGHT_OFF,BYTE);  
  63. }
  64.  
  65. //Turn on the back light
  66. void SerialLCD::backlight()
  67. {
  68.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);  
  69.     NewSoftSerial::print(SLCD_BACKLIGHT_ON,BYTE);  
  70. }
  71.  
  72. //Turn off the LCD
  73. void SerialLCD::power()
  74. {
  75.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);  
  76.     NewSoftSerial::print(SLCD_POWER_ON,BYTE);  
  77. }
  78.  
  79. //Turn on the LCD
  80. void SerialLCD::noPower()
  81. {
  82.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);  
  83.     NewSoftSerial::print(SLCD_POWER_OFF,BYTE);  
  84. }
  85.  
  86. // Clear the display
  87. void SerialLCD::clear()
  88. {
  89.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);  
  90.     NewSoftSerial::print(SLCD_CLEAR_DISPLAY,BYTE);
  91.     delay(2);//this command needs more time;  
  92. }
  93.  
  94. // Return to home(top-left corner of LCD)
  95. void SerialLCD::home()
  96. {
  97.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  98.     NewSoftSerial::print(SLCD_RETURN_HOME,BYTE);  
  99.     delay(2);//this command needs more time;  
  100. }
  101.  
  102. // Set Cursor to (Column,Row) Position
  103. void SerialLCD::setCursor(uint8_t column, uint8_t row)
  104. {
  105.     delay(2);//this command needs more time;  
  106.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  107.     NewSoftSerial::print(SLCD_CURSOR_HEADER,BYTE); //cursor header command
  108.     while(1)
  109.     {
  110.         if (NewSoftSerial::available() > 0 &&NewSoftSerial::read()==SLCD_CURSOR_ACK)
  111.             break;
  112.     }
  113.     NewSoftSerial::print(column,BYTE);
  114.     NewSoftSerial::print(row,BYTE);
  115. //one more to make sure the cursor is right
  116.     delay(2);//this command needs more time;  
  117.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  118.     NewSoftSerial::print(SLCD_CURSOR_HEADER,BYTE); //cursor header command
  119.     while(1)
  120.     {
  121.         if (NewSoftSerial::available() > 0 &&NewSoftSerial::read()==SLCD_CURSOR_ACK)
  122.             break;
  123.     }
  124.     NewSoftSerial::print(column,BYTE);
  125.     NewSoftSerial::print(row,BYTE);
  126. }
  127.  
  128. // Switch the display off without clearing RAM
  129. void SerialLCD::noDisplay()
  130. {
  131.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  132.     NewSoftSerial::print(SLCD_DISPLAY_OFF,BYTE);    
  133. }
  134.  
  135. // Switch the display on
  136. void SerialLCD::display()
  137. {
  138.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  139.     NewSoftSerial::print(SLCD_DISPLAY_ON,BYTE);    
  140. }
  141.  
  142. // Switch the underline cursor off
  143. void SerialLCD::noCursor()
  144. {
  145.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  146.     NewSoftSerial::print(SLCD_CURSOR_OFF,BYTE);    
  147. }
  148.  
  149. // Switch the underline cursor on
  150. void SerialLCD::cursor()
  151. {
  152.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  153.     NewSoftSerial::print(SLCD_CURSOR_ON,BYTE);    
  154. }
  155.  
  156. // Switch off the blinking cursor
  157. void SerialLCD::noBlink()
  158. {
  159.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  160.     NewSoftSerial::print(SLCD_BLINK_OFF,BYTE);    
  161. }
  162.  
  163. // Switch on the blinking cursor
  164. void SerialLCD::blink()
  165. {
  166.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  167.     NewSoftSerial::print(SLCD_BLINK_ON,BYTE);    
  168. }
  169.  
  170. // Scroll the display left without changing the RAM
  171. void SerialLCD::scrollDisplayLeft(void)
  172. {
  173.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  174.     NewSoftSerial::print(SLCD_SCROLL_LEFT,BYTE);
  175. }
  176.  
  177. // Scroll the display right without changing the RAM
  178. void SerialLCD::scrollDisplayRight(void)
  179. {
  180.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  181.     NewSoftSerial::print(SLCD_SCROLL_RIGHT,BYTE);
  182. }
  183.  
  184. // Set the text flow "Left to Right"
  185. void SerialLCD::leftToRight(void)
  186. {
  187.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  188.     NewSoftSerial::print(SLCD_LEFT_TO_RIGHT,BYTE);
  189. }
  190.  
  191. // Set the text flow "Right to Left"
  192. void SerialLCD::rightToLeft(void)
  193. {
  194.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  195.     NewSoftSerial::print(SLCD_RIGHT_TO_LEFT,BYTE);
  196. }
  197.  
  198. // This will 'right justify' text from the cursor
  199. void SerialLCD::autoscroll(void)
  200. {
  201.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  202.     NewSoftSerial::print(SLCD_AUTO_SCROLL,BYTE);
  203. }
  204.  
  205. // This will 'left justify' text from the cursor
  206. void SerialLCD::noAutoscroll(void)
  207. {
  208.     NewSoftSerial::print(SLCD_CONTROL_HEADER,BYTE);
  209.     NewSoftSerial::print(SLCD_NO_AUTO_SCROLL,BYTE);
  210. }
  211.  
  212. // Print Commands
  213.  
  214. void SerialLCD::print(uint8_t b)
  215. {
  216.     NewSoftSerial::print(SLCD_CHAR_HEADER,BYTE);
  217.     NewSoftSerial::print(b);
  218. }
  219. void SerialLCD::print(const char b[])
  220. {
  221.     NewSoftSerial::print(SLCD_CHAR_HEADER,BYTE);
  222.     NewSoftSerial::print(b);
  223. }
  224.  
  225. void SerialLCD::print(unsigned long n, uint8_t base)
  226. {
  227.     unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
  228.     unsigned long i = 0;
  229.  
  230.     if (base == 0) print(n);
  231.  
  232.     else if(base!=0)
  233.     {
  234.         if (n == 0) {
  235.             print('0');
  236.             return;
  237.         }
  238.  
  239.         while (n > 0) {
  240.             buf[i++] = n % base;
  241.             n /= base;
  242.         }
  243.  
  244.         for (; i > 0; i--)
  245.             print((char) (buf[i - 1] < 10 ?
  246.                           '0' + buf[i - 1] :
  247.                           'A' + buf[i - 1] - 10));
  248.     }
  249.     //delay(1);
  250. }

Raw Paste


Login or Register to edit or fork this paste. It's free.