added puticon() for SB1602B

Dependencies:   mbed

Fork of TextLCD_SB1602E by Tedd OKANO

Tedd OKANO さんの Strawberry Linux の i2c LCD SB1602E ライブラリSB1602B のアイコン表示メソッドを追加。STM32 Nucleo L152RE/mbed でテストしてます。 /media/uploads/masato/sb1602b.jpg

こういう時は Pull Request すべきなのかな。

Committer:
masato
Date:
Sat May 03 13:33:42 2014 +0000
Revision:
1:39110c58e55c
Parent:
0:694061176edf
add puticon() for SB1602B

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 0:694061176edf 1 /*
okano 0:694061176edf 2 * LCD module "SB1602E" library
okano 0:694061176edf 3 *
okano 0:694061176edf 4 * SB1602E is an I2C based low voltage text LCD panel (based Sitronix ST7032 chip)
okano 0:694061176edf 5 * The module by StrawberryLinux
okano 0:694061176edf 6 * http://strawberry-linux.com/catalog/items?code=27002 (Online shop page (Japanese))
okano 0:694061176edf 7 * http://strawberry-linux.com/pub/ST7032i.pdf (datasheet of the chip)
okano 0:694061176edf 8 *
okano 0:694061176edf 9 * This is a library to operate this module easy.
okano 0:694061176edf 10 *
okano 0:694061176edf 11 * Copyright (c) 2010 Tedd OKANO
okano 0:694061176edf 12 * Released under the MIT License: http://mbed.org/license/mit
okano 0:694061176edf 13 *
okano 0:694061176edf 14 * revision 1.0 22-Jan-2010 a. 1st release
okano 0:694061176edf 15 * revision 1.1 23-Jan-2010 a. class name has been changed from lcd_SB1602E to TextLCD_SB1602E
masato 1:39110c58e55c 16 * b. printf() added
okano 0:694061176edf 17 * c. copyright notice added
masato 1:39110c58e55c 18 * revision 1.3 02-May-2014 a. puticon() added (for SB1602B)
okano 0:694061176edf 19 */
okano 0:694061176edf 20
okano 0:694061176edf 21 #ifndef MBED_TextLCD_SB1602E
okano 0:694061176edf 22 #define MBED_TextLCD_SB1602E
okano 0:694061176edf 23
okano 0:694061176edf 24 #include <stdarg.h>
okano 0:694061176edf 25 #include "mbed.h"
okano 0:694061176edf 26 #include "I2cBusDevice.h"
okano 0:694061176edf 27
okano 0:694061176edf 28 // SB1602E IIC address
okano 0:694061176edf 29
okano 0:694061176edf 30 const char SB1602E_addr = 0x7C;
okano 0:694061176edf 31
okano 0:694061176edf 32 // SB1602E initialization command sequence
okano 0:694061176edf 33
okano 0:694061176edf 34 #ifdef INIT_VALUE_DATASHEET_ORIGINAL
okano 0:694061176edf 35
okano 0:694061176edf 36 const char Comm_FunctionSet_Normal = 0x38;
okano 0:694061176edf 37 const char Comm_FunctionSet_Extended = 0x39;
okano 0:694061176edf 38 const char Comm_InternalOscFrequency = 0x14;
okano 0:694061176edf 39 const char Comm_ContrastSet = 0x78;
okano 0:694061176edf 40 const char Comm_PwrIconContrast = 0x5E;
okano 0:694061176edf 41 const char Comm_FollowerCtrl = 0x6A;
okano 0:694061176edf 42 const char Comm_DisplayOnOff = 0x0C;
okano 0:694061176edf 43 const char Comm_ClearDisplay = 0x01;
okano 0:694061176edf 44 const char Comm_EntryModeSet = 0x06;
okano 0:694061176edf 45
okano 0:694061176edf 46 #else
okano 0:694061176edf 47
okano 0:694061176edf 48 const char Comm_FunctionSet_Normal = 0x38;
okano 0:694061176edf 49 const char Comm_FunctionSet_Extended = 0x39;
okano 0:694061176edf 50 const char Comm_InternalOscFrequency = 0x14;
okano 0:694061176edf 51 const char Comm_ContrastSet = 0x70;
okano 0:694061176edf 52 const char Comm_PwrIconContrast = 0x5C;
okano 0:694061176edf 53 const char Comm_FollowerCtrl = 0x60;
okano 0:694061176edf 54 const char Comm_DisplayOnOff = 0x0C;
okano 0:694061176edf 55 const char Comm_ClearDisplay = 0x01;
okano 0:694061176edf 56 const char Comm_EntryModeSet = 0x04;
okano 0:694061176edf 57 const char Comm_ReturnHome = 0x02;
okano 0:694061176edf 58
okano 0:694061176edf 59 #endif
okano 0:694061176edf 60
okano 0:694061176edf 61 // SB1602E general commands
okano 0:694061176edf 62
okano 0:694061176edf 63 const char Comm_SetDDRAMAddress = 0x80;
okano 0:694061176edf 64 const char DDRAMAddress_Ofst[] = { 0x00, 0x40 };
okano 0:694061176edf 65
okano 0:694061176edf 66 const char Comm_SetCGRAM = 0x40;
okano 0:694061176edf 67
okano 0:694061176edf 68 // SB1602E setting values
okano 0:694061176edf 69
okano 0:694061176edf 70 const char default_Contrast = 0x35;
okano 0:694061176edf 71
okano 0:694061176edf 72 const char COMMAND = 0x00;
okano 0:694061176edf 73 const char DATA = 0x40;
okano 0:694061176edf 74
okano 0:694061176edf 75 const char MaxCharsInALine = 0x10; // buffer deoth for one line (no scroll function used)
okano 0:694061176edf 76
okano 0:694061176edf 77 const char init_seq0_length = 7;
okano 0:694061176edf 78 const char init_seq0[ init_seq0_length ]
okano 0:694061176edf 79 = {
okano 0:694061176edf 80 Comm_FunctionSet_Normal,
okano 0:694061176edf 81 Comm_ReturnHome, // This may be required to reset the scroll function
okano 0:694061176edf 82 Comm_FunctionSet_Extended,
okano 0:694061176edf 83 Comm_InternalOscFrequency,
okano 0:694061176edf 84 Comm_ContrastSet | ( default_Contrast & 0xF),
okano 0:694061176edf 85 Comm_PwrIconContrast | ((default_Contrast >> 4) & 0x3),
okano 0:694061176edf 86 Comm_FollowerCtrl | 0x0A,
okano 0:694061176edf 87
okano 0:694061176edf 88 };
okano 0:694061176edf 89 // required 30us interval
okano 0:694061176edf 90
okano 0:694061176edf 91 const char init_seq1_length = 3;
okano 0:694061176edf 92 const char init_seq1[ init_seq1_length ]
okano 0:694061176edf 93 = {
okano 0:694061176edf 94 Comm_DisplayOnOff,
okano 0:694061176edf 95 Comm_ClearDisplay,
okano 0:694061176edf 96 Comm_EntryModeSet,
okano 0:694061176edf 97 };
okano 0:694061176edf 98 // required 30us, 2ms interval
okano 0:694061176edf 99
masato 1:39110c58e55c 100 const unsigned char icon_data[]={
masato 1:39110c58e55c 101 // アイコンアドレス, 該当ビット
masato 1:39110c58e55c 102 0x00, 0x10, // 0b10000,
masato 1:39110c58e55c 103 0x02, 0x10, // 0b10000,
masato 1:39110c58e55c 104 0x04, 0x10, // 0b10000,
masato 1:39110c58e55c 105 0x06, 0x10, // 0b10000,
masato 1:39110c58e55c 106
masato 1:39110c58e55c 107 0x07, 0x10, // 0b10000,
masato 1:39110c58e55c 108 0x07, 0x08, // 0b01000,
masato 1:39110c58e55c 109 0x09, 0x10, // 0b10000,
masato 1:39110c58e55c 110 0x0B, 0x10, // 0b10000,
masato 1:39110c58e55c 111
masato 1:39110c58e55c 112 0x0D, 0x08, // 0b01000,
masato 1:39110c58e55c 113 0x0D, 0x04, // 0b00100,
masato 1:39110c58e55c 114 0x0D, 0x02, // 0b00010,
masato 1:39110c58e55c 115 0x0D, 0x10, // 0b10000,
masato 1:39110c58e55c 116
masato 1:39110c58e55c 117 0x0F, 0x10, // 0b10000, // アンテナマーク
masato 1:39110c58e55c 118 };
masato 1:39110c58e55c 119
okano 0:694061176edf 120
okano 0:694061176edf 121 class TextLCD_SB1602E : I2cBusDevice {
okano 0:694061176edf 122 public:
okano 0:694061176edf 123
okano 0:694061176edf 124 explicit TextLCD_SB1602E( I2C *i2c, char dev_address = SB1602E_addr, char *init_massage = NULL ) : I2cBusDevice( i2c, dev_address ) {
okano 0:694061176edf 125 wait( 0.04 ); // interval after hardware reset
masato 1:39110c58e55c 126 i2c->frequency(400000); // max 400KHz
okano 0:694061176edf 127 for ( int i = 0; i < init_seq0_length; i++ ) {
okano 0:694061176edf 128 lcd_command( init_seq0[ i ] );
okano 0:694061176edf 129 wait( 30e-6 );
okano 0:694061176edf 130 }
okano 0:694061176edf 131
okano 0:694061176edf 132 wait( 0.2 );
okano 0:694061176edf 133
okano 0:694061176edf 134 for ( int i = 0; i < init_seq1_length; i++ ) {
okano 0:694061176edf 135 lcd_command( init_seq1[ i ] );
okano 0:694061176edf 136 wait( 2e-3 );
okano 0:694061176edf 137 }
okano 0:694061176edf 138
okano 0:694061176edf 139 if ( init_massage )
okano 0:694061176edf 140 puts( 0, init_massage );
okano 0:694061176edf 141
okano 0:694061176edf 142 set_CGRAM( 7, '\x1F' );
okano 0:694061176edf 143
okano 0:694061176edf 144 curs[ 0 ] = 0;
okano 0:694061176edf 145 curs[ 1 ] = 0;
okano 0:694061176edf 146 }
okano 0:694061176edf 147
okano 0:694061176edf 148
okano 0:694061176edf 149 ~TextLCD_SB1602E() {
okano 0:694061176edf 150 }
okano 0:694061176edf 151
okano 0:694061176edf 152 void clear( void ) {
okano 0:694061176edf 153 lcd_command( Comm_ClearDisplay );
okano 0:694061176edf 154 wait( 2e-3 );
okano 0:694061176edf 155 curs[ 0 ] = 0;
okano 0:694061176edf 156 curs[ 1 ] = 0;
okano 0:694061176edf 157 }
okano 0:694061176edf 158
okano 0:694061176edf 159
okano 0:694061176edf 160 void put_custom_char( char c_code, const char *cg, char x, char y ) {
okano 0:694061176edf 161 for ( int i = 0; i < 5; i++ ) {
okano 0:694061176edf 162 set_CGRAM( c_code, cg );
okano 0:694061176edf 163 putcxy( c_code, x, y );
okano 0:694061176edf 164 }
okano 0:694061176edf 165 }
okano 0:694061176edf 166
okano 0:694061176edf 167 void contrast( char contrast ) {
okano 0:694061176edf 168 lcd_command( Comm_FunctionSet_Extended );
okano 0:694061176edf 169 lcd_command( Comm_ContrastSet | (contrast & 0x0f) );
okano 0:694061176edf 170 lcd_command( Comm_PwrIconContrast | ((contrast>>4) & 0x03) );
okano 0:694061176edf 171 lcd_command( Comm_FunctionSet_Normal );
okano 0:694061176edf 172 }
okano 0:694061176edf 173
okano 0:694061176edf 174 void set_CGRAM( char char_code, const char* cg ) {
okano 0:694061176edf 175 for ( int i = 0; i < 8; i++ ) {
okano 0:694061176edf 176 lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) );
okano 0:694061176edf 177 lcd_data( *cg++ );
okano 0:694061176edf 178 }
okano 0:694061176edf 179 }
okano 0:694061176edf 180
okano 0:694061176edf 181 void set_CGRAM( char char_code, char v ) {
okano 0:694061176edf 182 char c[ 8 ];
okano 0:694061176edf 183
okano 0:694061176edf 184 for ( int i = 0; i < 8; i++ )
okano 0:694061176edf 185 c[ i ] = v;
okano 0:694061176edf 186
okano 0:694061176edf 187 set_CGRAM( char_code, c );
okano 0:694061176edf 188 }
okano 0:694061176edf 189
okano 0:694061176edf 190 void putcxy( char c, char x, char y ) {
okano 0:694061176edf 191 if ( (x >= MaxCharsInALine) || (y >= 2) )
okano 0:694061176edf 192 return;
okano 0:694061176edf 193
okano 0:694061176edf 194 lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[ y ]) + x );
okano 0:694061176edf 195 lcd_data( c );
okano 0:694061176edf 196 }
okano 0:694061176edf 197
okano 0:694061176edf 198 void putc( char line, char c ) {
okano 0:694061176edf 199 if ( (c == '\n') || (c == '\r') ) {
okano 0:694061176edf 200 clear_lest_of_line( line );
okano 0:694061176edf 201 curs[ line ] = 0;
okano 0:694061176edf 202 return;
okano 0:694061176edf 203 }
okano 0:694061176edf 204
okano 0:694061176edf 205 putcxy( c, curs[ line ]++, line );
okano 0:694061176edf 206 }
okano 0:694061176edf 207
okano 0:694061176edf 208 void puts( char line, char *s ) {
okano 0:694061176edf 209 while ( char c = *s++ )
okano 0:694061176edf 210 putc( line, c );
okano 0:694061176edf 211 }
okano 0:694061176edf 212
okano 0:694061176edf 213 void printf( char line, char *format, ... ) {
okano 0:694061176edf 214 char s[ 32 ];
okano 0:694061176edf 215 va_list args;
okano 0:694061176edf 216
okano 0:694061176edf 217 va_start( args, format );
okano 0:694061176edf 218 vsnprintf( s, 32, format, args );
okano 0:694061176edf 219 va_end( args );
okano 0:694061176edf 220
okano 0:694061176edf 221 puts( line, s );
okano 0:694061176edf 222 }
masato 1:39110c58e55c 223
masato 1:39110c58e55c 224 void puticon(unsigned short flg)
masato 1:39110c58e55c 225 {
masato 1:39110c58e55c 226 static unsigned char icon_buff[16]; // アイコンの編集用
masato 1:39110c58e55c 227 unsigned char i;
masato 1:39110c58e55c 228
masato 1:39110c58e55c 229 for(i=0;i<sizeof(icon_data)/2;i++){
masato 1:39110c58e55c 230 if(flg & (0x1000>>i)){ // 該当ビットが立っていたら
masato 1:39110c58e55c 231 icon_buff[icon_data[i*2]] |= icon_data[i*2+1]; // バッファを立てます。
masato 1:39110c58e55c 232 } else {
masato 1:39110c58e55c 233 icon_buff[icon_data[i*2]] &= ~icon_data[i*2+1]; // バッファをクリアします。
masato 1:39110c58e55c 234 }
masato 1:39110c58e55c 235 }
masato 1:39110c58e55c 236 // 一括でLCDに書き込みます。
masato 1:39110c58e55c 237 for(i=0;i<16;i++){
masato 1:39110c58e55c 238 lcd_command(Comm_FunctionSet_Extended); // 0b00111001); // コマンド
masato 1:39110c58e55c 239 lcd_command(Comm_SetCGRAM + i); // 0b01000000+i); // アイコン領域のアドレスを設定
masato 1:39110c58e55c 240 lcd_data(icon_buff[i]); // アイコンデータ
masato 1:39110c58e55c 241 }
masato 1:39110c58e55c 242 }
okano 0:694061176edf 243
okano 0:694061176edf 244 private:
okano 0:694061176edf 245 char curs[2];
okano 0:694061176edf 246
okano 0:694061176edf 247 void clear_lest_of_line( char line ) {
okano 0:694061176edf 248 for ( int i = curs[ line ]; i < MaxCharsInALine; i++ )
okano 0:694061176edf 249 putcxy( ' ', i, line );
okano 0:694061176edf 250 }
okano 0:694061176edf 251
okano 0:694061176edf 252 int lcd_write( char first, char second ) {
okano 0:694061176edf 253 char cmd[2];
okano 0:694061176edf 254
okano 0:694061176edf 255 cmd[ 0 ] = first;
okano 0:694061176edf 256 cmd[ 1 ] = second;
okano 0:694061176edf 257
okano 0:694061176edf 258 return ( write( cmd, 2 ) );
okano 0:694061176edf 259
okano 0:694061176edf 260 }
okano 0:694061176edf 261
okano 0:694061176edf 262 int lcd_command( char command ) {
okano 0:694061176edf 263 return ( lcd_write( COMMAND, command ) );
okano 0:694061176edf 264 }
okano 0:694061176edf 265
okano 0:694061176edf 266 int lcd_data( char data ) {
okano 0:694061176edf 267 return ( lcd_write( DATA, data ) );
okano 0:694061176edf 268 }
okano 0:694061176edf 269 }
okano 0:694061176edf 270 ;
okano 0:694061176edf 271
okano 0:694061176edf 272 #endif