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:29:09 2013 +0000
Revision:
1:51961974fe55
Parent:
0:7efa6655d94b
Child:
2:e6c002c680a6
reformatted code

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 1:51961974fe55 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 1:51961974fe55 37 200, // CENTER DEPRESSED
SomeRandomBloke 1:51961974fe55 38 400, // DOWN
SomeRandomBloke 1:51961974fe55 39 600, // UP
SomeRandomBloke 1:51961974fe55 40 800 // RIGHT
SomeRandomBloke 1:51961974fe55 41 // 1024 CENTER NOT DEPRESSED
SomeRandomBloke 1:51961974fe55 42 };
SomeRandomBloke 1:51961974fe55 43
SomeRandomBloke 0:7efa6655d94b 44 Joystick::Joystick(PinName jstick) : joystick(jstick)
SomeRandomBloke 0:7efa6655d94b 45 {
SomeRandomBloke 0:7efa6655d94b 46 // reset button arrays
SomeRandomBloke 1:51961974fe55 47 for (int i = 0; i < NUM_KEYS; i++) {
SomeRandomBloke 0:7efa6655d94b 48 buttonCount[i] = 0;
SomeRandomBloke 0:7efa6655d94b 49 buttonStatus[i] = 0;
SomeRandomBloke 0:7efa6655d94b 50 buttonFlag[i] = 0;
SomeRandomBloke 0:7efa6655d94b 51 }
SomeRandomBloke 0:7efa6655d94b 52 }
SomeRandomBloke 0:7efa6655d94b 53
SomeRandomBloke 0:7efa6655d94b 54 int Joystick::getKeyState(int i)
SomeRandomBloke 0:7efa6655d94b 55 {
SomeRandomBloke 0:7efa6655d94b 56 int retval = 0;
SomeRandomBloke 1:51961974fe55 57
SomeRandomBloke 1:51961974fe55 58 if (i < NUM_KEYS) {
SomeRandomBloke 0:7efa6655d94b 59 retval = buttonFlag[i];
SomeRandomBloke 0:7efa6655d94b 60 }
SomeRandomBloke 1:51961974fe55 61
SomeRandomBloke 0:7efa6655d94b 62 return retval;
SomeRandomBloke 0:7efa6655d94b 63 }
SomeRandomBloke 0:7efa6655d94b 64
SomeRandomBloke 0:7efa6655d94b 65 void Joystick::resetKeyState(int i)
SomeRandomBloke 0:7efa6655d94b 66 {
SomeRandomBloke 1:51961974fe55 67 if (i < NUM_KEYS) {
SomeRandomBloke 0:7efa6655d94b 68 buttonFlag[i] = 0;
SomeRandomBloke 0:7efa6655d94b 69 }
SomeRandomBloke 0:7efa6655d94b 70 }
SomeRandomBloke 0:7efa6655d94b 71
SomeRandomBloke 0:7efa6655d94b 72 void Joystick::updateADCKey()
SomeRandomBloke 0:7efa6655d94b 73 {
SomeRandomBloke 1:51961974fe55 74 // NOTE: the mbed analog in is 0 - 3.3V, represented as 0.0 - 1.0. It is important
SomeRandomBloke 0:7efa6655d94b 75 // that the LCD shield is powered from a 3.3V supply in order for the 'right' joystick
SomeRandomBloke 0:7efa6655d94b 76 // key to function correctly.
SomeRandomBloke 1:51961974fe55 77
SomeRandomBloke 0:7efa6655d94b 78 int adcKeyIn = joystick * 1024; // scale this up so we can use int
SomeRandomBloke 0:7efa6655d94b 79 int keyIn = getKey(adcKeyIn);
SomeRandomBloke 0:7efa6655d94b 80 pc.printf("%d \n",adcKeyIn );
SomeRandomBloke 1:51961974fe55 81
SomeRandomBloke 1:51961974fe55 82 for (int i = 0; i < NUM_KEYS; i++) {
SomeRandomBloke 1:51961974fe55 83 if (keyIn == i) { //one key is pressed
SomeRandomBloke 1:51961974fe55 84 if (buttonCount[i] < DEBOUNCE_MAX) {
SomeRandomBloke 0:7efa6655d94b 85 buttonCount[i]++;
SomeRandomBloke 1:51961974fe55 86 if (buttonCount[i] > DEBOUNCE_ON) {
SomeRandomBloke 1:51961974fe55 87 if (buttonStatus[i] == 0) {
SomeRandomBloke 0:7efa6655d94b 88 buttonFlag[i] = 1;
SomeRandomBloke 0:7efa6655d94b 89 buttonStatus[i] = 1; //button debounced to 'pressed' status
SomeRandomBloke 0:7efa6655d94b 90 }
SomeRandomBloke 0:7efa6655d94b 91 }
SomeRandomBloke 0:7efa6655d94b 92 }
SomeRandomBloke 1:51961974fe55 93 } else { // no button pressed
SomeRandomBloke 1:51961974fe55 94 if (buttonCount[i] > 0) {
SomeRandomBloke 1:51961974fe55 95 buttonFlag[i] = 0;
SomeRandomBloke 0:7efa6655d94b 96 buttonCount[i]--;
SomeRandomBloke 1:51961974fe55 97 if (buttonCount[i] < DEBOUNCE_OFF) {
SomeRandomBloke 0:7efa6655d94b 98 buttonStatus[i] = 0; //button debounced to 'released' status
SomeRandomBloke 0:7efa6655d94b 99 }
SomeRandomBloke 0:7efa6655d94b 100 }
SomeRandomBloke 0:7efa6655d94b 101 }
SomeRandomBloke 0:7efa6655d94b 102 }
SomeRandomBloke 0:7efa6655d94b 103 }
SomeRandomBloke 1:51961974fe55 104
SomeRandomBloke 0:7efa6655d94b 105 // Convert ADC value to key number
SomeRandomBloke 0:7efa6655d94b 106 int Joystick::getKey(int input)
SomeRandomBloke 0:7efa6655d94b 107 {
SomeRandomBloke 0:7efa6655d94b 108 int k;
SomeRandomBloke 1:51961974fe55 109
SomeRandomBloke 1:51961974fe55 110 for (k = 0; k < NUM_KEYS; k++) {
SomeRandomBloke 0:7efa6655d94b 111 if (input < adcKeyVal[k]) return k;
SomeRandomBloke 0:7efa6655d94b 112 }
SomeRandomBloke 1:51961974fe55 113
SomeRandomBloke 0:7efa6655d94b 114 if (k >= NUM_KEYS) k = -1; // No valid key pressed
SomeRandomBloke 1:51961974fe55 115
SomeRandomBloke 0:7efa6655d94b 116 return k;
SomeRandomBloke 0:7efa6655d94b 117 }