nucho / Mbed 2 deprecated NokiaLCD_AddNOKIA3300

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NokiaLCD.cpp Source File

NokiaLCD.cpp

00001 /* mbed Nokia LCD Library
00002  * Copyright (c) 2007-2010, sford
00003  */
00004 
00005 #include "NokiaLCD.h"
00006 
00007 #include "mbed.h"
00008 
00009 #define NOKIALCD_ROWS 16
00010 #define NOKIALCD_COLS 16
00011 #define NOKIALCD_WIDTH 130
00012 #define NOKIALCD_HEIGHT 130
00013 #define NOKIALCD_FREQUENCY 5000000
00014 
00015 NokiaLCD::NokiaLCD(PinName mosi, PinName sclk, PinName cs, PinName rst, LCDType type)
00016         : _spi(mosi, NC, sclk)
00017         , _rst(rst)
00018         , _cs(cs) {
00019 
00020     _type = type;
00021 
00022     _row = 0;
00023     _column = 0;
00024     _foreground = 0x00FFFFFF;
00025     _background = 0x00000000;
00026 
00027     reset();
00028 }
00029 
00030 void NokiaLCD::reset() {
00031 
00032     // setup the SPI interface and bring display out of reset
00033     _cs = 1;
00034     _rst = 0;
00035     _spi.format(9);
00036     _spi.frequency(NOKIALCD_FREQUENCY);
00037     wait_ms(1);
00038     _rst = 1;
00039     wait_ms(1);
00040 
00041     _cs = 0;
00042 
00043     switch (_type) {
00044         case LCD6100:
00045             command(0xCA); // display control
00046             data(0);
00047             data(32);
00048             data(0);
00049             command(0xBB);
00050             data(1);
00051             command(0xD1); // oscillator on
00052             command(0x94); // sleep out
00053             command(0x20); // power control
00054             data(0x0F);
00055             command(0xA7); // invert display
00056             command(0x81); // Voltage control
00057             data(39);      // contrast setting: 0..63
00058             data(3);       // resistance ratio
00059             wait_ms(1);
00060             command(0xBC);
00061             data(0);
00062             data(1);
00063             data(4);
00064             command(0xAF);  // turn on the display
00065             break;
00066 
00067         case LCD6610:
00068             command(0xCA);    // display control
00069             data(0);
00070             data(31);
00071             data(0);
00072             command(0xBB);
00073             data(1);
00074             command(0xD1); // oscillator on
00075             command(0x94); // sleep out
00076             command(0x20); // power control
00077             data(0x0F);
00078             command(0xA7); // invert display
00079             command(0x81); // Voltage control
00080             data(39);      // contrast setting: 0..63
00081             data(3);       // resistance ratio
00082             wait_ms(1);
00083             command(0xBC);
00084             data(0);
00085             data(0);
00086             data(2);
00087             command(0xAF);  // turn on the display
00088             break;
00089 
00090         case LCD3300:
00091             command(0xCA); // display control
00092             data(0);
00093             data(32);
00094             data(0);
00095             command(0xBB);
00096             data(1);
00097             command(0xD1); // oscillator on
00098             command(0x94); // sleep out
00099             command(0x20); // power control
00100             data(0x0F);
00101             command(0xA7); // invert display
00102             command(0x81); // Voltage control
00103             data(39);      // contrast setting: 0..63
00104             data(3);       // resistance ratio
00105             wait_ms(1);
00106             command(0xBC);
00107             data(1);
00108             data(0);
00109             data(4);
00110             command(0xAF);  // turn on the display
00111             break;
00112             
00113         case PCF8833:
00114             command(0x11);  // sleep out
00115             command(0x3A);  // column mode
00116             data(0x05);
00117             command(0x36);  // madctl
00118             data(0x60);     // vertical RAM, flip x
00119             command(0x25);  // setcon
00120             data(0x30);// contrast 0x30
00121             wait_ms(2);
00122             command(0x29);//DISPON
00123             command(0x03);//BSTRON
00124             break;
00125     }
00126 
00127     _cs = 1;
00128 
00129     cls();
00130 }
00131 
00132 void NokiaLCD::command(int value) {
00133     _spi.write(value & 0xFF);
00134 }
00135 
00136 void NokiaLCD::data(int value) {
00137     _spi.write(value | 0x100);
00138 }
00139 
00140 void NokiaLCD::_window(int x, int y, int width, int height) {
00141     int x1 = x + 2;
00142     int y1 = y + 0;
00143     int x2 = x1 + width - 1;
00144     int y2 = y1 + height - 1;
00145 
00146     switch (_type) {
00147         case LCD6100:
00148         case LCD6610:
00149         case LCD3300:
00150             command(0x15); // column
00151             data(x1);
00152             data(x2);
00153             command(0x75); // row
00154             data(y1);
00155             data(y2);
00156             command(0x5C); // start write to ram
00157             break;
00158         case PCF8833:
00159             command(0x2A);  // column
00160             data(x1);
00161             data(x2);
00162             command(0x2B); // row
00163             data(y1);
00164             data(y2);
00165             command(0x2C); // start write to ram
00166             break;
00167     }
00168 }
00169 
00170 void NokiaLCD::_putp(int colour) {
00171     int gr = ((colour >> 20) & 0x0F)
00172              | ((colour >> 8 ) & 0xF0);
00173     int nb = ((colour >> 4 ) & 0x0F);
00174     data(nb);
00175     data(gr);
00176 }
00177 
00178 const unsigned char FONT8x8[97][8] = {
00179     0x08,0x08,0x08,0x00,0x00,0x00,0x00,0x00, // columns, rows, num_bytes_per_char
00180     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // space 0x20
00181     0x30,0x78,0x78,0x30,0x30,0x00,0x30,0x00, // !
00182     0x6C,0x6C,0x6C,0x00,0x00,0x00,0x00,0x00, // "
00183     0x6C,0x6C,0xFE,0x6C,0xFE,0x6C,0x6C,0x00, // #
00184     0x18,0x3E,0x60,0x3C,0x06,0x7C,0x18,0x00, // $
00185     0x00,0x63,0x66,0x0C,0x18,0x33,0x63,0x00, // %
00186     0x1C,0x36,0x1C,0x3B,0x6E,0x66,0x3B,0x00, // &
00187     0x30,0x30,0x60,0x00,0x00,0x00,0x00,0x00, // '
00188     0x0C,0x18,0x30,0x30,0x30,0x18,0x0C,0x00, // (
00189     0x30,0x18,0x0C,0x0C,0x0C,0x18,0x30,0x00, // )
00190     0x00,0x66,0x3C,0xFF,0x3C,0x66,0x00,0x00, // *
00191     0x00,0x30,0x30,0xFC,0x30,0x30,0x00,0x00, // +
00192     0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x30, // ,
00193     0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00, // -
00194     0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00, // .
00195     0x03,0x06,0x0C,0x18,0x30,0x60,0x40,0x00, // / (forward slash)
00196     0x3E,0x63,0x63,0x6B,0x63,0x63,0x3E,0x00, // 0 0x30
00197     0x18,0x38,0x58,0x18,0x18,0x18,0x7E,0x00, // 1
00198     0x3C,0x66,0x06,0x1C,0x30,0x66,0x7E,0x00, // 2
00199     0x3C,0x66,0x06,0x1C,0x06,0x66,0x3C,0x00, // 3
00200     0x0E,0x1E,0x36,0x66,0x7F,0x06,0x0F,0x00, // 4
00201     0x7E,0x60,0x7C,0x06,0x06,0x66,0x3C,0x00, // 5
00202     0x1C,0x30,0x60,0x7C,0x66,0x66,0x3C,0x00, // 6
00203     0x7E,0x66,0x06,0x0C,0x18,0x18,0x18,0x00, // 7
00204     0x3C,0x66,0x66,0x3C,0x66,0x66,0x3C,0x00, // 8
00205     0x3C,0x66,0x66,0x3E,0x06,0x0C,0x38,0x00, // 9
00206     0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x00, // :
00207     0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x30, // ;
00208     0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x00, // <
00209     0x00,0x00,0x7E,0x00,0x00,0x7E,0x00,0x00, // =
00210     0x30,0x18,0x0C,0x06,0x0C,0x18,0x30,0x00, // >
00211     0x3C,0x66,0x06,0x0C,0x18,0x00,0x18,0x00, // ?
00212     0x3E,0x63,0x6F,0x69,0x6F,0x60,0x3E,0x00, // @ 0x40
00213     0x18,0x3C,0x66,0x66,0x7E,0x66,0x66,0x00, // A
00214     0x7E,0x33,0x33,0x3E,0x33,0x33,0x7E,0x00, // B
00215     0x1E,0x33,0x60,0x60,0x60,0x33,0x1E,0x00, // C
00216     0x7C,0x36,0x33,0x33,0x33,0x36,0x7C,0x00, // D
00217     0x7F,0x31,0x34,0x3C,0x34,0x31,0x7F,0x00, // E
00218     0x7F,0x31,0x34,0x3C,0x34,0x30,0x78,0x00, // F
00219     0x1E,0x33,0x60,0x60,0x67,0x33,0x1F,0x00, // G
00220     0x66,0x66,0x66,0x7E,0x66,0x66,0x66,0x00, // H
00221     0x3C,0x18,0x18,0x18,0x18,0x18,0x3C,0x00, // I
00222     0x0F,0x06,0x06,0x06,0x66,0x66,0x3C,0x00, // J
00223     0x73,0x33,0x36,0x3C,0x36,0x33,0x73,0x00, // K
00224     0x78,0x30,0x30,0x30,0x31,0x33,0x7F,0x00, // L
00225     0x63,0x77,0x7F,0x7F,0x6B,0x63,0x63,0x00, // M
00226     0x63,0x73,0x7B,0x6F,0x67,0x63,0x63,0x00, // N
00227     0x3E,0x63,0x63,0x63,0x63,0x63,0x3E,0x00, // O
00228     0x7E,0x33,0x33,0x3E,0x30,0x30,0x78,0x00, // P 0x50
00229     0x3C,0x66,0x66,0x66,0x6E,0x3C,0x0E,0x00, // Q
00230     0x7E,0x33,0x33,0x3E,0x36,0x33,0x73,0x00, // R
00231     0x3C,0x66,0x30,0x18,0x0C,0x66,0x3C,0x00, // S
00232     0x7E,0x5A,0x18,0x18,0x18,0x18,0x3C,0x00, // T
00233     0x66,0x66,0x66,0x66,0x66,0x66,0x7E,0x00, // U
00234     0x66,0x66,0x66,0x66,0x66,0x3C,0x18,0x00, // V
00235     0x63,0x63,0x63,0x6B,0x7F,0x77,0x63,0x00, // W
00236     0x63,0x63,0x36,0x1C,0x1C,0x36,0x63,0x00, // X
00237     0x66,0x66,0x66,0x3C,0x18,0x18,0x3C,0x00, // Y
00238     0x7F,0x63,0x46,0x0C,0x19,0x33,0x7F,0x00, // Z
00239     0x3C,0x30,0x30,0x30,0x30,0x30,0x3C,0x00, // [
00240     0x60,0x30,0x18,0x0C,0x06,0x03,0x01,0x00, // \ (back slash)
00241     0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00, // ]
00242     0x08,0x1C,0x36,0x63,0x00,0x00,0x00,0x00, // ^
00243     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF, // _
00244     0x18,0x18,0x0C,0x00,0x00,0x00,0x00,0x00, // ` 0x60
00245     0x00,0x00,0x3C,0x06,0x3E,0x66,0x3B,0x00, // a
00246     0x70,0x30,0x3E,0x33,0x33,0x33,0x6E,0x00, // b
00247     0x00,0x00,0x3C,0x66,0x60,0x66,0x3C,0x00, // c
00248     0x0E,0x06,0x3E,0x66,0x66,0x66,0x3B,0x00, // d
00249     0x00,0x00,0x3C,0x66,0x7E,0x60,0x3C,0x00, // e
00250     0x1C,0x36,0x30,0x78,0x30,0x30,0x78,0x00, // f
00251     0x00,0x00,0x3B,0x66,0x66,0x3E,0x06,0x7C, // g
00252     0x70,0x30,0x36,0x3B,0x33,0x33,0x73,0x00, // h
00253     0x18,0x00,0x38,0x18,0x18,0x18,0x3C,0x00, // i
00254     0x06,0x00,0x06,0x06,0x06,0x66,0x66,0x3C, // j
00255     0x70,0x30,0x33,0x36,0x3C,0x36,0x73,0x00, // k
00256     0x38,0x18,0x18,0x18,0x18,0x18,0x3C,0x00, // l
00257     0x00,0x00,0x66,0x7F,0x7F,0x6B,0x63,0x00, // m
00258     0x00,0x00,0x7C,0x66,0x66,0x66,0x66,0x00, // n
00259     0x00,0x00,0x3C,0x66,0x66,0x66,0x3C,0x00, // o
00260     0x00,0x00,0x6E,0x33,0x33,0x3E,0x30,0x78, // p
00261     0x00,0x00,0x3B,0x66,0x66,0x3E,0x06,0x0F, // q
00262     0x00,0x00,0x6E,0x3B,0x33,0x30,0x78,0x00, // r
00263     0x00,0x00,0x3E,0x60,0x3C,0x06,0x7C,0x00, // s
00264     0x08,0x18,0x3E,0x18,0x18,0x1A,0x0C,0x00, // t
00265     0x00,0x00,0x66,0x66,0x66,0x66,0x3B,0x00, // u
00266     0x00,0x00,0x66,0x66,0x66,0x3C,0x18,0x00, // v
00267     0x00,0x00,0x63,0x6B,0x7F,0x7F,0x36,0x00, // w
00268     0x00,0x00,0x63,0x36,0x1C,0x36,0x63,0x00, // x
00269     0x00,0x00,0x66,0x66,0x66,0x3E,0x06,0x7C, // y
00270     0x00,0x00,0x7E,0x4C,0x18,0x32,0x7E,0x00, // z
00271     0x0E,0x18,0x18,0x70,0x18,0x18,0x0E,0x00, // {
00272     0x0C,0x0C,0x0C,0x00,0x0C,0x0C,0x0C,0x00, // |
00273     0x70,0x18,0x18,0x0E,0x18,0x18,0x70,0x00, // }
00274     0x3B,0x6E,0x00,0x00,0x00,0x00,0x00,0x00, // ~
00275     0x1C,0x36,0x36,0x1C,0x00,0x00,0x00,0x00
00276 }; // DEL
00277 
00278 void NokiaLCD::locate(int column, int row) {
00279     _column = column;
00280     _row = row;
00281 }
00282 
00283 void NokiaLCD::newline() {
00284     _column = 0;
00285     _row++;
00286     if (_row >= _rows) {
00287         _row = 0;
00288     }
00289 }
00290 
00291 int NokiaLCD::_putc(int value) {
00292     int x = _column * 8;  // FIXME: Char sizes
00293     int y = _row * 8;
00294     bitblit(x + 1, y + 1, 8, 8, (char*)&(FONT8x8[value - 0x1F][0]));
00295 
00296     _column++;
00297 
00298     if (_column >= NOKIALCD_COLS) {
00299         _row++;
00300         _column = 0;
00301     }
00302 
00303     if (_row >= NOKIALCD_ROWS) {
00304         _row = 0;
00305     }
00306 
00307     return value;
00308 }
00309 
00310 void NokiaLCD::cls() {
00311     fill(0, 0, NOKIALCD_WIDTH, NOKIALCD_HEIGHT, _background);
00312     _row = 0;
00313     _column = 0;
00314 }
00315 
00316 
00317 void NokiaLCD::window(int x, int y, int width, int height) {
00318     _cs = 0;
00319     _window(x, y, width, height);
00320     _cs = 1;
00321 }
00322 
00323 void NokiaLCD::putp(int colour) {
00324     _cs = 0;
00325     _putp(colour);
00326     _cs = 1;
00327 }
00328 
00329 void NokiaLCD::pixel(int x, int y, int colour) {
00330     _cs = 0;
00331     _window(x, y, 1, 1);
00332     _putp(colour);
00333     _cs = 1;
00334 }
00335 
00336 void NokiaLCD::fill(int x, int y, int width, int height, int colour) {
00337     _cs = 0;
00338     _window(x, y, width, height);
00339     for (int i=0; i<width*height; i++) {
00340         _putp(colour);
00341     }
00342     _window(0, 0, NOKIALCD_WIDTH, NOKIALCD_HEIGHT);
00343     _cs = 1;
00344 }
00345 
00346 void NokiaLCD::blit(int x, int y, int width, int height, const int* colour) {
00347     _cs = 0;
00348     _window(x, y, width, height);
00349     for (int i=0; i<width*height; i++) {
00350         _putp(colour[i]);
00351     }
00352     _window(0, 0, NOKIALCD_WIDTH, NOKIALCD_HEIGHT);
00353     _cs = 1;
00354 }
00355 
00356 void NokiaLCD::bitblit(int x, int y, int width, int height, const char* bitstream) {
00357     _cs = 0;
00358     _window(x, y, width, height);
00359     for (int i=0; i<height*width; i++) {
00360         int byte = i / 8;
00361         int bit = i % 8;
00362         int colour = ((bitstream[byte] << bit) & 0x80) ? _foreground : _background;
00363         _putp(colour);
00364     }
00365     _window(0, 0, _width, _height);
00366     _cs = 1;
00367 }
00368 
00369 void NokiaLCD::foreground(int c) {
00370     _foreground = c;
00371 }
00372 
00373 void NokiaLCD::background(int c) {
00374     _background = c;
00375 }
00376 
00377 int NokiaLCD::width() {
00378     return NOKIALCD_WIDTH;
00379 }
00380 
00381 int NokiaLCD::height() {
00382     return NOKIALCD_HEIGHT;
00383 }
00384 
00385 int NokiaLCD::columns() {
00386     return NOKIALCD_COLS;
00387 }
00388 
00389 int NokiaLCD::rows() {
00390     return NOKIALCD_ROWS;
00391 }