fixed compiler warnings

Fork of TextLCD by Wim Huiskamp

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