Library for Nuelectronics Nokia 3310/5110 LCD Display and joystick.

Dependents:   LEDFun NetTester

Fork of N3310LCD by Andrew Lindsay

Library for Nuelectronics Nokia 3310/5110 LCD Display and joystick.

Committer:
SomeRandomBloke
Date:
Sun Mar 10 18:15:25 2013 +0000
Revision:
0:7efa6655d94b
Child:
1:51961974fe55
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:7efa6655d94b 1 /*
SomeRandomBloke 0:7efa6655d94b 2 * N3310LCD. A program to interface mbed with the nuelectronics
SomeRandomBloke 0:7efa6655d94b 3 * Nokia 3310 LCD shield from www.nuelectronics.com. Ported from
SomeRandomBloke 0:7efa6655d94b 4 * the nuelectronics Arduino code.
SomeRandomBloke 0:7efa6655d94b 5 *
SomeRandomBloke 0:7efa6655d94b 6 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
SomeRandomBloke 0:7efa6655d94b 7 *
SomeRandomBloke 0:7efa6655d94b 8 * Converted to a mbed library by Andrew D. Lindsay
SomeRandomBloke 0:7efa6655d94b 9 *
SomeRandomBloke 0:7efa6655d94b 10 * This file is part of N3310LCD.
SomeRandomBloke 0:7efa6655d94b 11 *
SomeRandomBloke 0:7efa6655d94b 12 * N3310LCD is free software: you can redistribute it and/or modify
SomeRandomBloke 0:7efa6655d94b 13 * it under the terms of the GNU General Public License as published by
SomeRandomBloke 0:7efa6655d94b 14 * the Free Software Foundation, either version 3 of the License, or
SomeRandomBloke 0:7efa6655d94b 15 * (at your option) any later version.
SomeRandomBloke 0:7efa6655d94b 16 *
SomeRandomBloke 0:7efa6655d94b 17 * N3310LCD is distributed in the hope that it will be useful,
SomeRandomBloke 0:7efa6655d94b 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
SomeRandomBloke 0:7efa6655d94b 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
SomeRandomBloke 0:7efa6655d94b 20 * GNU General Public License for more details.
SomeRandomBloke 0:7efa6655d94b 21 *
SomeRandomBloke 0:7efa6655d94b 22 * You should have received a copy of the GNU General Public License
SomeRandomBloke 0:7efa6655d94b 23 * along with N3310LCD. If not, see <http://www.gnu.org/licenses/>.
SomeRandomBloke 0:7efa6655d94b 24 */
SomeRandomBloke 0:7efa6655d94b 25
SomeRandomBloke 0:7efa6655d94b 26 #include <mbed.h>
SomeRandomBloke 0:7efa6655d94b 27 #include "Joystick.h"
SomeRandomBloke 0:7efa6655d94b 28
SomeRandomBloke 0:7efa6655d94b 29 //keypad debounce parameter
SomeRandomBloke 0:7efa6655d94b 30 #define DEBOUNCE_MAX 15
SomeRandomBloke 0:7efa6655d94b 31 #define DEBOUNCE_ON 10
SomeRandomBloke 0:7efa6655d94b 32 #define DEBOUNCE_OFF 3
SomeRandomBloke 0:7efa6655d94b 33 Serial pc(USBTX,USBRX);
SomeRandomBloke 0:7efa6655d94b 34
SomeRandomBloke 0:7efa6655d94b 35 // values correspond to use of a 3.3V supply for the LCD shield.
SomeRandomBloke 0:7efa6655d94b 36 const int Joystick::adcKeyVal[NUM_KEYS] = {50, // LEFT
SomeRandomBloke 0:7efa6655d94b 37 200, // CENTER DEPRESSED
SomeRandomBloke 0:7efa6655d94b 38 400, // DOWN
SomeRandomBloke 0:7efa6655d94b 39 600, // UP
SomeRandomBloke 0:7efa6655d94b 40 800 // RIGHT
SomeRandomBloke 0:7efa6655d94b 41 // 1024 CENTER NOT DEPRESSED
SomeRandomBloke 0:7efa6655d94b 42 };
SomeRandomBloke 0:7efa6655d94b 43
SomeRandomBloke 0:7efa6655d94b 44 Joystick::Joystick(PinName jstick) : joystick(jstick)
SomeRandomBloke 0:7efa6655d94b 45 {
SomeRandomBloke 0:7efa6655d94b 46 // reset button arrays
SomeRandomBloke 0:7efa6655d94b 47 for (int i = 0; i < NUM_KEYS; i++)
SomeRandomBloke 0:7efa6655d94b 48 {
SomeRandomBloke 0:7efa6655d94b 49 buttonCount[i] = 0;
SomeRandomBloke 0:7efa6655d94b 50 buttonStatus[i] = 0;
SomeRandomBloke 0:7efa6655d94b 51 buttonFlag[i] = 0;
SomeRandomBloke 0:7efa6655d94b 52 }
SomeRandomBloke 0:7efa6655d94b 53 }
SomeRandomBloke 0:7efa6655d94b 54
SomeRandomBloke 0:7efa6655d94b 55 int Joystick::getKeyState(int i)
SomeRandomBloke 0:7efa6655d94b 56 {
SomeRandomBloke 0:7efa6655d94b 57 int retval = 0;
SomeRandomBloke 0:7efa6655d94b 58
SomeRandomBloke 0:7efa6655d94b 59 if (i < NUM_KEYS)
SomeRandomBloke 0:7efa6655d94b 60 {
SomeRandomBloke 0:7efa6655d94b 61 retval = buttonFlag[i];
SomeRandomBloke 0:7efa6655d94b 62 }
SomeRandomBloke 0:7efa6655d94b 63
SomeRandomBloke 0:7efa6655d94b 64 return retval;
SomeRandomBloke 0:7efa6655d94b 65 }
SomeRandomBloke 0:7efa6655d94b 66
SomeRandomBloke 0:7efa6655d94b 67 void Joystick::resetKeyState(int i)
SomeRandomBloke 0:7efa6655d94b 68 {
SomeRandomBloke 0:7efa6655d94b 69 if (i < NUM_KEYS)
SomeRandomBloke 0:7efa6655d94b 70 {
SomeRandomBloke 0:7efa6655d94b 71 buttonFlag[i] = 0;
SomeRandomBloke 0:7efa6655d94b 72 }
SomeRandomBloke 0:7efa6655d94b 73 }
SomeRandomBloke 0:7efa6655d94b 74
SomeRandomBloke 0:7efa6655d94b 75 void Joystick::updateADCKey()
SomeRandomBloke 0:7efa6655d94b 76 {
SomeRandomBloke 0:7efa6655d94b 77 // NOTE: the mbed analog in is 0 - 3.3V, represented as 0.0 - 1.0. It is important
SomeRandomBloke 0:7efa6655d94b 78 // that the LCD shield is powered from a 3.3V supply in order for the 'right' joystick
SomeRandomBloke 0:7efa6655d94b 79 // key to function correctly.
SomeRandomBloke 0:7efa6655d94b 80
SomeRandomBloke 0:7efa6655d94b 81 int adcKeyIn = joystick * 1024; // scale this up so we can use int
SomeRandomBloke 0:7efa6655d94b 82 int keyIn = getKey(adcKeyIn);
SomeRandomBloke 0:7efa6655d94b 83 pc.printf("%d \n",adcKeyIn );
SomeRandomBloke 0:7efa6655d94b 84
SomeRandomBloke 0:7efa6655d94b 85 for (int i = 0; i < NUM_KEYS; i++)
SomeRandomBloke 0:7efa6655d94b 86 {
SomeRandomBloke 0:7efa6655d94b 87 if (keyIn == i) //one key is pressed
SomeRandomBloke 0:7efa6655d94b 88 {
SomeRandomBloke 0:7efa6655d94b 89 if (buttonCount[i] < DEBOUNCE_MAX)
SomeRandomBloke 0:7efa6655d94b 90 {
SomeRandomBloke 0:7efa6655d94b 91 buttonCount[i]++;
SomeRandomBloke 0:7efa6655d94b 92 if (buttonCount[i] > DEBOUNCE_ON)
SomeRandomBloke 0:7efa6655d94b 93 {
SomeRandomBloke 0:7efa6655d94b 94 if (buttonStatus[i] == 0)
SomeRandomBloke 0:7efa6655d94b 95 {
SomeRandomBloke 0:7efa6655d94b 96 buttonFlag[i] = 1;
SomeRandomBloke 0:7efa6655d94b 97 buttonStatus[i] = 1; //button debounced to 'pressed' status
SomeRandomBloke 0:7efa6655d94b 98 }
SomeRandomBloke 0:7efa6655d94b 99 }
SomeRandomBloke 0:7efa6655d94b 100 }
SomeRandomBloke 0:7efa6655d94b 101 }
SomeRandomBloke 0:7efa6655d94b 102 else // no button pressed
SomeRandomBloke 0:7efa6655d94b 103 {
SomeRandomBloke 0:7efa6655d94b 104 if (buttonCount[i] > 0)
SomeRandomBloke 0:7efa6655d94b 105 {
SomeRandomBloke 0:7efa6655d94b 106 buttonFlag[i] = 0;
SomeRandomBloke 0:7efa6655d94b 107 buttonCount[i]--;
SomeRandomBloke 0:7efa6655d94b 108 if (buttonCount[i] < DEBOUNCE_OFF)
SomeRandomBloke 0:7efa6655d94b 109 {
SomeRandomBloke 0:7efa6655d94b 110 buttonStatus[i] = 0; //button debounced to 'released' status
SomeRandomBloke 0:7efa6655d94b 111 }
SomeRandomBloke 0:7efa6655d94b 112 }
SomeRandomBloke 0:7efa6655d94b 113 }
SomeRandomBloke 0:7efa6655d94b 114 }
SomeRandomBloke 0:7efa6655d94b 115 }
SomeRandomBloke 0:7efa6655d94b 116
SomeRandomBloke 0:7efa6655d94b 117 // Convert ADC value to key number
SomeRandomBloke 0:7efa6655d94b 118 int Joystick::getKey(int input)
SomeRandomBloke 0:7efa6655d94b 119 {
SomeRandomBloke 0:7efa6655d94b 120 int k;
SomeRandomBloke 0:7efa6655d94b 121
SomeRandomBloke 0:7efa6655d94b 122 for (k = 0; k < NUM_KEYS; k++)
SomeRandomBloke 0:7efa6655d94b 123 {
SomeRandomBloke 0:7efa6655d94b 124 if (input < adcKeyVal[k]) return k;
SomeRandomBloke 0:7efa6655d94b 125 }
SomeRandomBloke 0:7efa6655d94b 126
SomeRandomBloke 0:7efa6655d94b 127 if (k >= NUM_KEYS) k = -1; // No valid key pressed
SomeRandomBloke 0:7efa6655d94b 128
SomeRandomBloke 0:7efa6655d94b 129 return k;
SomeRandomBloke 0:7efa6655d94b 130 }