Contrast not optimal
Fork of SB1602E by
SB1602E.cpp@2:baf578069dfc, 2015-04-07 (annotated)
- Committer:
- MACRUM
- Date:
- Tue Apr 07 05:03:44 2015 +0000
- Revision:
- 2:baf578069dfc
- Parent:
- 1:fce3e353410c
- Child:
- 3:712f2b6919f0
Add printf with X-Y parameters and setter of number of chars in a line
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okano | 0:995f80348e02 | 1 | /** Text LCD module "SB1602E" class library |
okano | 0:995f80348e02 | 2 | * |
MACRUM | 2:baf578069dfc | 3 | * @author Tedd OKANO, Masato YAMANISHI & Toyomasa Watarai |
MACRUM | 2:baf578069dfc | 4 | * @version 2.1 |
MACRUM | 2:baf578069dfc | 5 | * @date 07-April-2015 |
okano | 0:995f80348e02 | 6 | * |
okano | 0:995f80348e02 | 7 | * SB1602E is an I2C based low voltage text LCD panel (based Sitronix ST7032 chip) |
okano | 0:995f80348e02 | 8 | * The module by StrawberryLinux |
okano | 0:995f80348e02 | 9 | * http://strawberry-linux.com/catalog/items?code=27002 (Online shop page (Japanese)) |
okano | 0:995f80348e02 | 10 | * http://strawberry-linux.com/pub/ST7032i.pdf (datasheet of the chip) |
okano | 0:995f80348e02 | 11 | * |
okano | 0:995f80348e02 | 12 | * This is a library to operate this module easy. |
okano | 0:995f80348e02 | 13 | * |
okano | 0:995f80348e02 | 14 | * Released under the Apache 2 license License |
okano | 0:995f80348e02 | 15 | * |
okano | 0:995f80348e02 | 16 | * revision history (class lib name was "TextLCD_SB1602E") |
okano | 0:995f80348e02 | 17 | * revision 1.0 22-Jan-2010 a. 1st release |
okano | 0:995f80348e02 | 18 | * revision 1.1 23-Jan-2010 a. class name has been changed from lcd_SB1602E to TextLCD_SB1602E |
okano | 0:995f80348e02 | 19 | * b. printf() added |
okano | 0:995f80348e02 | 20 | * c. copyright notice added |
okano | 0:995f80348e02 | 21 | * revision 1.3 02-May-2014 a. puticon() added (for SB1602B) by Masato YAMANISHI san |
okano | 0:995f80348e02 | 22 | * revision 2.0 20-Oct-2014 a. class name is changed and published as "SB1602E" |
okano | 0:995f80348e02 | 23 | * b. re-written for better usability |
MACRUM | 2:baf578069dfc | 24 | * revision 2.1 07-Apl-2015 a. add printf() with X and Y position |
MACRUM | 2:baf578069dfc | 25 | * b. add setter for number of chars in a line (e.g. 8x2 LCD support) |
okano | 0:995f80348e02 | 26 | */ |
okano | 0:995f80348e02 | 27 | |
okano | 0:995f80348e02 | 28 | #include <stdarg.h> |
okano | 0:995f80348e02 | 29 | #include "mbed.h" |
okano | 0:995f80348e02 | 30 | #include "SB1602E.h" |
okano | 0:995f80348e02 | 31 | |
okano | 0:995f80348e02 | 32 | |
MACRUM | 2:baf578069dfc | 33 | SB1602E::SB1602E( PinName I2C_sda, PinName I2C_scl, char *init_massage ) : i2c_p( new I2C( I2C_sda, I2C_scl ) ), i2c( *i2c_p ), charsInLine( MaxCharsInALine ) |
okano | 0:995f80348e02 | 34 | { |
okano | 0:995f80348e02 | 35 | init( init_massage ); |
okano | 0:995f80348e02 | 36 | } |
okano | 0:995f80348e02 | 37 | |
MACRUM | 2:baf578069dfc | 38 | SB1602E::SB1602E( I2C &i2c_, char *init_massage ) : i2c_p( NULL ), i2c( i2c_ ), charsInLine( MaxCharsInALine ) |
okano | 0:995f80348e02 | 39 | { |
okano | 0:995f80348e02 | 40 | init( init_massage ); |
okano | 0:995f80348e02 | 41 | } |
okano | 0:995f80348e02 | 42 | |
okano | 0:995f80348e02 | 43 | SB1602E::~SB1602E() |
okano | 0:995f80348e02 | 44 | { |
okano | 0:995f80348e02 | 45 | if ( NULL != i2c_p ) |
okano | 0:995f80348e02 | 46 | delete i2c_p; |
okano | 0:995f80348e02 | 47 | } |
okano | 0:995f80348e02 | 48 | |
okano | 1:fce3e353410c | 49 | #define DEFAULT_CONTRAST 0x35 |
okano | 1:fce3e353410c | 50 | |
okano | 0:995f80348e02 | 51 | void SB1602E::init( char *init_massage ) |
okano | 0:995f80348e02 | 52 | { |
okano | 0:995f80348e02 | 53 | const char init_seq0[] = { |
okano | 0:995f80348e02 | 54 | Comm_FunctionSet_Normal, |
okano | 0:995f80348e02 | 55 | Comm_ReturnHome, // This may be required to reset the scroll function |
okano | 0:995f80348e02 | 56 | Comm_FunctionSet_Extended, |
okano | 0:995f80348e02 | 57 | Comm_InternalOscFrequency, |
okano | 0:995f80348e02 | 58 | Comm_ContrastSet | ( DEFAULT_CONTRAST & 0xF), |
okano | 0:995f80348e02 | 59 | Comm_PwrIconContrast | ((DEFAULT_CONTRAST >> 4) & 0x3), |
okano | 0:995f80348e02 | 60 | Comm_FollowerCtrl | 0x0A, |
okano | 0:995f80348e02 | 61 | }; |
okano | 0:995f80348e02 | 62 | const char init_seq1[] = { |
okano | 0:995f80348e02 | 63 | Comm_DisplayOnOff, |
okano | 0:995f80348e02 | 64 | Comm_ClearDisplay, |
okano | 0:995f80348e02 | 65 | Comm_EntryModeSet, |
okano | 0:995f80348e02 | 66 | }; |
okano | 0:995f80348e02 | 67 | |
okano | 0:995f80348e02 | 68 | i2c_addr = 0x7C; |
okano | 0:995f80348e02 | 69 | |
okano | 0:995f80348e02 | 70 | wait( 0.04 ); // interval after hardware reset |
okano | 0:995f80348e02 | 71 | |
okano | 0:995f80348e02 | 72 | for ( int i = 0; i < sizeof( init_seq0 ); i++ ) { |
okano | 0:995f80348e02 | 73 | lcd_command( init_seq0[ i ] ); |
okano | 0:995f80348e02 | 74 | wait( 30e-6 ); |
okano | 0:995f80348e02 | 75 | } |
okano | 0:995f80348e02 | 76 | |
okano | 0:995f80348e02 | 77 | wait( 0.2 ); |
okano | 0:995f80348e02 | 78 | |
okano | 0:995f80348e02 | 79 | for ( int i = 0; i < sizeof( init_seq1 ); i++ ) { |
okano | 0:995f80348e02 | 80 | lcd_command( init_seq1[ i ] ); |
okano | 0:995f80348e02 | 81 | wait( 2e-3 ); |
okano | 0:995f80348e02 | 82 | } |
okano | 0:995f80348e02 | 83 | |
okano | 0:995f80348e02 | 84 | set_CGRAM( 7, '\x1F' ); |
okano | 0:995f80348e02 | 85 | |
okano | 0:995f80348e02 | 86 | curs[ 0 ] = 0; |
okano | 0:995f80348e02 | 87 | curs[ 1 ] = 0; |
okano | 0:995f80348e02 | 88 | |
okano | 0:995f80348e02 | 89 | if ( init_massage ) |
okano | 0:995f80348e02 | 90 | { |
okano | 0:995f80348e02 | 91 | puts( 0, init_massage ); |
okano | 0:995f80348e02 | 92 | curs[ 0 ] = 0; |
okano | 0:995f80348e02 | 93 | } |
okano | 0:995f80348e02 | 94 | } |
okano | 0:995f80348e02 | 95 | |
okano | 0:995f80348e02 | 96 | void SB1602E::printf( char line, char *format, ... ) |
okano | 0:995f80348e02 | 97 | { |
okano | 0:995f80348e02 | 98 | char s[ 32 ]; |
okano | 0:995f80348e02 | 99 | va_list args; |
okano | 0:995f80348e02 | 100 | |
okano | 0:995f80348e02 | 101 | va_start( args, format ); |
okano | 0:995f80348e02 | 102 | vsnprintf( s, 32, format, args ); |
okano | 0:995f80348e02 | 103 | va_end( args ); |
okano | 0:995f80348e02 | 104 | |
okano | 0:995f80348e02 | 105 | puts( line, s ); |
okano | 0:995f80348e02 | 106 | } |
okano | 0:995f80348e02 | 107 | |
MACRUM | 2:baf578069dfc | 108 | void SB1602E::printf( char x, char y, char *format, ... ) |
MACRUM | 2:baf578069dfc | 109 | { |
MACRUM | 2:baf578069dfc | 110 | char s[ 32 ]; |
MACRUM | 2:baf578069dfc | 111 | va_list args; |
MACRUM | 2:baf578069dfc | 112 | |
MACRUM | 2:baf578069dfc | 113 | va_start( args, format ); |
MACRUM | 2:baf578069dfc | 114 | vsnprintf( s, 32, format, args ); |
MACRUM | 2:baf578069dfc | 115 | va_end( args ); |
MACRUM | 2:baf578069dfc | 116 | |
MACRUM | 2:baf578069dfc | 117 | curs[ y ] = x; |
MACRUM | 2:baf578069dfc | 118 | puts( y, s ); |
MACRUM | 2:baf578069dfc | 119 | } |
MACRUM | 2:baf578069dfc | 120 | |
okano | 0:995f80348e02 | 121 | void SB1602E::putc( char line, char c ) |
okano | 0:995f80348e02 | 122 | { |
okano | 0:995f80348e02 | 123 | if ( (c == '\n') || (c == '\r') ) { |
okano | 0:995f80348e02 | 124 | clear_lest_of_line( line ); |
okano | 0:995f80348e02 | 125 | curs[ line ] = 0; |
okano | 0:995f80348e02 | 126 | return; |
okano | 0:995f80348e02 | 127 | } |
okano | 0:995f80348e02 | 128 | |
okano | 0:995f80348e02 | 129 | putcxy( c, curs[ line ]++, line ); |
okano | 0:995f80348e02 | 130 | } |
okano | 0:995f80348e02 | 131 | |
okano | 0:995f80348e02 | 132 | void SB1602E::puts( char line, char *s ) |
okano | 0:995f80348e02 | 133 | { |
okano | 0:995f80348e02 | 134 | while ( char c = *s++ ) |
okano | 0:995f80348e02 | 135 | putc( line, c ); |
okano | 0:995f80348e02 | 136 | } |
okano | 0:995f80348e02 | 137 | |
okano | 0:995f80348e02 | 138 | void SB1602E::putcxy( char c, char x, char y ) |
okano | 0:995f80348e02 | 139 | { |
okano | 0:995f80348e02 | 140 | const char Comm_SetDDRAMAddress = 0x80; |
okano | 0:995f80348e02 | 141 | const char DDRAMAddress_Ofst[] = { 0x00, 0x40 }; |
okano | 0:995f80348e02 | 142 | |
MACRUM | 2:baf578069dfc | 143 | if ( (x >= charsInLine) || (y >= 2) ) |
okano | 0:995f80348e02 | 144 | return; |
okano | 0:995f80348e02 | 145 | |
okano | 0:995f80348e02 | 146 | lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[ y ]) + x ); |
okano | 0:995f80348e02 | 147 | lcd_data( c ); |
okano | 0:995f80348e02 | 148 | } |
okano | 0:995f80348e02 | 149 | |
okano | 0:995f80348e02 | 150 | void SB1602E::clear( void ) |
okano | 0:995f80348e02 | 151 | { |
okano | 0:995f80348e02 | 152 | lcd_command( Comm_ClearDisplay ); |
okano | 0:995f80348e02 | 153 | wait( 2e-3 ); |
okano | 0:995f80348e02 | 154 | curs[ 0 ] = 0; |
okano | 0:995f80348e02 | 155 | curs[ 1 ] = 0; |
okano | 0:995f80348e02 | 156 | } |
okano | 0:995f80348e02 | 157 | |
okano | 0:995f80348e02 | 158 | void SB1602E::contrast( char contrast ) |
okano | 0:995f80348e02 | 159 | { |
okano | 0:995f80348e02 | 160 | lcd_command( Comm_FunctionSet_Extended ); |
okano | 0:995f80348e02 | 161 | lcd_command( Comm_ContrastSet | (contrast & 0x0f) ); |
okano | 0:995f80348e02 | 162 | lcd_command( Comm_PwrIconContrast | ((contrast>>4) & 0x03) ); |
okano | 0:995f80348e02 | 163 | lcd_command( Comm_FunctionSet_Normal ); |
okano | 0:995f80348e02 | 164 | } |
okano | 0:995f80348e02 | 165 | |
okano | 0:995f80348e02 | 166 | void SB1602E::put_custom_char( char c_code, const char *cg, char x, char y ) |
okano | 0:995f80348e02 | 167 | { |
okano | 0:995f80348e02 | 168 | for ( int i = 0; i < 5; i++ ) { |
okano | 0:995f80348e02 | 169 | set_CGRAM( c_code, cg ); |
okano | 0:995f80348e02 | 170 | putcxy( c_code, x, y ); |
okano | 0:995f80348e02 | 171 | } |
okano | 0:995f80348e02 | 172 | } |
okano | 0:995f80348e02 | 173 | |
okano | 0:995f80348e02 | 174 | void SB1602E::set_CGRAM( char char_code, const char* cg ) |
okano | 0:995f80348e02 | 175 | { |
okano | 0:995f80348e02 | 176 | for ( int i = 0; i < 8; i++ ) { |
okano | 0:995f80348e02 | 177 | lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) ); |
okano | 0:995f80348e02 | 178 | lcd_data( *cg++ ); |
okano | 0:995f80348e02 | 179 | } |
okano | 0:995f80348e02 | 180 | } |
okano | 0:995f80348e02 | 181 | |
okano | 0:995f80348e02 | 182 | void SB1602E::set_CGRAM( char char_code, char v ) |
okano | 0:995f80348e02 | 183 | { |
okano | 0:995f80348e02 | 184 | char c[ 8 ]; |
okano | 0:995f80348e02 | 185 | |
okano | 0:995f80348e02 | 186 | for ( int i = 0; i < 8; i++ ) |
okano | 0:995f80348e02 | 187 | c[ i ] = v; |
okano | 0:995f80348e02 | 188 | |
okano | 0:995f80348e02 | 189 | set_CGRAM( char_code, c ); |
okano | 0:995f80348e02 | 190 | } |
okano | 0:995f80348e02 | 191 | |
okano | 0:995f80348e02 | 192 | void SB1602E::clear_lest_of_line( char line ) |
okano | 0:995f80348e02 | 193 | { |
MACRUM | 2:baf578069dfc | 194 | for ( int i = curs[ line ]; i < charsInLine; i++ ) |
okano | 0:995f80348e02 | 195 | putcxy( ' ', i, line ); |
okano | 0:995f80348e02 | 196 | } |
okano | 0:995f80348e02 | 197 | |
okano | 0:995f80348e02 | 198 | int SB1602E::lcd_write( char first, char second ) |
okano | 0:995f80348e02 | 199 | { |
okano | 0:995f80348e02 | 200 | char cmd[2]; |
okano | 0:995f80348e02 | 201 | |
okano | 0:995f80348e02 | 202 | cmd[ 0 ] = first; |
okano | 0:995f80348e02 | 203 | cmd[ 1 ] = second; |
okano | 0:995f80348e02 | 204 | |
okano | 0:995f80348e02 | 205 | return ( i2c.write( i2c_addr, cmd, 2 ) ); |
okano | 0:995f80348e02 | 206 | |
okano | 0:995f80348e02 | 207 | } |
okano | 0:995f80348e02 | 208 | |
okano | 0:995f80348e02 | 209 | int SB1602E::lcd_command( char command ) |
okano | 0:995f80348e02 | 210 | { |
okano | 0:995f80348e02 | 211 | return ( lcd_write( COMMAND, command ) ); |
okano | 0:995f80348e02 | 212 | } |
okano | 0:995f80348e02 | 213 | |
okano | 0:995f80348e02 | 214 | int SB1602E::lcd_data( char data ) |
okano | 0:995f80348e02 | 215 | { |
okano | 0:995f80348e02 | 216 | return ( lcd_write( DATA, data ) ); |
okano | 0:995f80348e02 | 217 | } |
okano | 0:995f80348e02 | 218 | |
okano | 0:995f80348e02 | 219 | // Following function has been imported from Masato YAMANISHI san's code. |
okano | 0:995f80348e02 | 220 | // Thank you! |
okano | 0:995f80348e02 | 221 | // http://developer.mbed.org/users/masato/code/TextLCD_SB1602E/file/39110c58e55c/TextLCD_SB1602E.h |
okano | 0:995f80348e02 | 222 | |
okano | 0:995f80348e02 | 223 | const unsigned char icon_data[]= { |
okano | 0:995f80348e02 | 224 | // アイコンアドレス, 該当ビット |
okano | 0:995f80348e02 | 225 | 0x00, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 226 | 0x02, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 227 | 0x04, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 228 | 0x06, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 229 | |
okano | 0:995f80348e02 | 230 | 0x07, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 231 | 0x07, 0x08, // 0b01000, |
okano | 0:995f80348e02 | 232 | 0x09, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 233 | 0x0B, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 234 | |
okano | 0:995f80348e02 | 235 | 0x0D, 0x08, // 0b01000, |
okano | 0:995f80348e02 | 236 | 0x0D, 0x04, // 0b00100, |
okano | 0:995f80348e02 | 237 | 0x0D, 0x02, // 0b00010, |
okano | 0:995f80348e02 | 238 | 0x0D, 0x10, // 0b10000, |
okano | 0:995f80348e02 | 239 | |
okano | 0:995f80348e02 | 240 | 0x0F, 0x10, // 0b10000, // アンテナマーク |
okano | 0:995f80348e02 | 241 | }; |
okano | 0:995f80348e02 | 242 | |
okano | 0:995f80348e02 | 243 | void SB1602E::puticon(unsigned short flg) |
okano | 0:995f80348e02 | 244 | { |
okano | 0:995f80348e02 | 245 | static unsigned char icon_buff[16]; // アイコンの編集用 |
okano | 0:995f80348e02 | 246 | unsigned char i; |
okano | 0:995f80348e02 | 247 | |
okano | 0:995f80348e02 | 248 | for(i=0; i<sizeof(icon_data)/2; i++) { |
okano | 0:995f80348e02 | 249 | if(flg & (0x1000>>i)) { // 該当ビットが立っていたら |
okano | 0:995f80348e02 | 250 | icon_buff[icon_data[i*2]] |= icon_data[i*2+1]; // バッファを立てます。 |
okano | 0:995f80348e02 | 251 | } else { |
okano | 0:995f80348e02 | 252 | icon_buff[icon_data[i*2]] &= ~icon_data[i*2+1]; // バッファをクリアします。 |
okano | 0:995f80348e02 | 253 | } |
okano | 0:995f80348e02 | 254 | } |
okano | 0:995f80348e02 | 255 | // 一括でLCDに書き込みます。 |
okano | 0:995f80348e02 | 256 | for(i=0; i<16; i++) { |
okano | 0:995f80348e02 | 257 | lcd_command(Comm_FunctionSet_Extended); // 0b00111001); // コマンド |
okano | 0:995f80348e02 | 258 | lcd_command(Comm_SetCGRAM + i); // 0b01000000+i); // アイコン領域のアドレスを設定 |
okano | 0:995f80348e02 | 259 | lcd_data(icon_buff[i]); // アイコンデータ |
okano | 0:995f80348e02 | 260 | } |
okano | 0:995f80348e02 | 261 | } |
okano | 0:995f80348e02 | 262 | |
okano | 0:995f80348e02 | 263 | |
okano | 0:995f80348e02 | 264 | |
okano | 0:995f80348e02 | 265 | |
okano | 0:995f80348e02 | 266 | |
okano | 0:995f80348e02 | 267 |