![]() |
LCD Library 1.1.7
LCD Library - LCD control class hierarchy library. Drop in replacement for the LiquidCrystal Library.
|
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 HD44780.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 HD44780 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. 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 // 00030 // This library is only compatible with Arduino's SDK version 1.0 00031 // 00032 // @version API 1.0.0 00033 // 00034 // @author F. Malpartida - fmalpartida@gmail.com 00035 // --------------------------------------------------------------------------- 00036 #include <stdio.h> 00037 #include <string.h> 00038 #include <inttypes.h> 00039 00040 #if (ARDUINO < 100) 00041 #include <WProgram.h> 00042 #else 00043 #include <Arduino.h> 00044 #endif 00045 #include <LCD.h> 00046 00047 // CLASS CONSTRUCTORS 00048 // --------------------------------------------------------------------------- 00049 // Constructor 00050 LCD::LCD () 00051 { 00052 00053 } 00054 00055 // PUBLIC METHODS 00056 // --------------------------------------------------------------------------- 00057 00058 // Common LCD Commands 00059 // --------------------------------------------------------------------------- 00060 void LCD::clear() 00061 { 00062 command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero 00063 delayMicroseconds(HOME_CLEAR_EXEC); // this command is time consuming 00064 } 00065 00066 void LCD::home() 00067 { 00068 command(LCD_RETURNHOME); // set cursor position to zero 00069 delayMicroseconds(HOME_CLEAR_EXEC); // This command is time consuming 00070 } 00071 00072 void LCD::setCursor(uint8_t col, uint8_t row) 00073 { 00074 const byte row_offsetsDef[] = { 0x00, 0x40, 0x14, 0x54 }; // For regular LCDs 00075 const byte row_offsetsLarge[] = { 0x00, 0x40, 0x10, 0x50 }; // For 16x4 LCDs 00076 00077 if ( row >= _numlines ) 00078 { 00079 row = _numlines-1; // rows start at 0 00080 } 00081 00082 // 16x4 LCDs have special memory map layout 00083 // ---------------------------------------- 00084 if ( _cols == 16 && _numlines == 4 ) 00085 { 00086 command(LCD_SETDDRAMADDR | (col + row_offsetsLarge[row])); 00087 } 00088 else 00089 { 00090 command(LCD_SETDDRAMADDR | (col + row_offsetsDef[row])); 00091 } 00092 00093 } 00094 00095 // Turn the display on/off 00096 void LCD::noDisplay() 00097 { 00098 _displaycontrol &= ~LCD_DISPLAYON; 00099 command(LCD_DISPLAYCONTROL | _displaycontrol); 00100 } 00101 00102 void LCD::display() 00103 { 00104 _displaycontrol |= LCD_DISPLAYON; 00105 command(LCD_DISPLAYCONTROL | _displaycontrol); 00106 } 00107 00108 // Turns the underline cursor on/off 00109 void LCD::noCursor() 00110 { 00111 _displaycontrol &= ~LCD_CURSORON; 00112 command(LCD_DISPLAYCONTROL | _displaycontrol); 00113 } 00114 void LCD::cursor() 00115 { 00116 _displaycontrol |= LCD_CURSORON; 00117 command(LCD_DISPLAYCONTROL | _displaycontrol); 00118 } 00119 00120 // Turns on/off the blinking cursor 00121 void LCD::noBlink() 00122 { 00123 _displaycontrol &= ~LCD_BLINKON; 00124 command(LCD_DISPLAYCONTROL | _displaycontrol); 00125 } 00126 00127 void LCD::blink() 00128 { 00129 _displaycontrol |= LCD_BLINKON; 00130 command(LCD_DISPLAYCONTROL | _displaycontrol); 00131 } 00132 00133 // These commands scroll the display without changing the RAM 00134 void LCD::scrollDisplayLeft(void) 00135 { 00136 command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); 00137 } 00138 00139 void LCD::scrollDisplayRight(void) { 00140 command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT); 00141 } 00142 00143 // This is for text that flows Left to Right 00144 void LCD::leftToRight(void) 00145 { 00146 _displaymode |= LCD_ENTRYLEFT; 00147 command(LCD_ENTRYMODESET | _displaymode); 00148 } 00149 00150 // This is for text that flows Right to Left 00151 void LCD::rightToLeft(void) 00152 { 00153 _displaymode &= ~LCD_ENTRYLEFT; 00154 command(LCD_ENTRYMODESET | _displaymode); 00155 } 00156 00157 // This will 'right justify' text from the cursor 00158 void LCD::autoscroll(void) 00159 { 00160 _displaymode |= LCD_ENTRYSHIFTINCREMENT; 00161 command(LCD_ENTRYMODESET | _displaymode); 00162 } 00163 00164 // This will 'left justify' text from the cursor 00165 void LCD::noAutoscroll(void) 00166 { 00167 _displaymode &= ~LCD_ENTRYSHIFTINCREMENT; 00168 command(LCD_ENTRYMODESET | _displaymode); 00169 } 00170 00171 // Write to CGRAM of new characters 00172 void LCD::createChar(uint8_t location, uint8_t charmap[]) 00173 { 00174 location &= 0x7; // we only have 8 locations 0-7 00175 00176 command(LCD_SETCGRAMADDR | (location << 3)); 00177 00178 for (int i=0; i<8; i++) 00179 { 00180 write(charmap[i]); // call the virtual write method 00181 } 00182 } 00183 00184 // General LCD commands - generic methods used by the rest of the commands 00185 // --------------------------------------------------------------------------- 00186 void LCD::command(uint8_t value) 00187 { 00188 send(value, COMMAND); 00189 } 00190 00191 #if (ARDUINO < 100) 00192 void LCD::write(uint8_t value) 00193 { 00194 send(value, DATA); 00195 } 00196 #else 00197 size_t LCD::write(uint8_t value) 00198 { 00199 send(value, DATA); 00200 return 1; // assume OK 00201 } 00202 #endif