Andrew Lindsay
/
N3310LCD_Demo
N3310LCD library demo
Fork of N3310LCD by
Joystick.cpp@0:36fb749b83c7, 2009-12-07 (annotated)
- Committer:
- snatch59
- Date:
- Mon Dec 07 17:24:52 2009 +0000
- Revision:
- 0:36fb749b83c7
Who changed what in which revision?
User | Revision | Line number | New 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 <mbed.h> |
snatch59 | 0:36fb749b83c7 | 25 | #include "Joystick.h" |
snatch59 | 0:36fb749b83c7 | 26 | |
snatch59 | 0:36fb749b83c7 | 27 | //keypad debounce parameter |
snatch59 | 0:36fb749b83c7 | 28 | #define DEBOUNCE_MAX 15 |
snatch59 | 0:36fb749b83c7 | 29 | #define DEBOUNCE_ON 10 |
snatch59 | 0:36fb749b83c7 | 30 | #define DEBOUNCE_OFF 3 |
snatch59 | 0:36fb749b83c7 | 31 | |
snatch59 | 0:36fb749b83c7 | 32 | // values correspond to use of a 3.3V supply for the LCD shield. |
snatch59 | 0:36fb749b83c7 | 33 | const int Joystick::adcKeyVal[NUM_KEYS] = {50, // LEFT |
snatch59 | 0:36fb749b83c7 | 34 | 200, // CENTER DEPRESSED |
snatch59 | 0:36fb749b83c7 | 35 | 400, // DOWN |
snatch59 | 0:36fb749b83c7 | 36 | 600, // UP |
snatch59 | 0:36fb749b83c7 | 37 | 800 // RIGHT |
snatch59 | 0:36fb749b83c7 | 38 | // 1024 CENTER NOT DEPRESSED |
snatch59 | 0:36fb749b83c7 | 39 | }; |
snatch59 | 0:36fb749b83c7 | 40 | |
snatch59 | 0:36fb749b83c7 | 41 | Joystick::Joystick(PinName jstick) : joystick(jstick) |
snatch59 | 0:36fb749b83c7 | 42 | { |
snatch59 | 0:36fb749b83c7 | 43 | // reset button arrays |
snatch59 | 0:36fb749b83c7 | 44 | for (int i = 0; i < NUM_KEYS; i++) |
snatch59 | 0:36fb749b83c7 | 45 | { |
snatch59 | 0:36fb749b83c7 | 46 | buttonCount[i] = 0; |
snatch59 | 0:36fb749b83c7 | 47 | buttonStatus[i] = 0; |
snatch59 | 0:36fb749b83c7 | 48 | buttonFlag[i] = 0; |
snatch59 | 0:36fb749b83c7 | 49 | } |
snatch59 | 0:36fb749b83c7 | 50 | } |
snatch59 | 0:36fb749b83c7 | 51 | |
snatch59 | 0:36fb749b83c7 | 52 | int Joystick::getKeyState(int i) |
snatch59 | 0:36fb749b83c7 | 53 | { |
snatch59 | 0:36fb749b83c7 | 54 | int retval = 0; |
snatch59 | 0:36fb749b83c7 | 55 | |
snatch59 | 0:36fb749b83c7 | 56 | if (i < NUM_KEYS) |
snatch59 | 0:36fb749b83c7 | 57 | { |
snatch59 | 0:36fb749b83c7 | 58 | retval = buttonFlag[i]; |
snatch59 | 0:36fb749b83c7 | 59 | } |
snatch59 | 0:36fb749b83c7 | 60 | |
snatch59 | 0:36fb749b83c7 | 61 | return retval; |
snatch59 | 0:36fb749b83c7 | 62 | } |
snatch59 | 0:36fb749b83c7 | 63 | |
snatch59 | 0:36fb749b83c7 | 64 | void Joystick::resetKeyState(int i) |
snatch59 | 0:36fb749b83c7 | 65 | { |
snatch59 | 0:36fb749b83c7 | 66 | if (i < NUM_KEYS) |
snatch59 | 0:36fb749b83c7 | 67 | { |
snatch59 | 0:36fb749b83c7 | 68 | buttonFlag[i] = 0; |
snatch59 | 0:36fb749b83c7 | 69 | } |
snatch59 | 0:36fb749b83c7 | 70 | } |
snatch59 | 0:36fb749b83c7 | 71 | |
snatch59 | 0:36fb749b83c7 | 72 | void Joystick::updateADCKey() |
snatch59 | 0:36fb749b83c7 | 73 | { |
snatch59 | 0:36fb749b83c7 | 74 | // NOTE: the mbed analog in is 0 - 3.3V, represented as 0.0 - 1.0. It is important |
snatch59 | 0:36fb749b83c7 | 75 | // that the LCD shield is powered from a 3.3V supply in order for the 'right' joystick |
snatch59 | 0:36fb749b83c7 | 76 | // key to function correctly. |
snatch59 | 0:36fb749b83c7 | 77 | |
snatch59 | 0:36fb749b83c7 | 78 | int adcKeyIn = joystick * 1024; // scale this up so we can use int |
snatch59 | 0:36fb749b83c7 | 79 | int keyIn = getKey(adcKeyIn); |
snatch59 | 0:36fb749b83c7 | 80 | |
snatch59 | 0:36fb749b83c7 | 81 | for (int i = 0; i < NUM_KEYS; i++) |
snatch59 | 0:36fb749b83c7 | 82 | { |
snatch59 | 0:36fb749b83c7 | 83 | if (keyIn == i) //one key is pressed |
snatch59 | 0:36fb749b83c7 | 84 | { |
snatch59 | 0:36fb749b83c7 | 85 | if (buttonCount[i] < DEBOUNCE_MAX) |
snatch59 | 0:36fb749b83c7 | 86 | { |
snatch59 | 0:36fb749b83c7 | 87 | buttonCount[i]++; |
snatch59 | 0:36fb749b83c7 | 88 | if (buttonCount[i] > DEBOUNCE_ON) |
snatch59 | 0:36fb749b83c7 | 89 | { |
snatch59 | 0:36fb749b83c7 | 90 | if (buttonStatus[i] == 0) |
snatch59 | 0:36fb749b83c7 | 91 | { |
snatch59 | 0:36fb749b83c7 | 92 | buttonFlag[i] = 1; |
snatch59 | 0:36fb749b83c7 | 93 | buttonStatus[i] = 1; //button debounced to 'pressed' status |
snatch59 | 0:36fb749b83c7 | 94 | } |
snatch59 | 0:36fb749b83c7 | 95 | } |
snatch59 | 0:36fb749b83c7 | 96 | } |
snatch59 | 0:36fb749b83c7 | 97 | } |
snatch59 | 0:36fb749b83c7 | 98 | else // no button pressed |
snatch59 | 0:36fb749b83c7 | 99 | { |
snatch59 | 0:36fb749b83c7 | 100 | if (buttonCount[i] > 0) |
snatch59 | 0:36fb749b83c7 | 101 | { |
snatch59 | 0:36fb749b83c7 | 102 | buttonFlag[i] = 0; |
snatch59 | 0:36fb749b83c7 | 103 | buttonCount[i]--; |
snatch59 | 0:36fb749b83c7 | 104 | if (buttonCount[i] < DEBOUNCE_OFF) |
snatch59 | 0:36fb749b83c7 | 105 | { |
snatch59 | 0:36fb749b83c7 | 106 | buttonStatus[i] = 0; //button debounced to 'released' status |
snatch59 | 0:36fb749b83c7 | 107 | } |
snatch59 | 0:36fb749b83c7 | 108 | } |
snatch59 | 0:36fb749b83c7 | 109 | } |
snatch59 | 0:36fb749b83c7 | 110 | } |
snatch59 | 0:36fb749b83c7 | 111 | } |
snatch59 | 0:36fb749b83c7 | 112 | |
snatch59 | 0:36fb749b83c7 | 113 | // Convert ADC value to key number |
snatch59 | 0:36fb749b83c7 | 114 | int Joystick::getKey(int input) |
snatch59 | 0:36fb749b83c7 | 115 | { |
snatch59 | 0:36fb749b83c7 | 116 | int k; |
snatch59 | 0:36fb749b83c7 | 117 | |
snatch59 | 0:36fb749b83c7 | 118 | for (k = 0; k < NUM_KEYS; k++) |
snatch59 | 0:36fb749b83c7 | 119 | { |
snatch59 | 0:36fb749b83c7 | 120 | if (input < adcKeyVal[k]) return k; |
snatch59 | 0:36fb749b83c7 | 121 | } |
snatch59 | 0:36fb749b83c7 | 122 | |
snatch59 | 0:36fb749b83c7 | 123 | if (k >= NUM_KEYS) k = -1; // No valid key pressed |
snatch59 | 0:36fb749b83c7 | 124 | |
snatch59 | 0:36fb749b83c7 | 125 | return k; |
snatch59 | 0:36fb749b83c7 | 126 | } |