LCD Library 1.3.0
LCD Library - LCD control class hierarchy library. Drop in replacement for the LiquidCrystal Library.
LCD.h
Go to the documentation of this file.
00001 // ---------------------------------------------------------------------------
00002 // Created by Francisco Malpartida on 20/08/11.
00003 // Copyright 2011 - Under creative commons license 3.0:
00004 //        Attribution-ShareAlike CC BY-SA
00005 //
00006 // This software is furnished "as is", without technical support, and with no 
00007 // warranty, express or implied, as to its usefulness for any purpose.
00008 //
00009 // Thread Safe: No
00010 // Extendable: Yes
00011 //
00012 // @file LCD.h
00013 // This file implements a basic liquid crystal library that comes as standard
00014 // in the Arduino SDK.
00015 // 
00016 // @brief 
00017 // This is a basic implementation of the LiquidCrystal library of the
00018 // Arduino SDK. This library is a refactored version of the one supplied
00019 // in the Arduino SDK in such a way that it simplifies its extension
00020 // to support other mechanism to communicate to LCDs such as I2C, Serial, SR, 
00021 // The original library has been reworked in such a way that this will be
00022 // the base class implementing all generic methods to command an LCD based
00023 // on the Hitachi HD44780 and compatible chipsets.
00024 //
00025 // This base class is a pure abstract class and needs to be extended. As reference,
00026 // it has been extended to drive 4 and 8 bit mode control, LCDs and I2C extension
00027 // backpacks such as the I2CLCDextraIO using the PCF8574* I2C IO Expander ASIC.
00028 //
00029 // The functionality provided by this class and its base class is identical
00030 // to the original functionality of the Arduino LiquidCrystal library.
00031 //
00032 // @version API 1.1.0
00033 //
00034 //
00035 // @author F. Malpartida - fmalpartida@gmail.com
00036 // ---------------------------------------------------------------------------
00037 #ifndef _LCD_H_
00038 #define _LCD_H_
00039 
00040 #if (ARDUINO <  100)
00041 #include <WProgram.h>
00042 #else
00043 #include <Arduino.h>
00044 #endif
00045 
00046 #ifdef __AVR__
00047 #include <avr/pgmspace.h>
00048 #endif
00049 
00050 #include <inttypes.h>
00051 #include <Print.h>
00052 
00053 
00062 #ifdef __AVR__
00063 #define FAST_MODE
00064 #endif
00065 
00075 inline static void waitUsec ( uint16_t uSec )
00076 {
00077 #ifndef FAST_MODE
00078    delayMicroseconds ( uSec );
00079 #endif // FAST_MODE
00080 }
00081 
00082 
00090 // LCD Commands
00091 // ---------------------------------------------------------------------------
00092 #define LCD_CLEARDISPLAY        0x01
00093 #define LCD_RETURNHOME          0x02
00094 #define LCD_ENTRYMODESET        0x04
00095 #define LCD_DISPLAYCONTROL      0x08
00096 #define LCD_CURSORSHIFT         0x10
00097 #define LCD_FUNCTIONSET         0x20
00098 #define LCD_SETCGRAMADDR        0x40
00099 #define LCD_SETDDRAMADDR        0x80
00100 
00101 // flags for display entry mode
00102 // ---------------------------------------------------------------------------
00103 #define LCD_ENTRYRIGHT          0x00
00104 #define LCD_ENTRYLEFT           0x02
00105 #define LCD_ENTRYSHIFTINCREMENT 0x01
00106 #define LCD_ENTRYSHIFTDECREMENT 0x00
00107 
00108 // flags for display on/off and cursor control
00109 // ---------------------------------------------------------------------------
00110 #define LCD_DISPLAYON           0x04
00111 #define LCD_DISPLAYOFF          0x00
00112 #define LCD_CURSORON            0x02
00113 #define LCD_CURSOROFF           0x00
00114 #define LCD_BLINKON             0x01
00115 #define LCD_BLINKOFF            0x00
00116 
00117 // flags for display/cursor shift
00118 // ---------------------------------------------------------------------------
00119 #define LCD_DISPLAYMOVE         0x08
00120 #define LCD_CURSORMOVE          0x00
00121 #define LCD_MOVERIGHT           0x04
00122 #define LCD_MOVELEFT            0x00
00123 
00124 // flags for function set
00125 // ---------------------------------------------------------------------------
00126 #define LCD_8BITMODE            0x10
00127 #define LCD_4BITMODE            0x00
00128 #define LCD_2LINE               0x08
00129 #define LCD_1LINE               0x00
00130 #define LCD_5x10DOTS            0x04
00131 #define LCD_5x8DOTS             0x00
00132 
00133 
00134 // Define COMMAND and DATA LCD Rs (used by send method).
00135 // ---------------------------------------------------------------------------
00136 #define COMMAND                 0
00137 #define DATA                    1
00138 #define FOUR_BITS               2
00139 
00140 
00147 #define HOME_CLEAR_EXEC      2000
00148 
00155 #define BACKLIGHT_OFF           0
00156 
00163 #define BACKLIGHT_ON          255
00164 
00165 
00171 typedef enum { POSITIVE, NEGATIVE } t_backlighPol;
00172 
00173 class LCD : public Print 
00174 {
00175 public:
00176    
00183    LCD ( );
00184    
00200    virtual void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);
00201    
00212    void clear();
00213    
00225    void home();
00226    
00235    void noDisplay();
00236    
00246    void display();
00247    
00254    void noBlink();
00255    
00264    void blink();
00265    
00272    void noCursor();
00273    
00282    void cursor();
00283    
00291    void scrollDisplayLeft();
00292    
00300    void scrollDisplayRight();
00301    
00313    void leftToRight();
00314    
00326    void rightToLeft();
00327    
00334    void moveCursorLeft();
00335    
00336    
00343    void moveCursorRight();
00344    
00358    void autoscroll();
00359    
00368    void noAutoscroll();
00369    
00386    void createChar(uint8_t location, uint8_t charmap[]);
00387 
00388 #ifdef __AVR__
00389 
00407    void createChar(uint8_t location, const prog_uchar charmap[]);
00408 #endif // __AVR__
00409    
00419    void setCursor(uint8_t col, uint8_t row);
00420    
00428    void backlight ( void );
00429    
00437    void noBacklight ( void );
00438    
00446    void on ( void );
00447 
00455    void off ( void );
00456    
00457    //
00458    // virtual class methods
00459    // --------------------------------------------------------------------------
00470    virtual void setBacklightPin ( uint8_t value, t_backlighPol pol ) { };
00471    
00489    virtual void setBacklight ( uint8_t value ) { };
00490    
00502 #if (ARDUINO <  100)
00503    virtual void write(uint8_t value);
00504 #else
00505    virtual size_t write(uint8_t value);
00506 #endif
00507    
00508 #if (ARDUINO <  100)
00509    using Print::write;
00510 #else
00511    using Print::write;
00512 #endif   
00513    
00514 protected:
00515    // Internal LCD variables to control the LCD shared between all derived
00516    // classes.
00517    uint8_t _displayfunction;  // LCD_5x10DOTS or LCD_5x8DOTS, LCD_4BITMODE or 
00518                               // LCD_8BITMODE, LCD_1LINE or LCD_2LINE
00519    uint8_t _displaycontrol;   // LCD base control command LCD on/off, blink, cursor
00520                               // all commands are "ored" to its contents.
00521    uint8_t _displaymode;      // Text entry mode to the LCD
00522    uint8_t _numlines;         // Number of lines of the LCD, initialized with begin()
00523    uint8_t _cols;             // Number of columns in the LCD
00524    t_backlighPol _polarity;   // Backlight polarity
00525    
00526 private:
00539    void command(uint8_t value);
00540 
00554 #if (ARDUINO <  100)
00555    virtual void send(uint8_t value, uint8_t mode) { };
00556 #else
00557    virtual void send(uint8_t value, uint8_t mode) = 0;
00558 #endif
00559    
00560 };
00561 
00562 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines