Code to interface mbed to a Nokia 3310 LCD using the Nokia 3310 LCD shield from nuelectronics. Includes joystick interface and demo.

Dependencies:   mbed

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 * This file is part of N3310LCD.
00009 *
00010 * N3310LCD is free software: you can redistribute it and/or modify
00011 * it under the terms of the GNU General Public License as published by
00012 * the Free Software Foundation, either version 3 of the License, or
00013 * (at your option) any later version.
00014 * 
00015 * N3310LCD is distributed in the hope that it will be useful,
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 * GNU General Public License for more details.
00019 *
00020 * You should have received a copy of the GNU General Public License
00021 * along with N3310LCD.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 
00024 #include "N3310SPIConfig.h"
00025 #include "N3310LCD.h"
00026 #include "Joystick.h"
00027 #include "mbed_bmp.h"
00028 
00029 // demo for nuelectronics Nokia 3310 LCD shield (www.nuelectronics.com)
00030 // 
00031 
00032 // menu starting points
00033 #define MENU_X    10        // 0-83
00034 #define MENU_Y    1        // 0-5
00035 
00036 #define DEMO_ITEMS 4
00037 
00038 // menu definition
00039 char menu_items[DEMO_ITEMS][12] =
00040 {
00041     "TEMPERATURE",
00042     "CHAR MAP",
00043     "BITMAP",
00044     "ABOUT"    
00045 };
00046 
00047 void temperature(N3310LCD* lcd)
00048 {
00049     lcd->writeStringBig(5, 1, "+21.12", NORMAL);
00050     lcd->writeString(73, 2, "C", NORMAL);
00051 }
00052 
00053 void charmap(N3310LCD* lcd)
00054 {
00055     for(int i = 0; i < 5; i++)
00056     {
00057         for(int j = 0; j < 14; j++)
00058         {
00059           lcd->locate(j*6, i);
00060           lcd->writeChar(i*14 + j + 32, NORMAL);
00061         }
00062     }
00063 }
00064 
00065 void bitmap(N3310LCD* lcd)
00066 {
00067     lcd->drawBitmap(20, 1, mbed_bmp, 48, 24);
00068 }
00069 
00070 void about(N3310LCD* lcd)
00071 {
00072     lcd->writeString(0, 1, "Nokia 3310 LCD", NORMAL);
00073     lcd->writeString(15, 2, "driven by", NORMAL);
00074     lcd->writeString(30, 3, "mbed", NORMAL);
00075 }
00076 
00077 void (*menu_funcs[DEMO_ITEMS])(N3310LCD*) = 
00078 {
00079     temperature,
00080     charmap,
00081     bitmap,
00082     about
00083 };
00084 
00085 void initMenu(N3310LCD* lcd)
00086 {
00087     lcd->writeString(MENU_X, MENU_Y, menu_items[0], HIGHLIGHT );
00088     
00089     for (int i = 1; i < DEMO_ITEMS; i++)
00090     {
00091         lcd->writeString(MENU_X, MENU_Y + i, menu_items[i], NORMAL);
00092     }
00093 }
00094 
00095 void waitforOKKey(N3310LCD* lcd, Joystick* jstick)
00096 {
00097     lcd->writeString(38, 5, "OK", HIGHLIGHT );
00098 
00099     int key = 0xFF;
00100     while (key != CENTER_KEY)
00101     {
00102         for (int i = 0; i < NUM_KEYS; i++)
00103         {
00104             if (jstick->getKeyState(i) !=0)
00105             {
00106                 jstick->resetKeyState(i);  // reset
00107                 if (CENTER_KEY == i) key = CENTER_KEY;
00108             }
00109         }
00110     }
00111 }
00112 
00113 void autoDemo(N3310LCD* lcd)
00114 {
00115     while (true)
00116     {
00117         for (int i = 0; i < DEMO_ITEMS; i++)
00118         {
00119             lcd->cls();
00120             lcd->backlight(ON);
00121             wait(1);
00122                 
00123             (*menu_funcs[i])(lcd);
00124         
00125             wait(3);
00126         
00127             lcd->backlight(OFF);
00128             wait(3);
00129         }
00130     }
00131 }
00132 
00133 int main() 
00134 {
00135     Joystick jstick(N3310SPIPort::AD0);
00136     N3310LCD lcd(N3310SPIPort::MOSI, N3310SPIPort::MISO, N3310SPIPort::SCK,
00137                  N3310SPIPort::CE, N3310SPIPort::DAT_CMD, N3310SPIPort::LCD_RST,
00138                  N3310SPIPort::BL_ON);
00139     lcd.init();
00140     lcd.cls();
00141     lcd.backlight(ON);
00142     
00143     // demo stuff
00144     // autoDemo(&lcd);
00145     
00146     initMenu(&lcd);
00147     int currentMenuItem = 0;
00148     Ticker jstickPoll;
00149     jstickPoll.attach(&jstick, &Joystick::updateADCKey, 0.01);    // check ever 10ms
00150     
00151     
00152     while (true)
00153     {
00154     for (int i = 0; i < NUM_KEYS; i++)
00155     {
00156         if (jstick.getKeyState(i) != 0)
00157         {    
00158             jstick.resetKeyState(i);  // reset button flag
00159             switch(i)
00160             {
00161                 case UP_KEY:
00162                     // current item to normal display
00163                     lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], NORMAL);
00164                     currentMenuItem -=1;
00165                     if (currentMenuItem <0)  currentMenuItem = DEMO_ITEMS -1;
00166                     // next item to highlight display
00167                     lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], HIGHLIGHT);
00168                     break;
00169                     
00170                 case DOWN_KEY:
00171                     // current item to normal display
00172                     lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], NORMAL);
00173                     currentMenuItem +=1;
00174                     if(currentMenuItem >(DEMO_ITEMS - 1))  currentMenuItem = 0;
00175                     // next item to highlight display
00176                     lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], HIGHLIGHT);
00177                     break;
00178                     
00179                 case LEFT_KEY:
00180                     initMenu(&lcd);
00181                     currentMenuItem = 0;
00182                     break;
00183                     
00184                 case RIGHT_KEY:
00185                     lcd.cls();
00186                     (*menu_funcs[currentMenuItem])(&lcd);
00187                     waitforOKKey(&lcd, &jstick);
00188                     lcd.cls();
00189                     initMenu(&lcd);
00190                     currentMenuItem = 0;
00191                     break;    
00192             }        
00193         }
00194     }
00195     }    
00196          
00197     return EXIT_SUCCESS;
00198 }