Accel
Dependencies: mbed PowerControl SDFileSystem
Fork of HeptaAccel by
hepta_sat/HeptaLcd.h@1:63c3921c608c, 2016-12-13 (annotated)
- Committer:
- tomoya123
- Date:
- Tue Dec 13 07:51:40 2016 +0000
- Revision:
- 1:63c3921c608c
- Parent:
- 0:d721efd58e4e
Accel
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tomoya123 | 0:d721efd58e4e | 1 | #ifndef MBED_HeptaLcd |
tomoya123 | 0:d721efd58e4e | 2 | #define MBED_HeptaLcd |
tomoya123 | 0:d721efd58e4e | 3 | |
tomoya123 | 0:d721efd58e4e | 4 | #include <stdarg.h> |
tomoya123 | 0:d721efd58e4e | 5 | #include "mbed.h" |
tomoya123 | 0:d721efd58e4e | 6 | #include "I2cBusDevice.h" |
tomoya123 | 0:d721efd58e4e | 7 | |
tomoya123 | 0:d721efd58e4e | 8 | // SB1602E IIC address |
tomoya123 | 0:d721efd58e4e | 9 | |
tomoya123 | 0:d721efd58e4e | 10 | const char SB1602E_addr = 0x7C; |
tomoya123 | 0:d721efd58e4e | 11 | |
tomoya123 | 0:d721efd58e4e | 12 | // SB1602E initialization command sequence |
tomoya123 | 0:d721efd58e4e | 13 | |
tomoya123 | 0:d721efd58e4e | 14 | #ifdef INIT_VALUE_DATASHEET_ORIGINAL |
tomoya123 | 0:d721efd58e4e | 15 | |
tomoya123 | 0:d721efd58e4e | 16 | const char Comm_FunctionSet_Normal = 0x38; |
tomoya123 | 0:d721efd58e4e | 17 | const char Comm_FunctionSet_Extended = 0x39; |
tomoya123 | 0:d721efd58e4e | 18 | const char Comm_InternalOscFrequency = 0x14; |
tomoya123 | 0:d721efd58e4e | 19 | const char Comm_ContrastSet = 0x78; |
tomoya123 | 0:d721efd58e4e | 20 | const char Comm_PwrIconContrast = 0x5E; |
tomoya123 | 0:d721efd58e4e | 21 | const char Comm_FollowerCtrl = 0x6A; |
tomoya123 | 0:d721efd58e4e | 22 | const char Comm_DisplayOnOff = 0x0C; |
tomoya123 | 0:d721efd58e4e | 23 | const char Comm_ClearDisplay = 0x01; |
tomoya123 | 0:d721efd58e4e | 24 | const char Comm_EntryModeSet = 0x06; |
tomoya123 | 0:d721efd58e4e | 25 | |
tomoya123 | 0:d721efd58e4e | 26 | #else |
tomoya123 | 0:d721efd58e4e | 27 | |
tomoya123 | 0:d721efd58e4e | 28 | const char Comm_FunctionSet_Normal = 0x38; |
tomoya123 | 0:d721efd58e4e | 29 | const char Comm_FunctionSet_Extended = 0x39; |
tomoya123 | 0:d721efd58e4e | 30 | const char Comm_InternalOscFrequency = 0x14; |
tomoya123 | 0:d721efd58e4e | 31 | const char Comm_ContrastSet = 0x70; |
tomoya123 | 0:d721efd58e4e | 32 | const char Comm_PwrIconContrast = 0x5C; |
tomoya123 | 0:d721efd58e4e | 33 | const char Comm_FollowerCtrl = 0x60; |
tomoya123 | 0:d721efd58e4e | 34 | const char Comm_DisplayOnOff = 0x0C; |
tomoya123 | 0:d721efd58e4e | 35 | const char Comm_ClearDisplay = 0x01; |
tomoya123 | 0:d721efd58e4e | 36 | const char Comm_EntryModeSet = 0x04; |
tomoya123 | 0:d721efd58e4e | 37 | const char Comm_ReturnHome = 0x02; |
tomoya123 | 0:d721efd58e4e | 38 | |
tomoya123 | 0:d721efd58e4e | 39 | #endif |
tomoya123 | 0:d721efd58e4e | 40 | |
tomoya123 | 0:d721efd58e4e | 41 | // SB1602E general commands |
tomoya123 | 0:d721efd58e4e | 42 | |
tomoya123 | 0:d721efd58e4e | 43 | const char Comm_SetDDRAMAddress = 0x80; |
tomoya123 | 0:d721efd58e4e | 44 | const char DDRAMAddress_Ofst[] = { 0x00, 0x40 }; |
tomoya123 | 0:d721efd58e4e | 45 | |
tomoya123 | 0:d721efd58e4e | 46 | const char Comm_SetCGRAM = 0x40; |
tomoya123 | 0:d721efd58e4e | 47 | |
tomoya123 | 0:d721efd58e4e | 48 | // SB1602E setting values |
tomoya123 | 0:d721efd58e4e | 49 | |
tomoya123 | 0:d721efd58e4e | 50 | const char default_Contrast = 0x35; |
tomoya123 | 0:d721efd58e4e | 51 | |
tomoya123 | 0:d721efd58e4e | 52 | const char COMMAND = 0x00; |
tomoya123 | 0:d721efd58e4e | 53 | const char DATA = 0x40; |
tomoya123 | 0:d721efd58e4e | 54 | |
tomoya123 | 0:d721efd58e4e | 55 | const char MaxCharsInALine = 0x10; // buffer deoth for one line (no scroll function used) |
tomoya123 | 0:d721efd58e4e | 56 | |
tomoya123 | 0:d721efd58e4e | 57 | const char init_seq0_length = 7; |
tomoya123 | 0:d721efd58e4e | 58 | const char init_seq0[ init_seq0_length ] |
tomoya123 | 0:d721efd58e4e | 59 | = { |
tomoya123 | 0:d721efd58e4e | 60 | Comm_FunctionSet_Normal, |
tomoya123 | 0:d721efd58e4e | 61 | Comm_ReturnHome, // This may be required to reset the scroll function |
tomoya123 | 0:d721efd58e4e | 62 | Comm_FunctionSet_Extended, |
tomoya123 | 0:d721efd58e4e | 63 | Comm_InternalOscFrequency, |
tomoya123 | 0:d721efd58e4e | 64 | Comm_ContrastSet | ( default_Contrast & 0xF), |
tomoya123 | 0:d721efd58e4e | 65 | Comm_PwrIconContrast | ((default_Contrast >> 4) & 0x3), |
tomoya123 | 0:d721efd58e4e | 66 | Comm_FollowerCtrl | 0x0A, |
tomoya123 | 0:d721efd58e4e | 67 | |
tomoya123 | 0:d721efd58e4e | 68 | }; |
tomoya123 | 0:d721efd58e4e | 69 | // required 30us interval |
tomoya123 | 0:d721efd58e4e | 70 | |
tomoya123 | 0:d721efd58e4e | 71 | const char init_seq1_length = 3; |
tomoya123 | 0:d721efd58e4e | 72 | const char init_seq1[ init_seq1_length ] |
tomoya123 | 0:d721efd58e4e | 73 | = { |
tomoya123 | 0:d721efd58e4e | 74 | Comm_DisplayOnOff, |
tomoya123 | 0:d721efd58e4e | 75 | Comm_ClearDisplay, |
tomoya123 | 0:d721efd58e4e | 76 | Comm_EntryModeSet, |
tomoya123 | 0:d721efd58e4e | 77 | }; |
tomoya123 | 0:d721efd58e4e | 78 | // required 30us, 2ms interval |
tomoya123 | 0:d721efd58e4e | 79 | |
tomoya123 | 0:d721efd58e4e | 80 | |
tomoya123 | 0:d721efd58e4e | 81 | class HeptaLcd : I2cBusDevice { |
tomoya123 | 0:d721efd58e4e | 82 | public: |
tomoya123 | 0:d721efd58e4e | 83 | I2C lcd; |
tomoya123 | 0:d721efd58e4e | 84 | int addr; |
tomoya123 | 0:d721efd58e4e | 85 | explicit HeptaLcd(PinName sda, PinName scl, char dev_address = SB1602E_addr, char *init_massage = " " ) : lcd(sda,scl),addr(dev_address),I2cBusDevice(&lcd, dev_address ) { |
tomoya123 | 0:d721efd58e4e | 86 | wait( 0.04 ); // interval after hardware reset |
tomoya123 | 0:d721efd58e4e | 87 | |
tomoya123 | 0:d721efd58e4e | 88 | for ( int i = 0; i < init_seq0_length; i++ ) { |
tomoya123 | 0:d721efd58e4e | 89 | lcd_command( init_seq0[ i ] ); |
tomoya123 | 0:d721efd58e4e | 90 | wait( 30e-6 ); |
tomoya123 | 0:d721efd58e4e | 91 | } |
tomoya123 | 0:d721efd58e4e | 92 | |
tomoya123 | 0:d721efd58e4e | 93 | wait( 0.2 ); |
tomoya123 | 0:d721efd58e4e | 94 | |
tomoya123 | 0:d721efd58e4e | 95 | for ( int i = 0; i < init_seq1_length; i++ ) { |
tomoya123 | 0:d721efd58e4e | 96 | lcd_command( init_seq1[ i ] ); |
tomoya123 | 0:d721efd58e4e | 97 | wait( 2e-3 ); |
tomoya123 | 0:d721efd58e4e | 98 | } |
tomoya123 | 0:d721efd58e4e | 99 | |
tomoya123 | 0:d721efd58e4e | 100 | if ( init_massage ) |
tomoya123 | 0:d721efd58e4e | 101 | puts( 0, init_massage ); |
tomoya123 | 0:d721efd58e4e | 102 | |
tomoya123 | 0:d721efd58e4e | 103 | set_CGRAM( 7, '\x1F' ); |
tomoya123 | 0:d721efd58e4e | 104 | |
tomoya123 | 0:d721efd58e4e | 105 | curs[ 0 ] = 0; |
tomoya123 | 0:d721efd58e4e | 106 | curs[ 1 ] = 0; |
tomoya123 | 0:d721efd58e4e | 107 | } |
tomoya123 | 0:d721efd58e4e | 108 | |
tomoya123 | 0:d721efd58e4e | 109 | |
tomoya123 | 0:d721efd58e4e | 110 | ~HeptaLcd() { |
tomoya123 | 0:d721efd58e4e | 111 | } |
tomoya123 | 0:d721efd58e4e | 112 | |
tomoya123 | 0:d721efd58e4e | 113 | void clear( void ) { |
tomoya123 | 0:d721efd58e4e | 114 | lcd_command( Comm_ClearDisplay ); |
tomoya123 | 0:d721efd58e4e | 115 | wait( 2e-3 ); |
tomoya123 | 0:d721efd58e4e | 116 | curs[ 0 ] = 0; |
tomoya123 | 0:d721efd58e4e | 117 | curs[ 1 ] = 0; |
tomoya123 | 0:d721efd58e4e | 118 | } |
tomoya123 | 0:d721efd58e4e | 119 | |
tomoya123 | 0:d721efd58e4e | 120 | void initilize(){ |
tomoya123 | 0:d721efd58e4e | 121 | char *init_massage = " "; |
tomoya123 | 0:d721efd58e4e | 122 | for ( int i = 0; i < init_seq0_length; i++ ) { |
tomoya123 | 0:d721efd58e4e | 123 | lcd_command( init_seq0[ i ] ); |
tomoya123 | 0:d721efd58e4e | 124 | wait( 30e-6 ); |
tomoya123 | 0:d721efd58e4e | 125 | } |
tomoya123 | 0:d721efd58e4e | 126 | |
tomoya123 | 0:d721efd58e4e | 127 | wait( 0.2 ); |
tomoya123 | 0:d721efd58e4e | 128 | |
tomoya123 | 0:d721efd58e4e | 129 | for ( int i = 0; i < init_seq1_length; i++ ) { |
tomoya123 | 0:d721efd58e4e | 130 | lcd_command( init_seq1[ i ] ); |
tomoya123 | 0:d721efd58e4e | 131 | wait( 2e-3 ); |
tomoya123 | 0:d721efd58e4e | 132 | } |
tomoya123 | 0:d721efd58e4e | 133 | |
tomoya123 | 0:d721efd58e4e | 134 | if ( init_massage ) |
tomoya123 | 0:d721efd58e4e | 135 | puts( 0, init_massage ); |
tomoya123 | 0:d721efd58e4e | 136 | |
tomoya123 | 0:d721efd58e4e | 137 | set_CGRAM( 7, '\x1F' ); |
tomoya123 | 0:d721efd58e4e | 138 | |
tomoya123 | 0:d721efd58e4e | 139 | curs[ 0 ] = 0; |
tomoya123 | 0:d721efd58e4e | 140 | curs[ 1 ] = 0; |
tomoya123 | 0:d721efd58e4e | 141 | } |
tomoya123 | 0:d721efd58e4e | 142 | void put_custom_char( char c_code, const char *cg, char x, char y ) { |
tomoya123 | 0:d721efd58e4e | 143 | for ( int i = 0; i < 5; i++ ) { |
tomoya123 | 0:d721efd58e4e | 144 | set_CGRAM( c_code, cg ); |
tomoya123 | 0:d721efd58e4e | 145 | putcxy( c_code, x, y ); |
tomoya123 | 0:d721efd58e4e | 146 | } |
tomoya123 | 0:d721efd58e4e | 147 | } |
tomoya123 | 0:d721efd58e4e | 148 | |
tomoya123 | 0:d721efd58e4e | 149 | void contrast( char contrast ) { |
tomoya123 | 0:d721efd58e4e | 150 | lcd_command( Comm_FunctionSet_Extended ); |
tomoya123 | 0:d721efd58e4e | 151 | lcd_command( Comm_ContrastSet | (contrast & 0x0f) ); |
tomoya123 | 0:d721efd58e4e | 152 | lcd_command( Comm_PwrIconContrast | ((contrast>>4) & 0x03) ); |
tomoya123 | 0:d721efd58e4e | 153 | lcd_command( Comm_FunctionSet_Normal ); |
tomoya123 | 0:d721efd58e4e | 154 | } |
tomoya123 | 0:d721efd58e4e | 155 | |
tomoya123 | 0:d721efd58e4e | 156 | void set_CGRAM( char char_code, const char* cg ) { |
tomoya123 | 0:d721efd58e4e | 157 | for ( int i = 0; i < 8; i++ ) { |
tomoya123 | 0:d721efd58e4e | 158 | lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) ); |
tomoya123 | 0:d721efd58e4e | 159 | lcd_data( *cg++ ); |
tomoya123 | 0:d721efd58e4e | 160 | } |
tomoya123 | 0:d721efd58e4e | 161 | } |
tomoya123 | 0:d721efd58e4e | 162 | |
tomoya123 | 0:d721efd58e4e | 163 | void set_CGRAM( char char_code, char v ) { |
tomoya123 | 0:d721efd58e4e | 164 | char c[ 8 ]; |
tomoya123 | 0:d721efd58e4e | 165 | |
tomoya123 | 0:d721efd58e4e | 166 | for ( int i = 0; i < 8; i++ ) |
tomoya123 | 0:d721efd58e4e | 167 | c[ i ] = v; |
tomoya123 | 0:d721efd58e4e | 168 | |
tomoya123 | 0:d721efd58e4e | 169 | set_CGRAM( char_code, c ); |
tomoya123 | 0:d721efd58e4e | 170 | } |
tomoya123 | 0:d721efd58e4e | 171 | |
tomoya123 | 0:d721efd58e4e | 172 | void putcxy( char c, char x, char y ) { |
tomoya123 | 0:d721efd58e4e | 173 | if ( (x >= MaxCharsInALine) || (y >= 2) ) |
tomoya123 | 0:d721efd58e4e | 174 | return; |
tomoya123 | 0:d721efd58e4e | 175 | |
tomoya123 | 0:d721efd58e4e | 176 | lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[ y ]) + x ); |
tomoya123 | 0:d721efd58e4e | 177 | lcd_data( c ); |
tomoya123 | 0:d721efd58e4e | 178 | } |
tomoya123 | 0:d721efd58e4e | 179 | |
tomoya123 | 0:d721efd58e4e | 180 | void putc( char line, char c ) { |
tomoya123 | 0:d721efd58e4e | 181 | if ( (c == '\n') || (c == '\r') ) { |
tomoya123 | 0:d721efd58e4e | 182 | clear_lest_of_line( line ); |
tomoya123 | 0:d721efd58e4e | 183 | curs[ line ] = 0; |
tomoya123 | 0:d721efd58e4e | 184 | return; |
tomoya123 | 0:d721efd58e4e | 185 | } |
tomoya123 | 0:d721efd58e4e | 186 | |
tomoya123 | 0:d721efd58e4e | 187 | putcxy( c, curs[ line ]++, line ); |
tomoya123 | 0:d721efd58e4e | 188 | } |
tomoya123 | 0:d721efd58e4e | 189 | |
tomoya123 | 0:d721efd58e4e | 190 | void puts( char line, char *s ) { |
tomoya123 | 0:d721efd58e4e | 191 | while ( char c = *s++ ) |
tomoya123 | 0:d721efd58e4e | 192 | putc( line, c ); |
tomoya123 | 0:d721efd58e4e | 193 | } |
tomoya123 | 0:d721efd58e4e | 194 | |
tomoya123 | 0:d721efd58e4e | 195 | void printf( char line, char *format, ... ) { |
tomoya123 | 0:d721efd58e4e | 196 | char s[ 32 ]; |
tomoya123 | 0:d721efd58e4e | 197 | va_list args; |
tomoya123 | 0:d721efd58e4e | 198 | |
tomoya123 | 0:d721efd58e4e | 199 | va_start( args, format ); |
tomoya123 | 0:d721efd58e4e | 200 | vsnprintf( s, 32, format, args ); |
tomoya123 | 0:d721efd58e4e | 201 | va_end( args ); |
tomoya123 | 0:d721efd58e4e | 202 | |
tomoya123 | 0:d721efd58e4e | 203 | puts( line, s ); |
tomoya123 | 0:d721efd58e4e | 204 | } |
tomoya123 | 0:d721efd58e4e | 205 | |
tomoya123 | 0:d721efd58e4e | 206 | private: |
tomoya123 | 0:d721efd58e4e | 207 | char curs[2]; |
tomoya123 | 0:d721efd58e4e | 208 | |
tomoya123 | 0:d721efd58e4e | 209 | void clear_lest_of_line( char line ) { |
tomoya123 | 0:d721efd58e4e | 210 | for ( int i = curs[ line ]; i < MaxCharsInALine; i++ ) |
tomoya123 | 0:d721efd58e4e | 211 | putcxy( ' ', i, line ); |
tomoya123 | 0:d721efd58e4e | 212 | } |
tomoya123 | 0:d721efd58e4e | 213 | |
tomoya123 | 0:d721efd58e4e | 214 | int lcd_write( char first, char second ) { |
tomoya123 | 0:d721efd58e4e | 215 | char cmd[2]; |
tomoya123 | 0:d721efd58e4e | 216 | |
tomoya123 | 0:d721efd58e4e | 217 | cmd[ 0 ] = first; |
tomoya123 | 0:d721efd58e4e | 218 | cmd[ 1 ] = second; |
tomoya123 | 0:d721efd58e4e | 219 | |
tomoya123 | 0:d721efd58e4e | 220 | return ( write( cmd, 2 ) ); |
tomoya123 | 0:d721efd58e4e | 221 | |
tomoya123 | 0:d721efd58e4e | 222 | } |
tomoya123 | 0:d721efd58e4e | 223 | |
tomoya123 | 0:d721efd58e4e | 224 | int lcd_command( char command ) { |
tomoya123 | 0:d721efd58e4e | 225 | return ( lcd_write( COMMAND, command ) ); |
tomoya123 | 0:d721efd58e4e | 226 | } |
tomoya123 | 0:d721efd58e4e | 227 | |
tomoya123 | 0:d721efd58e4e | 228 | int lcd_data( char data ) { |
tomoya123 | 0:d721efd58e4e | 229 | return ( lcd_write( DATA, data ) ); |
tomoya123 | 0:d721efd58e4e | 230 | } |
tomoya123 | 0:d721efd58e4e | 231 | } |
tomoya123 | 0:d721efd58e4e | 232 | ; |
tomoya123 | 0:d721efd58e4e | 233 | |
tomoya123 | 0:d721efd58e4e | 234 | #endif |
tomoya123 | 0:d721efd58e4e | 235 | |
tomoya123 | 0:d721efd58e4e | 236 | |
tomoya123 | 0:d721efd58e4e | 237 | |
tomoya123 | 0:d721efd58e4e | 238 | |
tomoya123 | 0:d721efd58e4e | 239 | |
tomoya123 | 0:d721efd58e4e | 240 | |
tomoya123 | 0:d721efd58e4e | 241 | |
tomoya123 | 0:d721efd58e4e | 242 |