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:
Searle95
Date:
Thu Aug 01 09:14:23 2013 +0000
Revision:
8:0380b34047ad
Parent:
2:e6c002c680a6
Modified for use in NetTester project.

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