N3310LCD library demo

Dependencies:   N3310LCD mbed

Fork of N3310LCD by Petras Saduikis

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 * N3310LCD. A program to interface mbed with the nuelectronics
00003 * Nokia 3310 LCD shield from www.nuelectronics.com. Ported from
00004 * the nuelectronics Arduino code.
00005 *
00006 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
00007 *
00008 * Converted to a mbed library by Andrew D. Lindsay
00009 *
00010 * This file is part of N3310LCD.
00011 *
00012 * N3310LCD is free software: you can redistribute it and/or modify
00013 * it under the terms of the GNU General Public License as published by
00014 * the Free Software Foundation, either version 3 of the License, or
00015 * (at your option) any later version.
00016 *
00017 * N3310LCD is distributed in the hope that it will be useful,
00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 * GNU General Public License for more details.
00021 *
00022 * You should have received a copy of the GNU General Public License
00023 * along with N3310LCD.  If not, see <http://www.gnu.org/licenses/>.
00024 */
00025 
00026 #include "N3310SPIConfig.h"
00027 #include "N3310LCD.h"
00028 #include "Joystick.h"
00029 #include "mbed_bmp.h"
00030 #include "pacman.h"
00031 #include "bitmap.h"
00032 
00033 // demo for nuelectronics Nokia 3310 LCD shield (www.nuelectronics.com)
00034 //
00035 
00036 // menu starting points
00037 #define MENU_X    10        // 0-83
00038 #define MENU_Y    0        // 0-5
00039 
00040 #define DEMO_ITEMS 5
00041 
00042 void temperature(N3310LCD* lcd, Joystick* jstick);
00043 void charmap(N3310LCD* lcd, Joystick* jstick);
00044 void bitmap(N3310LCD* lcd, Joystick* jstick);
00045 void about(N3310LCD* lcd, Joystick* jstick);
00046 void graphics(N3310LCD* lcd, Joystick* jstick);
00047 
00048 // menu definition
00049 char menu_items[DEMO_ITEMS][12] = {
00050     "TEMPERATURE",
00051     "CHAR MAP",
00052     "BITMAP",
00053     "GRAPHICS",
00054     "ABOUT"
00055 };
00056 
00057 void (*menu_funcs[DEMO_ITEMS])(N3310LCD*,Joystick*) = {
00058     temperature,
00059     charmap,
00060     bitmap,
00061     graphics,
00062     about
00063 };
00064 
00065 void temperature(N3310LCD* lcd, Joystick* jstick)
00066 {
00067     lcd->writeStringBig(5, 1, "+21.12", NORMAL);
00068     lcd->writeString(73, 2, "C", NORMAL);
00069 }
00070 
00071 void charmap(N3310LCD* lcd, Joystick* jstick)
00072 {
00073     for(int i = 0; i < 5; i++) {
00074         for(int j = 0; j < 14; j++) {
00075             lcd->locate(j*6, i);
00076             lcd->writeChar(i*14 + j + 32, NORMAL);
00077         }
00078     }
00079 }
00080 
00081 void bitmap(N3310LCD* lcd, Joystick* jstick)
00082 {
00083     for( int i=0; i<10; i++ ) {
00084         lcd->drawBitmap(20, 1, mbed_bmp, 48, 24);
00085         wait(1);
00086         lcd->clearBitmap(20, 1, 48, 24);
00087         lcd->drawBitmap(0, 0, fy1, 84, 48);
00088         wait(1);
00089         lcd->clearBitmap(0, 0, 84, 48);
00090     }
00091     lcd->drawBitmap(20, 1, mbed_bmp, 48, 24);
00092 }
00093 
00094 void about(N3310LCD* lcd, Joystick* jstick)
00095 {
00096     lcd->writeString(0, 1, "Nokia 3310 LCD", NORMAL);
00097     lcd->writeString(15, 2, "driven by", NORMAL);
00098     lcd->writeString(10, 3, "KL25Z mbed", NORMAL);
00099 }
00100 
00101 
00102 
00103 void initMenu(N3310LCD* lcd)
00104 {
00105     lcd->writeString(MENU_X, MENU_Y, menu_items[0], HIGHLIGHT );
00106 
00107     for (int i = 1; i < DEMO_ITEMS; i++) {
00108         lcd->writeString(MENU_X, MENU_Y + i, menu_items[i], NORMAL);
00109     }
00110 }
00111 
00112 void waitforOKKey(N3310LCD* lcd, Joystick* jstick)
00113 {
00114     lcd->writeString(38, 5, "OK", HIGHLIGHT );
00115 
00116     int key = 0xFF;
00117     while (key != CENTER_KEY) {
00118         for (int i = 0; i < NUM_KEYS; i++) {
00119             if (jstick->getKeyState(i) !=0) {
00120                 jstick->resetKeyState(i);  // reset
00121                 if (CENTER_KEY == i) key = CENTER_KEY;
00122             }
00123         }
00124     }
00125 }
00126 
00127 // Check if joystick is moved or pressed
00128 uint8_t checkKeypressed( Joystick* jstick)
00129 {
00130     uint8_t key = 0xFF;
00131 
00132     for(int i=0; i<NUM_KEYS; i++) {
00133         if (jstick->getKeyState(i) !=0) {
00134             jstick->resetKeyState(i);  // reset
00135             if (CENTER_KEY == i) key = CENTER_KEY;
00136         }
00137     }
00138     return key;
00139 }
00140 
00141 /*
00142 void autoDemo(N3310LCD* lcd)
00143 {
00144     while (true)
00145     {
00146         for (int i = 0; i < DEMO_ITEMS; i++)
00147         {
00148             lcd->cls();
00149             lcd->backlight(ON);
00150             wait(1);
00151 
00152             (*menu_funcs[i])(lcd);
00153 
00154             wait(3);
00155 
00156             lcd->backlight(OFF);
00157             wait(3);
00158         }
00159     }
00160 }
00161 */
00162 
00163 // Display the simple graphics demo
00164 void graphics(N3310LCD* lcd, Joystick* jstick)
00165 {
00166     lcd->writeString( 0, 1, "Text Demo", NORMAL);
00167     lcd->writeString(24, 5, "START", HIGHLIGHT );
00168     checkKeypressed(jstick);
00169     lcd->cls();
00170 
00171     lcd->writeStringBig( 0, 0, "123456", NORMAL );
00172     lcd->writeStringBig( 0, 3, "7890+-.", NORMAL );
00173     wait(2);
00174 
00175     lcd->writeStringBig( 0, 0, "123456", HIGHLIGHT );
00176     lcd->writeStringBig( 0, 3, "7890+-.", HIGHLIGHT );
00177     wait(2);
00178 
00179     lcd->cls();
00180 
00181     lcd->writeString( 0, 1, "Graphic Demo", NORMAL);
00182     lcd->writeString(24, 5, "START", HIGHLIGHT );
00183     checkKeypressed(jstick);
00184     lcd->cls();
00185     // Draw some circles pulsing in and out
00186     for(int a=0; a< 4; a++) {
00187         for( int r = 1; r < 49; r+=1 ) {
00188             lcd->drawCircle(42, 24, r, PIXEL_ON );
00189             wait(0.010);
00190         }
00191         wait(0.010);
00192         for( int r = 48; r >0; r-=1 ) {
00193             wait(0.010);
00194             lcd->drawCircle(42, 24, r, PIXEL_OFF );
00195         }
00196     }
00197 
00198     // More circles
00199     for( int xc = 10; xc < 85; xc+=15 ) {
00200         lcd->drawCircle(xc, 24, 20, PIXEL_ON );
00201     }
00202     wait( 2 );
00203 
00204     // Draw diagonal lines using XOR colour
00205     lcd->drawLine(0,0,83,47, PIXEL_XOR);
00206     lcd->drawLine(0,43,83,0, PIXEL_XOR);
00207 
00208     wait( 2 );
00209 
00210     // Draw a rectangle
00211     lcd->drawRectangle(5,5,78,42, PIXEL_ON);
00212 
00213     wait( 2 );
00214 
00215     // Draw 2 filled rectangles
00216     lcd->drawFilledRectangle(5,3,78,20, PIXEL_ON);
00217     lcd->drawFilledRectangle(5,25,78,42, PIXEL_ON);
00218 
00219     wait( 2 );
00220 
00221     // Draw bitmap image
00222     lcd->drawBitmap( 0,0, fy1,84,48);
00223     wait(5);
00224 
00225     // Pacman animation
00226     lcd->cls();
00227     int px = 0;
00228     int py = 1;
00229     float pause=0.060;
00230     for( int p=0; p <9; p++) {
00231         lcd->drawBitmap( px,py, pacman1,32,32);
00232         wait( pause );
00233         lcd->clearBitmap( px++,py, 32,32);
00234         lcd->drawBitmap( px,py, pacman2,32,32);
00235         wait( pause );
00236         lcd->clearBitmap( px++,py, 32,32);
00237         lcd->drawBitmap( px,py, pacman3,32,32);
00238         wait( pause );
00239         lcd->clearBitmap( px++,py, 32,32);
00240         lcd->drawBitmap( px,py, pacman4,32,32);
00241         wait( pause );
00242         lcd->clearBitmap( px++,py, 32,32);
00243         lcd->drawBitmap( px,py, pacman3,32,32);
00244         wait( pause );
00245         lcd->clearBitmap( px++,py, 32,32);
00246         lcd->drawBitmap( px,py, pacman2,32,32);
00247         wait( pause );
00248         lcd->clearBitmap( px++,py, 32,32);
00249     }
00250     lcd->drawBitmap( px,py, pacman1,32,32);
00251 
00252     wait( 5 );
00253 
00254     lcd->cls();
00255 
00256     lcd->writeString( 0, 1, "Graphic Demo", NORMAL);
00257     lcd->writeString( 0, 3, "The End!!", NORMAL);
00258 //  lcd->writeString(38, 5, "OK", HIGHLIGHT );
00259     waitforOKKey(lcd,jstick);
00260 }
00261 
00262 
00263 int main()
00264 {
00265     Joystick jstick(N3310SPIPort::AD0);
00266     N3310LCD lcd(N3310SPIPort::MOSI, N3310SPIPort::MISO, N3310SPIPort::SCK,
00267                  N3310SPIPort::CE, N3310SPIPort::DAT_CMD, N3310SPIPort::LCD_RST,
00268                  N3310SPIPort::BL_ON);
00269     lcd.init();
00270     lcd.cls();
00271     lcd.backlight(ON);
00272 
00273     // demo stuff
00274     // autoDemo(&lcd);
00275 
00276     initMenu(&lcd);
00277     int currentMenuItem = 0;
00278     Ticker jstickPoll;
00279     jstickPoll.attach(&jstick, &Joystick::updateADCKey, 0.01);    // check ever 10ms
00280 
00281 
00282     while (true) {
00283         for (int i = 0; i < NUM_KEYS; i++) {
00284             if (jstick.getKeyState(i) != 0) {
00285                 jstick.resetKeyState(i);  // reset button flag
00286                 switch(i) {
00287                     case UP_KEY:
00288                         // current item to normal display
00289                         lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], NORMAL);
00290                         currentMenuItem -=1;
00291                         if (currentMenuItem <0)  currentMenuItem = DEMO_ITEMS -1;
00292                         // next item to highlight display
00293                         lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], HIGHLIGHT);
00294                         break;
00295 
00296                     case DOWN_KEY:
00297                         // current item to normal display
00298                         lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], NORMAL);
00299                         currentMenuItem +=1;
00300                         if(currentMenuItem >(DEMO_ITEMS - 1))  currentMenuItem = 0;
00301                         // next item to highlight display
00302                         lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], HIGHLIGHT);
00303                         break;
00304 
00305                     case LEFT_KEY:
00306                         initMenu(&lcd);
00307                         currentMenuItem = 0;
00308                         break;
00309 
00310                     case RIGHT_KEY:
00311                         lcd.cls();
00312                         (*menu_funcs[currentMenuItem])(&lcd, &jstick);
00313                         waitforOKKey(&lcd, &jstick);
00314                         lcd.cls();
00315                         initMenu(&lcd);
00316                         currentMenuItem = 0;
00317                         break;
00318                 }
00319             }
00320         }
00321     }
00322 
00323 
00324 }