For Nokia Mobile LCD

Dependents:   PE_08-08_MobileLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MobileLCD.cpp Source File

MobileLCD.cpp

00001 /* mbed Library - MobileLCD
00002  * Copyright (c) 2007, sford
00003  */
00004  
00005 #include "MobileLCD.h"
00006 
00007 #include "mbed.h"
00008 
00009 using namespace mbed;
00010 
00011 const unsigned char FONT8x8[97][8] = {
00012 0x08,0x08,0x08,0x00,0x00,0x00,0x00,0x00, // columns, rows, num_bytes_per_char
00013 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // space 0x20
00014 0x30,0x78,0x78,0x30,0x30,0x00,0x30,0x00, // !
00015 0x6C,0x6C,0x6C,0x00,0x00,0x00,0x00,0x00, // "
00016 0x6C,0x6C,0xFE,0x6C,0xFE,0x6C,0x6C,0x00, // #
00017 0x18,0x3E,0x60,0x3C,0x06,0x7C,0x18,0x00, // $
00018 0x00,0x63,0x66,0x0C,0x18,0x33,0x63,0x00, // %
00019 0x1C,0x36,0x1C,0x3B,0x6E,0x66,0x3B,0x00, // &
00020 0x30,0x30,0x60,0x00,0x00,0x00,0x00,0x00, // '
00021 0x0C,0x18,0x30,0x30,0x30,0x18,0x0C,0x00, // (
00022 0x30,0x18,0x0C,0x0C,0x0C,0x18,0x30,0x00, // )
00023 0x00,0x66,0x3C,0xFF,0x3C,0x66,0x00,0x00, // *
00024 0x00,0x30,0x30,0xFC,0x30,0x30,0x00,0x00, // +
00025 0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x30, // ,
00026 0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00, // -
00027 0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00, // .
00028 0x03,0x06,0x0C,0x18,0x30,0x60,0x40,0x00, // / (forward slash)
00029 0x3E,0x63,0x63,0x6B,0x63,0x63,0x3E,0x00, // 0 0x30
00030 0x18,0x38,0x58,0x18,0x18,0x18,0x7E,0x00, // 1
00031 0x3C,0x66,0x06,0x1C,0x30,0x66,0x7E,0x00, // 2
00032 0x3C,0x66,0x06,0x1C,0x06,0x66,0x3C,0x00, // 3
00033 0x0E,0x1E,0x36,0x66,0x7F,0x06,0x0F,0x00, // 4
00034 0x7E,0x60,0x7C,0x06,0x06,0x66,0x3C,0x00, // 5
00035 0x1C,0x30,0x60,0x7C,0x66,0x66,0x3C,0x00, // 6
00036 0x7E,0x66,0x06,0x0C,0x18,0x18,0x18,0x00, // 7
00037 0x3C,0x66,0x66,0x3C,0x66,0x66,0x3C,0x00, // 8
00038 0x3C,0x66,0x66,0x3E,0x06,0x0C,0x38,0x00, // 9
00039 0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x00, // :
00040 0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x30, // ;
00041 0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x00, // <
00042 0x00,0x00,0x7E,0x00,0x00,0x7E,0x00,0x00, // =
00043 0x30,0x18,0x0C,0x06,0x0C,0x18,0x30,0x00, // >
00044 0x3C,0x66,0x06,0x0C,0x18,0x00,0x18,0x00, // ?
00045 0x3E,0x63,0x6F,0x69,0x6F,0x60,0x3E,0x00, // @ 0x40
00046 0x18,0x3C,0x66,0x66,0x7E,0x66,0x66,0x00, // A
00047 0x7E,0x33,0x33,0x3E,0x33,0x33,0x7E,0x00, // B
00048 0x1E,0x33,0x60,0x60,0x60,0x33,0x1E,0x00, // C
00049 0x7C,0x36,0x33,0x33,0x33,0x36,0x7C,0x00, // D
00050 0x7F,0x31,0x34,0x3C,0x34,0x31,0x7F,0x00, // E
00051 0x7F,0x31,0x34,0x3C,0x34,0x30,0x78,0x00, // F
00052 0x1E,0x33,0x60,0x60,0x67,0x33,0x1F,0x00, // G
00053 0x66,0x66,0x66,0x7E,0x66,0x66,0x66,0x00, // H
00054 0x3C,0x18,0x18,0x18,0x18,0x18,0x3C,0x00, // I
00055 0x0F,0x06,0x06,0x06,0x66,0x66,0x3C,0x00, // J
00056 0x73,0x33,0x36,0x3C,0x36,0x33,0x73,0x00, // K
00057 0x78,0x30,0x30,0x30,0x31,0x33,0x7F,0x00, // L
00058 0x63,0x77,0x7F,0x7F,0x6B,0x63,0x63,0x00, // M
00059 0x63,0x73,0x7B,0x6F,0x67,0x63,0x63,0x00, // N
00060 0x3E,0x63,0x63,0x63,0x63,0x63,0x3E,0x00, // O
00061 0x7E,0x33,0x33,0x3E,0x30,0x30,0x78,0x00, // P 0x50
00062 0x3C,0x66,0x66,0x66,0x6E,0x3C,0x0E,0x00, // Q
00063 0x7E,0x33,0x33,0x3E,0x36,0x33,0x73,0x00, // R
00064 0x3C,0x66,0x30,0x18,0x0C,0x66,0x3C,0x00, // S
00065 0x7E,0x5A,0x18,0x18,0x18,0x18,0x3C,0x00, // T
00066 0x66,0x66,0x66,0x66,0x66,0x66,0x7E,0x00, // U
00067 0x66,0x66,0x66,0x66,0x66,0x3C,0x18,0x00, // V
00068 0x63,0x63,0x63,0x6B,0x7F,0x77,0x63,0x00, // W
00069 0x63,0x63,0x36,0x1C,0x1C,0x36,0x63,0x00, // X
00070 0x66,0x66,0x66,0x3C,0x18,0x18,0x3C,0x00, // Y
00071 0x7F,0x63,0x46,0x0C,0x19,0x33,0x7F,0x00, // Z
00072 0x3C,0x30,0x30,0x30,0x30,0x30,0x3C,0x00, // [
00073 0x60,0x30,0x18,0x0C,0x06,0x03,0x01,0x00, // \ (back slash)
00074 0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00, // ]
00075 0x08,0x1C,0x36,0x63,0x00,0x00,0x00,0x00, // ^
00076 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF, // _
00077 0x18,0x18,0x0C,0x00,0x00,0x00,0x00,0x00, // ` 0x60
00078 0x00,0x00,0x3C,0x06,0x3E,0x66,0x3B,0x00, // a
00079 0x70,0x30,0x3E,0x33,0x33,0x33,0x6E,0x00, // b
00080 0x00,0x00,0x3C,0x66,0x60,0x66,0x3C,0x00, // c
00081 0x0E,0x06,0x3E,0x66,0x66,0x66,0x3B,0x00, // d
00082 0x00,0x00,0x3C,0x66,0x7E,0x60,0x3C,0x00, // e
00083 0x1C,0x36,0x30,0x78,0x30,0x30,0x78,0x00, // f
00084 0x00,0x00,0x3B,0x66,0x66,0x3E,0x06,0x7C, // g
00085 0x70,0x30,0x36,0x3B,0x33,0x33,0x73,0x00, // h
00086 0x18,0x00,0x38,0x18,0x18,0x18,0x3C,0x00, // i
00087 0x06,0x00,0x06,0x06,0x06,0x66,0x66,0x3C, // j
00088 0x70,0x30,0x33,0x36,0x3C,0x36,0x73,0x00, // k
00089 0x38,0x18,0x18,0x18,0x18,0x18,0x3C,0x00, // l
00090 0x00,0x00,0x66,0x7F,0x7F,0x6B,0x63,0x00, // m
00091 0x00,0x00,0x7C,0x66,0x66,0x66,0x66,0x00, // n
00092 0x00,0x00,0x3C,0x66,0x66,0x66,0x3C,0x00, // o
00093 0x00,0x00,0x6E,0x33,0x33,0x3E,0x30,0x78, // p
00094 0x00,0x00,0x3B,0x66,0x66,0x3E,0x06,0x0F, // q
00095 0x00,0x00,0x6E,0x3B,0x33,0x30,0x78,0x00, // r
00096 0x00,0x00,0x3E,0x60,0x3C,0x06,0x7C,0x00, // s
00097 0x08,0x18,0x3E,0x18,0x18,0x1A,0x0C,0x00, // t
00098 0x00,0x00,0x66,0x66,0x66,0x66,0x3B,0x00, // u
00099 0x00,0x00,0x66,0x66,0x66,0x3C,0x18,0x00, // v
00100 0x00,0x00,0x63,0x6B,0x7F,0x7F,0x36,0x00, // w
00101 0x00,0x00,0x63,0x36,0x1C,0x36,0x63,0x00, // x
00102 0x00,0x00,0x66,0x66,0x66,0x3E,0x06,0x7C, // y
00103 0x00,0x00,0x7E,0x4C,0x18,0x32,0x7E,0x00, // z
00104 0x0E,0x18,0x18,0x70,0x18,0x18,0x0E,0x00, // {
00105 0x0C,0x0C,0x0C,0x00,0x0C,0x0C,0x0C,0x00, // |
00106 0x70,0x18,0x18,0x0E,0x18,0x18,0x70,0x00, // }
00107 0x3B,0x6E,0x00,0x00,0x00,0x00,0x00,0x00, // ~
00108 0x1C,0x36,0x36,0x1C,0x00,0x00,0x00,0x00}; // DEL
00109 
00110 void MobileLCD::command(int value) {
00111     _spi.write(value & 0xFF);
00112 }
00113 
00114 void MobileLCD::data(int value) {
00115     _spi.write(value | 0x100);
00116 }
00117 
00118 void MobileLCD::_select() {
00119     _cs = 0;    
00120 }
00121     
00122 void MobileLCD::_deselect() {
00123     _cs = 1;
00124 }
00125 
00126 int MobileLCD::width() { 
00127     return _width; 
00128 }
00129 
00130 int MobileLCD::height() { 
00131     return _height; 
00132 }
00133 
00134 int MobileLCD::columns() { 
00135     return _columns; 
00136 }
00137 
00138 int MobileLCD::rows() { 
00139     return _rows; 
00140 }
00141 
00142 void MobileLCD::foreground(int v) {
00143     _foreground = v;
00144 }
00145 
00146 void MobileLCD::background(int v) {
00147     _background = v;
00148 }
00149 
00150 void MobileLCD::locate(int column, int row) {
00151     _row = row;
00152     _column = column;
00153 }
00154     
00155 void MobileLCD::newline() {
00156     _column = 0;
00157     _row++;
00158     if(_row >= _rows) {
00159         _row = 0;
00160     }
00161 }
00162 
00163 /*************************************************************************/
00164     
00165 MobileLCD::MobileLCD(PinName mosi, PinName miso, PinName clk, PinName cs, PinName rst) 
00166     : _spi(mosi, miso, clk)
00167     , _rst(rst)
00168     , _cs(cs) {
00169 
00170     _row = 0;
00171     _column = 0;
00172     _rows = 16;
00173     _columns = 16;
00174     _width = 130;
00175     _height = 130;
00176     foreground(0xFFFFFF);
00177     background(0x000000);
00178     reset();
00179 }
00180     
00181 void MobileLCD::reset() {
00182 
00183     _cs = 1;
00184     _rst = 0;
00185     _spi.format(9);
00186     _spi.frequency(5000000);
00187     
00188     wait_ms(1);
00189 
00190     _rst = 1;
00191 
00192     wait_ms(1);
00193 
00194     _select();
00195 
00196     command(0xCA); // display control  
00197     data(0x0);
00198       data(31);
00199      data(0x0);
00200      
00201     command(0xBB);  // scan
00202     data(0x1);
00203     
00204       command(0xD1); // oscillator on
00205       command(0x94); // sleep out
00206     command(0x20); // power control
00207       data(0x0F); 
00208 
00209       command(0xA7); // invert display
00210 
00211     command(0x81); // Voltage control
00212        data(39);      // contrast setting: 0..63
00213        data(3);       // resistance ratio
00214 
00215     wait_ms(1);
00216 
00217     command(0xBC); // data control
00218     data(0x0); // scan dirs
00219       data(0x0); // RGB
00220       data(0x2); // grayscale
00221 
00222     command(0xAF);  // turn on the display
00223               
00224     _deselect();    
00225     
00226     cls();
00227 }
00228                  
00229 void MobileLCD::_window(int x, int y, int width, int height) {
00230     int x1, x2, y1, y2;
00231     x1 = x + 0;
00232     y1 = y + 2;
00233     x2 = x1 + width - 1;
00234     y2 = y1 + height - 1;        
00235     command(0x15); // column
00236     data(x1);       
00237     data(x2); 
00238     command(0x75); // page
00239       data(y1);            
00240       data(y2);
00241        command(0x5C); // start write to ram
00242 }
00243 
00244 void MobileLCD::fill(int x, int y, int width, int height, int colour) {
00245     _select();
00246     _window(x, y, width, height);
00247 
00248     int r4 = (colour >> (16 + 4)) & 0xF;
00249     int g4 = (colour >> (8 + 4)) & 0xF;
00250     int b4 = (colour >> (0 + 4)) & 0xF;
00251     
00252     int d1 = (r4 << 4) | g4;
00253     int d2 = (b4 << 4) | r4;
00254     int d3 = (g4 << 4) | b4;
00255     
00256     for(int i=0; i<(width*height+1)/2; i++) {
00257         data(d1); 
00258         data(d2); 
00259         data(d3); 
00260     }
00261      _deselect();        
00262 } 
00263 
00264 void MobileLCD::bitblit(int x, int y, int width, int height, const char* bitstream) {
00265     _select();
00266     _window(x, y, width, height);
00267     for(int i=0; i<height*width/2; i++) {
00268     int byte1 = (i*2) / 8;
00269     int bit1 = (i*2) % 8;    
00270     int colour1 = ((bitstream[byte1] << bit1) & 0x80) ? _foreground : _background;
00271     int byte2 = (i*2+1) / 8;
00272     int bit2 = (i*2+1) % 8;    
00273     int colour2 = ((bitstream[byte2] << bit2) & 0x80) ? _foreground : _background;
00274 
00275     int r41 = (colour1 >> (16 + 4)) & 0xF;
00276     int g41 = (colour1 >> (8 + 4)) & 0xF;
00277     int b41 = (colour1 >> (0 + 4)) & 0xF;
00278     
00279     int r42 = (colour2 >> (16 + 4)) & 0xF;
00280     int g42 = (colour2 >> (8 + 4)) & 0xF;
00281     int b42 = (colour2 >> (0 + 4)) & 0xF;    
00282     int d1 = (r41 << 4) | g41;
00283     int d2 = (b41 << 4) | r42;
00284     int d3 = (g42 << 4) | b42;                
00285         data(d1); 
00286         data(d2); 
00287         data(d3); 
00288     }
00289     _deselect();
00290 }
00291 
00292 void MobileLCD::cls() {
00293     fill(0, 0, 130, 130, _background);
00294     _row = 0;
00295     _column = 0;
00296 }    
00297         
00298 int MobileLCD::_putc(int value) { 
00299     int x = _column * 8;  // FIXME: Char sizes
00300     int y = _row * 8;
00301     bitblit(x + 1, y + 1, 8, 8, (char*)&(FONT8x8[value - 0x1F][0]));
00302     
00303     _column++;
00304 
00305     if(_column >= _columns) {
00306         _row++;
00307         _column = 0;
00308     }
00309         
00310     if(_row >= _rows) {
00311         _row = 0;
00312     }            
00313 
00314     return value;
00315 }
00316      
00317 void MobileLCD::pixel(int x, int y, int colour) {
00318     fill(x, y, 1, 1, colour);
00319 }
00320