New LiquidCrystal library  1.5.0
Generic LCD control library
/Users/fmalpartida/Documents/development/mercurial repos/SW/NewLiquidCrystal_lib/LCD.h
1 // ---------------------------------------------------------------------------
2 // Created by Francisco Malpartida on 20/08/11.
3 // Copyright (C) - 2018
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License v3.0
16 // along with this program.
17 // If not, see <https://www.gnu.org/licenses/gpl-3.0.en.html>.
18 //
19 // ---------------------------------------------------------------------------
20 //
21 // Thread Safe: No
22 // Extendable: Yes
23 //
24 // @file LCD.h
25 // This file implements a basic liquid crystal library that comes as standard
26 // in the Arduino SDK.
27 //
28 // @brief
29 // This is a basic implementation of the LiquidCrystal library of the
30 // Arduino SDK. This library is a refactored version of the one supplied
31 // in the Arduino SDK in such a way that it simplifies its extension
32 // to support other mechanism to communicate to LCDs such as I2C, Serial, SR,
33 // The original library has been reworked in such a way that this will be
34 // the base class implementing all generic methods to command an LCD based
35 // on the Hitachi HD44780 and compatible chipsets.
36 //
37 // This base class is a pure abstract class and needs to be extended. As reference,
38 // it has been extended to drive 4 and 8 bit mode control, LCDs and I2C extension
39 // backpacks such as the I2CLCDextraIO using the PCF8574* I2C IO Expander ASIC.
40 //
41 // The functionality provided by this class and its base class is identical
42 // to the original functionality of the Arduino LiquidCrystal library.
43 //
44 // @version API 1.1.0
45 //
46 //
47 // @author F. Malpartida - fmalpartida@gmail.com
48 // ---------------------------------------------------------------------------
49 #ifndef _LCD_H_
50 #define _LCD_H_
51 
52 #if (ARDUINO < 100)
53 #include <WProgram.h>
54 #else
55 #include <Arduino.h>
56 #endif
57 
58 #ifdef __AVR__
59 #include <avr/pgmspace.h>
60 #endif
61 
62 #include <inttypes.h>
63 #include <Print.h>
64 
65 
76 #ifndef _BV
77 #define _BV(bit) (1 << (bit))
78 #endif
79 
88 #ifdef __AVR__
89 #define FAST_MODE
90 #endif
91 
101 inline static void waitUsec ( uint16_t uSec )
102 {
103 #ifndef FAST_MODE
104  delayMicroseconds ( uSec );
105 #endif // FAST_MODE
106 }
107 
108 
116 // LCD Commands
117 // ---------------------------------------------------------------------------
118 #define LCD_CLEARDISPLAY 0x01
119 #define LCD_RETURNHOME 0x02
120 #define LCD_ENTRYMODESET 0x04
121 #define LCD_DISPLAYCONTROL 0x08
122 #define LCD_CURSORSHIFT 0x10
123 #define LCD_FUNCTIONSET 0x20
124 #define LCD_SETCGRAMADDR 0x40
125 #define LCD_SETDDRAMADDR 0x80
126 
127 // flags for display entry mode
128 // ---------------------------------------------------------------------------
129 #define LCD_ENTRYRIGHT 0x00
130 #define LCD_ENTRYLEFT 0x02
131 #define LCD_ENTRYSHIFTINCREMENT 0x01
132 #define LCD_ENTRYSHIFTDECREMENT 0x00
133 
134 // flags for display on/off and cursor control
135 // ---------------------------------------------------------------------------
136 #define LCD_DISPLAYON 0x04
137 #define LCD_DISPLAYOFF 0x00
138 #define LCD_CURSORON 0x02
139 #define LCD_CURSOROFF 0x00
140 #define LCD_BLINKON 0x01
141 #define LCD_BLINKOFF 0x00
142 
143 // flags for display/cursor shift
144 // ---------------------------------------------------------------------------
145 #define LCD_DISPLAYMOVE 0x08
146 #define LCD_CURSORMOVE 0x00
147 #define LCD_MOVERIGHT 0x04
148 #define LCD_MOVELEFT 0x00
149 
150 // flags for function set
151 // ---------------------------------------------------------------------------
152 #define LCD_8BITMODE 0x10
153 #define LCD_4BITMODE 0x00
154 #define LCD_2LINE 0x08
155 #define LCD_1LINE 0x00
156 #define LCD_5x10DOTS 0x04
157 #define LCD_5x8DOTS 0x00
158 
159 
160 // Define COMMAND and DATA LCD Rs (used by send method).
161 // ---------------------------------------------------------------------------
162 #define COMMAND 0
163 #define LCD_DATA 1
164 #define FOUR_BITS 2
165 
166 
173 #define HOME_CLEAR_EXEC 2000
174 
181 #define BACKLIGHT_OFF 0
182 
189 #define BACKLIGHT_ON 255
190 
191 
197 typedef enum { POSITIVE, NEGATIVE } t_backlightPol;
198 
199 class LCD : public Print
200 {
201 public:
202 
209  LCD ( );
210 
226  virtual void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);
227 
238  void clear();
239 
251  void home();
252 
261  void noDisplay();
262 
272  void display();
273 
280  void noBlink();
281 
290  void blink();
291 
298  void noCursor();
299 
308  void cursor();
309 
317  void scrollDisplayLeft();
318 
326  void scrollDisplayRight();
327 
339  void leftToRight();
340 
352  void rightToLeft();
353 
360  void moveCursorLeft();
361 
362 
369  void moveCursorRight();
370 
384  void autoscroll();
385 
394  void noAutoscroll();
395 
412  void createChar(uint8_t location, uint8_t charmap[]);
413 
414 #ifdef __AVR__
415 
435  void createChar(uint8_t location, const char *charmap);
436 #endif // __AVR__
437 
447  void setCursor(uint8_t col, uint8_t row);
448 
456  void backlight ( void );
457 
465  void noBacklight ( void );
466 
474  void on ( void );
475 
483  void off ( void );
484 
485  //
486  // virtual class methods
487  // --------------------------------------------------------------------------
498  virtual void setBacklightPin ( uint8_t value, t_backlightPol pol ) { };
499 
517  virtual void setBacklight ( uint8_t value ) { };
518 
530 #if (ARDUINO < 100)
531  virtual void write(uint8_t value);
532 #else
533  virtual size_t write(uint8_t value);
534 #endif
535 
536 #if (ARDUINO < 100)
537  using Print::write;
538 #else
539  using Print::write;
540 #endif
541 
542 protected:
543  // Internal LCD variables to control the LCD shared between all derived
544  // classes.
545  uint8_t _displayfunction; // LCD_5x10DOTS or LCD_5x8DOTS, LCD_4BITMODE or
546  // LCD_8BITMODE, LCD_1LINE or LCD_2LINE
547  uint8_t _displaycontrol; // LCD base control command LCD on/off, blink, cursor
548  // all commands are "ored" to its contents.
549  uint8_t _displaymode; // Text entry mode to the LCD
550  uint8_t _numlines; // Number of lines of the LCD, initialized with begin()
551  uint8_t _cols; // Number of columns in the LCD
552  t_backlightPol _polarity; // Backlight polarity
553 
554 private:
567  void command(uint8_t value);
568 
582 #if (ARDUINO < 100)
583  virtual void send(uint8_t value, uint8_t mode) { };
584 #else
585  virtual void send(uint8_t value, uint8_t mode) = 0;
586 #endif
587 
588 };
589 
590 #endif
Definition: LCD.h:199
void createChar(uint8_t location, uint8_t charmap[])
Definition: LCD.cpp:306
void autoscroll()
Definition: LCD.cpp:292
void leftToRight()
Definition: LCD.cpp:265
void backlight(void)
Definition: LCD.cpp:338
void clear()
Definition: LCD.cpp:180
void on(void)
Definition: LCD.cpp:352
void off(void)
Definition: LCD.cpp:360
void noBlink()
Definition: LCD.cpp:241
void scrollDisplayRight()
Definition: LCD.cpp:259
void display()
Definition: LCD.cpp:222
void setCursor(uint8_t col, uint8_t row)
Definition: LCD.cpp:192
virtual void setBacklightPin(uint8_t value, t_backlightPol pol)
Definition: LCD.h:498
void rightToLeft()
Definition: LCD.cpp:272
void noAutoscroll()
Definition: LCD.cpp:299
void home()
Definition: LCD.cpp:186
void blink()
Definition: LCD.cpp:247
virtual void setBacklight(uint8_t value)
Definition: LCD.h:517
void moveCursorRight()
Definition: LCD.cpp:279
LCD()
Definition: LCD.cpp:64
void scrollDisplayLeft()
Definition: LCD.cpp:254
virtual void begin(uint8_t cols, uint8_t rows, uint8_t charsize=LCD_5x8DOTS)
Definition: LCD.cpp:91
void noDisplay()
Definition: LCD.cpp:216
void noBacklight(void)
Definition: LCD.cpp:345
void noCursor()
Definition: LCD.cpp:229
void moveCursorLeft()
Definition: LCD.cpp:285
virtual void write(uint8_t value)
Definition: LCD.cpp:374
void cursor()
Definition: LCD.cpp:234