Version 2.1 of TextLCD_SB1602E (forked).

Dependents:   BME280_environmental_recorder kids_workshop

Fork of SB1602E by Tedd OKANO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SB1602E.cpp Source File

SB1602E.cpp

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