added puticon() for SB1602B

Dependencies:   mbed

Fork of TextLCD_SB1602E by Tedd OKANO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TextLCD_SB1602E.h Source File

TextLCD_SB1602E.h

00001 /*
00002  *  LCD module "SB1602E" library
00003  *
00004  *  SB1602E is an I2C based low voltage text LCD panel (based Sitronix ST7032 chip)
00005  *  The module by StrawberryLinux
00006  *  http://strawberry-linux.com/catalog/items?code=27002 (Online shop page (Japanese))
00007  *  http://strawberry-linux.com/pub/ST7032i.pdf (datasheet of the chip)
00008  *
00009  *  This is a library to operate this module easy.
00010  *
00011  *  Copyright (c) 2010 Tedd OKANO
00012  *  Released under the MIT License: http://mbed.org/license/mit
00013  *
00014  *  revision 1.0  22-Jan-2010   a. 1st release
00015  *  revision 1.1  23-Jan-2010   a. class name has been changed from lcd_SB1602E to TextLCD_SB1602E
00016  *                              b. printf() added
00017  *                              c. copyright notice added
00018  *  revision 1.3  02-May-2014   a. puticon() added (for SB1602B)
00019  */
00020 
00021 #ifndef        MBED_TextLCD_SB1602E
00022 #define        MBED_TextLCD_SB1602E
00023 
00024 #include    <stdarg.h>
00025 #include    "mbed.h"
00026 #include    "I2cBusDevice.h"
00027 
00028 //  SB1602E IIC address
00029 
00030 const char    SB1602E_addr = 0x7C;
00031 
00032 //  SB1602E initialization command sequence
00033 
00034 #ifdef INIT_VALUE_DATASHEET_ORIGINAL
00035 
00036 const char    Comm_FunctionSet_Normal      = 0x38;
00037 const char    Comm_FunctionSet_Extended    = 0x39;
00038 const char    Comm_InternalOscFrequency    = 0x14;
00039 const char    Comm_ContrastSet             = 0x78;
00040 const char    Comm_PwrIconContrast         = 0x5E;
00041 const char    Comm_FollowerCtrl            = 0x6A;
00042 const char    Comm_DisplayOnOff            = 0x0C;
00043 const char    Comm_ClearDisplay            = 0x01;
00044 const char    Comm_EntryModeSet            = 0x06;
00045 
00046 #else
00047 
00048 const char    Comm_FunctionSet_Normal      = 0x38;
00049 const char    Comm_FunctionSet_Extended    = 0x39;
00050 const char    Comm_InternalOscFrequency    = 0x14;
00051 const char    Comm_ContrastSet             = 0x70;
00052 const char    Comm_PwrIconContrast         = 0x5C;
00053 const char    Comm_FollowerCtrl            = 0x60;
00054 const char    Comm_DisplayOnOff            = 0x0C;
00055 const char    Comm_ClearDisplay            = 0x01;
00056 const char    Comm_EntryModeSet            = 0x04;
00057 const char    Comm_ReturnHome              = 0x02;
00058 
00059 #endif
00060 
00061 //  SB1602E general commands
00062 
00063 const char    Comm_SetDDRAMAddress        = 0x80;
00064 const char    DDRAMAddress_Ofst[]         = { 0x00, 0x40 };
00065 
00066 const char    Comm_SetCGRAM               = 0x40;
00067 
00068 //  SB1602E setting values
00069 
00070 const char    default_Contrast            = 0x35;
00071 
00072 const char    COMMAND                     = 0x00;
00073 const char    DATA                        = 0x40;
00074 
00075 const char    MaxCharsInALine             = 0x10;    //    buffer deoth for one line (no scroll function used)
00076 
00077 const char    init_seq0_length  = 7;
00078 const char    init_seq0[ init_seq0_length ]
00079 =    {
00080     Comm_FunctionSet_Normal,
00081     Comm_ReturnHome,             //    This may be required to reset the scroll function
00082     Comm_FunctionSet_Extended,
00083     Comm_InternalOscFrequency,
00084     Comm_ContrastSet            | ( default_Contrast       & 0xF),
00085     Comm_PwrIconContrast        | ((default_Contrast >> 4) & 0x3),
00086     Comm_FollowerCtrl           | 0x0A,
00087 
00088 };
00089 // required 30us interval
00090 
00091 const char    init_seq1_length  = 3;
00092 const char    init_seq1[ init_seq1_length ]
00093 =    {
00094     Comm_DisplayOnOff,
00095     Comm_ClearDisplay,
00096     Comm_EntryModeSet,
00097 };
00098 // required 30us, 2ms interval
00099 
00100 const unsigned char icon_data[]={
00101     // アイコンアドレス, 該当ビット
00102     0x00, 0x10, // 0b10000,
00103     0x02, 0x10, // 0b10000,
00104     0x04, 0x10, // 0b10000,
00105     0x06, 0x10, // 0b10000,
00106     
00107     0x07, 0x10, // 0b10000,
00108     0x07, 0x08, // 0b01000,
00109     0x09, 0x10, // 0b10000,
00110     0x0B, 0x10, // 0b10000,
00111     
00112     0x0D, 0x08, // 0b01000,
00113     0x0D, 0x04, // 0b00100,
00114     0x0D, 0x02, // 0b00010,
00115     0x0D, 0x10, // 0b10000,
00116     
00117     0x0F, 0x10, // 0b10000, // アンテナマーク
00118 };
00119 
00120 
00121 class TextLCD_SB1602E : I2cBusDevice {
00122 public:
00123 
00124     explicit TextLCD_SB1602E( I2C *i2c, char dev_address = SB1602E_addr, char *init_massage = NULL ) : I2cBusDevice( i2c, dev_address ) {
00125         wait( 0.04 );    //    interval after hardware reset
00126         i2c->frequency(400000); // max 400KHz
00127         for ( int i = 0; i < init_seq0_length; i++ ) {
00128             lcd_command( init_seq0[ i ] );
00129             wait( 30e-6 );
00130         }
00131 
00132         wait( 0.2 );
00133 
00134         for ( int i = 0; i < init_seq1_length; i++ ) {
00135             lcd_command( init_seq1[ i ] );
00136             wait( 2e-3 );
00137         }
00138 
00139         if ( init_massage )
00140             puts( 0, init_massage );
00141 
00142         set_CGRAM( 7, '\x1F' );
00143 
00144         curs[ 0 ]    = 0;
00145         curs[ 1 ]    = 0;
00146     }
00147 
00148 
00149     ~TextLCD_SB1602E() {
00150     }
00151 
00152     void clear( void ) {
00153         lcd_command( Comm_ClearDisplay );
00154         wait( 2e-3 );
00155         curs[ 0 ]    = 0;
00156         curs[ 1 ]    = 0;
00157     }
00158 
00159 
00160     void put_custom_char( char c_code, const char *cg, char x, char y ) {
00161         for ( int i = 0; i < 5; i++ ) {
00162             set_CGRAM( c_code, cg );
00163             putcxy( c_code, x, y );
00164         }
00165     }
00166 
00167     void contrast( char contrast ) {
00168         lcd_command( Comm_FunctionSet_Extended );
00169         lcd_command( Comm_ContrastSet         |  (contrast     & 0x0f) );
00170         lcd_command( Comm_PwrIconContrast     | ((contrast>>4) & 0x03) );
00171         lcd_command( Comm_FunctionSet_Normal   );
00172     }
00173 
00174     void set_CGRAM( char char_code, const char* cg ) {
00175         for ( int i = 0; i < 8; i++ ) {
00176             lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) );
00177             lcd_data( *cg++ );
00178         }
00179     }
00180 
00181     void set_CGRAM( char char_code, char v ) {
00182         char    c[ 8 ];
00183 
00184         for ( int i = 0; i < 8; i++ )
00185             c[ i ]    = v;
00186 
00187         set_CGRAM( char_code, c );
00188     }
00189 
00190     void putcxy( char c, char x, char y ) {
00191         if ( (x >= MaxCharsInALine) || (y >= 2) )
00192             return;
00193 
00194         lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[ y ]) + x );
00195         lcd_data( c );
00196     }
00197 
00198     void putc( char line, char c ) {
00199         if ( (c == '\n') || (c == '\r') ) {
00200             clear_lest_of_line( line );
00201             curs[ line ]    = 0;
00202             return;
00203         }
00204 
00205         putcxy( c, curs[ line ]++, line );
00206     }
00207 
00208     void puts( char line, char *s ) {
00209         while ( char c    = *s++ )
00210             putc( line, c );
00211     }
00212 
00213     void printf( char line, char *format, ... ) {
00214         char        s[ 32 ];
00215         va_list        args;
00216 
00217         va_start( args, format );
00218         vsnprintf( s, 32, format, args );
00219         va_end( args );
00220 
00221         puts( line, s );
00222     }
00223     
00224     void puticon(unsigned short flg)
00225     {
00226         static unsigned char icon_buff[16]; // アイコンの編集用
00227         unsigned char i;
00228     
00229         for(i=0;i<sizeof(icon_data)/2;i++){
00230             if(flg & (0x1000>>i)){  // 該当ビットが立っていたら
00231                 icon_buff[icon_data[i*2]] |= icon_data[i*2+1];  // バッファを立てます。
00232             } else {
00233                 icon_buff[icon_data[i*2]] &= ~icon_data[i*2+1]; // バッファをクリアします。
00234             }
00235         }
00236         // 一括でLCDに書き込みます。
00237         for(i=0;i<16;i++){
00238             lcd_command(Comm_FunctionSet_Extended); // 0b00111001);    // コマンド
00239             lcd_command(Comm_SetCGRAM + i); // 0b01000000+i);  // アイコン領域のアドレスを設定
00240             lcd_data(icon_buff[i]); // アイコンデータ
00241         }
00242     }
00243 
00244 private:
00245     char    curs[2];
00246 
00247     void clear_lest_of_line( char line ) {
00248         for ( int i = curs[ line ]; i < MaxCharsInALine; i++ )
00249             putcxy( ' ', i, line );
00250     }
00251 
00252     int lcd_write( char first, char second ) {
00253         char cmd[2];
00254 
00255         cmd[ 0 ]    = first;
00256         cmd[ 1 ]    = second;
00257 
00258         return ( write( cmd, 2 ) );
00259 
00260     }
00261 
00262     int lcd_command( char command ) {
00263         return ( lcd_write( COMMAND, command ) );
00264     }
00265 
00266     int lcd_data( char data ) {
00267         return ( lcd_write( DATA, data ) );
00268     }
00269 }
00270 ;
00271 
00272 #endif