TextLCD library for controlling various LCD panels based on the HD44780 4-bit interface

Dependents:   STM32_Button_Interrupt_dla_taty

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextLCD.cpp Source File

TextLCD.cpp

00001 /* mbed TextLCD Library, for LCDs based on HD44780 controllers
00002  * Copyright (c) 2007-2010, sford, http://mbed.org
00003  *               2013, v01: WH, Added LCD types, fixed LCD address issues, added Cursor and UDCs 
00004  *               2013, v02: WH, Added I2C and SPI bus interfaces  
00005  *               2013, v03: WH, Added support for LCD40x4 which uses 2 controllers 
00006  *               2013, v04: WH, Added support for Display On/Off, improved 4bit bootprocess
00007  *               2013, v05: WH, Added support for 8x2B, added some UDCs   
00008  *               2013, v06: WH, Added support for devices that use internal DC/DC converters 
00009  *               2013, v07: WH, Added support for backlight and include portdefinitions for LCD2004 Module from DFROBOT 
00010  *               2014, v08: WH, Refactored in Base and Derived Classes to deal with mbed lib change regarding 'NC' defined pins 
00011  *               2014, v09: WH/EO, Added Class for Native SPI controllers such as ST7032 
00012  *               2014, v10: WH, Added Class for Native I2C controllers such as ST7032i, Added support for MCP23008 I2C portexpander, Added support for Adafruit module  
00013  *               2014, v11: WH, Added support for native I2C controllers such as PCF21XX, Improved the _initCtrl() method to deal with differences between all supported controllers  
00014  *               2014, v12: WH, Added support for native I2C controller PCF2119 and native I2C/SPI controllers SSD1803, ST7036, added setContrast method (by JH1PJL) for supported devices (eg ST7032i) 
00015  *               2014, v13: WH, Added support for controllers US2066/SSD1311 (OLED), added setUDCBlink() method for supported devices (eg SSD1803), fixed issue in setPower() 
00016  *               2014, v14: WH, Added support for PT6314 (VFD), added setOrient() method for supported devices (eg SSD1803, US2066), added Double Height lines for supported devices, 
00017  *                              added 16 UDCs for supported devices (eg PCF2103), moved UDC defines to TextLCD_UDC file, added TextLCD_Config.h for feature and footprint settings.
00018  *               2014, v15: WH, Added AC780 support, added I2C expander modules, fixed setBacklight() for inverted logic modules. Fixed bug in LCD_SPI_N define 
00019  *               2014, v16: WH, Added ST7070 and KS0073 support, added setIcon(), clrIcon() and setInvert() method for supported devices 
00020  *               2015, v17: WH, Clean up low-level _writeCommand() and _writeData(), Added support for alternative fonttables (eg PCF21XX), Added ST7066_ACM controller for ACM1602 module 
00021  *
00022  * Permission is hereby granted, free of charge, to any person obtaining a copy
00023  * of this software and associated documentation files (the "Software"), to deal
00024  * in the Software without restriction, including without limitation the rights
00025  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00026  * copies of the Software, and to permit persons to whom the Software is
00027  * furnished to do so, subject to the following conditions:
00028  *
00029  * The above copyright notice and this permission notice shall be included in
00030  * all copies or substantial portions of the Software.
00031  *
00032  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00033  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00034  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00035  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00036  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00037  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00038  * THE SOFTWARE.
00039  */
00040 #include "mbed.h"
00041 #include "TextLCD.h"
00042 #include "TextLCD_UDC.inc"
00043    
00044 /** Create a TextLCD_Base interface
00045   *
00046   * @param type  Sets the panel size/addressing mode (default = LCD16x2)
00047   * @param ctrl  LCD controller (default = HD44780)           
00048   */
00049 TextLCD_Base::TextLCD_Base(LCDType type, LCDCtrl ctrl) : _type(type), _ctrl(ctrl) {
00050     
00051   // Extract LCDType data  
00052 
00053   // Columns encoded in b7..b0
00054   _nr_cols = (_type & 0xFF);          
00055 
00056   // Rows encoded in b15..b8  
00057   _nr_rows = ((_type >> 8) & 0xFF);  
00058 
00059   // Addressing mode encoded in b19..b16  
00060   _addr_mode = _type & LCD_T_ADR_MSK;
00061   
00062   // Font table, encoded in LCDCtrl  
00063   _font = _type & LCD_C_FNT_MSK;
00064 }
00065 
00066 /**  Init the LCD Controller(s)
00067   *  Clear display 
00068   *  @param _LCDDatalength dl sets the datalength of data/commands
00069   *  @return none 
00070   */
00071 void TextLCD_Base::_init(_LCDDatalength dl) {
00072   
00073   // Select and configure second LCD controller when needed
00074   if(_type==LCD40x4) {
00075     _ctrl_idx=_LCDCtrl_1;        // Select 2nd controller   
00076     _initCtrl(dl);               // Init 2nd controller   
00077   }
00078     
00079   // Select and configure primary LCD controller
00080   _ctrl_idx=_LCDCtrl_0;          // Select primary controller  
00081   _initCtrl(dl);                 // Init primary controller
00082 
00083   // Clear whole display and Reset Cursor location
00084   // Note: This will make sure that some 3-line displays that skip topline of a 4-line configuration 
00085   //       are cleared and init cursor correctly.
00086   cls();   
00087 } 
00088 
00089 /**  Init the LCD controller
00090   *   Set number of lines, fonttype, no cursor etc
00091   *   The controller is accessed in 4-bit parallel mode either directly via mbed pins or through I2C or SPI expander.
00092   *   Some controllers also support native I2C or SPI interfaces. 
00093   *
00094   *  @param _LCDDatalength dl sets the 4 or 8 bit datalength of data/commands. Required for some native serial modes.
00095   *  @return none  
00096   *
00097   *  Note: some configurations are commented out because they have not yet been tested due to lack of hardware
00098   */
00099 void TextLCD_Base::_initCtrl(_LCDDatalength dl) {
00100   int _bias_lines=0; // Set Bias and lines (Instr Set 1), temporary variable.
00101   int _lines=0;      // Set lines (Ext Instr Set), temporary variable.
00102 
00103     this->_setRS(false); // command mode
00104     
00105     wait_ms(20);         // Wait 20ms to ensure powered up
00106 
00107     if (dl == _LCD_DL_4) {
00108       // The Controller could be in 8 bit mode (power-on reset) or in 4 bit mode (warm reboot) at this point.
00109       // Follow this procedure to make sure the Controller enters the correct state. The hardware interface
00110       // between the uP and the LCD can only write the 4 most significant bits (Most Significant Nibble, MSN).
00111       // In 4 bit mode the LCD expects the MSN first, followed by the LSN.
00112       //
00113       //    Current state:               8 bit mode                |  4 bit mode, MSN is next      | 4 bit mode, LSN is next          
00114                            //-------------------------------------------------------------------------------------------------                          
00115       _writeNibble(0x3);   //  set 8 bit mode (MSN) and dummy LSN, |   set 8 bit mode (MSN),       |    set dummy LSN, 
00116                            //  remains in 8 bit mode               |    change to 8 bit mode       |  remains in 4 bit mode
00117       wait_ms(15);         //                           
00118      
00119       _writeNibble(0x3);   //  set 8 bit mode and dummy LSN,       | set 8 bit mode and dummy LSN, |    set 8bit mode (MSN), 
00120                            //  remains in 8 bit mode               |   remains in 8 bit mode       |  remains in 4 bit mode
00121       wait_ms(15);         // 
00122     
00123       _writeNibble(0x3);   //  set 8 bit mode and dummy LSN,       | set 8 bit mode and dummy LSN, |    set dummy LSN, 
00124                            //  remains in 8 bit mode               |   remains in 8 bit mode       |  change to 8 bit mode
00125       wait_ms(15);         // 
00126 
00127       // Controller is now in 8 bit mode
00128 
00129       _writeNibble(0x2);   // Change to 4-bit mode (MSN), the LSN is undefined dummy
00130       wait_us(40);         // most instructions take 40us
00131 
00132       // Controller is now in 4-bit mode
00133       // Note: 4/8 bit mode is ignored for most native SPI and I2C devices. They dont use the parallel bus.
00134       //       However, _writeNibble() method is void anyway for native SPI and I2C devices.
00135     }      
00136    
00137     // Device specific initialisations: DC/DC converter to generate VLCD or VLED, number of lines etc
00138     switch (_ctrl) {
00139 
00140       case KS0073:
00141           // Initialise Display configuration
00142           switch (_type) {
00143             case LCD8x1:         //8x1 is a regular 1 line display
00144             case LCD12x1:                                
00145             case LCD16x1:                                            
00146             case LCD20x1:
00147             case LCD24x1:
00148 //            case LCD32x1:        // EXT pin is High, extension driver needed
00149 //            case LCD40x1:        // EXT pin is High, extension driver needed                        
00150               _function  = 0x02;    // Function set 001 DL N RE(0) DH REV (Std Regs)
00151                                     //   DL=0  (4 bits bus)             
00152                                     //    N=0  (1-line mode, N=1 2-line mode)
00153                                     //   RE=0  (Dis. Extended Regs, special mode for KS0073)
00154                                     //   DH=1  (Disp shift enable, special mode for KS0073)                                
00155                                     //   REV=0 (Reverse normal, special mode for KS0073)
00156                                     
00157               _function_1 = 0x04;   // Function set 001 DL N RE(1) BE LP (Ext Regs)
00158                                     //   DL=0  (4 bits bus)             
00159                                     //    N=0  (1-line mode, N=1 2-line mode)
00160                                     //   RE=1  (Ena Extended Regs, special mode for KS0073)
00161                                     //   BE=0  (Blink Enable, CG/SEG RAM, special mode for KS0073)                                
00162                                     //   LP=0  (LP=1 Low power mode, LP=0 Normal)
00163 
00164               _function_x = 0x00;   // Ext Function set 0000 1 FW BW NW (Ext Regs)
00165                                     //    NW=0  (1,2 line), NW=1 (4 Line, special mode for KS0073)                                
00166               break;                                
00167 
00168 //            case LCD12x3D:         // Special mode for KS0073, KS0078 and PCF21XX            
00169 //            case LCD12x3D1:        // Special mode for KS0073, KS0078 and PCF21XX            
00170             case LCD12x4D:         // Special mode for KS0073, KS0078 and PCF21XX            
00171 //            case LCD16x3D:         // Special mode for KS0073, KS0078             
00172 //            case LCD16x4D:         // Special mode for KS0073, KS0078            
00173             case LCD20x4D:         // Special mode for KS0073, KS0078            
00174               _function  = 0x02;    // Function set 001 DL N RE(0) DH REV (Std Regs)
00175                                     //   DL=0  (4 bits bus)             
00176                                     //    N=0  (dont care for 4 line mode)              
00177                                     //   RE=0  (Dis. Extended Regs, special mode for KS0073)
00178                                     //   DH=1  (Disp shift enable, special mode for KS0073)                                
00179                                     //   REV=0 (Reverse normal, special mode for KS0073)
00180                                     
00181               _function_1 = 0x04;   // Function set 001 DL N RE(1) BE LP (Ext Regs)
00182                                     //   DL=0  (4 bits bus)             
00183                                     //    N=0  (1-line mode), N=1 (2-line mode)
00184                                     //   RE=1  (Ena Extended Regs, special mode for KS0073)
00185                                     //   BE=0  (Blink Enable, CG/SEG RAM, special mode for KS0073)                                
00186                                     //   LP=0  (LP=1 Low power mode, LP=0 Normal)                                    
00187 
00188               _function_x = 0x01;   // Ext Function set 0000 1 FW BW NW (Ext Regs)
00189                                     //    NW=0  (1,2 line), NW=1 (4 Line, special mode for KS0073)                                
00190               break;                                
00191 
00192 
00193             case LCD16x3G:            // Special mode for ST7036                        
00194 //            case LCD24x3D:         // Special mode for KS0078
00195 //            case LCD24x3D1:        // Special mode for KS0078
00196             case LCD24x4D:         // Special mode for KS0078
00197               error("Error: LCD Controller type does not support this Display type\n\r"); 
00198               break;  
00199 
00200             default:
00201               // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)            
00202               _function  = 0x0A;    // Function set 001 DL N RE(0) DH REV (Std Regs)
00203                                     //   DL=0  (4 bits bus)             
00204                                     //    N=1  (2-line mode), N=0 (1-line mode)
00205                                     //   RE=0  (Dis. Extended Regs, special mode for KS0073)
00206                                     //   DH=1  (Disp shift enable, special mode for KS0073)                                
00207                                     //   REV=0 (Reverse normal, special mode for KS0073)
00208                                     
00209               _function_1 = 0x0C;   // Function set 001 DL N RE(1) BE LP (Ext Regs)
00210                                     //   DL=0  (4 bits bus)             
00211                                     //    N=1  (2 line mode), N=0 (1-line mode)
00212                                     //   RE=1  (Ena Extended Regs, special mode for KS0073)
00213                                     //   BE=0  (Blink Enable, CG/SEG RAM, special mode for KS0073)                   
00214                                     //   LP=0  (LP=1 Low power mode, LP=0 Normal)                                                                                     
00215 
00216               _function_x = 0x00;   // Ext Function set 0000 1 FW BW NW (Ext Regs)
00217                                     //   NW=0  (1,2 line), NW=1 (4 Line, special mode for KS0073)                                
00218               break;
00219           } // switch type
00220 
00221           // init special features
00222           _writeCommand(0x20 | _function_1);// Function set 001 DL N RE(1) BE LP (Ext Regs)
00223                                            //   DL=0 (4 bits bus), DL=1 (8 bits mode)            
00224                                            //    N=0 (1 line mode), N=1 (2 line mode)
00225                                            //   RE=1 (Ena Extended Regs, special mode for KS0073)
00226                                            //   BE=0 (Blink Enable/Disable, CG/SEG RAM, special mode for KS0073)                                
00227                                            //   LP=0  (LP=1 Low power mode, LP=0 Normal)                                                                                                                                
00228 
00229           _writeCommand(0x08 | _function_x); // Ext Function set 0000 1 FW BW NW (Ext Regs)
00230                                            //   FW=0  (5-dot font, special mode for KS0073)
00231                                            //   BW=0  (Cur BW invert disable, special mode for KS0073)
00232                                            //   NW=0  (1,2 Line), NW=1 (4 line, special mode for KS0073)                                
00233 
00234           _writeCommand(0x10);             // Scroll/Shift set 0001 DS/HS4 DS/HS3 DS/HS2 DS/HS1 (Ext Regs)
00235                                            //   Dotscroll/Display shift enable (Special mode for KS0073)
00236 
00237           _writeCommand(0x80);             // Scroll Quantity set 1 0 SQ5 SQ4 SQ3 SQ2 SQ1 SQ0 (Ext Regs)
00238                                            //   Scroll quantity (Special mode for KS0073)
00239 
00240           _writeCommand(0x20 | _function); // Function set 001 DL N RE(0) DH REV (Std Regs)
00241                                            //   DL=0  (4 bits bus), DL=1 (8 bits mode)             
00242                                            //    N=0  (1 line mode), N=1 (2 line mode)
00243                                            //   RE=0  (Dis. Extended Regs, special mode for KS0073)
00244                                            //   DH=1  (Disp shift enable/disable, special mode for KS0073)                                
00245                                            //   REV=0 (Reverse/Normal, special mode for KS0073)
00246           break; // case KS0073 Controller
00247 
00248 
00249       case KS0078:
00250           // Initialise Display configuration
00251           switch (_type) {
00252             case LCD8x1:         //8x1 is a regular 1 line display
00253             case LCD8x2B:        //8x2B is a special case of 16x1
00254 //            case LCD12x1:                                
00255             case LCD16x1:                                            
00256 //            case LCD20x1:
00257             case LCD24x1:
00258               _function  = 0x02;    // Function set 001 DL N RE(0) DH REV (Std Regs)
00259                                     //   DL=0  (4 bits bus)             
00260                                     //    N=0  (1 line mode), N=1 (2 line mode)
00261                                     //   RE=0  (Dis. Extended Regs, special mode for KS0078)
00262                                     //   DH=1  (Disp shift enable, special mode for KS0078)                                
00263                                     //   REV=0 (Reverse normal, special mode for KS0078)
00264                                     
00265               _function_1 = 0x04;   // Function set 001 DL N RE(1) BE 0 (Ext Regs)
00266                                     //   DL=0  (4 bits bus)             
00267                                     //    N=0  (1 line mode), N=1 (2 line mode)
00268                                     //   RE=1  (Ena Extended Regs, special mode for KS0078)
00269                                     //   BE=0  (Blink Enable, CG/SEG RAM, special mode for KS0078)                                
00270                                     //      0 
00271 
00272               _function_x = 0x00;   // Ext Function set 0000 1 FW BW NW (Ext Regs)
00273                                     //    NW=0  (1,2 line), NW=1 (4 Line, special mode for KS0078)                                
00274               break;                                
00275 
00276 //            case LCD12x3D:         // Special mode for KS0073, KS0078 and PCF21XX            
00277 //            case LCD12x3D1:        // Special mode for KS0073, KS0078 and PCF21XX            
00278 //            case LCD12x4D:         // Special mode for KS0073, KS0078 and PCF21XX            
00279 //            case LCD16x3D:         // Special mode for KS0073, KS0078             
00280 //            case LCD16x4D:         // Special mode for KS0073, KS0078            
00281 //            case LCD20x4D:         // Special mode for KS0073, KS0078            
00282 //            case LCD24x3D:         // Special mode for KS0078
00283 //            case LCD24x3D1:        // Special mode for KS0078
00284             case LCD24x4D:         // Special mode for KS0078
00285               _function  = 0x02;    // Function set 001 DL N RE(0) DH REV (Std Regs)
00286                                     //   DL=0  (4 bits bus)             
00287                                     //    N=0  (dont care for 4 line mode)              
00288                                     //   RE=0  (Dis. Extended Regs, special mode for KS0078)
00289                                     //   DH=1  (Disp shift enable, special mode for KS0078)                                
00290                                     //   REV=0 (Reverse normal, special mode for KS0078)
00291                                     
00292               _function_1 = 0x04;   // Function set 001 DL N RE(1) BE 0 (Ext Regs)
00293                                     //   DL=0  (4 bits bus)             
00294                                     //    N=0  (1 line mode), N=1 (2 line mode)
00295                                     //   RE=1  (Ena Extended Regs, special mode for KS0078)
00296                                     //   BE=0  (Blink Enable, CG/SEG RAM, special mode for KS0078)                                
00297                                     //      0 
00298 
00299               _function_x = 0x01;   // Ext Function set 0000 1 FW BW NW (Ext Regs)
00300                                     //    NW=0  (1,2 line), NW=1 (4 Line, special mode for KS0078)                                
00301               break;                                
00302 
00303             case LCD16x3G:            // Special mode for ST7036                        
00304               error("Error: LCD Controller type does not support this Display type\n\r"); 
00305               break;  
00306               
00307             default:
00308               // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)            
00309               _function  = 0x0A;    // Function set 001 DL N RE(0) DH REV (Std Regs)
00310                                     //   DL=0  (4 bits bus)             
00311                                     //    N=1  (1 line mode), N=1 (2 line mode)
00312                                     //   RE=0  (Dis. Extended Regs, special mode for KS0078)
00313                                     //   DH=1  (Disp shift enable, special mode for KS0078)                                
00314                                     //   REV=0 (Reverse normal, special mode for KS0078)
00315                                     
00316               _function_1 = 0x0C;   // Function set 001 DL N RE(1) BE 0 (Ext Regs)
00317                                     //   DL=0  (4 bits bus)             
00318                                     //    N=1  (1 line mode), N=1 (2 line mode)
00319                                     //   RE=1  (Ena Extended Regs, special mode for KS0078)
00320                                     //   BE=0  (Blink Enable, CG/SEG RAM, special mode for KS0078)                                
00321                                     //      0 
00322 
00323               _function_x = 0x00;   // Ext Function set 0000 1 FW BW NW (Ext Regs)
00324                                     //   NW=0  (1,2 line), NW=1 (4 Line, special mode for KS0078)                                
00325               break;
00326           } // switch type
00327 
00328           // init special features
00329           _writeCommand(0x20 | _function_1);// Function set 001 DL N RE(1) BE 0 (Ext Regs)
00330                                            //   DL=0 (4 bits bus), DL=1 (8 bits mode)            
00331                                            //    N=0 (1 line mode), N=1 (2 line mode)
00332                                            //   RE=1 (Ena Extended Regs, special mode for KS0078)
00333                                            //   BE=0 (Blink Enable/Disable, CG/SEG RAM, special mode for KS0078)                                
00334                                            //      0 
00335 
00336           _writeCommand(0x08 | _function_x); // Ext Function set 0000 1 FW BW NW (Ext Regs)
00337                                            //   FW=0  (5-dot font, special mode for KS0078)
00338                                            //   BW=0  (Cur BW invert disable, special mode for KS0078)
00339                                            //   NW=0  (1,2 Line), NW=1 (4 line, special mode for KS0078)                                
00340 
00341           _writeCommand(0x10);             // Scroll/Shift set 0001 DS/HS4 DS/HS3 DS/HS2 DS/HS1 (Ext Regs)
00342                                            //   Dotscroll/Display shift enable (Special mode for KS0078)
00343 
00344           _writeCommand(0x80);             // Scroll Quantity set 1 0 SQ5 SQ4 SQ3 SQ2 SQ1 SQ0 (Ext Regs)
00345                                            //   Scroll quantity (Special mode for KS0078)
00346 
00347           _writeCommand(0x20 | _function); // Function set 001 DL N RE(0) DH REV (Std Regs)
00348                                            //   DL=0  (4 bits bus), DL=1 (8 bits mode)             
00349                                            //    N=0  (1 line mode), N=1 (2 line mode)
00350                                            //   RE=0  (Dis. Extended Regs, special mode for KS0078)
00351                                            //   DH=1  (Disp shift enable/disable, special mode for KS0078)                                
00352                                            //   REV=0 (Reverse/Normal, special mode for KS0078)
00353           break; // case KS0078 Controller
00354               
00355       case ST7032_3V3:
00356           // ST7032 controller: Initialise Voltage booster for VLCD. VDD=3V3
00357       case ST7032_5V:
00358           // ST7032 controller: Disable Voltage booster for VLCD. VDD=5V
00359 
00360           // Initialise Display configuration
00361           switch (_type) {
00362             case LCD8x1:         //8x1 is a regular 1 line display
00363             case LCD8x2B:        //8x2B is a special case of 16x1
00364 //            case LCD12x1:                                
00365             case LCD16x1:                                            
00366 //            case LCD20x1:                    
00367             case LCD24x1:
00368               _function = 0x00;       // FUNCTION SET 0 0 1 DL=0 (4 bit), N=0 (1-line display mode), F=0 (5*7dot), 0, IS
00369                                       // Note: 4 bit mode is ignored for native SPI and I2C devices
00370                                       // Saved to allow switch between Instruction sets at later time
00371               break;  
00372 
00373             case LCD12x3D:            // Special mode for KS0078 and PCF21XX
00374             case LCD12x3D1:           // Special mode for KS0078 and PCF21XX
00375             case LCD12x4D:            // Special mode for KS0078 and PCF21XX
00376             case LCD16x3G:            // Special mode for ST7036                        
00377             case LCD24x4D:            // Special mode for KS0078
00378               error("Error: LCD Controller type does not support this Display type\n\r"); 
00379               break;  
00380 
00381             default:
00382               // All other LCD types are initialised as 2 Line displays        
00383               _function = 0x08;       // FUNCTION SET 0 0 1 DL=0 (4 bit), N=1 (2-line display mode), F=0 (5*7dot), 0, IS              
00384                                       // Note: 4 bit mode is ignored for native SPI and I2C devices
00385                                       // Saved to allow switch between Instruction sets at later time
00386               break;                                                                        
00387           } // switch type    
00388                                      
00389           // init special features 
00390           _writeCommand(0x20 | _function | 0x01);           // Set function,  0 0 1 DL N F 0 IS=1 Select Instr Set = 1              
00391 
00392           _writeCommand(0x1C);                              // Internal OSC frequency adjustment Framefreq=183HZ, Bias will be 1/4 (Instr Set=1)
00393 
00394           _contrast = LCD_ST7032_CONTRAST;              
00395           _writeCommand(0x70 | (_contrast & 0x0F));         // Set Contrast Low bits, 0 1 1 1 C3 C2 C1 C0 (IS=1)
00396 
00397 
00398           if (_ctrl == ST7032_3V3) {
00399 //            _icon_power = 0x04;                             // Icon display off, Booster circuit is turned on  (IS=1)
00400             _icon_power = 0x0C;                             // Icon display on, Booster circuit is turned on  (IS=1)            
00401                                                             // Saved to allow contrast change at later time
00402           }
00403           else { 
00404 //            _icon_power = 0x00;                             // Icon display off, Booster circuit is turned off  (IS=1)
00405             _icon_power = 0x08;                             // Icon display on, Booster circuit is turned off  (IS=1)            
00406                                                             // Saved to allow contrast change at later time
00407           }
00408           _writeCommand(0x50 | _icon_power | ((_contrast >> 4) & 0x03));  // Set Icon, Booster and Contrast High bits, 0 1 0 1 Ion Bon C5 C4 (IS=1)
00409           wait_ms(10);            // Wait 10ms to ensure powered up
00410           
00411           _writeCommand(0x68 | (LCD_ST7032_RAB & 0x07));      // Voltage follower, 0 1 1 0 FOn=1, Ampl ratio Rab2=1, Rab1=0, Rab0=0  (IS=1)
00412           wait_ms(10);            // Wait 10ms to ensure powered up
00413           
00414           _writeCommand(0x20 | _function);                  // Select Instruction Set = 0
00415 
00416           break; // case ST7032_3V3 Controller
00417                  // case ST7032_5V Controller
00418 
00419       case ST7036_3V3:
00420           // ST7036 controller: Initialise Voltage booster for VLCD. VDD=3V3
00421           // Note: supports 1,2 (LCD_T_A) or 3 lines (LCD_T_G)
00422       case ST7036_5V:
00423           // ST7036 controller: Disable Voltage booster for VLCD. VDD=5V
00424           // Note: supports 1,2 (LCD_T_A) or 3 lines (LCD_T_G)
00425                     
00426           // Initialise Display configuration
00427           switch (_type) {
00428             case LCD8x1:         //8x1 is a regular 1 line display
00429             case LCD8x2B:        //8x2D is a special case of 16x1
00430 //            case LCD12x1:                                
00431             case LCD16x1:   
00432             case LCD24x1:                                                                         
00433               _function = 0x00;     // Set function, 0 0 1 DL=0 (4-bit Databus), N=0 (1 Line), DH=0 (5x7font), IS2, IS1 (Select Instruction Set)
00434                                     // Note: 4 bit mode is ignored for native SPI and I2C devices
00435                                     // Saved to allow switch between Instruction sets at later time
00436               
00437               _bias_lines = 0x04;   // Bias: 1/5, 1 or 2-Lines LCD 
00438               break;  
00439 
00440 //            case LCD12x3G:          // Special mode for ST7036
00441             case LCD16x3G:          // Special mode for ST7036
00442               _function = 0x08;     // Set function, 0 0 1 DL=0 (4-bit Databus), N=1 (2 Line), DH=0 (5x7font), IS2,IS1 (Select Instruction Set)              
00443                                     // Note: 4 bit mode is ignored for native SPI and I2C devices
00444                                     // Saved to allow switch between Instruction sets at later time
00445               
00446               _bias_lines = 0x05;   // Bias: 1/5, 3-Lines LCD           
00447               break;  
00448 
00449 //            case LCD12x3D1:           // Special mode for KS0078 and PCF21XX
00450 //            case LCD16x3D1:           // Special mode for SSD1803
00451             case LCD12x4D:            // Special mode for PCF2116
00452             case LCD24x4D:            // Special mode for KS0078
00453               error("Error: LCD Controller type does not support this Display type\n\r"); 
00454               break;  
00455 
00456             default:
00457               // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)       
00458               _function = 0x08;     // Set function, 0 0 1 DL=0 (4-bit Databus), N=1 (2 Line), DH=0 (5x7font), IS2,IS1 (Select Instruction Set)
00459                                     // Note: 4 bit mode is ignored for native SPI and I2C devices
00460                                     // Saved to allow switch between Instruction sets at later time
00461               
00462               _bias_lines = 0x04;   // Bias: 1/5, 1 or 2-Lines LCD 
00463               break;                
00464           } // switch type
00465 
00466 
00467           // init special features 
00468           _writeCommand(0x20 | _function | 0x01);   // Set function, IS2,IS1 = 01 (Select Instr Set = 1)
00469           _writeCommand(0x10 | _bias_lines);        // Set Bias and 1,2 or 3 lines (Instr Set 1)
00470 
00471           _contrast = LCD_ST7036_CONTRAST;
00472           _writeCommand(0x70 | (_contrast & 0x0F)); // Set Contrast, 0 1 1 1 C3 C2 C1 C0 (Instr Set 1)
00473                            
00474           if (_ctrl == ST7036_3V3) {
00475             _icon_power = 0x0C;                       // Set Icon, Booster, Contrast High bits, 0 1 0 1 Ion=1 Bon=1 C5 C4 (Instr Set 1)            
00476 //            _icon_power = 0x04;                       // Set Icon, Booster, Contrast High bits, 0 1 0 1 Ion=0 Bon=1 C5 C4 (Instr Set 1)
00477                                                       // Saved to allow contrast change at later time
00478           } 
00479           else {
00480             _icon_power = 0x08;                       // Set Icon, Booster, Contrast High bits, 0 1 0 1 Ion=1 Bon=0 C5 C4 (Instr Set 1)             
00481 //            _icon_power = 0x00;                       // Set Icon, Booster, Contrast High bits, 0 1 0 1 Ion=0 Bon=0 C5 C4 (Instr Set 1)                         
00482           }
00483           
00484           _writeCommand(0x50 | _icon_power | ((_contrast >> 4) & 0x03));   // Set Contrast C5, C4 (Instr Set 1)
00485           wait_ms(10);            // Wait 10ms to ensure powered up
00486 
00487           _writeCommand(0x68 | (LCD_ST7036_RAB & 0x07));  // Voltagefollower On = 1, Ampl ratio Rab2, Rab1, Rab0 = 1 0 1 (Instr Set 1)
00488           wait_ms(10);            // Wait 10ms to ensure powered up
00489 
00490           _writeCommand(0x20 | _function);          // Set function, IS2,IS1 = 00 (Select Instruction Set = 0)
00491          
00492           break; // case ST7036_3V3 Controller
00493                  // case ST7036_5V Controller
00494 
00495       case ST7070:                   
00496           // Initialise Display configuration
00497           switch (_type) {
00498             case LCD8x1:         //8x1 is a regular 1 line display
00499             case LCD8x2B:        //8x2D is a special case of 16x1
00500 //            case LCD12x1:                                
00501             case LCD16x1:   
00502             case LCD24x1:                                                                         
00503               _function = dl | 0x00;      // Set function, 0 0 1 DL=0 (4-bit Databus), N=0 (1 Line), EXT=0, x, x 
00504                                           // Note: 4 bit mode is NOT ignored for native SPI !
00505                                           // Saved to allow switch between Instruction sets at later time
00506               break;  
00507 
00508 //            case LCD12x3D1:           // Special mode for KS0078 and PCF21XX
00509 //            case LCD16x3D1:           // Special mode for SSD1803
00510             case LCD12x4D:            // Special mode for PCF2116
00511             case LCD24x4D:            // Special mode for KS0078
00512 //            case LCD12x3G:          // Special mode for ST7036
00513             case LCD16x3G:          // Special mode for ST7036           
00514               error("Error: LCD Controller type does not support this Display type\n\r"); 
00515               break;  
00516 
00517             default:
00518               // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)       
00519               _function = dl | 0x08;   // Set function, 0 0 1 DL, N=1 (2 Line), EXT=0, x, x                                                                 
00520                                        // Note: 4 bit mode is NOT ignored for native SPI !
00521                                        // Saved to allow switch between Instruction sets at later time
00522               break;                
00523           } // switch type
00524 
00525 //          _writeCommand(0x00);                      // NOP, make sure to sync SPI
00526           
00527           // init special features                                                    
00528           _writeCommand(0x20 | _function | 0x04);   // Set function, 0 0 1 DL N EXT=1 x x (Select Instr Set = 1)
00529 
00530           _writeCommand(0x04 | 0x00);               // Set Bias resistors  0 0 0 0 0 1 Rb1,Rb0= 0 0 (Extern Res) (Instr Set 1)
00531 
00532           _writeCommand(0x40 | 0x00);               // COM/SEG directions 0 1 0 0 C1, C2, S1, S2  (Instr Set 1)
00533                                                     // C1=1: Com1-8 -> Com8-1;   C2=1: Com9-16 -> Com16-9
00534                                                     // S1=1: Seg1-40 -> Seg40-1; S2=1: Seg41-80 -> Seg80-41                                                    
00535           
00536           _writeCommand(0x20 | _function);          // Set function, EXT=0 (Select Instr Set = 0)
00537          
00538           break; // case ST7070 Controller
00539          
00540       case SSD1803_3V3:
00541           // SSD1803 controller: Initialise Voltage booster for VLCD. VDD=3V3
00542           // Note: supports 1,2, 3 or 4 lines
00543 //      case SSD1803_5V:
00544           // SSD1803 controller: No Voltage booster for VLCD. VDD=5V
00545                     
00546           // Initialise Display configuration
00547           switch (_type) {
00548             case LCD8x1:         //8x1 is a regular 1 line display
00549             case LCD8x2B:        //8x2D is a special case of 16x1
00550 //            case LCD12x1:                                
00551             case LCD16x1:   
00552             case LCD24x1:                                                                         
00553               _function = 0x00;     //  Set function 0 0 1 DL N DH RE(0) IS 
00554                                     //  Saved to allow switch between Instruction sets at later time
00555                                     //    DL=0 4-bit Databus,
00556                                     //         Note: 4 bit mode is ignored for native SPI and I2C devices
00557                                     //     N=0 1 Line / 3 Line
00558                                     //    DH=0 Double Height disable 
00559                                     //    IS=0
00560           
00561               _function_1 = 0x02;   // Set function, 0 0 1 DL N BE RE(1) REV
00562                                     //  Saved to allow switch between Instruction sets at later time
00563                                     //    DL=0 4-bit Databus,
00564                                     //         Note: 4 bit mode is ignored for native SPI and I2C devices
00565                                     //     N=0 1 Line / 3 Line
00566                                     //    BE=0 Blink Enable off, special feature of SSD1803
00567                                     //   REV=0 Reverse off, special feature of SSD1803            
00568                         
00569               _lines = 0x00;        // Ext function set 0 0 0 0 1 FW BW NW 
00570                                     //    NW=0 1-Line LCD (N=0)
00571               break;  
00572 
00573             case LCD12x3D:          // Special mode for KS0078 and PCF21XX                                  
00574 //            case LCD12x3D1:           // Special mode for KS0078 and PCF21XX            
00575             case LCD16x3D:          // Special mode for KS0078
00576 //            case LCD16x3D1:           // Special mode for SSD1803
00577 //            case LCD20x3D:            // Special mode for SSD1803
00578               _function = 0x00;     //  Set function 0 0 1 DL N DH RE(0) IS 
00579                                     //  Saved to allow switch between Instruction sets at later time
00580                                     //    DL=0 4-bit Databus,
00581                                     //         Note: 4 bit mode is ignored for native SPI and I2C devices
00582                                     //     N=0 1 Line / 3 Line
00583                                     //    DH=0 Double Height disable 
00584                                     //    IS=0
00585           
00586               _function_1 = 0x02;   // Set function, 0 0 1 DL N BE RE(1) REV
00587                                     //  Saved to allow switch between Instruction sets at later time
00588                                     //    DL=0 4-bit Databus,
00589                                     //         Note: 4 bit mode is ignored for native SPI and I2C devices
00590                                     //     N=0 1 Line / 3 Line
00591                                     //    BE=0 Blink Enable off, special feature of SSD1803
00592                                     //   REV=0 Reverse off, special feature of SSD1803            
00593                         
00594               _lines = 0x00;        // Ext function set 0 0 0 0 1 FW BW NW 
00595                                     //    NW=1 3-Line LCD (N=0)
00596               break;  
00597 
00598             case LCD20x4D:          // Special mode for SSD1803
00599               _function = 0x08;     //  Set function 0 0 1 DL N DH RE(0) IS 
00600                                     //  Saved to allow switch between Instruction sets at later time
00601                                     //    DL=0 4-bit Databus,
00602                                     //         Note: 4 bit mode is ignored for native SPI and I2C devices
00603                                     //     N=1 4 Line
00604                                     //    DH=0 Double Height disable 
00605                                     //    IS=0
00606           
00607               _function_1 = 0x0A;   // Set function, 0 0 1 DL N BE RE(1) REV
00608                                     //  Saved to allow switch between Instruction sets at later time
00609                                     //    DL=0 4-bit Databus,
00610                                     //         Note: 4 bit mode is ignored for native SPI and I2C devices
00611                                     //     N=1 4 Line
00612                                     //    BE=0 Blink Enable off, special feature of SSD1803
00613                                     //   REV=0 Reverse off, special feature of SSD1803            
00614                         
00615               _lines = 0x01;        // Ext function set 0 0 0 0 1 FW BW NW 
00616                                     //    NW=1 4-Line LCD (N=1)
00617               break;  
00618 
00619             case LCD16x3G:          // Special mode for ST7036            
00620             case LCD24x4D:          // Special mode for KS0078
00621               error("Error: LCD Controller type does not support this Display type\n\r"); 
00622               break;  
00623 
00624             default:
00625               // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)       
00626               _function = 0x08;     //  Set function 0 0 1 DL N DH RE(0) IS 
00627                                     //  Saved to allow switch between Instruction sets at later time
00628                                     //    DL=0 4-bit Databus,
00629                                     //         Note: 4 bit mode is ignored for native SPI and I2C devices
00630                                     //     N=1 2 line / 4 Line
00631                                     //    DH=0 Double Height disable 
00632                                     //    RE=0
00633                                     //    IS=0
00634           
00635               _function_1 = 0x0A;   // Set function, 0 0 1 DL N BE RE(1) REV
00636                                     //  Saved to allow switch between Instruction sets at later time
00637                                     //    DL=0 4-bit Databus,
00638                                     //         Note: 4 bit mode is ignored for native SPI and I2C devices
00639                                     //     N=1 2 line / 4 Line
00640                                     //    BE=0 Blink Enable off, special feature of SSD1803
00641                                     //    RE=1
00642                                     //   REV=0 Reverse off, special feature of SSD1803            
00643                         
00644               _lines = 0x00;        // Ext function set 0 0 0 0 1 FW BW NW 
00645                                     //    NW=0 2-Line LCD (N=1)
00646               break;                
00647           } // switch type
00648 
00649 
00650           // init special features 
00651           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 DL N BE RE(1) REV 
00652                                                     // Select Extended Instruction Set
00653           
00654           _writeCommand(0x06);                      // Set ext entry mode, 0 0 0 0 0 1 BDC=1 COM1-32, BDS=0 SEG100-1    "Bottom View" (Ext Instr Set)
00655 //          _writeCommand(0x05);                      // Set ext entry mode, 0 0 0 0 0 1 BDC=0 COM32-1, BDS=1 SEG1-100    "Top View" (Ext Instr Set)          
00656           wait_ms(5);                               // Wait to ensure completion or SSD1803 fails to set Top/Bottom after reset..
00657          
00658           _writeCommand(0x08 | _lines);             // Set ext function 0 0 0 0 1 FW BW NW 1,2,3 or 4 lines (Ext Instr Set)
00659 
00660           _writeCommand(0x10);                      // Double Height and Bias, 0 0 0 1 UD2=0, UD1=0, BS1=0 Bias 1/5, DH=0 (Ext Instr Set)
00661 
00662 //          _writeCommand(0x76);                      // Set TC Control, 0 1 1 1 0 1 1 0 (Ext Instr Set)
00663 //          _writeData(0x02);                         // Set TC data,    0 0 0 0 0 TC2,TC1,TC0 = 0 1 0 (Ext Instr Set)
00664 
00665           _writeCommand(0x20 | _function | 0x01);   // Set function, 0 0 1 DL N DH RE(0) IS=1 Select Instruction Set 1
00666                                                     // Select Std Instr set, Select IS=1  
00667 
00668           _contrast = LCD_SSD1_CONTRAST;
00669           _writeCommand(0x70 | (_contrast & 0x0F)); // Set Contrast 0 1 1 1 C3, C2, C1, C0 (Instr Set 1)
00670                            
00671 //          _icon_power = 0x04;                       // Icon off, Booster on (Instr Set 1)
00672           _icon_power = 0x0C;                       // Icon on, Booster on (Instr Set 1)          
00673                                                     // Saved to allow contrast change at later time
00674           _writeCommand(0x50 | _icon_power | ((_contrast >> 4) & 0x03));   // Set Power, Icon and Contrast, 0 1 0 1 Ion Bon C5 C4 (Instr Set 1)
00675           wait_ms(10);            // Wait 10ms to ensure powered up
00676 
00677           _writeCommand(0x68 | (LCD_SSD1_RAB & 0x07));  // Set Voltagefollower 0 1 1 0 Don = 1, Ampl ratio Rab2, Rab1, Rab0 = 1 1 0  (Instr Set 1)
00678           wait_ms(10);            // Wait 10ms to ensure powered up
00679 
00680           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 DL N BE RE(1) REV 
00681                                                     // Select Extended Instruction Set 1
00682           _writeCommand(0x10);                      // Shift/Scroll enable, 0 0 0 1 DS4/HS4 DS3/HS3 DS2/HS2 DS1/HS1  (Ext Instr Set 1)
00683 
00684 
00685           _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
00686                                                     // Select Std Instr set, Select IS=0
00687          
00688           break; // case SSD1803 Controller
00689 
00690           
00691       // Note1: The PCF21XX family of controllers has several types that dont have an onboard voltage generator for V-LCD.
00692       //        You must supply this LCD voltage externally and not try to enable VGen. 
00693       // Note2: The early versions of PCF2116 controllers (eg PCF2116C) can not generate sufficiently negative voltage for the LCD at a VDD of 3V3. 
00694       //        You must supply this voltage externally and not enable VGen or you must use a higher VDD (e.g. 5V) and enable VGen.
00695       //        More recent versions of the controller (eg PCF2116K) have an improved VGen that will work with 3V3.
00696       // Note3: See datasheet, PCF2116 and other types provide a V0 pin to control the LCD contrast voltage that is provided by VGen. This pins allows 
00697       //        contrast control similar to that of pin 3 on the standard 14pin LCD module connector.
00698       //        You can disable VGen by connecting Vo to VDD. VLCD will then be used directly as LCD voltage.
00699       // Note4: PCF2113 and PCF2119 are different wrt to VLCD generator! There is no V0 pin. The contrast voltage is software controlled by setting the VA and VB registers.
00700       //        Vgen is automatically switched off when the contrast voltage VA or VB is set to 0x00. Note that certain limits apply to allowed values for VA and VB.                    
00701       // Note5: See datasheet, members of the PCF21XX family support different numbers of rows/columns. Not all can support 3 or 4 rows.
00702       // Note6: See datasheet, the PCF21XX-C and PCF21XX-K use a non-standard character set. This may result is strange looking text when not corrected..
00703 
00704       case PCF2103_3V3:
00705           // PCF2103 controller: No Voltage generator for VLCD, VDD=3V3..5V, VLCD input controls contrast voltage.                 
00706           // Initialise Display configuration
00707           switch (_type) {
00708             case LCD24x1:                    
00709               _function = 0x00;       //FUNCTION SET 0 0 1 DL=0 4-bit, 0, M=0 1-line/24 chars display mode, 0, H=0 
00710                                       //Note: 4 bit mode is ignored for I2C mode
00711               break;  
00712 
00713 //            case LCD12x1D:            //Special mode for PCF21XX, Only top line used
00714             case LCD12x2:
00715               _function = 0x04;       //FUNCTION SET 0 0 1 DL=0 4-bit, 0, M=1 2-line/12 chars display mode, 0, H=0
00716                                       //Note: 4 bit mode is ignored for I2C mode
00717               break;  
00718               
00719             default:
00720               error("Error: LCD Controller type does not support this Display type\n\r"); 
00721               break;  
00722             
00723           } // switch type    
00724 
00725           _writeCommand(0x20 | _function | 0x01);          // Set function, Select Instr Set = 1              
00726           wait_ms(10);            // Wait 10ms to ensure powered up                                                    
00727 
00728 // Note: Display from GA628 shows 12 chars. This is actually the right half of a 24x1 display. The commons have been connected in reverse order.
00729           _writeCommand(0x05);                             // Display Conf Set         0000 0, 1, P=0, Q=1               (Instr. Set 1)
00730                                                         
00731           _writeCommand(0x02);                             // Screen Config            0000 001, L=0  (Instr. Set 1)
00732           _writeCommand(0x08);                             // ICON Conf                0000 1, IM=0 (Char mode), IB=0 (no Icon blink), 0 (Instr. Set 1) 
00733 
00734           _writeCommand(0x20 | _function);                 // Set function, Select Instr Set = 0             
00735           
00736 #if(0)
00737           // Select CG RAM
00738           _writeCommand(0x40); //Set CG-RAM address, 8 sequential locations needed per UDC
00739           // Store UDC/Icon pattern: 
00740           //   3 x 8 rows x 5 bits = 120 bits for Normal pattern (UDC 0..2) and
00741           //   3 x 8 rows x 5 bits = 120 bits for Blink pattern (UDC 4..6) 
00742           for (int i=0; i<(8 * 8); i++) {
00743 //            _writeData(0x1F);  // All On
00744             _writeData(0x00);  // All Off            
00745           }
00746 #endif
00747           break; // case PCF2103_3V3 Controller
00748 
00749       case PCF2113_3V3:
00750           // PCF2113 controller: Initialise Voltage booster for VLCD. VDD=3V3. VA and VB control contrast.
00751           // Initialise Display configuration
00752           switch (_type) {
00753 //            case LCD12x1:                                
00754 //              _function = 0x02;       // FUNCTION SET 0 0 1 DL=0 4 bit, 0, M=0 1-line/12 chars display mode, SL=1, IS=0
00755                                       // Note: 4 bit mode is ignored for I2C mode
00756             case LCD24x1:                    
00757               _function = 0x00;       // FUNCTION SET 0 0 1 DL=0 4 bit, 0, M=0 1-line/24 chars display mode, SL=0, IS=0            
00758                                       // Note: 4 bit mode is ignored for I2C mode
00759               break;  
00760 
00761             case LCD12x2:                    
00762               _function = 0x04;       // FUNCTION SET 0 0 1 DL=0 4 bit, 0, M=1 2-line/12 chars display mode, SL=0, IS=0            
00763               break;  
00764              
00765             default:
00766               error("Error: LCD Controller type does not support this Display type\n\r"); 
00767               break;  
00768                          
00769           } // switch type    
00770 
00771           // Init special features
00772           _writeCommand(0x20 | _function | 0x01);          // Set function, Select Instr Set = 1              
00773 
00774           _writeCommand(0x04);                             // Display Conf Set         0000 0, 1, P=0, Q=0               (Instr. Set 1)
00775           _writeCommand(0x10);                             // Temp Compensation Set    0001 0, 0, TC1=0, TC2=0           (Instr. Set 1)
00776 //          _writeCommand(0x42);                             // HV GEN                   0100 S1=1, S2=0 (2x multiplier)   (Instr. Set 1)
00777           _writeCommand(0x40 | (LCD_PCF2_S12 & 0x03));     // HV Gen                   0100 S1=1, S2=0 (2x multiplier)   (Instr. Set 1)
00778           
00779           _contrast = LCD_PCF2_CONTRAST;              
00780           _writeCommand(0x80 | 0x00 | (_contrast & 0x3F));      // VLCD_set (Instr. Set 1)  1, V=0, VA=contrast
00781           _writeCommand(0x80 | 0x40 | (_contrast & 0x3F));      // VLCD_set (Instr. Set 1)  1, V=1, VB=contrast
00782           wait_ms(10);            // Wait 10ms to ensure powered up
00783           
00784           _writeCommand(0x02);                             // Screen Config            0000 001, L=0  (Instr. Set 1)
00785           _writeCommand(0x08);                             // ICON Conf                0000 1, IM=0 (Char mode), IB=0 (no icon blink) DM=0 (no direct mode) (Instr. Set 1) 
00786 
00787           _writeCommand(0x20 | _function);                 // Set function, Select Instr Set = 0             
00788 
00789           break; // case PCF2113_3V3 Controller
00790 
00791 
00792 //      case PCF2113_5V:
00793           // PCF2113 controller: No Voltage generator for VLCD. VDD=5V. Contrast voltage controlled by VA or VB.
00794 //@TODO                            
00795 
00796 
00797       case PCF2116_3V3:
00798           // PCF2116 controller: Voltage generator for VLCD. VDD=5V. V0 controls contrast voltage.                 
00799           // Initialise Display configuration
00800           switch (_type) {
00801 //            case LCD12x1:
00802 //            case LCD12x2:                                                                            
00803             case LCD24x1:                    
00804               _writeCommand(0x22);    //FUNCTION SET 0 0 1 DL=0 4-bit, N=0/M=0 1-line/24 chars display mode, G=1 Vgen on, 0 
00805                                       //Note: 4 bit mode is ignored for I2C mode
00806               wait_ms(10);            // Wait 10ms to ensure powered up                                                    
00807               break;  
00808 
00809             case LCD12x3D:            // Special mode for KS0078 and PCF21XX                            
00810             case LCD12x3D1:           // Special mode for PCF21XX                     
00811             case LCD12x4D:            // Special mode for PCF21XX:
00812               _writeCommand(0x2E);    //FUNCTION SET 0 0 1 DL=0 4-bit, N=1/M=1 4-line/12 chars display mode, G=1 VGen on, 0                               
00813                                       //Note: 4 bit mode is ignored for I2C mode              
00814               wait_ms(10);            // Wait 10ms to ensure powered up                                                    
00815               break;  
00816 
00817             case LCD24x2:
00818               _writeCommand(0x2A);    //FUNCTION SET 0 0 1 DL=0 4-bit, N=1/M=0 2-line/24 chars display mode, G=1 VGen on, 0
00819                                       //Note: 4 bit mode is ignored for I2C mode
00820               wait_ms(10);            // Wait 10ms to ensure powered up   
00821               break;  
00822               
00823             default:
00824               error("Error: LCD Controller type does not support this Display type\n\r"); 
00825               break;  
00826             
00827           } // switch type    
00828 
00829           break; // case PCF2116_3V3 Controller
00830 
00831 
00832 //Experimental for cellphone 3-line display, SA=0x74, No Ack supported, Character set C or K, DL = 8 bit, N=0,M=1 (reserved mode !!), external VLCD -2V5
00833 //@TODO                            
00834       case PCF2116_5V:
00835           // PCF2116 controller: No Voltage generator for VLCD. VDD=5V. V0 controls contrast voltage.                           
00836           // Initialise Display configuration
00837           switch (_type) {
00838 //            case LCD12x1:
00839 //            case LCD12x2:                                                                            
00840 //            case LCD24x1:                    
00841 //              _writeCommand(0x20);    //FUNCTION SET 0 0 1 DL=0 4-bit, N=0/M=0 1-line/24 chars display mode, G=0 no Vgen, 0 
00842                                       //Note: 4 bit mode is ignored for I2C mode
00843 //              wait_ms(10);            // Wait 10ms to ensure powered up                                                    
00844 //              break;  
00845 
00846             case LCD12x3D:            // Special mode for KS0078 and PCF21XX                            
00847             case LCD12x3D1:           // Special mode for PCF21XX                     
00848             case LCD12x4D:            // Special mode for PCF21XX:
00849 //              _writeCommand(0x34);    //FUNCTION SET 8 bit, N=0/M=1 4-line/12 chars display mode      OK
00850 //              _writeCommand(0x24);    //FUNCTION SET 4 bit, N=0/M=1 4-line/12 chars display mode      OK                                            
00851               _writeCommand(0x2C);    //FUNCTION SET 0 0 1 DL=0 4-bit, N=1/M=1 4-line/12 chars display mode, G=0 no Vgen, 0  OK       
00852                                       //Note: 4 bit mode is ignored for I2C mode              
00853               wait_ms(10);            // Wait 10ms to ensure powered up                                                    
00854               break;  
00855 
00856 //            case LCD24x2:
00857 //              _writeCommand(0x28);    //FUNCTION SET 4 bit, N=1/M=0 2-line/24 chars display mode
00858                                       //Note: 4 bit mode is ignored for I2C mode
00859 //              wait_ms(10);            // Wait 10ms to ensure powered up   
00860 //              break;  
00861               
00862             default:
00863               error("Error: LCD Controller type does not support this Display type\n\r"); 
00864               break;  
00865             
00866           } // switch type    
00867 
00868           break; // case PCF2116_5V Controller
00869 
00870       case PCF2119_3V3:
00871           // PCF2119 controller: Initialise Voltage booster for VLCD. VDD=3V3. VA and VB control contrast.
00872           // Note1: See datasheet, the PCF2119 supports icons and provides separate constrast control for Icons and characters.
00873           // Note2: Vgen is switched off when the contrast voltage VA or VB is set to 0x00.
00874                   
00875 //POR or Hardware Reset should be applied
00876           wait_ms(10);            // Wait 10ms to ensure powered up   
00877 
00878           // Initialise Display configuration
00879           switch (_type) {
00880             case LCD8x1:
00881 //            case LCD12x1:
00882             case LCD16x1:           
00883               _function = 0x02;       // FUNCTION SET 0 0 1 DL=0 4-bit, 0 , M=0 1-line/16 chars display mode, SL=1
00884                                       // Note: 4 bit mode is ignored for I2C mode
00885               break;  
00886             
00887             case LCD24x1:                    
00888 //            case LCD32x1:                                
00889               _function = 0x00;       // FUNCTION SET 0 0 1 DL=0 4-bit, 0 , M=0 1-line/32 chars display mode, SL=0
00890                                       // Note: 4 bit mode is ignored for I2C mode
00891               break;  
00892 
00893             case LCD8x2:
00894 //            case LCD12x2:            
00895             case LCD16x2:
00896               _function = 0x04;       // FUNCTION SET 0 0 1 DL=0 4-bit, 0, M=1 2-line/16 chars display mode, SL=0
00897                                       // Note: 4 bit mode is ignored for I2C mode
00898               break;  
00899              
00900             default:
00901               error("Error: LCD Controller type does not support this Display type\n\r"); 
00902               break;  
00903             
00904           } // switch type    
00905 
00906           // Init special features 
00907           _writeCommand(0x20 | _function | 0x01);           // Set function, Select Instruction Set = 1              
00908 
00909           _writeCommand(0x04);    // DISP CONF SET (Instr. Set 1)   0000, 0, 1, P=0, Q=0 
00910           _writeCommand(0x10);    // TEMP CTRL SET (Instr. Set 1)   0001, 0, 0, TC1=0, TC2=0
00911 //          _writeCommand(0x42);    // HV GEN (Instr. Set 1)          0100, 0, 0, S1=1, S2=0 (2x multiplier)
00912           _writeCommand(0x40 | (LCD_PCF2_S12 & 0x03));      // HV GEN (Instr. Set 1)          0100, 0, 0, S1=1, S2=0 (2x multiplier)
00913 
00914           _contrast = LCD_PCF2_CONTRAST;              
00915           _writeCommand(0x80 | 0x00 | (_contrast & 0x3F));      // VLCD_set (Instr. Set 1)    V=0, VA=contrast
00916           _writeCommand(0x80 | 0x40 | (_contrast & 0x3F));      // VLCD_set (Instr. Set 1)    V=1, VB=contrast
00917           wait_ms(10);            // Wait 10ms to ensure powered up
00918           
00919           _writeCommand(0x02);    // SCRN CONF (Instr. Set 1)    L=0
00920           _writeCommand(0x08);    // ICON CONF (Instr. Set 1)    IM=0 (Char mode) IB=0 (no icon blink) DM=0 (no direct mode)
00921 
00922           _writeCommand(0x20 | _function);                  // Select Instruction Set = 0
00923 
00924           break; // case PCF2119_3V3 Controller
00925 
00926 //      case PCF2119_5V:
00927           // PCF2119 controller: No Voltage booster for VLCD. VDD=3V3. VA and VB control contrast.
00928           // Note1: See datasheet, the PCF2119 supports icons and provides separate constrast control for Icons and characters.
00929           // Note2: Vgen is switched off when the contrast voltage VA or VB is set to 0x00.                     
00930 //@TODO                            
00931 
00932       case WS0010:         
00933           // WS0010 OLED controller: Initialise DC/DC Voltage converter for LEDs
00934           // Note1: Identical to RS0010  
00935           // Note2: supports 1 or 2 lines (and 16x100 graphics)
00936           //        supports 4 fonts (English/Japanese (default), Western European-I, English/Russian, Western European-II)
00937                            // Cursor/Disp shift set 0001 SC RL  0 0
00938                            //
00939                            // Mode and Power set    0001 GC PWR 1 1                           
00940                            //  GC  = 0 (Graph Mode=1, Char Mode=0)             
00941                            //  PWR = 1 (DC/DC On/Off)
00942    
00943 //@Todo: This may be needed to enable a warm reboot
00944           //_writeCommand(0x13);   // Char mode, DC/DC off              
00945           //wait_ms(10);           // Wait 10ms to ensure powered down                  
00946           _writeCommand(0x17);   // Char mode, DC/DC on        
00947           wait_ms(10);           // Wait 10ms to ensure powered up        
00948 
00949           // Initialise Display configuration
00950           switch (_type) {                    
00951             case LCD8x1:         //8x1 is a regular 1 line display
00952             case LCD8x2B:        //8x2B is a special case of 16x1
00953 //            case LCD12x1:                                
00954             case LCD16x1:                                            
00955             case LCD24x1:
00956               _writeCommand(0x20); // Function set 001 DL N F FT1 FT0
00957                                    //  DL=0  (4 bits bus)             
00958                                    //   N=0  (1 line)
00959                                    //   F=0  (5x7 dots font)
00960                                    //  FT=00 (00 = Engl/Jap, 01 = WestEur1, 10 = Engl/Russian, 11 = WestEur2
00961               break;  
00962 
00963             case LCD12x3D:            // Special mode for KS0078 and PCF21XX                            
00964             case LCD12x3D1:           // Special mode for PCF21XX                     
00965             case LCD12x4D:            // Special mode for PCF21XX:
00966             case LCD16x3G:            // Special mode for ST7036            
00967             case LCD24x4D:            // Special mode for KS0078
00968               error("Error: LCD Controller type does not support this Display type\n\r"); 
00969               break;  
00970 
00971             default:
00972               // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)       
00973               _writeCommand(0x28); // Function set 001 DL N F FT1 FT0
00974                                    //  DL=0  (4 bits bus)
00975                                    //   N=1  (2 lines)
00976                                    //   F=0  (5x7 dots font)
00977                                    //  FT=00 (00 = Engl/Jap, 01 = WestEur1, 10 = Engl/Russian, 11 = WestEur2
00978 
00979               break;
00980            } // switch type
00981            
00982            break; // case WS0010 Controller
00983 
00984 
00985       case US2066_3V3:
00986           // US2066/SSD1311 OLED controller, Initialise for VDD=3V3
00987           // Note: supports 1,2, 3 or 4 lines
00988 //      case USS2066_5V:
00989           // US2066 controller, VDD=5V
00990                     
00991           // Initialise Display configuration
00992           switch (_type) {
00993             case LCD8x1:         //8x1 is a regular 1 line display
00994             case LCD8x2B:        //8x2D is a special case of 16x1
00995 //            case LCD12x1:                                
00996             case LCD16x1:   
00997 //            case LCD20x1:                                                                         
00998               _function = 0x00;     //  Set function 0 0 1 X N DH RE(0) IS 
00999                                     //  Saved to allow switch between Instruction sets at later time
01000                                     //    DL=X bit is ignored for US2066. Uses hardwired pins instead
01001                                     //     N=0 1 Line / 3 Line
01002                                     //    DH=0 Double Height disable 
01003                                     //    IS=0
01004           
01005               _function_1 = 0x02;   // Set function, 0 0 1 X N BE RE(1) REV
01006                                     //  Saved to allow switch between Instruction sets at later time
01007                                     //    DL=X bit is ignored for US2066. Uses hardwired pins instead                                    
01008                                     //     N=0 1 Line / 3 Line
01009                                     //    BE=0 Blink Enable off, special feature of SSD1803, US2066
01010                                     //   REV=0 Reverse off, special feature of SSD1803, US2066            
01011                         
01012               _lines = 0x00;        // Ext function set 0 0 0 0 1 FW BW NW 
01013                                     //    NW=0 1-Line LCD (N=0)
01014               break;  
01015 
01016             case LCD16x1C:
01017             case LCD8x2:
01018             case LCD16x2:
01019             case LCD20x2:            
01020               _function = 0x08;     //  Set function 0 0 1 X N DH RE(0) IS 
01021                                     //  Saved to allow switch between Instruction sets at later time
01022                                     //    DL=X bit is ignored for US2066. Uses hardwired pins instead                                                                        
01023                                     //     N=1 2 line / 4 Line
01024                                     //    DH=0 Double Height disable 
01025                                     //    IS=0
01026           
01027               _function_1 = 0x0A;   // Set function, 0 0 1 X N BE RE(1) REV
01028                                     //  Saved to allow switch between Instruction sets at later time
01029                                     //    DL=X bit is ignored for US2066. Uses hardwired pins instead                                                                        
01030                                     //     N=1 2 line / 4 Line
01031                                     //    BE=0 Blink Enable off, special feature of SSD1803, US2066
01032                                     //   REV=0 Reverse off, special feature of SSD1803, US2066            
01033                         
01034               _lines = 0x00;        // Ext function set 0 0 0 0 1 FW BW NW 
01035                                     //    NW=0 2-Line LCD (N=1)
01036               break;                
01037 
01038             case LCD12x3D:          // Special mode for KS0078 and PCF21XX 
01039 //            case LCD12x3D1:           // Special mode for KS0078 and PCF21XX            
01040             case LCD16x3D:          // Special mode for KS0078, SSD1803 and US2066
01041 //            case LCD16x3D1:           // Special mode for SSD1803, US2066
01042 //            case LCD20x3D:            // Special mode for SSD1803, US2066
01043               _function = 0x00;     //  Set function 0 0 1 X N DH RE(0) IS 
01044                                     //  Saved to allow switch between Instruction sets at later time
01045                                     //    DL=X bit is ignored for US2066. Uses hardwired pins instead                                    
01046                                     //     N=0 1 Line / 3 Line
01047                                     //    DH=0 Double Height disable 
01048                                     //    IS=0
01049           
01050               _function_1 = 0x02;   // Set function, 0 0 1 X N BE RE(1) REV
01051                                     //  Saved to allow switch between Instruction sets at later time
01052                                     //    DL=X bit is ignored for US2066. Uses hardwired pins instead                                    
01053                                     //     N=0 1 Line / 3 Line
01054                                     //    BE=0 Blink Enable off, special feature of SSD1803, US2066
01055                                     //   REV=0 Reverse off, special feature of SSD1803, US2066            
01056                         
01057               _lines = 0x00;        // Ext function set 0 0 0 0 1 FW BW NW 
01058                                     //    NW=1 3-Line LCD (N=0)
01059               break;  
01060 
01061             case LCD20x4D:          // Special mode for SSD1803, US2066
01062               _function = 0x08;     //  Set function 0 0 1 X N DH RE(0) IS 
01063                                     //  Saved to allow switch between Instruction sets at later time
01064                                     //    DL=X bit is ignored for US2066. Uses hardwired pins instead
01065                                     //     N=1 2 line / 4 Line
01066                                     //    DH=0 Double Height disable 
01067                                     //    IS=0
01068           
01069               _function_1 = 0x0A;   // Set function, 0 0 1 DL N BE RE(1) REV
01070                                     //  Saved to allow switch between Instruction sets at later time
01071                                     //    DL=0 bit is ignored for US2066. Uses hardwired pins instead                                    
01072                                     //     N=1 2 line / 4 Line
01073                                     //    BE=0 Blink Enable off, special feature of SSD1803, US2066
01074                                     //   REV=0 Reverse off, special feature of SSD1803, US2066            
01075                         
01076               _lines = 0x01;        // Ext function set 0 0 0 0 1 FW BW NW 
01077                                     //    NW=1 4-Line LCD (N=1)
01078               break;  
01079 
01080 //            case LCD24x1:                                                                         
01081 //            case LCD16x3G:          // Special mode for ST7036            
01082 //            case LCD24x4D:          // Special mode for KS0078
01083             default:            
01084               error("Error: LCD Controller type does not support this Display type\n\r"); 
01085               break;  
01086 
01087           } // switch type
01088 
01089           _writeCommand(0x00);                      // NOP, make sure to sync SPI
01090 
01091           // init special features 
01092           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 X N BE RE(1) REV 
01093                                                     // Select Extended Instruction Set
01094 
01095           _writeCommand(0x71);                      // Function Select A: 0 1 1 1 0 0 0 1 (Ext Instr Set)
01096           _writeData(0x00);                         // Disable Internal VDD
01097 
01098           _writeCommand(0x79);                      // Function Select OLED:  0 1 1 1 1 0 0 1 (Ext Instr Set)
01099 
01100           _writeCommand(0xD5);                      // Display Clock Divide Ratio: 1 1 0 1 0 1 0 1 (Ext Instr Set, OLED Instr Set)
01101           _writeCommand(0x70);                      // Display Clock Divide Ratio value: 0 1 1 1 0 0 0 0 (Ext Instr Set, OLED Instr Set)
01102                     
01103           _writeCommand(0x78);                      // Function Disable OLED: 0 1 1 1 1 0 0 0 (Ext Instr Set)
01104           
01105 //          _writeCommand(0x06);                      // Set ext entry mode, 0 0 0 0 0 1 BDC=1 COM1-32, BDS=0 SEG100-1    "Bottom View" (Ext Instr Set)
01106           _writeCommand(0x05);                      // Set ext entry mode, 0 0 0 0 0 1 BDC=0 COM32-1, BDS=1 SEG1-100    "Top View" (Ext Instr Set)          
01107          
01108           _writeCommand(0x08 | _lines);             // Set ext function 0 0 0 0 1 FW BW NW 1,2,3 or 4 lines (Ext Instr Set)
01109 
01110 //          _writeCommand(0x1C);                      // Double Height, 0 0 0 1 UD2=1, UD1=1, X, DH'=0 (Ext Instr Set)
01111 //                                                    // Default
01112 
01113           _writeCommand(0x72);                      // Function Select B: 0 1 1 1 0 0 1 0 (Ext Instr Set)
01114           _writeData(0x01);                         // Select ROM A (CGRAM 8, CGROM 248)
01115 
01116           _writeCommand(0x79);                      // Function Select OLED:  0 1 1 1 1 0 0 1 (Ext Instr Set)
01117 
01118           _writeCommand(0xDA);                      // Set Segm Pins Config:  1 1 0 1 1 0 1 0 (Ext Instr Set, OLED)
01119           _writeCommand(0x10);                      // Set Segm Pins Config value: Altern Odd/Even, Disable Remap (Ext Instr Set, OLED)
01120 
01121           _writeCommand(0xDC);                      // Function Select C: 1 1 0 1 1 1 0 0 (Ext Instr Set, OLED)
01122 //          _writeCommand(0x00);                      // Set internal VSL, GPIO pin HiZ (always read low)
01123           _writeCommand(0x80);                      // Set external VSL, GPIO pin HiZ (always read low)
01124 
01125           _contrast = LCD_US20_CONTRAST;
01126           _writeCommand(0x81);                      // Set Contrast Control: 1 0 0 0 0 0 0 1 (Ext Instr Set, OLED)
01127           _writeCommand((_contrast << 2) | 0x03);   // Set Contrast Value: 8 bits, use 6 bits for compatibility 
01128 
01129           _writeCommand(0xD9);                      // Set Phase Length: 1 1 0 1 1 0 0 1 (Ext Instr Set, OLED)
01130           _writeCommand(0xF1);                      // Set Phase Length Value: 
01131 
01132           _writeCommand(0xDB);                      // Set VCOMH Deselect Lvl: 1 1 0 1 1 0 1 1 (Ext Instr Set, OLED)
01133           _writeCommand(0x30);                      // Set VCOMH Deselect Value: 0.83 x VCC
01134 
01135           wait_ms(10);            // Wait 10ms to ensure powered up
01136 
01137 //Test Fade/Blinking. Hard Blink on/off, No fade in/out ??
01138 //          _writeCommand(0x23);                      // Set (Ext Instr Set, OLED)
01139 //          _writeCommand(0x3F);                      // Set interval 128 frames
01140 //End Test Blinking
01141 
01142           _writeCommand(0x78);                      // Function Disable OLED: 0 1 1 1 1 0 0 0 (Ext Instr Set)
01143           
01144           _writeCommand(0x20 | _function | 0x01);   // Set function, 0 0 1 X N DH RE(0) IS=1 Select Instruction Set 1
01145                                                     // Select Std Instr set, Select IS=1  
01146 
01147           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 X N BE RE(1) REV 
01148                                                     // Select Ext Instr Set, IS=1
01149           _writeCommand(0x10);                      // Shift/Scroll enable, 0 0 0 1 DS4/HS4 DS3/HS3 DS2/HS2 DS1/HS1  (Ext Instr Set, IS=1)
01150 
01151           _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
01152                                                     // Select Std Instr set, Select IS=0        
01153           break; // case US2066/SSD1311 Controller
01154 
01155       //not yet tested on hardware
01156       case PT6314 :
01157           // Initialise Display configuration
01158           switch (_type) {
01159             case LCD8x1:         //8x1 is a regular 1 line display
01160             case LCD8x2B:        //8x2B is a special case of 16x1
01161 //            case LCD12x1:                                
01162             case LCD16x1:                                            
01163             case LCD20x1:                                                        
01164             case LCD24x1:
01165               _function = 0x00;    // Function set 001 DL N X BR1 BR0
01166                                    //  DL=0 (4 bits bus)
01167                                    //  Note: 4 bit mode is ignored for native SPI and I2C devices                                                                                 
01168                                    //  N=0 (1 line)
01169                                    //  X
01170                                    //  BR1=0 (2 significant bits for brightness
01171                                    //  BR0=0 
01172                                    //           0x0 = 100%
01173                                    //           0x1 =  75%
01174                                    //           0x2 =  50%
01175                                    //           0x3 =  25%                
01176 
01177               break;                                
01178                                                   
01179             // All other valid LCD types are initialised as 2 Line displays
01180             case LCD8x2:  
01181             case LCD16x2:  
01182             case LCD20x2:
01183             case LCD24x2:
01184               _function = 0x08;    // Function set 001 DL N X BR1 BR2
01185                                    //  DL=0 (4 bits bus)
01186                                    //  Note: 4 bit mode is ignored for native SPI and I2C devices                                 
01187                                    //  N=1 (2 lines)
01188                                    //  X
01189                                    //  BR1=0 (2 significant bits for brightness
01190                                    //  BR0=0 
01191               break;
01192               
01193             default:            
01194               error("Error: LCD Controller type does not support this Display type\n\r"); 
01195               break;               
01196           } // switch type
01197            
01198           _contrast = LCD_PT63_CONTRAST;
01199           _writeCommand(0x20 | _function | ((~_contrast) >> 4));        // Invert and shift to use 2 MSBs     
01200           break; // case PT6314 Controller (VFD)
01201            
01202         case ST7066_ACM:                                                // ST7066 4/8 bit, I2C on ACM1602 using a PIC        
01203         default:
01204           // Devices fully compatible to HD44780 that do not use any DC/DC Voltage converters but external VLCD, no icons etc
01205 
01206           // Initialise Display configuration
01207           switch (_type) {
01208             case LCD8x1:         //8x1 is a regular 1 line display
01209             case LCD8x2B:        //8x2B is a special case of 16x1
01210 //            case LCD12x1:                                
01211             case LCD16x1:                                            
01212 //            case LCD20x1:                                                        
01213             case LCD24x1:
01214 //            case LCD40x1:            
01215               _function = 0x00;    // Function set 001 DL N F - -
01216                                    //  DL=0 (4 bits bus)             
01217                                    //   N=0 (1 line)
01218                                    //   F=0 (5x7 dots font)
01219               break;                                
01220                                                   
01221             case LCD12x3D:            // Special mode for KS0078 and PCF21XX                            
01222             case LCD12x3D1:           // Special mode for KS0078 and PCF21XX                     
01223             case LCD12x4D:            // Special mode for KS0078 and PCF21XX:
01224             case LCD16x3D:            // Special mode for KS0078
01225 //            case LCD16x3D1:           // Special mode for KS0078
01226 //            case LCD24x3D:            // Special mode for KS0078
01227 //            case LCD24x3D1:           // Special mode for KS0078            
01228             case LCD24x4D:            // Special mode for KS0078
01229               error("Error: LCD Controller type does not support this Display type\n\r"); 
01230               break;  
01231 
01232             // All other LCD types are initialised as 2 Line displays (including LCD16x1C and LCD40x4)
01233             default:
01234               _function = 0x08;    // Function set 001 DL N F - -
01235                                    //  DL=0 (4 bits bus)
01236                                    //  Note: 4 bit mode is ignored for native SPI and I2C devices                                 
01237                                    //   N=1 (2 lines)
01238                                    //   F=0 (5x7 dots font, only option for 2 line display)
01239                                    //    -  (Don't care)
01240               break;
01241           } // switch type
01242 
01243           _writeCommand(0x20 | _function);                         
01244           break; // case default Controller
01245           
01246     } // switch Controller specific initialisations    
01247 
01248     // Controller general initialisations                                          
01249 //    _writeCommand(0x01); // cls, and set cursor to 0
01250 //    wait_ms(10);         // The CLS command takes 1.64 ms.
01251 //                         // Since we are not using the Busy flag, Lets be safe and take 10 ms  
01252 
01253     _writeCommand(0x02); // Return Home 
01254                          //   Cursor Home, DDRAM Address to Origin
01255 
01256     _writeCommand(0x06); // Entry Mode 0000 0 1 I/D S 
01257                          //   Cursor Direction and Display Shift
01258                          //   I/D=1 (Cur incr)
01259                          //     S=0 (No display shift)                        
01260 
01261     _writeCommand(0x14); // Cursor or Display shift 0001 S/C R/L x x 
01262                          //   S/C=0 Cursor moves
01263                          //   R/L=1 Right
01264                          // 
01265 
01266 //    _writeCommand(0x0C); // Display Ctrl 0000 1 D C B
01267 //                         //   Display On, Cursor Off, Blink Off   
01268 
01269     setCursor(CurOff_BlkOff);     
01270     setMode(DispOn);     
01271 }
01272 
01273 
01274 /** Clear the screen, Cursor home. 
01275   */
01276 void TextLCD_Base::cls() {
01277 
01278   // Select and configure second LCD controller when needed
01279   if(_type==LCD40x4) {
01280     _ctrl_idx=_LCDCtrl_1; // Select 2nd controller
01281 
01282     // Second LCD controller Cursor always Off
01283     _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);
01284 
01285     // Second LCD controller Clearscreen
01286     _writeCommand(0x01);  // cls, and set cursor to 0    
01287     wait_ms(10);          // The CLS command takes 1.64 ms.
01288                           // Since we are not using the Busy flag, Lets be safe and take 10 ms
01289   
01290     _ctrl_idx=_LCDCtrl_0; // Select primary controller
01291   }
01292   
01293   // Primary LCD controller Clearscreen
01294   _writeCommand(0x01);    // cls, and set cursor to 0
01295   wait_ms(10);            // The CLS command takes 1.64 ms.
01296                           // Since we are not using the Busy flag, Lets be safe and take 10 ms
01297 
01298   // Restore cursormode on primary LCD controller when needed
01299   if(_type==LCD40x4) {
01300     _setCursorAndDisplayMode(_currentMode,_currentCursor);     
01301   }
01302                    
01303   setAddress(0, 0);  // Reset Cursor location
01304                      // Note: This is needed because some displays (eg PCF21XX) don't use line 0 in the '3 Line' mode.   
01305 }
01306 
01307 /** Locate cursor to a screen column and row
01308   *
01309   * @param column  The horizontal position from the left, indexed from 0
01310   * @param row     The vertical position from the top, indexed from 0
01311   */ 
01312 void TextLCD_Base::locate(int column, int row) {
01313     
01314    // setAddress() does all the heavy lifting:
01315    //   check column and row sanity, 
01316    //   switch controllers for LCD40x4 if needed
01317    //   switch cursor for LCD40x4 if needed
01318    //   set the new memory address to show cursor at correct location
01319    setAddress(column, row);      
01320 }
01321    
01322 
01323 /** Write a single character (Stream implementation)
01324   */
01325 int TextLCD_Base::_putc(int value) {
01326   int addr;
01327     
01328     if (value == '\n') {
01329       //No character to write
01330       
01331       //Update Cursor      
01332       _column = 0;
01333       _row++;
01334       if (_row >= rows()) {
01335         _row = 0;
01336       }      
01337     }
01338     else {
01339       //Character to write
01340 #if (LCD_DEFAULT_FONT == 1)      
01341       _writeData(value);
01342 #else
01343       _writeData(ASCII_2_LCD(value));
01344 #endif              
01345       //Update Cursor
01346       _column++;
01347       if (_column >= columns()) {
01348         _column = 0;
01349         _row++;
01350         if (_row >= rows()) {
01351           _row = 0;
01352         }
01353       }          
01354     } //else
01355 
01356     //Set next memoryaddress, make sure cursor blinks at next location
01357     addr = getAddress(_column, _row);
01358     _writeCommand(0x80 | addr);
01359             
01360     return value;
01361 }
01362 
01363 
01364 // get a single character (Stream implementation)
01365 int TextLCD_Base::_getc() {
01366     return -1;
01367 }
01368 
01369 /** Convert ASCII character code to the LCD fonttable code
01370   *
01371   * @param c The character to write to the display
01372   * @return The character code for the specific fonttable of the controller
01373   */
01374 int TextLCD_Base::ASCII_2_LCD (int c) {
01375     
01376 //LCD_C_FT0 is default for HD44780 and compatible series
01377     if (_font == LCD_C_FT0) return c;
01378 
01379 //LCD_C_FT1 for PCF21XXC series    
01380 //Used code from Suga koubou library for PCF2119
01381     if (((c >= ' ') && (c <= '?')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) {
01382         c |= 0x80;
01383     } else if (c >= 0xf0 && c <= 0xff) {
01384         c &= 0x0f;
01385     }
01386     return c;
01387 
01388 //LCD_C_FT2 ...
01389 //@TODO add more, eg cyrillic 
01390 //@TODO add method to switch between fonts for controllers that support this
01391 }
01392 
01393 
01394 #if(LCD_PRINTF != 1)
01395 /** Write a character to the LCD
01396   *
01397   * @param c The character to write to the display
01398   */
01399 int TextLCD_Base::putc(int c){
01400   return _putc(c);
01401 }  
01402 
01403 
01404 /** Write a raw string to the LCD
01405   *
01406   * @param string text, may be followed by variables to emulate formatting the string.
01407   *                     However, printf formatting is NOT supported and variables will be ignored! 
01408   */
01409 int TextLCD_Base::printf(const char* text, ...) {
01410   
01411   while (*text !=0) {
01412     _putc(*text);
01413     text++;
01414   }
01415   return 0;
01416 }
01417 #endif    
01418 
01419 
01420 // Write a nibble using the 4-bit interface
01421 void TextLCD_Base::_writeNibble(int value) {
01422 
01423 // Enable is Low
01424     this->_setEnable(true);        
01425     this->_setData(value);        // Low nibble
01426     wait_us(1); // Data setup time        
01427     this->_setEnable(false);    
01428     wait_us(1); // Datahold time
01429 // Enable is Low
01430 }
01431 
01432 // Write a byte using the 4-bit interface
01433 void TextLCD_Base::_writeByte(int value) {
01434 
01435 // Enable is Low
01436     this->_setEnable(true);          
01437     this->_setData(value >> 4);   // High nibble
01438     wait_us(1); // Data setup time    
01439     this->_setEnable(false);   
01440     wait_us(1); // Data hold time
01441     
01442     this->_setEnable(true);        
01443     this->_setData(value);        // Low nibble
01444     wait_us(1); // Data setup time        
01445     this->_setEnable(false);    
01446     wait_us(1); // Datahold time
01447 
01448 // Enable is Low
01449 }
01450 
01451 // Write a command byte to the LCD controller
01452 void TextLCD_Base::_writeCommand(int command) {
01453 
01454     this->_setRS(false);        
01455     wait_us(1);  // Data setup time for RS       
01456     
01457     this->_writeByte(command);   
01458     wait_us(40); // most instructions take 40us            
01459 }
01460 
01461 // Write a data byte to the LCD controller
01462 void TextLCD_Base::_writeData(int data) {
01463 
01464     this->_setRS(true);            
01465     wait_us(1);  // Data setup time for RS 
01466         
01467     this->_writeByte(data);
01468     wait_us(40); // data writes take 40us                
01469 }
01470 
01471 
01472 // This replaces the original _address() method.
01473 // It is confusing since it returns the memoryaddress or-ed with the set memorycommand 0x80.
01474 // Left it in here for compatibility with older code. New applications should use getAddress() instead.
01475 int TextLCD_Base::_address(int column, int row) {
01476   return 0x80 | getAddress(column, row);
01477 }
01478 
01479 
01480 // This is new method to return the memory address based on row, column and displaytype.
01481 //
01482 /** Return the memoryaddress of screen column and row location
01483    *
01484    * @param column  The horizontal position from the left, indexed from 0
01485    * @param row     The vertical position from the top, indexed from 0
01486    * @return        The memoryaddress of screen column and row location
01487    *
01488    */
01489 int TextLCD_Base::getAddress(int column, int row) {
01490 
01491     switch (_addr_mode) {
01492 
01493         case LCD_T_A:
01494           //Default addressing mode for 1, 2 and 4 rows (except 40x4)
01495           //The two available rows are split and stacked on top of eachother. Addressing for 3rd and 4th line continues where lines 1 and 2 were split.          
01496           //Displays top rows when less than four are used.          
01497           switch (row) {
01498             case 0:
01499               return 0x00 + column;
01500             case 1:
01501               return 0x40 + column;
01502             case 2:
01503               return 0x00 + _nr_cols + column;
01504             case 3:
01505               return 0x40 + _nr_cols + column;
01506             // Should never get here.
01507             default:            
01508               return 0x00;                    
01509             }
01510           
01511         case LCD_T_B:
01512           // LCD8x2B is a special layout of LCD16x1
01513           if (row==0) 
01514             return 0x00 + column;                        
01515           else   
01516 //            return _nr_cols + column;                                    
01517             return 0x08 + column;                        
01518 
01519         case LCD_T_C:
01520           // LCD16x1C is a special layout of LCD8x2
01521           // LCD32x1C is a special layout of LCD16x2                    
01522           // LCD40x1C is a special layout of LCD20x2          
01523 #if(0)
01524           if (column < 8) 
01525             return 0x00 + column;                        
01526           else   
01527             return 0x40 + (column - 8);                        
01528 #else
01529           if (column < (_nr_cols >> 1)) 
01530             return 0x00 + column;                        
01531           else   
01532             return 0x40 + (column - (_nr_cols >> 1));                        
01533 #endif
01534 
01535 // Not sure about this one, seems wrong.
01536 // Left in for compatibility with original library
01537 //        case LCD16x2B:      
01538 //            return 0x00 + (row * 40) + column;
01539 
01540         case LCD_T_D:
01541           //Alternate addressing mode for 3 and 4 row displays (except 40x4). Used by PCF21XX, KS0073, KS0078, SSD1803
01542           //The 4 available rows start at a hardcoded address.                    
01543           //Displays top rows when less than four are used.
01544           switch (row) {
01545             case 0:
01546               return 0x00 + column;
01547             case 1:
01548               return 0x20 + column;
01549             case 2:
01550               return 0x40 + column;
01551             case 3:
01552               return 0x60 + column;
01553             // Should never get here.
01554             default:            
01555               return 0x00;                    
01556             }
01557 
01558         case LCD_T_D1:
01559           //Alternate addressing mode for 3 row displays. Used by PCF21XX, KS0073, KS0078, SSD1803
01560           //The 4 available rows start at a hardcoded address.                              
01561           //Skips top row of 4 row display and starts display at row 1
01562           switch (row) {
01563             case 0:
01564               return 0x20 + column;
01565             case 1:
01566               return 0x40 + column;
01567             case 2:
01568               return 0x60 + column;
01569             // Should never get here.
01570             default:            
01571               return 0x00;                    
01572             }
01573         
01574         case LCD_T_E:                
01575           // LCD40x4 is a special case since it has 2 controllers.
01576           // Each controller is configured as 40x2 (Type A)
01577           if (row<2) { 
01578             // Test to see if we need to switch between controllers  
01579             if (_ctrl_idx != _LCDCtrl_0) {
01580 
01581               // Second LCD controller Cursor Off
01582               _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);    
01583 
01584               // Select primary controller
01585               _ctrl_idx = _LCDCtrl_0;
01586 
01587               // Restore cursormode on primary LCD controller
01588               _setCursorAndDisplayMode(_currentMode, _currentCursor);    
01589             }           
01590             
01591             return 0x00 + (row * 0x40) + column;          
01592           }
01593           else {
01594 
01595             // Test to see if we need to switch between controllers  
01596             if (_ctrl_idx != _LCDCtrl_1) {
01597               // Primary LCD controller Cursor Off
01598               _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);    
01599 
01600               // Select secondary controller
01601               _ctrl_idx = _LCDCtrl_1;
01602 
01603               // Restore cursormode on secondary LCD controller
01604               _setCursorAndDisplayMode(_currentMode, _currentCursor);    
01605             }           
01606                                    
01607             return 0x00 + ((row-2) * 0x40) + column;          
01608           } 
01609             
01610         case LCD_T_F:
01611           //Alternate addressing mode for 3 row displays.
01612           //The first half of 3rd row continues from 1st row, the second half continues from 2nd row.                              
01613           switch (row) {
01614             case 0:
01615               return 0x00 + column;
01616             case 1:
01617               return 0x40 + column;
01618             case 2:
01619               if (column < (_nr_cols >> 1)) // check first or second half of line
01620                 return (0x00 + _nr_cols + column);                        
01621               else   
01622                 return (0x40 + _nr_cols + (column - (_nr_cols >> 1)));                        
01623             // Should never get here.
01624             default:            
01625               return 0x00;                    
01626           }
01627 
01628         case LCD_T_G:
01629           //Alternate addressing mode for 3 row displays. Used by ST7036
01630           switch (row) {
01631             case 0:
01632               return 0x00 + column;
01633             case 1:
01634               return 0x10 + column;
01635             case 2:
01636               return 0x20 + column;
01637             // Should never get here.
01638             default:            
01639               return 0x00;                    
01640             }
01641 
01642         // Should never get here.
01643         default:            
01644             return 0x00;        
01645 
01646     } // switch _addr_mode
01647 }
01648 
01649 
01650 /** Set the memoryaddress of screen column and row location
01651   *
01652   * @param column  The horizontal position from the left, indexed from 0
01653   * @param row     The vertical position from the top, indexed from 0
01654   */
01655 void TextLCD_Base::setAddress(int column, int row) {
01656    
01657 // Sanity Check column
01658     if (column < 0) {
01659       _column = 0;
01660     }
01661     else if (column >= _nr_cols) {
01662       _column = _nr_cols - 1;
01663     } else _column = column;
01664     
01665 // Sanity Check row
01666     if (row < 0) {
01667       _row = 0;
01668     }
01669     else if (row >= _nr_rows) {
01670       _row = _nr_rows - 1;
01671     } else _row = row;
01672     
01673     
01674 // Compute the memory address
01675 // For LCD40x4:  switch controllers if needed
01676 //               switch cursor if needed
01677     int addr = getAddress(_column, _row);
01678     
01679     _writeCommand(0x80 | addr);
01680 }
01681 
01682 
01683 /** Return the number of columns
01684   *
01685   * @return  The number of columns
01686   *
01687   * Note: some configurations are commented out because they have not yet been tested due to lack of hardware     
01688   */   
01689 int TextLCD_Base::columns() {
01690     
01691   // Columns encoded in b7..b0
01692   //return (_type & 0xFF);          
01693   return _nr_cols;           
01694 }
01695 
01696 /** Return the number of rows
01697   *
01698   * @return  The number of rows
01699   *
01700   * Note: some configurations are commented out because they have not yet been tested due to lack of hardware     
01701   */
01702 int TextLCD_Base::rows() {
01703 
01704   // Rows encoded in b15..b8  
01705   //return ((_type >> 8) & 0xFF); 
01706   return _nr_rows;          
01707 }
01708 
01709 /** Set the Cursormode
01710   *
01711   * @param cursorMode  The Cursor mode (CurOff_BlkOff, CurOn_BlkOff, CurOff_BlkOn, CurOn_BlkOn)
01712   */
01713 void TextLCD_Base::setCursor(LCDCursor cursorMode) { 
01714 
01715   // Save new cursor mode, needed when 2 controllers are in use or when display is switched off/on
01716   _currentCursor = cursorMode;
01717     
01718   // Configure only current LCD controller
01719   _setCursorAndDisplayMode(_currentMode, _currentCursor);    
01720 }
01721 
01722 /** Set the Displaymode
01723   *
01724   * @param displayMode The Display mode (DispOff, DispOn)
01725   */
01726 void TextLCD_Base::setMode(LCDMode displayMode) { 
01727 
01728   // Save new displayMode, needed when 2 controllers are in use or when cursor is changed
01729   _currentMode = displayMode;
01730     
01731   // Select and configure second LCD controller when needed
01732   if(_type==LCD40x4) {
01733     if (_ctrl_idx==_LCDCtrl_0) {      
01734       // Configure primary LCD controller
01735       _setCursorAndDisplayMode(_currentMode, _currentCursor);
01736 
01737       // Select 2nd controller
01738       _ctrl_idx=_LCDCtrl_1;
01739   
01740       // Configure secondary LCD controller    
01741       _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);
01742 
01743       // Restore current controller
01744       _ctrl_idx=_LCDCtrl_0;       
01745     }
01746     else {
01747       // Select primary controller
01748       _ctrl_idx=_LCDCtrl_0;
01749     
01750       // Configure primary LCD controller
01751       _setCursorAndDisplayMode(_currentMode, CurOff_BlkOff);
01752        
01753       // Restore current controller
01754       _ctrl_idx=_LCDCtrl_1;
01755 
01756       // Configure secondary LCD controller    
01757       _setCursorAndDisplayMode(_currentMode, _currentCursor);
01758     }
01759   }
01760   else {
01761     // Configure primary LCD controller
01762     _setCursorAndDisplayMode(_currentMode, _currentCursor);
01763   }       
01764 }
01765 
01766 /** Low level method to restore the cursortype and display mode for current controller
01767   */     
01768 void TextLCD_Base::_setCursorAndDisplayMode(LCDMode displayMode, LCDCursor cursorType) {    
01769 
01770   // Configure current LCD controller   
01771   switch (_ctrl) {
01772     case ST7070: 
01773       //ST7070 does not support Cursorblink. The P bit selects the font instead !   
01774       _writeCommand(0x08 | displayMode | (cursorType & 0x02));    
01775       break;
01776     default:      
01777       _writeCommand(0x08 | displayMode | cursorType);
01778       break;
01779   } //switch      
01780 }
01781 
01782 /** Set the Backlight mode
01783   *
01784   *  @param backlightMode The Backlight mode (LightOff, LightOn)
01785   */
01786 void TextLCD_Base::setBacklight(LCDBacklight backlightMode) {
01787 
01788 #if (BACKLIGHT_INV==0)      
01789     // Positive Backlight control pin logic
01790     if (backlightMode == LightOn) {
01791       this->_setBL(true);     
01792     }
01793     else {
01794       this->_setBL(false);    
01795     }
01796 #else
01797     // Inverted Backlight control pin logic
01798     if (backlightMode == LightOn) {
01799       this->_setBL(false);    
01800     }
01801     else {
01802       this->_setBL(true);           
01803     }
01804 #endif    
01805 } 
01806 
01807 /** Set User Defined Characters
01808   *
01809   * @param unsigned char c   The Index of the UDC (0..7) for HD44780 or clones and (0..15) for some more advanced controllers 
01810   * @param char *udc_data    The bitpatterns for the UDC (8 bytes of 5 significant bits for bitpattern and 3 bits for blinkmode (advanced types))     
01811   */
01812 void TextLCD_Base::setUDC(unsigned char c, char *udc_data) {
01813   
01814   // Select and configure second LCD controller when needed
01815   if(_type==LCD40x4) {
01816     _LCDCtrl_Idx current_ctrl_idx = _ctrl_idx; // Temp save current controller
01817    
01818     // Select primary controller     
01819     _ctrl_idx=_LCDCtrl_0;
01820     
01821     // Configure primary LCD controller
01822     _setUDC(c, udc_data);
01823 
01824     // Select 2nd controller
01825     _ctrl_idx=_LCDCtrl_1;
01826   
01827     // Configure secondary LCD controller    
01828     _setUDC(c, udc_data);
01829 
01830     // Restore current controller
01831     _ctrl_idx=current_ctrl_idx;       
01832   }
01833   else {
01834     // Configure primary LCD controller
01835     _setUDC(c, udc_data); 
01836   }   
01837 }
01838 
01839 /** Low level method to store user defined characters for current controller
01840   *
01841   * @param unsigned char c   The Index of the UDC (0..7) for HD44780 clones and (0..15) for some more advanced controllers 
01842   * @param char *udc_data    The bitpatterns for the UDC (8 bytes of 5 significant bits for bitpattern and 3 bits for blinkmode (advanced types))       
01843   */     
01844 void TextLCD_Base::_setUDC(unsigned char c, char *udc_data) {
01845   
01846   switch (_ctrl) {
01847     case PCF2103_3V3 : // Some UDCs may be used for Icons                  
01848     case PCF2113_3V3 : // Some UDCs may be used for Icons                      
01849     case PCF2116_3V3 :          
01850     case PCF2116_5V  :              
01851     case PCF2119_3V3 : // Some UDCs may be used for Icons             
01852       c = c & 0x0F; // mask down to valid range
01853       break;
01854 
01855     default:     
01856       c = c & 0x07; // mask down to valid range    
01857       break;  
01858   } //switch _ctrl
01859 
01860   // Select DD RAM for current LCD controller
01861   // This is needed to correctly set Bit 6 of the addresspointer for controllers that support 16 UDCs
01862   _writeCommand(0x80 | ((c << 3) & 0x40)) ;  
01863     
01864   // Select CG RAM for current LCD controller
01865   _writeCommand(0x40 | ((c << 3) & 0x3F)); //Set CG-RAM address, (note that Bit 6 is retained and can not be set by this command !)
01866                                            //8 sequential locations needed per UDC
01867   // Store UDC pattern 
01868   for (int i=0; i<8; i++) {
01869     _writeData(*udc_data++);
01870   }
01871    
01872   //Select DD RAM again for current LCD controller and restore the addresspointer
01873   int addr = getAddress(_column, _row);
01874   _writeCommand(0x80 | addr);  
01875 }
01876 
01877 /** Set UDC Blink and Icon blink
01878   * setUDCBlink method is supported by some compatible devices (eg SSD1803) 
01879   *
01880   * @param blinkMode The Blink mode (BlinkOff, BlinkOn)
01881   */
01882 void TextLCD_Base::setUDCBlink(LCDBlink blinkMode){
01883   // Blinking UDCs (and icons) are enabled when a specific controlbit (BE) is set.
01884   // The blinking pixels in the UDC and icons can be controlled by setting additional bits in the UDC or icon bitpattern.
01885   // UDCs are defined by an 8 byte bitpattern. The P0..P4 form the character pattern.
01886   //     P7 P6 P5 P4 P3 P2 P1 P0 
01887   // 0   B1 B0  x  0  1  1  1  0
01888   // 1   B1 B0  x  1  0  0  0  1
01889   //        .............
01890   // 7   B1 B0  x  1  0  0  0  1
01891   //
01892   // Bit 6 and Bit 7 in the pattern will control the blinking mode when Blink is enabled through BE. 
01893   //     B1 B0  Mode
01894   //      0  0  No Blinking in this row of the UDC
01895   //      0  1  Enabled pixels in P4 will blink
01896   //      1  x  Enabled pixels in P0..P4 will blink
01897   //
01898   // Note: the PCF2103 and PCF2113 use UDCs to set Icons 
01899   //   3 x 8 rows x 5 bits = 120 bits Icons for Normal pattern (UDC 0..2) and
01900   //   3 x 8 rows x 5 bits = 120 bits Icons for Blink pattern (UDC 4..6) 
01901   // Note: the PCF2119 uses UDCs to set Icons 
01902   //   4 x 8 rows x 5 bits = 160 bits Icons for Normal pattern (UDC 0..3) and
01903   //   4 x 8 rows x 5 bits = 160 bits Icons for Blink pattern (UDC 4..7) 
01904   switch (blinkMode) {
01905     case BlinkOn: 
01906       // Controllers that support UDC/Icon Blink  
01907       switch (_ctrl) {
01908         case KS0073 :            
01909         case KS0078 :            
01910           _function_1 |= 0x02; // Enable UDC/Icon Blink        
01911           _writeCommand(0x20 | _function_1);        // Function set 0 0 1 DL N RE(1) BE 0/LP (Ext Regs)
01912 
01913           _writeCommand(0x20 | _function);          // Function set 0 0 1 DL N RE(0) DH REV (Std Regs)
01914           break; // case KS0073, KS0078 Controller
01915     
01916         case US2066_3V3 :  
01917         case SSD1803_3V3 :  
01918           _function_1 |= 0x04; // Enable UDC/Icon Blink
01919           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 DL N BE RE(1) REV 
01920                                                     // Select Ext Instr Set
01921 
01922           _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
01923                                                     // Select Std Instr set, Select IS=0
01924           break; // case SSD1803, US2066
01925 
01926         case PCF2103_3V3 :  
01927         case PCF2113_3V3 :  
01928         case PCF2119_3V3 :                  
01929           // Enable Icon Blink
01930           _writeCommand(0x20 | _function | 0x01);   // Set function, Select Instr Set = 1              
01931           _writeCommand(0x08 | 0x02);               // ICON Conf 0000 1, IM=0 (Char mode), IB=1 (Icon blink), 0 (Instr. Set 1) 
01932           _writeCommand(0x20 | _function);          // Set function, Select Instr Set = 0             
01933 
01934           break; 
01935        
01936         default:
01937           //Unsupported feature for other controllers        
01938           break; 
01939       } //switch _ctrl     
01940     
01941       break; // BlinkOn 
01942 
01943     case BlinkOff:
01944       // Controllers that support UDC Blink  
01945       switch (_ctrl) {
01946         case KS0073 :            
01947         case KS0078 :            
01948           _function_1 &= ~0x02; // Disable UDC/Icon Blink        
01949           _writeCommand(0x20 | _function_1);        // Function set 0 0 1 DL N RE(1) BE 0/LP (Ext Regs)
01950 
01951           _writeCommand(0x20 | _function);          // Function set 0 0 1 DL N RE(0) DH REV (Std Regs)
01952           break; // case KS0073, KS0078 Controller
01953     
01954         case US2066_3V3 :  
01955         case SSD1803_3V3 :  
01956           _function_1 &= ~0x04; // Disable UDC/Icon Blink
01957           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 DL N BE RE(1) REV 
01958                                                     // Select Ext Instr Set
01959 
01960           _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
01961                                                     // Select Std Instr set, Select IS=0
01962           break; // case SSD1803, US2066          
01963  
01964         case PCF2103_3V3 :
01965         case PCF2113_3V3 :  
01966         case PCF2119_3V3 :       
01967           // Disable Icon Blink
01968           _writeCommand(0x20 | _function | 0x01);   // Set function, Select Instr Set = 1              
01969           _writeCommand(0x08);                      // ICON Conf 0000 1, IM=0 (Char mode), IB=1 (Icon blink), 0 (Instr. Set 1) 
01970           _writeCommand(0x20 | _function);          // Set function, Select Instr Set = 0             
01971 
01972           break; 
01973        
01974         default:
01975           //Unsupported feature for other controllers        
01976           break; 
01977       } //switch _ctrl     
01978     
01979       break; //BlinkOff
01980       
01981     default:
01982       break;      
01983   } // blinkMode
01984   
01985 } // setUDCBlink()
01986 
01987 
01988 /** Set Contrast
01989   * setContrast method is supported by some compatible devices (eg ST7032i) that have onboard LCD voltage generation
01990   * Initial code for ST70XX imported from fork by JH1PJL
01991   *
01992   * @param unsigned char c   contrast data (6 significant bits, valid range 0..63, Value 0 will disable the Vgen)  
01993   * @return none
01994   */
01995 //@TODO Add support for 40x4 dual controller
01996 void TextLCD_Base::setContrast(unsigned char c) {
01997 
01998 // Function set mode stored during Init. Make sure we dont accidentally switch between 1-line and 2-line mode!
01999 // Icon/Booster mode stored during Init. Make sure we dont accidentally change this!
02000  
02001   _contrast = c & 0x3F; // Sanity check
02002   
02003   switch (_ctrl) {   
02004     case PCF2113_3V3 :  
02005     case PCF2119_3V3 :  
02006        if (_contrast <  5) _contrast = 0;  // See datasheet. Sanity check for PCF2113/PCF2119
02007        if (_contrast > 55) _contrast = 55;
02008       
02009        _writeCommand(0x20 | _function | 0x01);               // Set function, Select Instruction Set = 1              
02010        _writeCommand(0x80 | 0x00 | (_contrast & 0x3F));      // VLCD_set (Instr. Set 1)    V=0, VA=contrast
02011        _writeCommand(0x80 | 0x40 | (_contrast & 0x3F));      // VLCD_set (Instr. Set 1)    V=1, VB=contrast
02012        _writeCommand(0x20 | _function);                      // Select Instruction Set = 0
02013        break;
02014         
02015     case ST7032_3V3 :  
02016     case ST7032_5V :      
02017     case ST7036_3V3 :      
02018 //    case ST7036_5V :          
02019     case SSD1803_3V3 :      
02020       _writeCommand(0x20 | _function | 0x01);                        // Select Instruction Set = 1
02021       _writeCommand(0x70 | (_contrast & 0x0F));                      // Contrast Low bits
02022       _writeCommand(0x50 | _icon_power | ((_contrast >> 4) & 0x03)); // Contrast High bits 
02023       _writeCommand(0x20 | _function);                               // Select Instruction Set = 0
02024       break;
02025 
02026     case US2066_3V3 :      
02027       _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 DL N BE RE(1) REV 
02028                                                 // Select Extended Instruction Set
02029 
02030       _writeCommand(0x79);                      // Function Select OLED:  0 1 1 1 1 0 0 1 (Ext Instr Set)
02031          
02032       _writeCommand(0x81);                      // Set Contrast Control: 1 0 0 0 0 0 0 1 (Ext Instr Set, OLED)
02033       _writeCommand((_contrast << 2) | 0x03);   // Set Contrast Value: 8 bits. Use 6 bits for compatibility    
02034       
02035       _writeCommand(0x78);                      // Function Disable OLED: 0 1 1 1 1 0 0 0 (Ext Instr Set)          
02036 
02037       _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
02038                                                 // Select Std Instr set, Select IS=0
02039       break;
02040 
02041     //not yet tested on hardware
02042     case PT6314 :
02043       // Only 2 significant bits
02044       //   0x00 = 100%
02045       //   0x01 =  75%
02046       //   0x02 =  50%
02047       //   0x03 =  25%                
02048       _writeCommand(0x20 | _function | ((~_contrast) >> 4));        // Invert and shift to use 2 MSBs     
02049       break;
02050             
02051     default:  
02052       //Unsupported feature for other controllers
02053       break;               
02054   } // end switch     
02055 } // end setContrast()
02056 
02057 
02058 /** Set Power
02059   * setPower method is supported by some compatible devices (eg SSD1803) that have power down modes
02060   *
02061   * @param bool powerOn  Power on/off   
02062   * @return none
02063   */
02064 //@TODO Add support for 40x4 dual controller  
02065 void TextLCD_Base::setPower(bool powerOn) {
02066   
02067   if (powerOn) {
02068     // Switch on  
02069     setMode(DispOn);       
02070 
02071     // Controllers that supports specific Power Down mode
02072     switch (_ctrl) {
02073     
02074 //    case PCF2113_3V3 :  
02075 //    case PCF2119_3V3 :  
02076 //    case ST7032_3V3 :  
02077 //@todo
02078 //    enable Booster Bon
02079 
02080       case WS0010:      
02081         _writeCommand(0x17);   // Char mode, DC/DC on        
02082         wait_ms(10);           // Wait 10ms to ensure powered up             
02083         break;
02084 
02085       case KS0073:        
02086       case KS0078:        
02087       case SSD1803_3V3 :      
02088 //      case SSD1803_5V :            
02089         _writeCommand(0x20 | _function_1);                             // Select Ext Instr Set
02090         _writeCommand(0x02);                                           // Power On
02091         _writeCommand(0x20 | _function);                               // Select Std Instr Set
02092         break;
02093                     
02094       default:  
02095         //Unsupported feature for other controllers
02096         break;              
02097     } // end switch  
02098   }  
02099   else {
02100     // Switch off        
02101     setMode(DispOff);       
02102 
02103     // Controllers that support specific Power Down mode
02104     switch (_ctrl) {
02105     
02106 //    case PCF2113_3V3 :  
02107 //    case PCF2119_3V3 :  
02108 //    case ST7032_3V3 :  
02109 //@todo
02110 //    disable Booster Bon
02111 
02112       case WS0010:      
02113         _writeCommand(0x13);   // Char mode, DC/DC off              
02114         break;
02115         
02116       case KS0073:
02117       case KS0078:
02118       case SSD1803_3V3 :      
02119 //      case SSD1803_5V :            
02120         _writeCommand(0x20 | _function_1);                             // Select Ext Instr Set
02121         _writeCommand(0x03);                                           // Power Down
02122         _writeCommand(0x20 | _function);                               // Select Std Instr Set
02123         break;
02124 
02125       default:  
02126         //Unsupported feature for other controllers
02127         break;              
02128     } // end switch  
02129   }
02130 } // end setPower()
02131 
02132 
02133 /** Set Orient
02134   * setOrient method is supported by some compatible devices (eg SSD1803, US2066) that have top/bottom view modes
02135   *
02136   * @param LCDOrient orient Orientation 
02137   * @return none
02138   */
02139 void TextLCD_Base::setOrient(LCDOrient orient){
02140 
02141   switch (orient) {
02142        
02143     case Top:
02144       switch (_ctrl) {
02145         case PCF2103_3V3:              
02146         case PCF2116_3V3:        
02147         case PCF2116_5V:                
02148         case PCF2119_3V3:                
02149           _writeCommand(0x20 | _function | 0x01);          // Set function, Select Instr Set = 1              
02150           _writeCommand(0x05);                             // Display Conf Set         0000 0, 1, P=0, Q=1               (Instr. Set 1)
02151           _writeCommand(0x20 | _function);                 // Set function, Select Instr Set = 0             
02152           break;
02153                                
02154         case SSD1803_3V3 :      
02155 //      case SSD1803_5V :
02156         case US2066_3V3 :      
02157           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 X N BE RE(1) REV 
02158                                                     // Select Extended Instruction Set
02159 //          _writeCommand(0x06);                      // Set ext entry mode, 0 0 0 0 0 1 BDC=1 COM1-32, BDS=0 SEG100-1    "Bottom View" (Ext Instr Set)
02160           _writeCommand(0x05);                      // Set ext entry mode, 0 0 0 0 0 1 BDC=0 COM32-1, BDS=1 SEG1-100    "Top View" (Ext Instr Set)          
02161 
02162           _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
02163                                                     // Select Std Instr set, Select IS=0       
02164           break;
02165 
02166         case ST7070:      
02167           _writeCommand(0x20 | _function | 0x04);   // Set function, 0 0 1 DL, N, EXT=1, x, x (Select Instr Set = 1)
02168 
02169           _writeCommand(0x40 | 0x00);               // COM/SEG directions 0 1 0 0 C1, C2, S1, S2  (Instr Set 1)
02170                                                     // C1=1: Com1-8 -> Com8-1;   C2=1: Com9-16 -> Com16-9
02171                                                     // S1=1: Seg1-40 -> Seg40-1; S2=1: Seg41-80 -> Seg80-41                                                    
02172           wait_ms(5);                               // Wait to ensure completion or ST7070 fails to set Top/Bottom after reset..
02173           
02174           _writeCommand(0x20 | _function);          // Set function, EXT=0 (Select Instr Set = 0)
02175         
02176           break; // case ST7070 Controller
02177           
02178         default:  
02179           //Unsupported feature for other controllers
02180           break;              
02181 
02182       } // end switch _ctrl     
02183       break; // end Top
02184                 
02185     case Bottom:
02186       switch (_ctrl) {
02187         case PCF2103_3V3:              
02188         case PCF2116_3V3:        
02189         case PCF2116_5V:                
02190         case PCF2119_3V3:                       
02191           _writeCommand(0x20 | _function | 0x01);          // Set function, Select Instr Set = 1              
02192           _writeCommand(0x06);                             // Display Conf Set         0000 0, 1, P=1, Q=0               (Instr. Set 1)
02193           _writeCommand(0x20 | _function);                 // Set function, Select Instr Set = 0             
02194           break;
02195         
02196         case SSD1803_3V3 :      
02197 //      case SSD1803_5V :
02198         case US2066_3V3 :      
02199           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 X N BE RE(1) REV 
02200                                                     // Select Extended Instruction Set
02201           _writeCommand(0x06);                      // Set ext entry mode, 0 0 0 0 0 1 BDC=1 COM1-32, BDS=0 SEG100-1    "Bottom View" (Ext Instr Set)
02202 //          _writeCommand(0x05);                      // Set ext entry mode, 0 0 0 0 0 1 BDC=0 COM32-1, BDS=1 SEG1-100    "Top View" (Ext Instr Set)          
02203 
02204           _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
02205                                                     // Select Std Instr set, Select IS=0       
02206           break;
02207 
02208         case ST7070:
02209           //Note: this does not result in correct top/bottom view. 
02210           //The left and right half of each row are reversed and the addressing of both rows is also incorrect:
02211           //Top/bottomline when orientation is flipped:
02212           //  0x48...0x4F  0x40...0x47
02213           //  0x08...0x0F  0x00...0x07
02214           _writeCommand(0x20 | _function | 0x04);   // Set function, 0 0 1 DL N EXT=1 x x (Select Instr Set = 1)
02215 
02216           _writeCommand(0x40 | 0x0F);               // COM/SEG directions 0 1 0 0 C1, C2, S1, S2  (Instr Set 1)
02217                                                     // C1=1: Com1-8 -> Com8-1;   C2=1: Com9-16 -> Com16-9
02218                                                     // S1=1: Seg1-40 -> Seg40-1; S2=1: Seg41-80 -> Seg80-41                                                    
02219           wait_ms(5);                               // Wait to ensure completion or ST7070 fails to set Top/Bottom after reset..
02220           
02221           _writeCommand(0x20 | _function);          // Set function, EXT=0 (Select Instr Set = 0)
02222         
02223           break; // case ST7070 Controller
02224           
02225         default:  
02226           //Unsupported feature for other controllers
02227           break;              
02228 
02229       } // end switch _ctrl     
02230     
02231       break; // end Bottom
02232   } // end switch orient
02233 } // end setOrient()
02234 
02235 /** Set Big Font
02236   * setBigFont method is supported by some compatible devices (eg SSD1803, US2066) 
02237   *
02238   * @param lines  The selected Big Font lines (None, TopLine, CenterLine, BottomLine, TopBottomLine)
02239   *                                            Double height characters can be shown on lines 1+2, 2+3, 3+4 or 1+2 and 3+4
02240   *                                            Valid double height lines depend on the LCDs number of rows.
02241   */
02242 void TextLCD_Base::setBigFont(LCDBigFont lines) {
02243 
02244   switch (lines) {
02245     case None:
02246       switch (_ctrl) {
02247         case SSD1803_3V3 :      
02248         case US2066_3V3 :      
02249           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 X N BE RE(1) REV 
02250                                                     // Select Extended Instruction Set
02251           _writeCommand(0x1C);                      // Double Height, 0 0 0 1 UD2=1, UD1=1, X, DH'=0 (Ext Instr Set)
02252                                                     // Default
02253           _function = _function & ~0x04;            // Set function, 0 0 1 DL N DH=0 RE(0) IS=0 Select Instruction Set 0
02254           _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
02255                                                     // Select Std Instr set, Select IS=0        
02256           break; // end US2066      
02257 
02258         default:
02259           break; // end default      
02260       } // end switch _ctrl     
02261       break; // end None      
02262     
02263     case TopLine:
02264       if (_nr_rows < 2) return; //Sanity check  
02265 
02266       switch (_ctrl) {
02267         case SSD1803_3V3 :              
02268         case US2066_3V3 :      
02269           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 X N BE RE(1) REV 
02270                                                     // Select Extended Instruction Set
02271           _writeCommand(0x1C);                      // Double Height, 0 0 0 1 UD2=1, UD1=1, X, DH'=0 (Ext Instr Set)
02272                                                     // Default
02273           _function = _function | 0x04;             // Set function, 0 0 1 DL N DH=1 RE(0) IS=0 Select Instruction Set 0
02274           _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
02275                                                     // Select Std Instr set, Select IS=0        
02276           break; // end US2066, SSD1803      
02277 
02278         default:
02279           break; // end default      
02280       } // end switch _ctrl     
02281       break; // end TopLine              
02282 
02283     case CenterLine:
02284       if (_nr_rows != 4) return; //Sanity check  
02285           
02286       switch (_ctrl) {
02287         case SSD1803_3V3 :              
02288         case US2066_3V3 :      
02289           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 X N BE RE(1) REV 
02290                                                     // Select Extended Instruction Set
02291           _writeCommand(0x14);                      // Double Height, 0 0 0 1 UD2=0, UD1=1, X, DH'=0 (Ext Instr Set)
02292                                                     // Default
02293           _function = _function | 0x04;             // Set function, 0 0 1 DL N DH=1 RE(0) IS=0 Select Instruction Set 0
02294           _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
02295                                                     // Select Std Instr set, Select IS=0        
02296           break; // end US2066, SSD1803      
02297 
02298         default:
02299           break; // end default      
02300       } // end switch _ctrl        
02301       break; // end CenterLine              
02302 
02303     case BottomLine:
02304       if (_nr_rows < 3) return; //Sanity check  
02305           
02306       switch (_ctrl) {
02307         case SSD1803_3V3 :              
02308         case US2066_3V3 :      
02309           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 X N BE RE(1) REV 
02310                                                     // Select Extended Instruction Set
02311           if (_nr_rows == 3) {
02312             _writeCommand(0x14);                      // Double Height, 0 0 0 1 UD2=0, UD1=1, X, DH'=0 (Ext Instr Set)
02313           }  
02314           else {
02315             _writeCommand(0x10);                      // Double Height, 0 0 0 1 UD2=0, UD1=0, X, DH'=0 (Ext Instr Set)
02316           }  
02317           _function = _function | 0x04;             // Set function, 0 0 1 DL N DH=1 RE(0) IS=0 Select Instruction Set 0
02318           _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
02319                                                     // Select Std Instr set, Select IS=0        
02320           break; // end US2066, SSD1803      
02321 
02322         default:
02323           break; // end default      
02324       } // end switch _ctrl           
02325       break; // end BottomLine          
02326       
02327     case TopBottomLine:
02328       if (_nr_rows != 4) return; //Sanity check  
02329       
02330       switch (_ctrl) {
02331         case SSD1803_3V3 :        
02332         case US2066_3V3 :      
02333           _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 X N BE RE(1) REV 
02334                                                     // Select Extended Instruction Set
02335           _writeCommand(0x18);                      // Double Height, 0 0 0 1 UD2=1, UD1=0, X, DH'=0 (Ext Instr Set)
02336                                                     // Default
02337           _function = _function | 0x04;             // Set function, 0 0 1 DL N DH=1 RE(0) IS=0 Select Instruction Set 0
02338           _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS=0 Select Instruction Set 0
02339                                                     // Select Std Instr set, Select IS=0        
02340           break; // end US2066, SSD1803      
02341 
02342         default:
02343           break; // end default      
02344       } // end switch _ctrl           
02345       break; // end TopBottomLine          
02346 
02347   } // end switch lines
02348 
02349 } // end setBigFont()
02350 
02351 
02352 /** Set Icons
02353   *
02354   * @param unsigned char idx   The Index of the icon pattern (0..15) for KS0073 and similar controllers
02355   *                            and Index (0..31) for PCF2103 and similar controllers  
02356   * @param unsigned char data  The bitpattern for the icons (6 lsb for KS0073 bitpattern (5 lsb for KS0078) and 2 msb for blinkmode)       
02357   *                            The bitpattern for the PCF2103 icons is 5 lsb (UDC 0..2) and 5 lsb for blinkmode (UDC 4..6)         
02358   */
02359 void TextLCD_Base::setIcon(unsigned char idx, unsigned char data) {
02360   // Blinking icons are enabled when a specific controlbit (BE) is set.
02361   // The blinking pixels in the icons can be controlled by setting additional bits in the icon bitpattern.
02362   // Icons are defined by a byte bitpattern. The P0..P5 form the Icon pattern for KS0073, and P0..P4 for KS0078
02363   //     P7 P6 P5 P4 P3 P2 P1 P0 
02364   // 0   B1 B0  0  0  1  1  1  0
02365   // 1   B1 B0  1  1  0  0  0  1
02366   //        .............
02367   // 15  B1 B0  1  1  0  0  0  1
02368   //
02369   // Bit 6 and Bit 7 in the pattern will control the blinking mode when Blink is enabled through BE. 
02370   //     B1 B0  Mode
02371   //      0  0  No Blinking for this icon row
02372   //      0  1  Enabled pixels in P5 will blink
02373   //      1  x  Enabled pixels in P0..P5 will blink
02374   //
02375   // Note: the PCF2103 and PCF2113 use UDCs to set Icons 
02376   //   3 x 8 rows x 5 bits = 120 bits Icons for Normal pattern (UDC 0..2) and
02377   //   3 x 8 rows x 5 bits = 120 bits Icons for Blink pattern (UDC 4..6) 
02378   // Note: the PCF2119 uses UDCs to set Icons 
02379   //   4 x 8 rows x 5 bits = 160 bits Icons for Normal pattern (UDC 0..3) and
02380   //   4 x 8 rows x 5 bits = 160 bits Icons for Blink pattern (UDC 4..7) 
02381   
02382   switch (_ctrl) {
02383     case KS0073:
02384     case KS0078:    
02385        _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 DL N RE(1) BE LP 
02386                                                  // Select Extended Instruction Set
02387        _writeCommand(0x40 | (idx & 0x0F));       // Set Icon Address, mask Address to valid range (Ext Instr Set)
02388 
02389        _writeData(data);                         // Set Icon pattern (Ext Instr Set)
02390 
02391        _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N RE(0) DH REV Select Instruction Set 0
02392                                                  // Select Std Instr set, Select IS=0        
02393        break; // end KS0073, KS0078
02394 
02395     case ST7032_3V3:
02396     case ST7032_5V:
02397        _writeCommand(0x20 | _function | 0x01);   // Set function,  0 0 1 DL N F 0 IS=1 Select Instr Set = 1              
02398        _writeCommand(0x40 | (idx & 0x0F));       // Set Icon Address, mask Address to valid range (Instr Set 1)
02399 
02400        _writeData(data & 0x1F);                  // Set Icon pattern, no blink support (Instr Set 1) 
02401 
02402        _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N RE(0) DH REV Select Instruction Set 0
02403                                                  // Select Std Instr set, Select IS=0        
02404        break; // end ST7032
02405 
02406     case ST7036_3V3:
02407     case ST7036_5V:
02408        _writeCommand(0x20 | _function | 0x01);   // Set function, 0 0 1 DL N DH IS2,IS1 = 01 (Select Instr Set = 1)    
02409        _writeCommand(0x40 | (idx & 0x0F));       // Set Icon Address, mask Address to valid range (Instr Set 1)
02410 
02411        _writeData(data & 0x1F);                  // Set Icon pattern, no blink support (Instr Set 1) 
02412 
02413        _writeCommand(0x20 | _function);          // Set function, IS2,IS1 = 00 (Select Instr Set = 0)    
02414                                                  // Select Std Instr set, Select IS=0        
02415        break; // end ST7036
02416 
02417     case SSD1803_3V3:                  
02418 //    case SSD1803_5V:                      
02419        _writeCommand(0x20 | _function | 0x01);   // Set function, 0 0 1 DL N DH RE(0) IS 
02420                                                  // Select Instruction Set 1
02421        _writeCommand(0x40 | (idx & 0x0F));       // Set Icon Address, mask Address to valid range (Instr Set = 1)
02422        _writeData(data);                         // Set Icon pattern (Instr Set = 1)
02423 
02424        _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS
02425                                                  // Select IS=0        
02426        break; // end SSD1803      
02427 
02428     case PCF2103_3V3:
02429     case PCF2113_3V3:    
02430     case PCF2119_3V3:        
02431        // Store UDC/Icon pattern for PCF2103 and PCF2113: 
02432        //   3 x 8 rows x 5 bits = 120 bits for Normal pattern (UDC 0..2) and
02433        //   3 x 8 rows x 5 bits = 120 bits for Blink pattern (UDC 4..6) 
02434        // Store UDC/Icon pattern for PCF2119: 
02435        //   4 x 8 rows x 5 bits = 160 bits for Normal pattern (UDC 0..3) and
02436        //   4 x 8 rows x 5 bits = 160 bits for Blink pattern (UDC 4..7)        
02437        _writeCommand(0x40 | (idx & 0x3F));       //Set CG-RAM address, 8 sequential locations needed per UDC
02438        _writeData(data);                         // Set Icon pattern (Instr Set = 1)
02439        break; // case PCF2103_3V3 Controller
02440                  
02441     default:
02442        break; // end default      
02443   } // end switch _ctrl           
02444   
02445   //Select DD RAM again for current LCD controller and restore the addresspointer
02446   int addr = getAddress(_column, _row);
02447   _writeCommand(0x80 | addr);  
02448          
02449 } // end setIcon()
02450 
02451 /** Clear Icons
02452   *
02453   * @param  none
02454   * @return none
02455   */
02456   //@TODO Add support for 40x4 dual controller    
02457 void TextLCD_Base::clrIcon() {
02458   // Icons are defined by a byte bitpattern. The P0..P5 form the Icon pattern for KS0073, and P0..P4 for KS0078
02459   //     P7 P6 P5 P4 P3 P2 P1 P0 
02460   // 0   B1 B0  0  0  0  0  0  0
02461   // 1   B1 B0  0  0  0  0  0  0
02462   //        .............
02463   // 15  B1 B0  0  0  0  0  0  0
02464   //
02465   // Bit 6 and Bit 7 in the pattern will control the blinking mode when Blink is enabled through BE. 
02466   //     B1 B0  Mode
02467   //      0  0  No Blinking for this icon row
02468   //      0  1  Enabled pixels in P5 will blink
02469   //      1  x  Enabled pixels in P0..P5 will blink
02470   //
02471   // Note: the PCF2103 and PCF2113 use UDCs to set Icons 
02472   //   3 x 8 rows x 5 bits = 120 bits Icons for Normal pattern (UDC 0..2) and
02473   //   3 x 8 rows x 5 bits = 120 bits Icons for Blink pattern (UDC 4..6)  
02474   // Note: the PCF2119 uses UDCs to set Icons 
02475   //   4 x 8 rows x 5 bits = 160 bits Icons for Normal pattern (UDC 0..3) and
02476   //   4 x 8 rows x 5 bits = 160 bits Icons for Blink pattern (UDC 4..7)  
02477   int idx;
02478   
02479   switch (_ctrl) {
02480     case KS0073:              
02481     case KS0078:                  
02482        _writeCommand(0x20 | _function_1);        // Set function, 0 0 1 DL N RE(1) BE LP 
02483                                                  // Select Extended Instruction Set
02484        for (idx=0; idx<16; idx++) { 
02485          _writeCommand(0x40 | idx);              // Set Icon Address, mask Address to valid range (Ext Instr Set)
02486          _writeData(0x00);                       // Clear Icon pattern (Ext Instr Set)
02487        }  
02488        _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N RE(0) DH REV Select Std Instruction Set
02489                                                  // Select Std Instr set        
02490        break; // end KS0073, KS0078      
02491 
02492     case ST7032_3V3:
02493     case ST7032_5V:
02494        _writeCommand(0x20 | _function | 0x01);   // Set function,  0 0 1 DL N F 0 IS=1 Select Instr Set = 1              
02495 
02496        for (idx=0; idx<16; idx++) { 
02497          _writeCommand(0x40 | idx);              // Set Icon Address, mask Address to valid range (Instr Set 1)
02498          _writeData(0x00);                       // Clear Icon pattern (Instr Set 1)
02499        }  
02500 
02501        _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N RE(0) DH REV Select Instruction Set 0
02502                                                  // Select Std Instr set, Select IS=0        
02503        break; // end ST7032
02504 
02505     case ST7036_3V3:
02506     case ST7036_5V:
02507        _writeCommand(0x20 | _function | 0x01);   // Set function, 0 0 1 DL N DH IS2,IS1 = 01 (Select Instr Set = 1)    
02508 
02509        for (idx=0; idx<16; idx++) { 
02510          _writeCommand(0x40 | idx);              // Set Icon Address, mask Address to valid range (Instr Set 1)
02511          _writeData(0x00);                       // Clear Icon pattern (Instr Set 1)
02512        }  
02513 
02514        _writeCommand(0x20 | _function);          // Set function, IS2,IS1 = 00 (Select Instr Set = 0)    
02515                                                  // Select Std Instr set, Select IS=0        
02516        break; // end ST7036
02517 
02518     case SSD1803_3V3:                  
02519 //    case SSD1803_5V:                      
02520        _writeCommand(0x20 | _function | 0x01);   // Set function, 0 0 1 DL N DH RE(0) IS 
02521                                                  // Select Instruction Set 1
02522        for (idx=0; idx<16; idx++) { 
02523          _writeCommand(0x40 | idx);              // Set Icon Address, mask Address to valid range (Ext Instr Set)
02524          _writeData(0x00);                       // Clear Icon pattern (Ext Instr Set)
02525        }  
02526        _writeCommand(0x20 | _function);          // Set function, 0 0 1 DL N DH RE(0) IS
02527                                                  // Select IS=0        
02528        break; // end SSD1803      
02529 
02530      case PCF2103_3V3:
02531      case PCF2113_3V3:         
02532        // PCF2103 and PCF2113 use part of the UDC RAM to control Icons   
02533        // Select CG RAM
02534 
02535        _writeCommand(0x40 | (0 * 8)); //Set CG-RAM address, 8 sequential locations needed per UDC
02536        // Store UDC/Icon pattern: 
02537        //   3 x 8 rows x 5 bits = 120 bits for Normal pattern (UDC 0..2) and
02538        for (int i=0; i<(3 * 8); i++) {
02539 //       _writeData(0x1F);  // All On
02540          _writeData(0x00);  // All Off            
02541        }
02542 
02543        _writeCommand(0x40 | (4 * 8)); //Set CG-RAM address, 8 sequential locations needed per UDC
02544        //   3 x 8 rows x 5 bits = 120 bits for Blink pattern (UDC 4..6) 
02545        for (int i=0; i<(3 * 8); i++) {
02546 //       _writeData(0x1F);  // All On
02547          _writeData(0x00);  // All Off            
02548        }
02549        break; // case PCF2103_3V3 Controller
02550 
02551      case PCF2119_3V3:              
02552        // PCF2119 uses part of the UDC RAM to control Icons   
02553        // Select CG RAM
02554 
02555        _writeCommand(0x40 | (0 * 8)); //Set CG-RAM address, 8 sequential locations needed per UDC
02556        // Store UDC/Icon pattern: 
02557        //   4 x 8 rows x 5 bits = 160 bits for Normal pattern (UDC 0..3) and
02558        for (int i=0; i<(4 * 8); i++) {
02559 //       _writeData(0x1F);  // All On
02560          _writeData(0x00);  // All Off            
02561        }
02562 
02563        _writeCommand(0x40 | (4 * 8)); //Set CG-RAM address, 8 sequential locations needed per UDC
02564        //   4 x 8 rows x 5 bits = 160 bits for Blink pattern (UDC 4..7) 
02565        for (int i=0; i<(4 * 8); i++) {
02566 //       _writeData(0x1F);  // All On
02567          _writeData(0x00);  // All Off            
02568        }
02569        break; // case PCF2119_3V3 Controller
02570 
02571     default:
02572        break; // end default      
02573   } // end switch _ctrl           
02574   
02575   //Select DD RAM again for current LCD controller and restore the addresspointer
02576   int addr = getAddress(_column, _row);
02577   _writeCommand(0x80 | addr);
02578 } //end clrIcon()
02579 
02580 
02581 /** Set Invert
02582   * setInvert method is supported by some compatible devices (eg KS0073) to swap between black and white 
02583   *
02584   * @param bool invertOn  Invert on/off   
02585   * @return none
02586   */
02587 //@TODO Add support for 40x4 dual controller  
02588 void TextLCD_Base::setInvert(bool invertOn) {
02589   
02590   if (invertOn) {
02591     // Controllers that support Invert
02592     switch (_ctrl) {   
02593       case KS0073:        
02594       case KS0078:        
02595         _function = _function | 0x01;                                  // Enable Invert
02596         _writeCommand(0x20 | _function);                               // Activate Invert (Std Instr Set)
02597         break;
02598       case SSD1803_3V3 :      
02599 //      case SSD1803_5V :                 
02600       case US2066_3V3:
02601 //      case USS2066_5V:
02602         _function_1 = _function_1 | 0x01;                              // Enable Invert 
02603                                                                        // Set function, 0 0 1 DL N BE RE(1) REV  (SSD1803)
02604                                                                        // Set function, 0 0 1 X  N BE RE(1) REV  (US2066)    
02605         _writeCommand(0x20 | _function_1);                             // Activate Invert (Ext Instr Set)
02606         _writeCommand(0x20 | _function);                               // Return to Std Instr Set                            
02607         break;
02608       default:  
02609         //Unsupported feature for other controllers
02610         break;              
02611     } // end switch  
02612   }  
02613   else {
02614     // Controllers that support Invert
02615     switch (_ctrl) {    
02616       case KS0073:
02617       case KS0078:
02618         _function = _function & ~0x01;                                 // Disable Invert
02619         _writeCommand(0x20 | _function);                               // Disable Invert (Std Instr Set)
02620         break;
02621       case SSD1803_3V3 :      
02622 //      case SSD1803_5V :                 
02623       case US2066_3V3:
02624 //      case USS2066_5V:
02625         _function_1 = _function_1 & ~0x01;                             // Disable Invert 
02626                                                                        // Set function, 0 0 1 DL N BE RE(1) REV  (SSD1803)
02627                                                                        // Set function, 0 0 1 X  N BE RE(1) REV  (US2066)                                                                                                                                              
02628         _writeCommand(0x20 | _function_1);                             // Activate Invert (Ext Instr Set)
02629         _writeCommand(0x20 | _function);                               // Return to Std Instr Set                            
02630         break;
02631 
02632       default:  
02633         //Unsupported feature for other controllers
02634         break;              
02635     } // end switch  
02636   }
02637 } // end setInvert()
02638 
02639 //--------- End TextLCD_Base -----------
02640 
02641 
02642 //--------- Start TextLCD Bus -----------
02643 
02644 /* Create a TextLCD interface for using regular mbed pins
02645  *
02646  * @param rs     Instruction/data control line
02647  * @param e      Enable line (clock)
02648  * @param d4-d7  Data lines for using as a 4-bit interface
02649  * @param type   Sets the panel size/addressing mode (default = LCD16x2)
02650  * @param bl     Backlight control line (optional, default = NC)  
02651  * @param e2     Enable2 line (clock for second controller, LCD40x4 only) 
02652  * @param ctrl   LCD controller (default = HD44780)   
02653  */ 
02654 TextLCD::TextLCD(PinName rs, PinName e,
02655                  PinName d4, PinName d5, PinName d6, PinName d7,
02656                  LCDType type, PinName bl, PinName e2, LCDCtrl ctrl) :
02657                  TextLCD_Base(type, ctrl), 
02658                  _rs(rs), _e(e), _d(d4, d5, d6, d7) {
02659 
02660   // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
02661   if (bl != NC) {
02662     _bl = new DigitalOut(bl);   //Construct new pin 
02663     _bl->write(0);              //Deactivate    
02664   }
02665   else {
02666     // No Hardware Backlight pin       
02667     _bl = NULL;                 //Construct dummy pin     
02668   }  
02669 
02670   // The hardware Enable2 pin is only needed for LCD40x4. Test and make sure whether it exists or not to prevent illegal access.
02671   if (e2 != NC) {
02672     _e2 = new DigitalOut(e2);   //Construct new pin 
02673     _e2->write(0);              //Deactivate    
02674   }
02675   else {
02676     // No Hardware Enable pin       
02677     _e2 = NULL;                 //Construct dummy pin     
02678   }  
02679 
02680   _init(_LCD_DL_4);   // Set Datalength to 4 bit for mbed bus interfaces
02681 }
02682 
02683 /** Destruct a TextLCD interface for using regular mbed pins
02684   *
02685   * @param  none
02686   * @return none
02687   */ 
02688 TextLCD::~TextLCD() {
02689    if (_bl != NULL) {delete _bl;}  // BL pin
02690    if (_e2 != NULL) {delete _e2;}  // E2 pin
02691 }
02692 
02693 /** Set E pin (or E2 pin)
02694   * Used for mbed pins, I2C bus expander or SPI shiftregister
02695   * Default PinName value for E2 is NC, must be used as pointer to avoid issues with mbed lib and DigitalOut pins
02696   *   @param  value true or false
02697   *   @return none 
02698   */
02699 void TextLCD::_setEnable(bool value) {
02700 
02701   if(_ctrl_idx==_LCDCtrl_0) {
02702     if (value) {
02703       _e  = 1;    // Set E bit 
02704     }  
02705     else { 
02706       _e  = 0;    // Reset E bit  
02707     }  
02708   }    
02709   else { 
02710     if (value) {
02711       if (_e2 != NULL) {_e2->write(1);}  //Set E2 bit
02712     }  
02713     else { 
02714       if (_e2 != NULL) {_e2->write(0);}  //Reset E2 bit     
02715     }  
02716   }    
02717 }    
02718 
02719 // Set RS pin
02720 // Used for mbed pins, I2C bus expander or SPI shiftregister
02721 void TextLCD::_setRS(bool value) {
02722 
02723   if (value) {
02724     _rs  = 1;    // Set RS bit 
02725   }  
02726   else  {
02727     _rs  = 0;    // Reset RS bit 
02728   }  
02729 }    
02730 
02731 /** Set BL pin
02732   * Used for mbed pins, I2C bus expander or SPI shiftregister
02733   * Default PinName value is NC, must be used as pointer to avoid issues with mbed lib and DigitalOut pins
02734   *   @param  value true or false
02735   *   @return none  
02736   */
02737 void TextLCD::_setBL(bool value) {
02738 
02739   if (value) {
02740     if (_bl != NULL) {_bl->write(1);}  //Set BL bit
02741   }  
02742   else { 
02743     if (_bl != NULL) {_bl->write(0);}  //Reset BL bit  
02744   }  
02745 }    
02746 
02747 // Place the 4bit data on the databus
02748 // Used for mbed pins, I2C bus expander or SPI shifregister
02749 void TextLCD::_setData(int value) {
02750   _d = value & 0x0F;   // Write Databits 
02751 }    
02752 
02753 //----------- End TextLCD ---------------
02754 
02755 
02756 //--------- Start TextLCD_I2C -----------
02757 #if(LCD_I2C == 1) /* I2C Expander PCF8574/MCP23008 */
02758 /** Create a TextLCD interface using an I2C PC8574 (or PCF8574A) or MCP23008 portexpander
02759   *
02760   * @param i2c             I2C Bus
02761   * @param deviceAddress   I2C slave address (PCF8574, PCF8574A or MCP23008, default = 0x40)
02762   * @param type            Sets the panel size/addressing mode (default = LCD16x2)
02763   * @param ctrl            LCD controller (default = HD44780)    
02764   */
02765 TextLCD_I2C::TextLCD_I2C(I2C *i2c, char deviceAddress, LCDType type, LCDCtrl ctrl) :
02766                          TextLCD_Base(type, ctrl), 
02767                          _i2c(i2c){
02768                               
02769   _slaveAddress = deviceAddress & 0xFE;
02770 
02771   // Setup the I2C bus
02772   // The max bitrate for PCF8574 is 100kbit, the max bitrate for MCP23008 is 400kbit, 
02773   _i2c->frequency(100000);
02774   
02775 #if (MCP23008==1)
02776   // MCP23008 portexpander Init
02777   _writeRegister(IODIR,   0x00);  // All pins are outputs
02778   _writeRegister(IPOL,    0x00);  // No reverse polarity on inputs
02779   _writeRegister(GPINTEN, 0x00);  // No interrupt on change of input pins
02780   _writeRegister(DEFVAL,  0x00);  // Default value to compare against for interrupts
02781   _writeRegister(INTCON,  0x00);  // No interrupt on changes, compare against previous pin value 
02782   _writeRegister(IOCON,   0x20);  // b1=0 - Interrupt polarity active low  
02783                                   // b2=0 - Interrupt pin active driver output  
02784                                   // b4=0 - Slew rate enable on SDA
02785                                   // b5=0 - Auto-increment on registeraddress                                  
02786                                   // b5=1 - No auto-increment on registeraddress => needed for performance improved I2C expander mode
02787   _writeRegister(GPPU,    0x00);  // No Pullup 
02788 //               INTF             // Interrupt flags read (Read-Only)
02789 //               INTCAP           // Captured inputpins at time of interrupt (Read-Only)   
02790 //  _writeRegister(GPIO,    0x00);  // Output/Input pins   
02791 //  _writeRegister(OLAT,    0x00);  // Output Latch  
02792     
02793   // Init the portexpander bus
02794   _lcd_bus = D_LCD_BUS_DEF;
02795   
02796   // write the new data to the portexpander
02797   _writeRegister(GPIO, _lcd_bus);      
02798 #else
02799   // PCF8574 of PCF8574A portexpander
02800 
02801   // Init the portexpander bus
02802   _lcd_bus = D_LCD_BUS_DEF;
02803 
02804   // write the new data to the portexpander
02805   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
02806 #endif
02807 
02808   _init(_LCD_DL_4);   // Set Datalength to 4 bit for all serial expander interfaces
02809 }
02810 
02811 // Set E bit (or E2 bit) in the databus shadowvalue
02812 // Used for mbed I2C bus expander
02813 void TextLCD_I2C::_setEnableBit(bool value) {
02814 
02815   if(_ctrl_idx==_LCDCtrl_0) {
02816     if (value) {
02817       _lcd_bus |= D_LCD_E;     // Set E bit 
02818     }  
02819     else {                    
02820       _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
02821     }  
02822   }
02823   else {
02824     if (value) {
02825       _lcd_bus |= D_LCD_E2;    // Set E2 bit 
02826     }  
02827     else {
02828       _lcd_bus &= ~D_LCD_E2;   // Reset E2bit                     
02829     }  
02830   }    
02831 }    
02832 
02833 // Set E pin (or E2 pin)
02834 // Used for mbed pins, I2C bus expander or SPI shiftregister
02835 void TextLCD_I2C::_setEnable(bool value) {
02836 
02837   // Place the E or E2 bit data on the databus shadowvalue
02838   _setEnableBit(value);
02839 
02840 #if (MCP23008==1)
02841   // MCP23008 portexpander
02842   
02843   // write the new data to the portexpander
02844   _writeRegister(GPIO, _lcd_bus);      
02845 #else
02846   // PCF8574 of PCF8574A portexpander
02847 
02848   // write the new data to the I2C portexpander
02849   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
02850 #endif
02851 }    
02852 
02853 
02854 // Set RS pin
02855 // Used for mbed pins, I2C bus expander or SPI shiftregister
02856 void TextLCD_I2C::_setRS(bool value) {
02857 
02858   if (value) {
02859     _lcd_bus |= D_LCD_RS;    // Set RS bit 
02860   }  
02861   else {                    
02862     _lcd_bus &= ~D_LCD_RS;   // Reset RS bit                     
02863   }
02864 
02865 #if (MCP23008==1)
02866   // MCP23008 portexpander
02867   
02868   // write the new data to the portexpander
02869   _writeRegister(GPIO, _lcd_bus);      
02870 #else
02871   // PCF8574 of PCF8574A portexpander
02872 
02873   // write the new data to the I2C portexpander
02874   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
02875 #endif                  
02876 }    
02877 
02878 // Set BL pin
02879 // Used for mbed pins, I2C bus expander or SPI shiftregister
02880 void TextLCD_I2C::_setBL(bool value) {
02881 
02882   if (value) {
02883     _lcd_bus |= D_LCD_BL;    // Set BL bit 
02884   }  
02885   else {                    
02886     _lcd_bus &= ~D_LCD_BL;   // Reset BL bit                     
02887   }
02888   
02889 #if (MCP23008==1)
02890   // MCP23008 portexpander
02891   
02892   // write the new data to the portexpander
02893   _writeRegister(GPIO, _lcd_bus);      
02894 #else
02895   // PCF8574 of PCF8574A portexpander
02896 
02897   // write the new data to the I2C portexpander
02898   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
02899 #endif                 
02900 }    
02901 
02902 
02903 // Place the 4bit data in the databus shadowvalue
02904 // Used for mbed I2C bus expander
02905 void TextLCD_I2C::_setDataBits(int value) {
02906 
02907   // Set bit by bit to support any mapping of expander portpins to LCD pins 
02908   if (value & 0x01){
02909     _lcd_bus |= D_LCD_D4;   // Set Databit 
02910   }  
02911   else { 
02912     _lcd_bus &= ~D_LCD_D4;  // Reset Databit
02913   }  
02914 
02915   if (value & 0x02){
02916     _lcd_bus |= D_LCD_D5;   // Set Databit 
02917   }  
02918   else {
02919     _lcd_bus &= ~D_LCD_D5;  // Reset Databit
02920   }  
02921 
02922   if (value & 0x04) {
02923     _lcd_bus |= D_LCD_D6;   // Set Databit 
02924   }  
02925   else {                    
02926     _lcd_bus &= ~D_LCD_D6;  // Reset Databit
02927   }  
02928 
02929   if (value & 0x08) {
02930     _lcd_bus |= D_LCD_D7;   // Set Databit 
02931   }  
02932   else {
02933     _lcd_bus &= ~D_LCD_D7;  // Reset Databit
02934   }                                      
02935 }    
02936 
02937 // Place the 4bit data on the databus
02938 // Used for mbed pins, I2C bus expander or SPI shifregister
02939 void TextLCD_I2C::_setData(int value) {
02940 
02941   // Place the 4bit data on the databus shadowvalue
02942   _setDataBits(value); 
02943   
02944   // Place the 4bit data on the databus
02945 #if (MCP23008==1)
02946   // MCP23008 portexpander
02947   
02948   // write the new data to the portexpander
02949   _writeRegister(GPIO, _lcd_bus);      
02950 #else
02951   // PCF8574 of PCF8574A portexpander
02952 
02953   // write the new data to the I2C portexpander
02954   _i2c->write(_slaveAddress, &_lcd_bus, 1);    
02955 #endif                 
02956 }    
02957 
02958 // Write data to MCP23008 I2C portexpander
02959 // Used for mbed I2C bus expander
02960 void TextLCD_I2C::_writeRegister (int reg, int value) {
02961   char data[] = {reg, value};
02962     
02963   _i2c->write(_slaveAddress, data, 2); 
02964 }
02965 
02966 //New optimized
02967 //Test faster _writeByte 0.11s vs 0.27s for a 20x4 fillscreen (PCF8574)
02968 //Test faster _writeByte 0.14s vs 0.34s for a 20x4 fillscreen (MCP23008)
02969 
02970 // Write a byte using I2C
02971 void TextLCD_I2C::_writeByte(int value) {
02972   char data[6];
02973   
02974 #if (MCP23008==1)
02975   // MCP23008 portexpander
02976 
02977   data[0] = GPIO;                 // set registeraddres
02978                                   // Note: auto-increment is disabled so all data will go to GPIO register
02979   
02980   _setEnableBit(true);            // set E 
02981   _setDataBits(value >> 4);       // set data high  
02982   data[1] = _lcd_bus;
02983   
02984   _setEnableBit(false);           // clear E   
02985   data[2] = _lcd_bus;
02986   
02987   _setEnableBit(true);            // set E   
02988   _setDataBits(value);            // set data low    
02989   data[3] = _lcd_bus;
02990   
02991   _setEnableBit(false);           // clear E     
02992   data[4] = _lcd_bus;
02993   
02994   // write the packed data to the I2C portexpander
02995   _i2c->write(_slaveAddress, data, 5);    
02996 #else
02997   // PCF8574 of PCF8574A portexpander
02998   
02999   _setEnableBit(true);            // set E 
03000   _setDataBits(value >> 4);       // set data high  
03001   data[0] = _lcd_bus;
03002   
03003   _setEnableBit(false);           // clear E   
03004   data[1] = _lcd_bus;
03005   
03006   _setEnableBit(true);            // set E   
03007   _setDataBits(value);            // set data low    
03008   data[2] = _lcd_bus;
03009   
03010   _setEnableBit(false);           // clear E     
03011   data[3] = _lcd_bus;
03012   
03013   // write the packed data to the I2C portexpander
03014   _i2c->write(_slaveAddress, data, 4);    
03015 #endif
03016 }
03017 
03018 #endif /* I2C Expander PCF8574/MCP23008 */
03019 //---------- End TextLCD_I2C ------------
03020 
03021 
03022 //--------- Start TextLCD_SPI -----------
03023 #if(LCD_SPI == 1) /* SPI Expander SN74595          */
03024 
03025  /** Create a TextLCD interface using an SPI 74595 portexpander
03026    *
03027    * @param spi             SPI Bus
03028    * @param cs              chip select pin (active low)
03029    * @param type            Sets the panel size/addressing mode (default = LCD16x2)
03030    * @param ctrl            LCD controller (default = HD44780)      
03031    */
03032 TextLCD_SPI::TextLCD_SPI(SPI *spi, PinName cs, LCDType type, LCDCtrl ctrl) :
03033                          TextLCD_Base(type, ctrl), 
03034                          _spi(spi),        
03035                          _cs(cs) {      
03036         
03037   // Init cs
03038   _cs = 1;  
03039 
03040   // Setup the spi for 8 bit data, low steady state clock,
03041   // rising edge capture, with a 500KHz or 1MHz clock rate  
03042   _spi->format(8,0);
03043   _spi->frequency(500000);    
03044   //_spi.frequency(1000000);    
03045 
03046   // Init the portexpander bus
03047   _lcd_bus = D_LCD_BUS_DEF;
03048   
03049   // write the new data to the portexpander
03050   _cs = 0;  
03051   _spi->write(_lcd_bus);   
03052   _cs = 1;  
03053 
03054   _init(_LCD_DL_4);   // Set Datalength to 4 bit for all serial expander interfaces
03055 }
03056 
03057 // Set E pin (or E2 pin)
03058 // Used for mbed pins, I2C bus expander or SPI shiftregister
03059 void TextLCD_SPI::_setEnable(bool value) {
03060 
03061   if(_ctrl_idx==_LCDCtrl_0) {
03062     if (value) {
03063       _lcd_bus |= D_LCD_E;     // Set E bit 
03064     }  
03065     else {                    
03066       _lcd_bus &= ~D_LCD_E;    // Reset E bit                     
03067     }  
03068   }
03069   else {
03070     if (value) {
03071       _lcd_bus |= D_LCD_E2;    // Set E2 bit 
03072     }  
03073     else {
03074       _lcd_bus &= ~D_LCD_E2;   // Reset E2 bit                     
03075     }  
03076   }
03077                   
03078   // write the new data to the SPI portexpander
03079   _cs = 0;  
03080   _spi->write(_lcd_bus);   
03081   _cs = 1;    
03082 }    
03083 
03084 // Set RS pin
03085 // Used for mbed pins, I2C bus expander or SPI shiftregister and SPI_N
03086 void TextLCD_SPI::_setRS(bool value) {
03087 
03088   if (value) {
03089     _lcd_bus |= D_LCD_RS;    // Set RS bit 
03090   }  
03091   else {                    
03092     _lcd_bus &= ~D_LCD_RS;   // Reset RS bit                     
03093   }
03094      
03095   // write the new data to the SPI portexpander
03096   _cs = 0;  
03097   _spi->write(_lcd_bus);   
03098   _cs = 1;     
03099 }    
03100 
03101 // Set BL pin
03102 // Used for mbed pins, I2C bus expander or SPI shiftregister
03103 void TextLCD_SPI::_setBL(bool value) {
03104 
03105   if (value) {
03106     _lcd_bus |= D_LCD_BL;    // Set BL bit 
03107   }  
03108   else {
03109     _lcd_bus &= ~D_LCD_BL;   // Reset BL bit                     
03110   }
03111       
03112   // write the new data to the SPI portexpander
03113   _cs = 0;  
03114   _spi->write(_lcd_bus);   
03115   _cs = 1;      
03116 }    
03117 
03118 // Place the 4bit data on the databus
03119 // Used for mbed pins, I2C bus expander or SPI shiftregister
03120 void TextLCD_SPI::_setData(int value) {
03121 
03122   // Set bit by bit to support any mapping of expander portpins to LCD pins
03123   if (value & 0x01) {
03124     _lcd_bus |= D_LCD_D4;   // Set Databit 
03125   }  
03126   else {                    
03127     _lcd_bus &= ~D_LCD_D4;  // Reset Databit                     
03128   }
03129   
03130   if (value & 0x02) {
03131     _lcd_bus |= D_LCD_D5;   // Set Databit 
03132   }  
03133   else {
03134     _lcd_bus &= ~D_LCD_D5;  // Reset Databit                     
03135   }
03136   
03137   if (value & 0x04) {
03138     _lcd_bus |= D_LCD_D6;   // Set Databit 
03139   }  
03140   else {
03141     _lcd_bus &= ~D_LCD_D6;  // Reset Databit                     
03142   }
03143   
03144   if (value & 0x08) {
03145     _lcd_bus |= D_LCD_D7;   // Set Databit 
03146   }  
03147   else {
03148     _lcd_bus &= ~D_LCD_D7;  // Reset Databit
03149   }  
03150                     
03151   // write the new data to the SPI portexpander
03152   _cs = 0;  
03153   _spi->write(_lcd_bus);   
03154   _cs = 1;       
03155 }    
03156 
03157 #endif /* SPI Expander SN74595          */
03158 //---------- End TextLCD_SPI ------------
03159 
03160 
03161 //--------- Start TextLCD_I2C_N ---------
03162 #if(LCD_I2C_N == 1)  /* Native I2C */
03163 
03164  /** Create a TextLCD interface using a controller with native I2C interface
03165    *
03166    * @param i2c             I2C Bus
03167    * @param deviceAddress   I2C slave address (default = 0x7C)  
03168    * @param type            Sets the panel size/addressing mode (default = LCD16x2)
03169    * @param bl              Backlight control line (optional, default = NC)     
03170    * @param ctrl            LCD controller (default = ST7032_3V3)                     
03171    */
03172 TextLCD_I2C_N::TextLCD_I2C_N(I2C *i2c, char deviceAddress, LCDType type, PinName bl, LCDCtrl ctrl) : 
03173                                TextLCD_Base(type, ctrl), 
03174 
03175                                _i2c(i2c){
03176   
03177   _slaveAddress = deviceAddress & 0xFE;
03178   
03179   // Setup the I2C bus
03180   // The max bitrate for ST7032i is 400kbit, lets stick to default here
03181   _i2c->frequency(100000);
03182 
03183        
03184   // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
03185   if (bl != NC) {
03186     _bl = new DigitalOut(bl);   //Construct new pin 
03187     _bl->write(0);              //Deactivate    
03188   }
03189   else {
03190     // No Hardware Backlight pin       
03191     _bl = NULL;                 //Construct dummy pin     
03192   }  
03193   
03194   //Sanity check
03195   if (_ctrl & LCD_C_I2C) {
03196     _init(_LCD_DL_8);  // Set Datalength to 8 bit for all native serial interfaces      
03197   }
03198   else {
03199     error("Error: LCD Controller type does not support native I2C interface\n\r");           
03200   }
03201 }
03202 
03203 TextLCD_I2C_N::~TextLCD_I2C_N() {
03204    if (_bl != NULL) {delete _bl;}  // BL pin
03205 }
03206 
03207 // Not used in this mode
03208 void TextLCD_I2C_N::_setEnable(bool value) {
03209 }    
03210 
03211 // Set RS pin
03212 // Used for mbed pins, I2C bus expander or SPI shiftregister and native I2C or SPI
03213 void TextLCD_I2C_N::_setRS(bool value) {
03214 // The controlbyte defines the meaning of the next byte. This next byte can either be data or command.
03215 // Start Slaveaddress+RW  b7 b6 b5 b4 b3 b2 b1 b0   b7...........b0  Stop
03216 //                        Co RS RW  0  0  0  0  0   command or data
03217 //
03218 //   C0=1 indicates that another controlbyte will follow after the next data or command byte 
03219 //   RS=1 means that next byte is data, RS=0 means that next byte is command
03220 //   RW=0 means write to controller. RW=1 means that controller will be read from after the next command. 
03221 //        Many native I2C controllers dont support this option and it is not used by this lib. 
03222 //
03223 
03224   if (value) {
03225     _controlbyte = 0x40; // Next byte is data, No more control bytes will follow
03226   }
03227   else {
03228     _controlbyte = 0x00; // Next byte is command, No more control bytes will follow     
03229   }
03230 }    
03231 
03232 // Set BL pin
03233 void TextLCD_I2C_N::_setBL(bool value) {
03234     if (_bl) {
03235         _bl->write(value);   
03236     }    
03237 }    
03238     
03239 // Not used in this mode
03240 void TextLCD_I2C_N::_setData(int value) {
03241 }    
03242 
03243 // Write a byte using I2C
03244 void TextLCD_I2C_N::_writeByte(int value) {
03245 // The controlbyte defines the meaning of the next byte. This next byte can either be data or command.
03246 // Start Slaveaddress+RW  b7 b6 b5 b4 b3 b2 b1 b0   b7...........b0  Stop
03247 //                        Co RS RW  0  0  0  0  0   command or data
03248 //
03249 //   C0=1 indicates that another controlbyte will follow after the next data or command byte 
03250 //   RS=1 means that next byte is data, RS=0 means that next byte is command
03251 //   RW=0 means write to controller. RW=1 means that controller will be read from after the next command. 
03252 //        Many native I2C controllers dont support this option and it is not used by this lib. 
03253 //
03254   char data[] = {_controlbyte, value};
03255     
03256 #if(LCD_I2C_ACK==1)
03257 //Controllers that support ACK
03258   _i2c->write(_slaveAddress, data, 2); 
03259 #else  
03260 //Controllers that dont support ACK
03261 //Note: This may be issue with some mbed platforms that dont fully/correctly support I2C byte operations.
03262   _i2c->start(); 
03263   _i2c->write(_slaveAddress);   
03264   _i2c->write(data[0]); 
03265   _i2c->write(data[1]);     
03266   _i2c->stop();   
03267 #endif  
03268 }
03269 #endif /* Native I2C */
03270 //-------- End TextLCD_I2C_N ------------
03271 
03272 
03273 //--------- Start TextLCD_SPI_N ---------
03274 #if(LCD_SPI_N == 1) /* Native SPI bus     */
03275  /** Create a TextLCD interface using a controller with a native SPI4 interface
03276    *
03277    * @param spi             SPI Bus
03278    * @param cs              chip select pin (active low)
03279    * @param rs              Instruction/data control line
03280    * @param type            Sets the panel size/addressing mode (default = LCD16x2)
03281    * @param bl              Backlight control line (optional, default = NC)  
03282    * @param ctrl            LCD controller (default = ST7032_3V3) 
03283    */       
03284 TextLCD_SPI_N::TextLCD_SPI_N(SPI *spi, PinName cs, PinName rs, LCDType type, PinName bl, LCDCtrl ctrl) :
03285                              TextLCD_Base(type, ctrl), 
03286                              _spi(spi),        
03287                              _cs(cs),
03288                              _rs(rs) {      
03289         
03290   // Init CS
03291   _cs = 1;
03292 
03293   // Setup the spi for 8 bit data, high steady state clock,
03294   // rising edge capture, with a 500KHz or 1MHz clock rate    
03295 //  _spi->format(8,3);  
03296 //  _spi->frequency(500000); 
03297 //  _spi->frequency(1000000);    
03298   
03299   // Setup the spi for 8 bit data, low steady state clock,
03300   // rising edge capture, with a 500KHz or 1MHz clock rate  
03301   _spi->format(8,0);
03302 //  _spi->frequency(500000); 
03303   _spi->frequency(1000000);    
03304     
03305   // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
03306   if (bl != NC) {
03307     _bl = new DigitalOut(bl);   //Construct new pin 
03308     _bl->write(0);              //Deactivate    
03309   }
03310   else {
03311     // No Hardware Backlight pin       
03312     _bl = NULL;                 //Construct dummy pin     
03313   }  
03314 
03315   //Sanity check
03316   if (_ctrl & LCD_C_SPI4) {
03317     _init(_LCD_DL_8);   // Set Datalength to 8 bit for all native serial interfaces
03318                         // ST7070 must set datalength to 8 bits!
03319   }
03320   else {
03321     error("Error: LCD Controller type does not support native SPI4 interface\n\r");           
03322   }
03323 }
03324 
03325 TextLCD_SPI_N::~TextLCD_SPI_N() {
03326    if (_bl != NULL) {delete _bl;}  // BL pin
03327 }
03328 
03329 // Not used in this mode
03330 void TextLCD_SPI_N::_setEnable(bool value) {
03331 }    
03332 
03333 // Set RS pin
03334 // Used for mbed pins, I2C bus expander or SPI shiftregister, SPI_N
03335 void TextLCD_SPI_N::_setRS(bool value) {
03336     _rs = value;
03337 }    
03338 
03339 // Set BL pin
03340 void TextLCD_SPI_N::_setBL(bool value) {
03341     if (_bl) {
03342         _bl->write(value);   
03343     }    
03344 }    
03345 
03346 // Not used in this mode
03347 void TextLCD_SPI_N::_setData(int value) {
03348 }    
03349 
03350 // Write a byte using SPI
03351 void TextLCD_SPI_N::_writeByte(int value) {
03352     _cs = 0;
03353     wait_us(1);
03354     _spi->write(value);
03355     wait_us(1);
03356     _cs = 1;
03357 }
03358 #endif /* Native SPI bus     */  
03359 //-------- End TextLCD_SPI_N ------------
03360 
03361 
03362 //-------- Start TextLCD_SPI_N_3_8 --------
03363 #if(LCD_SPI_N_3_8 == 1) /* Native SPI bus     */
03364 
03365  /** Create a TextLCD interface using a controller with a native SPI3 8 bits interface
03366    * This mode is supported by ST7070. Note that implementation in TexTLCD is not very efficient due to
03367    * structure of the TextLCD library: each databyte is written separately and requires a separate 'count command' set to 1 byte.
03368    *
03369    * @param spi             SPI Bus
03370    * @param cs              chip select pin (active low)
03371    * @param type            Sets the panel size/addressing mode (default = LCD16x2)
03372    * @param bl              Backlight control line (optional, default = NC)  
03373    * @param ctrl            LCD controller (default = ST7070) 
03374    */       
03375 TextLCD_SPI_N_3_8::TextLCD_SPI_N_3_8(SPI *spi, PinName cs, LCDType type, PinName bl, LCDCtrl ctrl) :
03376                                      TextLCD_Base(type, ctrl), 
03377                                      _spi(spi),        
03378                                      _cs(cs) {      
03379 
03380   // Init CS
03381   _cs = 1;
03382 
03383   // Setup the spi for 8 bit data, high steady state clock,
03384   // rising edge capture, with a 500KHz or 1MHz clock rate  
03385 //  _spi->format(8,3);  
03386 //  _spi->frequency(500000);    
03387 //  _spi->frequency(1000000);    
03388 
03389   // Setup the spi for 8 bit data, low steady state clock,
03390   // rising edge capture, with a 500KHz or 1MHz clock rate  
03391   _spi->format(8,0);
03392 //  _spi->frequency(500000); 
03393   _spi->frequency(1000000);    
03394   
03395   
03396   // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
03397   if (bl != NC) {
03398     _bl = new DigitalOut(bl);   //Construct new pin 
03399     _bl->write(0);              //Deactivate    
03400   }
03401   else {
03402     // No Hardware Backlight pin       
03403     _bl = NULL;                 //Construct dummy pin     
03404   }  
03405 
03406   //Sanity check
03407   if (_ctrl & LCD_C_SPI3_8) { 
03408     _init(_LCD_DL_8);  // Set Datalength to 8 bit for all native serial interfaces   
03409   }
03410   else {
03411     error("Error: LCD Controller type does not support native SPI3 8 bits interface\n\r");           
03412   }
03413 }
03414 
03415 TextLCD_SPI_N_3_8::~TextLCD_SPI_N_3_8() {
03416    if (_bl != NULL) {delete _bl;}  // BL pin
03417 }
03418 
03419 // Not used in this mode
03420 void TextLCD_SPI_N_3_8::_setEnable(bool value) {
03421 }    
03422 
03423 // Used for mbed pins, I2C bus expander or SPI shiftregister, SPI_N
03424 //   RS=1 means that next byte is data, RS=0 means that next byte is command
03425 void TextLCD_SPI_N_3_8::_setRS(bool value) {
03426   
03427   if (value) {
03428     _controlbyte = 0x01; // Next byte is data, No more control bytes will follow
03429   }
03430   else {
03431     _controlbyte = 0x00; // Next byte is command, No more control bytes will follow     
03432   }
03433 }      
03434   
03435 // Set BL pin
03436 void TextLCD_SPI_N_3_8::_setBL(bool value) {
03437     if (_bl) {
03438         _bl->write(value);   
03439     }    
03440 }    
03441 
03442 // Not used in this mode
03443 void TextLCD_SPI_N_3_8::_setData(int value) {
03444 }    
03445 
03446 // Write a byte using SPI3 8 bits mode (ST7070)
03447 void TextLCD_SPI_N_3_8::_writeByte(int value) {
03448     
03449   if (_controlbyte == 0x00) { // Byte is command 
03450     _cs = 0;
03451     wait_us(1);
03452     _spi->write(value);
03453     wait_us(1);
03454     _cs = 1;
03455   }  
03456   else {                      // Byte is data 
03457     // Select Extended Instr Set
03458     _cs = 0;
03459     wait_us(1);
03460     _spi->write(0x20 | _function | 0x04);   // Set function, 0 0 1 DL N EXT=1 x x (Select Instr Set = 1));
03461     wait_us(1);
03462     _cs = 1;     
03463 
03464     wait_us(40);                            // Wait until command has finished...    
03465         
03466     // Set Count to 1 databyte
03467     _cs = 0;
03468     wait_us(1);    
03469     _spi->write(0x80);                      // Set display data length, 1 L6 L5 L4 L3 L2 L1 L0 (Instr Set = 1)
03470     wait_us(1);
03471     _cs = 1;
03472 
03473     wait_us(40);    
03474                 
03475     // Write 1 databyte     
03476     _cs = 0;
03477     wait_us(1);    
03478     _spi->write(value);                     // Write data (Instr Set = 1)
03479     wait_us(1);
03480     _cs = 1;         
03481 
03482     wait_us(40);    
03483         
03484     // Select Standard Instr Set    
03485     _cs = 0;
03486     wait_us(1);    
03487     _spi->write(0x20 | _function);          // Set function, 0 0 1 DL N EXT=0 x x (Select Instr Set = 0));
03488     wait_us(1);
03489     _cs = 1;     
03490   }  
03491 }
03492 #endif /* Native SPI bus     */  
03493 //------- End TextLCD_SPI_N_3_8 -----------
03494 
03495 
03496 //-------- Start TextLCD_SPI_N_3_9 --------
03497 #if(LCD_SPI_N_3_9 == 1) /* Native SPI bus     */
03498 //Code checked out on logic analyser. Not yet tested on hardware..
03499 
03500  /** Create a TextLCD interface using a controller with a native SPI3 9 bits interface
03501    *
03502    * @param spi             SPI Bus
03503    * @param cs              chip select pin (active low)
03504    * @param type            Sets the panel size/addressing mode (default = LCD16x2)
03505    * @param bl              Backlight control line (optional, default = NC)  
03506    * @param ctrl            LCD controller (default = AIP31068) 
03507    */       
03508 TextLCD_SPI_N_3_9::TextLCD_SPI_N_3_9(SPI *spi, PinName cs, LCDType type, PinName bl, LCDCtrl ctrl) :
03509                                      TextLCD_Base(type, ctrl), 
03510                                      _spi(spi),        
03511                                      _cs(cs) {      
03512 
03513   // Init CS
03514   _cs = 1;
03515 
03516   // Setup the spi for 9 bit data, high steady state clock,
03517   // rising edge capture, with a 500KHz or 1MHz clock rate  
03518   _spi->format(9,3);  
03519   _spi->frequency(1000000);    
03520   
03521   // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
03522   if (bl != NC) {
03523     _bl = new DigitalOut(bl);   //Construct new pin 
03524     _bl->write(0);              //Deactivate    
03525   }
03526   else {
03527     // No Hardware Backlight pin       
03528     _bl = NULL;                 //Construct dummy pin     
03529   }  
03530 
03531   //Sanity check
03532   if (_ctrl & LCD_C_SPI3_9) { 
03533     _init(_LCD_DL_8);  // Set Datalength to 8 bit for all native serial interfaces   
03534   }
03535   else {
03536     error("Error: LCD Controller type does not support native SPI3 9 bits interface\n\r");           
03537   }
03538 }
03539 
03540 TextLCD_SPI_N_3_9::~TextLCD_SPI_N_3_9() {
03541    if (_bl != NULL) {delete _bl;}  // BL pin
03542 }
03543 
03544 // Not used in this mode
03545 void TextLCD_SPI_N_3_9::_setEnable(bool value) {
03546 }    
03547 
03548 // Set RS pin
03549 // Used for mbed pins, I2C bus expander or SPI shiftregister
03550 void TextLCD_SPI_N_3_9::_setRS(bool value) {
03551 // The controlbits define the meaning of the next byte. This next byte can either be data or command.
03552 //   b8  b7...........b0 
03553 //   RS  command or data
03554 //
03555 //   RS=1 means that next byte is data, RS=0 means that next byte is command
03556 //
03557 
03558   if (value) {
03559     _controlbyte = 0x01; // Next byte is data
03560   }
03561   else {
03562     _controlbyte = 0x00; // Next byte is command
03563   }  
03564 }    
03565 
03566 // Set BL pin
03567 void TextLCD_SPI_N_3_9::_setBL(bool value) {
03568     if (_bl) {
03569         _bl->write(value);   
03570     }    
03571 }    
03572 
03573 // Not used in this mode
03574 void TextLCD_SPI_N_3_9::_setData(int value) {
03575 }    
03576 
03577 // Write a byte using SPI3 9 bits mode
03578 void TextLCD_SPI_N_3_9::_writeByte(int value) {
03579     _cs = 0;
03580     wait_us(1);
03581     _spi->write( (_controlbyte << 8) | (value & 0xFF));
03582     wait_us(1);
03583     _cs = 1;
03584 }
03585 #endif /* Native SPI bus     */  
03586 //------- End TextLCD_SPI_N_3_9 -----------
03587 
03588 
03589 //------- Start TextLCD_SPI_N_3_10 --------
03590 #if(LCD_SPI_N_3_10 == 1) /* Native SPI bus     */
03591 
03592  /** Create a TextLCD interface using a controller with a native SPI3 10 bits interface
03593    *
03594    * @param spi             SPI Bus
03595    * @param cs              chip select pin (active low)
03596    * @param type            Sets the panel size/addressing mode (default = LCD16x2)
03597    * @param bl              Backlight control line (optional, default = NC)  
03598    * @param ctrl            LCD controller (default = AIP31068) 
03599    */       
03600 TextLCD_SPI_N_3_10::TextLCD_SPI_N_3_10(SPI *spi, PinName cs, LCDType type, PinName bl, LCDCtrl ctrl) :
03601                                        TextLCD_Base(type, ctrl), 
03602                                        _spi(spi),        
03603                                        _cs(cs) {      
03604         
03605   // Init CS
03606   _cs = 1;
03607 
03608   // Setup the spi for 10 bit data, low steady state clock,
03609   // rising edge capture, with a 500KHz or 1MHz clock rate  
03610   _spi->format(10,0);
03611   _spi->frequency(1000000);    
03612   
03613   // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
03614   if (bl != NC) {
03615     _bl = new DigitalOut(bl);   //Construct new pin 
03616     _bl->write(0);              //Deactivate    
03617   }
03618   else {
03619     // No Hardware Backlight pin       
03620     _bl = NULL;                 //Construct dummy pin     
03621   }  
03622 
03623   //Sanity check
03624   if (_ctrl & LCD_C_SPI3_10) {
03625      _init(_LCD_DL_8);  // Set Datalength to 8 bit for all native serial interfaces            
03626   }
03627   else {
03628     error("Error: LCD Controller type does not support native SPI3 10 bits interface\n\r");           
03629   }
03630 }
03631 
03632 TextLCD_SPI_N_3_10::~TextLCD_SPI_N_3_10() {
03633    if (_bl != NULL) {delete _bl;}  // BL pin
03634 }
03635 
03636 // Not used in this mode
03637 void TextLCD_SPI_N_3_10::_setEnable(bool value) {
03638 }    
03639 
03640 // Set RS pin
03641 // Used for mbed pins, I2C bus expander or SPI shiftregister
03642 void TextLCD_SPI_N_3_10::_setRS(bool value) {
03643 // The controlbits define the meaning of the next byte. This next byte can either be data or command.
03644 //   b9 b8  b7...........b0 
03645 //   RS RW  command or data
03646 //
03647 //   RS=1 means that next byte is data, RS=0 means that next byte is command
03648 //   RW=0 means that next byte is writen, RW=1 means that next byte is read (not used in this lib)
03649 //
03650 
03651   if (value) {
03652     _controlbyte = 0x02; // Next byte is data
03653   }
03654   else {
03655     _controlbyte = 0x00; // Next byte is command
03656   }  
03657 }    
03658 
03659 // Set BL pin
03660 void TextLCD_SPI_N_3_10::_setBL(bool value) {
03661     if (_bl) {
03662         _bl->write(value);   
03663     }    
03664 }    
03665 
03666 // Not used in this mode
03667 void TextLCD_SPI_N_3_10::_setData(int value) {
03668 }    
03669 
03670 // Write a byte using SPI3 10 bits mode
03671 void TextLCD_SPI_N_3_10::_writeByte(int value) {
03672     _cs = 0;
03673     wait_us(1);
03674     _spi->write( (_controlbyte << 8) | (value & 0xFF));
03675     wait_us(1);
03676     _cs = 1;
03677 }
03678 #endif /* Native SPI bus     */  
03679 //------- End TextLCD_SPI_N_3_10 ----------
03680 
03681 
03682 //------- Start TextLCD_SPI_N_3_16 --------
03683 #if(LCD_SPI_N_3_16 == 1) /* Native SPI bus     */
03684 
03685  /** Create a TextLCD interface using a controller with a native SPI3 16 bits interface
03686    *
03687    * @param spi             SPI Bus
03688    * @param cs              chip select pin (active low)
03689    * @param type            Sets the panel size/addressing mode (default = LCD16x2)
03690    * @param bl              Backlight control line (optional, default = NC)  
03691    * @param ctrl            LCD controller (default = PT6314) 
03692    */       
03693 TextLCD_SPI_N_3_16::TextLCD_SPI_N_3_16(SPI *spi, PinName cs, LCDType type, PinName bl, LCDCtrl ctrl) :
03694                                        TextLCD_Base(type, ctrl), 
03695                                        _spi(spi),        
03696                                        _cs(cs) {      
03697         
03698   // Init CS
03699   _cs = 1;
03700 
03701   // Setup the spi for 8 bit data, low steady state clock,
03702   // rising edge capture, with a 500KHz or 1MHz clock rate  
03703   _spi->format(8,0);
03704   _spi->frequency(1000000);    
03705   
03706   // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
03707   if (bl != NC) {
03708     _bl = new DigitalOut(bl);   //Construct new pin 
03709     _bl->write(0);              //Deactivate    
03710   }
03711   else {
03712     // No Hardware Backlight pin       
03713     _bl = NULL;                 //Construct dummy pin     
03714   }  
03715 
03716   //Sanity check
03717   if (_ctrl & LCD_C_SPI3_16) {
03718      _init(_LCD_DL_8);  // Set Datalength to 8 bit for all native serial interfaces            
03719   }
03720   else {
03721     error("Error: LCD Controller type does not support native SPI3 16 bits interface\n\r");           
03722   }
03723 }
03724 
03725 TextLCD_SPI_N_3_16::~TextLCD_SPI_N_3_16() {
03726    if (_bl != NULL) {delete _bl;}  // BL pin
03727 }
03728 
03729 // Not used in this mode
03730 void TextLCD_SPI_N_3_16::_setEnable(bool value) {
03731 }    
03732 
03733 // Set RS pin
03734 // Used for mbed pins, I2C bus expander or SPI shiftregister
03735 void TextLCD_SPI_N_3_16::_setRS(bool value) {
03736 // The 16bit mode is split in 2 bytes. The first byte is for synchronisation and controlbits. The controlbits define the meaning of the next byte.
03737 // The 8 actual bits represent either a data or a command byte.
03738 //   b15 b14 b13 b12 b11 b10  b9  b8 - b7 b6 b5 b4 b3 b2 b1 b0 
03739 //     1   1   1   1   1  RW  RS   0   d7 d6 d5 d4 d3 d2 d1 d0
03740 //
03741 //   RS=1 means that next byte is data, RS=0 means that next byte is command
03742 //   RW=0 means that next byte is writen, RW=1 means that next byte is read (not used in this lib)
03743 //
03744 
03745   if (value) {
03746     _controlbyte = 0xFA; // Next byte is data
03747   }
03748   else {
03749     _controlbyte = 0xF8; // Next byte is command
03750   }  
03751 }    
03752 
03753 // Set BL pin
03754 void TextLCD_SPI_N_3_16::_setBL(bool value) {
03755     if (_bl) {
03756         _bl->write(value);   
03757     }    
03758 }    
03759 
03760 // Not used in this mode
03761 void TextLCD_SPI_N_3_16::_setData(int value) {
03762 }    
03763    
03764 // Write a byte using SPI3 16 bits mode
03765 void TextLCD_SPI_N_3_16::_writeByte(int value) {
03766     _cs = 0;
03767     wait_us(1);
03768 
03769     _spi->write(_controlbyte);
03770 
03771     _spi->write(value);     
03772 
03773     wait_us(1);
03774     _cs = 1;
03775 }
03776 #endif /* Native SPI bus     */  
03777 //------- End TextLCD_SPI_N_3_16 ----------
03778 
03779 
03780 //------- Start TextLCD_SPI_N_3_24 --------
03781 #if(LCD_SPI_N_3_24 == 1) /* Native SPI bus     */
03782 
03783  /** Create a TextLCD interface using a controller with a native SPI3 24 bits interface
03784    *
03785    * @param spi             SPI Bus
03786    * @param cs              chip select pin (active low)
03787    * @param type            Sets the panel size/addressing mode (default = LCD16x2)
03788    * @param bl              Backlight control line (optional, default = NC)  
03789    * @param ctrl            LCD controller (default = SSD1803) 
03790    */       
03791 TextLCD_SPI_N_3_24::TextLCD_SPI_N_3_24(SPI *spi, PinName cs, LCDType type, PinName bl, LCDCtrl ctrl) :
03792                                        TextLCD_Base(type, ctrl), 
03793                                        _spi(spi),        
03794                                        _cs(cs) {      
03795         
03796   // Init CS
03797   _cs = 1;
03798 
03799   // Setup the spi for 8 bit data, high steady state clock,
03800   // rising edge capture, with a 500KHz or 1MHz clock rate  
03801   _spi->format(8,3);
03802   _spi->frequency(1000000);    
03803   
03804   // The hardware Backlight pin is optional. Test and make sure whether it exists or not to prevent illegal access.
03805   if (bl != NC) {
03806     _bl = new DigitalOut(bl);   //Construct new pin 
03807     _bl->write(0);              //Deactivate    
03808   }
03809   else {
03810     // No Hardware Backlight pin       
03811     _bl = NULL;                 //Construct dummy pin     
03812   }  
03813 
03814   //Sanity check
03815   if (_ctrl & LCD_C_SPI3_24) {
03816     _init(_LCD_DL_8);  // Set Datalength to 8 bit for all native serial interfaces      
03817   }
03818   else {
03819     error("Error: LCD Controller type does not support native SPI3 24 bits interface\n\r");           
03820   }
03821 }
03822 
03823 TextLCD_SPI_N_3_24::~TextLCD_SPI_N_3_24() {
03824    if (_bl != NULL) {delete _bl;}  // BL pin
03825 }
03826 
03827 // Not used in this mode
03828 void TextLCD_SPI_N_3_24::_setEnable(bool value) {
03829 }    
03830 
03831 // Set RS pin
03832 // Used for mbed pins, I2C bus expander or SPI shiftregister
03833 void TextLCD_SPI_N_3_24::_setRS(bool value) {
03834 // The 24bit mode is split in 3 bytes. The first byte is for synchronisation and controlbits. The controlbits define the meaning of the next two bytes.
03835 // Each byte encodes 4 actual bits. The 8 actual bits represent either a data or a command byte.
03836 //   b23 b22 b21 b20 b19 b18 b17 b16 -  b15 b14 b13 b12 b11 b10 b9 b8 - b7 b6 b5 b4 b3 b2 b1 b0 
03837 //     1   1   1   1   1  RW  RS   0     d0  d1  d2  d3   0   0  0  0   d4 d5 d6 d7  0  0  0  0
03838 //
03839 //   RS=1 means that next byte is data, RS=0 means that next byte is command
03840 //   RW=0 means that next byte is writen, RW=1 means that next byte is read (not used in this lib)
03841 //
03842 // Note: SPI3_24 expects LSB first. This is inconsistent with regular SPI convention (and hardware) that sends MSB first.
03843 
03844   if (value) {
03845     _controlbyte = 0xFA; // Next byte is data
03846   }
03847   else {
03848     _controlbyte = 0xF8; // Next byte is command
03849   }   
03850 }    
03851 
03852 // Set BL pin
03853 void TextLCD_SPI_N_3_24::_setBL(bool value) {
03854     if (_bl) {
03855         _bl->write(value);   
03856     }    
03857 }    
03858 
03859 // Not used in this mode
03860 void TextLCD_SPI_N_3_24::_setData(int value) {
03861 }    
03862 
03863 //Mapping table to flip the bits around cause SPI3_24 expects LSB first.
03864 const uint8_t map3_24[16] = {0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0};
03865     
03866 // Write a byte using SPI3 24 bits mode
03867 void TextLCD_SPI_N_3_24::_writeByte(int value) {
03868     _cs = 0;
03869     wait_us(1);
03870     _spi->write(_controlbyte);
03871 
03872     //Map and send the LSB nibble
03873     _spi->write( map3_24[value & 0x0F]);     
03874 
03875     //Map and send the MSB nibble
03876     _spi->write( map3_24[(value >> 4) & 0x0F]);     
03877 
03878     wait_us(1);
03879     _cs = 1;
03880 }
03881 #endif /* Native SPI bus     */  
03882 //------- End TextLCD_SPI_N_3_24 ----------