Library for Nuelectronics Nokia 3310/5110 LCD Display and joystick.
Dependents: N3310LCD_Demo FRDM_N3110LCD
Diff: Joystick.cpp
- Revision:
- 1:51961974fe55
- Parent:
- 0:7efa6655d94b
- Child:
- 2:e6c002c680a6
--- a/Joystick.cpp Sun Mar 10 18:15:25 2013 +0000 +++ b/Joystick.cpp Sun Mar 10 18:29:09 2013 +0000 @@ -13,7 +13,7 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * N3310LCD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -34,18 +34,17 @@ // values correspond to use of a 3.3V supply for the LCD shield. const int Joystick::adcKeyVal[NUM_KEYS] = {50, // LEFT - 200, // CENTER DEPRESSED - 400, // DOWN - 600, // UP - 800 // RIGHT - // 1024 CENTER NOT DEPRESSED - }; - + 200, // CENTER DEPRESSED + 400, // DOWN + 600, // UP + 800 // RIGHT + // 1024 CENTER NOT DEPRESSED + }; + Joystick::Joystick(PinName jstick) : joystick(jstick) { // reset button arrays - for (int i = 0; i < NUM_KEYS; i++) - { + for (int i = 0; i < NUM_KEYS; i++) { buttonCount[i] = 0; buttonStatus[i] = 0; buttonFlag[i] = 0; @@ -55,76 +54,64 @@ int Joystick::getKeyState(int i) { int retval = 0; - - if (i < NUM_KEYS) - { + + if (i < NUM_KEYS) { retval = buttonFlag[i]; } - + return retval; } void Joystick::resetKeyState(int i) { - if (i < NUM_KEYS) - { + if (i < NUM_KEYS) { buttonFlag[i] = 0; } } void Joystick::updateADCKey() { - // NOTE: the mbed analog in is 0 - 3.3V, represented as 0.0 - 1.0. It is important + // NOTE: the mbed analog in is 0 - 3.3V, represented as 0.0 - 1.0. It is important // that the LCD shield is powered from a 3.3V supply in order for the 'right' joystick // key to function correctly. - + int adcKeyIn = joystick * 1024; // scale this up so we can use int int keyIn = getKey(adcKeyIn); pc.printf("%d \n",adcKeyIn ); - - for (int i = 0; i < NUM_KEYS; i++) - { - if (keyIn == i) //one key is pressed - { - if (buttonCount[i] < DEBOUNCE_MAX) - { + + for (int i = 0; i < NUM_KEYS; i++) { + if (keyIn == i) { //one key is pressed + if (buttonCount[i] < DEBOUNCE_MAX) { buttonCount[i]++; - if (buttonCount[i] > DEBOUNCE_ON) - { - if (buttonStatus[i] == 0) - { + if (buttonCount[i] > DEBOUNCE_ON) { + if (buttonStatus[i] == 0) { buttonFlag[i] = 1; buttonStatus[i] = 1; //button debounced to 'pressed' status } } } - } - else // no button pressed - { - if (buttonCount[i] > 0) - { - buttonFlag[i] = 0; + } else { // no button pressed + if (buttonCount[i] > 0) { + buttonFlag[i] = 0; buttonCount[i]--; - if (buttonCount[i] < DEBOUNCE_OFF) - { + if (buttonCount[i] < DEBOUNCE_OFF) { buttonStatus[i] = 0; //button debounced to 'released' status } } } } } - + // Convert ADC value to key number int Joystick::getKey(int input) { int k; - - for (k = 0; k < NUM_KEYS; k++) - { + + for (k = 0; k < NUM_KEYS; k++) { if (input < adcKeyVal[k]) return k; } - + if (k >= NUM_KEYS) k = -1; // No valid key pressed - + return k; }