![]() |
LCD Library 1.2.0
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 LiquidCrystal_I2C.c 00013 // This file implements a basic liquid crystal library that comes as standard 00014 // in the Arduino SDK but using an I2C IO extension board. 00015 // 00016 // @brief 00017 // This is a basic implementation of the LiquidCrystal library of the 00018 // Arduino SDK. The original library has been reworked in such a way that 00019 // this class implements the all methods to command an LCD based 00020 // on the Hitachi HD44780 and compatible chipsets using I2C extension 00021 // backpacks such as the I2CLCDextraIO with the PCF8574* I2C IO Expander ASIC. 00022 // 00023 // The functionality provided by this class and its base class is identical 00024 // to the original functionality of the Arduino LiquidCrystal library. 00025 // 00026 // 00027 // 00028 // @author F. Malpartida - fmalpartida@gmail.com 00029 // --------------------------------------------------------------------------- 00030 #if (ARDUINO < 100) 00031 #include <WProgram.h> 00032 #else 00033 #include <Arduino.h> 00034 #endif 00035 #include <inttypes.h> 00036 #include "I2CIO.h" 00037 #include "LiquidCrystal_I2C.h" 00038 00039 // CONSTANT definitions 00040 // --------------------------------------------------------------------------- 00041 00042 // flags for backlight control 00043 #define LCD_NOBACKLIGHT 0x00 00044 #define LCD_BACKLIGHT 0xFF 00045 00046 00047 // CONSTRUCTORS 00048 // --------------------------------------------------------------------------- 00049 LiquidCrystal_I2C::LiquidCrystal_I2C( uint8_t lcd_Addr ) 00050 { 00051 _Addr = lcd_Addr; 00052 00053 _backlightPin = 0x0; 00054 _backlightMask = LCD_NOBACKLIGHT; 00055 00056 _En = EN; 00057 _Rw = RW; 00058 _Rs = RS; 00059 00060 // Initialise default values data[0] pin 0, data[1] pin 1, ... 00061 for ( uint8_t i = 0; i < 4; i++ ) 00062 { 00063 _data_pins[i] = ( 1 << i ); 00064 } 00065 } 00066 00067 LiquidCrystal_I2C::LiquidCrystal_I2C( uint8_t lcd_Addr, uint8_t En, uint8_t Rw, 00068 uint8_t Rs) 00069 { 00070 _Addr = lcd_Addr; 00071 00072 _backlightPin = 0; 00073 _backlightMask = LCD_NOBACKLIGHT; 00074 00075 _En = ( 1 << En ); 00076 _Rw = ( 1 << Rw ); 00077 _Rs = ( 1 << Rs ); 00078 00079 // Initialise default values data[0] pin 0, data[1] pin 1, ... 00080 for ( uint8_t i = 0; i < 4; i++ ) 00081 { 00082 _data_pins[i] = ( 1 << i ); 00083 } 00084 } 00085 00086 LiquidCrystal_I2C::LiquidCrystal_I2C( uint8_t lcd_Addr, uint8_t En, uint8_t Rw, 00087 uint8_t Rs, uint8_t d0, uint8_t d1, 00088 uint8_t d2, uint8_t d3 ) 00089 { 00090 _Addr = lcd_Addr; 00091 00092 _backlightPin = 0; 00093 _backlightMask = LCD_NOBACKLIGHT; 00094 00095 _En = ( 1 << En ); 00096 _Rw = ( 1 << Rw ); 00097 _Rs = ( 1 << Rs ); 00098 00099 // Initialise pin mapping 00100 _data_pins[0] = ( 1 << d0 ); 00101 _data_pins[1] = ( 1 << d1 ); 00102 _data_pins[2] = ( 1 << d2 ); 00103 _data_pins[3] = ( 1 << d3 ); 00104 } 00105 00106 // PUBLIC METHODS 00107 // --------------------------------------------------------------------------- 00108 00109 // 00110 // begin 00111 void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) 00112 { 00113 00114 init(); // Initialise the I2C expander interface 00115 LCD::begin ( cols, lines, dotsize ); 00116 } 00117 00118 00119 // User commands - users can expand this section 00120 //---------------------------------------------------------------------------- 00121 00122 // Turn the (optional) backlight off/on 00123 // 00124 // setBacklight 00125 void LiquidCrystal_I2C::setBacklight( uint8_t mode ) 00126 { 00127 if ( mode == HIGH ) 00128 { 00129 _backlightMask = _backlightPin & LCD_BACKLIGHT; 00130 00131 } 00132 else 00133 { 00134 _backlightMask = _backlightPin & LCD_NOBACKLIGHT; 00135 } 00136 _i2cio.write( _backlightMask ); 00137 } 00138 00139 // 00140 // setBacklightPin 00141 void LiquidCrystal_I2C::setBacklightPin ( uint8_t pin ) 00142 { 00143 _backlightPin = ( 1 << pin ); 00144 } 00145 00146 // PRIVATE METHODS 00147 // --------------------------------------------------------------------------- 00148 00149 // 00150 // init 00151 int LiquidCrystal_I2C::init() 00152 { 00153 int status = 0; 00154 00155 // initialize the backpack IO expander 00156 // and display functions. 00157 // ------------------------------------------------------------------------ 00158 if ( _i2cio.begin ( _Addr ) == 1 ) 00159 { 00160 _i2cio.portMode ( OUTPUT ); // Set the entire IO extender to OUTPUT 00161 _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; 00162 status = 1; 00163 _i2cio.write(0); // Set the entire port to LOW 00164 } 00165 return ( status ); 00166 } 00167 00168 // low level data pushing commands 00169 //---------------------------------------------------------------------------- 00170 00171 // 00172 // send - write either command or data 00173 void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode) 00174 { 00175 // No need to use the delay routines since the time taken to write takes 00176 // longer that what is needed both for toggling and enable pin an to execute 00177 // the command. 00178 write4bits( (value >> 4), mode ); 00179 write4bits( (value & 0x0F), mode); 00180 } 00181 00182 // 00183 // write4bits 00184 void LiquidCrystal_I2C::write4bits ( uint8_t value, uint8_t mode ) 00185 { 00186 uint8_t pinMapValue = 0; 00187 00188 // Map the value to LCD pin mapping 00189 // -------------------------------- 00190 for ( uint8_t i = 0; i < 4; i++ ) 00191 { 00192 if ( ( value & 0x1 ) == 1 ) 00193 { 00194 pinMapValue |= _data_pins[i]; 00195 } 00196 value = ( value >> 1 ); 00197 } 00198 00199 // Is it a command or data 00200 // ----------------------- 00201 if ( mode == DATA ) 00202 { 00203 mode = _Rs; 00204 } 00205 00206 pinMapValue |= mode | _backlightMask; 00207 _i2cio.write ( pinMapValue ); 00208 pulseEnable ( pinMapValue ); 00209 } 00210 00211 // 00212 // pulseEnable 00213 void LiquidCrystal_I2C::pulseEnable (uint8_t _data) 00214 { 00215 _i2cio.write (_data | _En); // En HIGH 00216 _i2cio.write (_data & ~_En); // En LOW 00217 }