N3310LCD library demo

Dependencies:   N3310LCD mbed

Fork of N3310LCD by Petras Saduikis

Committer:
snatch59
Date:
Mon Dec 07 17:24:52 2009 +0000
Revision:
0:36fb749b83c7
Child:
1:740d0a3999a9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
snatch59 0:36fb749b83c7 1 /*
snatch59 0:36fb749b83c7 2 * N3310LCD. A program to interface mbed with the nuelectronics
snatch59 0:36fb749b83c7 3 * Nokia 3310 LCD shield from www.nuelectronics.com. Ported from
snatch59 0:36fb749b83c7 4 * the nuelectronics Arduino code.
snatch59 0:36fb749b83c7 5 *
snatch59 0:36fb749b83c7 6 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
snatch59 0:36fb749b83c7 7 *
snatch59 0:36fb749b83c7 8 * This file is part of N3310LCD.
snatch59 0:36fb749b83c7 9 *
snatch59 0:36fb749b83c7 10 * N3310LCD is free software: you can redistribute it and/or modify
snatch59 0:36fb749b83c7 11 * it under the terms of the GNU General Public License as published by
snatch59 0:36fb749b83c7 12 * the Free Software Foundation, either version 3 of the License, or
snatch59 0:36fb749b83c7 13 * (at your option) any later version.
snatch59 0:36fb749b83c7 14 *
snatch59 0:36fb749b83c7 15 * N3310LCD is distributed in the hope that it will be useful,
snatch59 0:36fb749b83c7 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
snatch59 0:36fb749b83c7 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
snatch59 0:36fb749b83c7 18 * GNU General Public License for more details.
snatch59 0:36fb749b83c7 19 *
snatch59 0:36fb749b83c7 20 * You should have received a copy of the GNU General Public License
snatch59 0:36fb749b83c7 21 * along with N3310LCD. If not, see <http://www.gnu.org/licenses/>.
snatch59 0:36fb749b83c7 22 */
snatch59 0:36fb749b83c7 23
snatch59 0:36fb749b83c7 24 #include "N3310SPIConfig.h"
snatch59 0:36fb749b83c7 25 #include "N3310LCD.h"
snatch59 0:36fb749b83c7 26 #include "Joystick.h"
snatch59 0:36fb749b83c7 27 #include "mbed_bmp.h"
snatch59 0:36fb749b83c7 28
snatch59 0:36fb749b83c7 29 // demo for nuelectronics Nokia 3310 LCD shield (www.nuelectronics.com)
snatch59 0:36fb749b83c7 30 //
snatch59 0:36fb749b83c7 31
snatch59 0:36fb749b83c7 32 // menu starting points
snatch59 0:36fb749b83c7 33 #define MENU_X 10 // 0-83
snatch59 0:36fb749b83c7 34 #define MENU_Y 1 // 0-5
snatch59 0:36fb749b83c7 35
snatch59 0:36fb749b83c7 36 #define DEMO_ITEMS 4
snatch59 0:36fb749b83c7 37
snatch59 0:36fb749b83c7 38 // menu definition
snatch59 0:36fb749b83c7 39 char menu_items[DEMO_ITEMS][12] =
snatch59 0:36fb749b83c7 40 {
snatch59 0:36fb749b83c7 41 "TEMPERATURE",
snatch59 0:36fb749b83c7 42 "CHAR MAP",
snatch59 0:36fb749b83c7 43 "BITMAP",
snatch59 0:36fb749b83c7 44 "ABOUT"
snatch59 0:36fb749b83c7 45 };
snatch59 0:36fb749b83c7 46
snatch59 0:36fb749b83c7 47 void temperature(N3310LCD* lcd)
snatch59 0:36fb749b83c7 48 {
snatch59 0:36fb749b83c7 49 lcd->writeStringBig(5, 1, "+21.12", NORMAL);
snatch59 0:36fb749b83c7 50 lcd->writeString(73, 2, "C", NORMAL);
snatch59 0:36fb749b83c7 51 }
snatch59 0:36fb749b83c7 52
snatch59 0:36fb749b83c7 53 void charmap(N3310LCD* lcd)
snatch59 0:36fb749b83c7 54 {
snatch59 0:36fb749b83c7 55 for(int i = 0; i < 5; i++)
snatch59 0:36fb749b83c7 56 {
snatch59 0:36fb749b83c7 57 for(int j = 0; j < 14; j++)
snatch59 0:36fb749b83c7 58 {
snatch59 0:36fb749b83c7 59 lcd->locate(j*6, i);
snatch59 0:36fb749b83c7 60 lcd->writeChar(i*14 + j + 32, NORMAL);
snatch59 0:36fb749b83c7 61 }
snatch59 0:36fb749b83c7 62 }
snatch59 0:36fb749b83c7 63 }
snatch59 0:36fb749b83c7 64
snatch59 0:36fb749b83c7 65 void bitmap(N3310LCD* lcd)
snatch59 0:36fb749b83c7 66 {
snatch59 0:36fb749b83c7 67 lcd->drawBitmap(20, 1, mbed_bmp, 48, 24);
snatch59 0:36fb749b83c7 68 }
snatch59 0:36fb749b83c7 69
snatch59 0:36fb749b83c7 70 void about(N3310LCD* lcd)
snatch59 0:36fb749b83c7 71 {
snatch59 0:36fb749b83c7 72 lcd->writeString(0, 1, "Nokia 3310 LCD", NORMAL);
snatch59 0:36fb749b83c7 73 lcd->writeString(15, 2, "driven by", NORMAL);
snatch59 0:36fb749b83c7 74 lcd->writeString(30, 3, "mbed", NORMAL);
snatch59 0:36fb749b83c7 75 }
snatch59 0:36fb749b83c7 76
snatch59 0:36fb749b83c7 77 void (*menu_funcs[DEMO_ITEMS])(N3310LCD*) =
snatch59 0:36fb749b83c7 78 {
snatch59 0:36fb749b83c7 79 temperature,
snatch59 0:36fb749b83c7 80 charmap,
snatch59 0:36fb749b83c7 81 bitmap,
snatch59 0:36fb749b83c7 82 about
snatch59 0:36fb749b83c7 83 };
snatch59 0:36fb749b83c7 84
snatch59 0:36fb749b83c7 85 void initMenu(N3310LCD* lcd)
snatch59 0:36fb749b83c7 86 {
snatch59 0:36fb749b83c7 87 lcd->writeString(MENU_X, MENU_Y, menu_items[0], HIGHLIGHT );
snatch59 0:36fb749b83c7 88
snatch59 0:36fb749b83c7 89 for (int i = 1; i < DEMO_ITEMS; i++)
snatch59 0:36fb749b83c7 90 {
snatch59 0:36fb749b83c7 91 lcd->writeString(MENU_X, MENU_Y + i, menu_items[i], NORMAL);
snatch59 0:36fb749b83c7 92 }
snatch59 0:36fb749b83c7 93 }
snatch59 0:36fb749b83c7 94
snatch59 0:36fb749b83c7 95 void waitforOKKey(N3310LCD* lcd, Joystick* jstick)
snatch59 0:36fb749b83c7 96 {
snatch59 0:36fb749b83c7 97 lcd->writeString(38, 5, "OK", HIGHLIGHT );
snatch59 0:36fb749b83c7 98
snatch59 0:36fb749b83c7 99 int key = 0xFF;
snatch59 0:36fb749b83c7 100 while (key != CENTER_KEY)
snatch59 0:36fb749b83c7 101 {
snatch59 0:36fb749b83c7 102 for (int i = 0; i < NUM_KEYS; i++)
snatch59 0:36fb749b83c7 103 {
snatch59 0:36fb749b83c7 104 if (jstick->getKeyState(i) !=0)
snatch59 0:36fb749b83c7 105 {
snatch59 0:36fb749b83c7 106 jstick->resetKeyState(i); // reset
snatch59 0:36fb749b83c7 107 if (CENTER_KEY == i) key = CENTER_KEY;
snatch59 0:36fb749b83c7 108 }
snatch59 0:36fb749b83c7 109 }
snatch59 0:36fb749b83c7 110 }
snatch59 0:36fb749b83c7 111 }
snatch59 0:36fb749b83c7 112
snatch59 0:36fb749b83c7 113 void autoDemo(N3310LCD* lcd)
snatch59 0:36fb749b83c7 114 {
snatch59 0:36fb749b83c7 115 while (true)
snatch59 0:36fb749b83c7 116 {
snatch59 0:36fb749b83c7 117 for (int i = 0; i < DEMO_ITEMS; i++)
snatch59 0:36fb749b83c7 118 {
snatch59 0:36fb749b83c7 119 lcd->cls();
snatch59 0:36fb749b83c7 120 lcd->backlight(ON);
snatch59 0:36fb749b83c7 121 wait(1);
snatch59 0:36fb749b83c7 122
snatch59 0:36fb749b83c7 123 (*menu_funcs[i])(lcd);
snatch59 0:36fb749b83c7 124
snatch59 0:36fb749b83c7 125 wait(3);
snatch59 0:36fb749b83c7 126
snatch59 0:36fb749b83c7 127 lcd->backlight(OFF);
snatch59 0:36fb749b83c7 128 wait(3);
snatch59 0:36fb749b83c7 129 }
snatch59 0:36fb749b83c7 130 }
snatch59 0:36fb749b83c7 131 }
snatch59 0:36fb749b83c7 132
snatch59 0:36fb749b83c7 133 int main()
snatch59 0:36fb749b83c7 134 {
snatch59 0:36fb749b83c7 135 Joystick jstick(N3310SPIPort::AD0);
snatch59 0:36fb749b83c7 136 N3310LCD lcd(N3310SPIPort::MOSI, N3310SPIPort::MISO, N3310SPIPort::SCK,
snatch59 0:36fb749b83c7 137 N3310SPIPort::CE, N3310SPIPort::DAT_CMD, N3310SPIPort::LCD_RST,
snatch59 0:36fb749b83c7 138 N3310SPIPort::BL_ON);
snatch59 0:36fb749b83c7 139 lcd.init();
snatch59 0:36fb749b83c7 140 lcd.cls();
snatch59 0:36fb749b83c7 141 lcd.backlight(ON);
snatch59 0:36fb749b83c7 142
snatch59 0:36fb749b83c7 143 // demo stuff
snatch59 0:36fb749b83c7 144 // autoDemo(&lcd);
snatch59 0:36fb749b83c7 145
snatch59 0:36fb749b83c7 146 initMenu(&lcd);
snatch59 0:36fb749b83c7 147 int currentMenuItem = 0;
snatch59 0:36fb749b83c7 148 Ticker jstickPoll;
snatch59 0:36fb749b83c7 149 jstickPoll.attach(&jstick, &Joystick::updateADCKey, 0.01); // check ever 10ms
snatch59 0:36fb749b83c7 150
snatch59 0:36fb749b83c7 151
snatch59 0:36fb749b83c7 152 while (true)
snatch59 0:36fb749b83c7 153 {
snatch59 0:36fb749b83c7 154 for (int i = 0; i < NUM_KEYS; i++)
snatch59 0:36fb749b83c7 155 {
snatch59 0:36fb749b83c7 156 if (jstick.getKeyState(i) != 0)
snatch59 0:36fb749b83c7 157 {
snatch59 0:36fb749b83c7 158 jstick.resetKeyState(i); // reset button flag
snatch59 0:36fb749b83c7 159 switch(i)
snatch59 0:36fb749b83c7 160 {
snatch59 0:36fb749b83c7 161 case UP_KEY:
snatch59 0:36fb749b83c7 162 // current item to normal display
snatch59 0:36fb749b83c7 163 lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], NORMAL);
snatch59 0:36fb749b83c7 164 currentMenuItem -=1;
snatch59 0:36fb749b83c7 165 if (currentMenuItem <0) currentMenuItem = DEMO_ITEMS -1;
snatch59 0:36fb749b83c7 166 // next item to highlight display
snatch59 0:36fb749b83c7 167 lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], HIGHLIGHT);
snatch59 0:36fb749b83c7 168 break;
snatch59 0:36fb749b83c7 169
snatch59 0:36fb749b83c7 170 case DOWN_KEY:
snatch59 0:36fb749b83c7 171 // current item to normal display
snatch59 0:36fb749b83c7 172 lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], NORMAL);
snatch59 0:36fb749b83c7 173 currentMenuItem +=1;
snatch59 0:36fb749b83c7 174 if(currentMenuItem >(DEMO_ITEMS - 1)) currentMenuItem = 0;
snatch59 0:36fb749b83c7 175 // next item to highlight display
snatch59 0:36fb749b83c7 176 lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], HIGHLIGHT);
snatch59 0:36fb749b83c7 177 break;
snatch59 0:36fb749b83c7 178
snatch59 0:36fb749b83c7 179 case LEFT_KEY:
snatch59 0:36fb749b83c7 180 initMenu(&lcd);
snatch59 0:36fb749b83c7 181 currentMenuItem = 0;
snatch59 0:36fb749b83c7 182 break;
snatch59 0:36fb749b83c7 183
snatch59 0:36fb749b83c7 184 case RIGHT_KEY:
snatch59 0:36fb749b83c7 185 lcd.cls();
snatch59 0:36fb749b83c7 186 (*menu_funcs[currentMenuItem])(&lcd);
snatch59 0:36fb749b83c7 187 waitforOKKey(&lcd, &jstick);
snatch59 0:36fb749b83c7 188 lcd.cls();
snatch59 0:36fb749b83c7 189 initMenu(&lcd);
snatch59 0:36fb749b83c7 190 currentMenuItem = 0;
snatch59 0:36fb749b83c7 191 break;
snatch59 0:36fb749b83c7 192 }
snatch59 0:36fb749b83c7 193 }
snatch59 0:36fb749b83c7 194 }
snatch59 0:36fb749b83c7 195 }
snatch59 0:36fb749b83c7 196
snatch59 0:36fb749b83c7 197 return EXIT_SUCCESS;
snatch59 0:36fb749b83c7 198 }