Code to interface mbed to a Nokia 3310 LCD using the Nokia 3310 LCD shield from nuelectronics. Includes joystick interface and demo.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Joystick.cpp Source File

Joystick.cpp

00001 /*
00002 * N3310LCD. A program to interface mbed with the nuelectronics
00003 * Nokia 3310 LCD shield from www.nuelectronics.com. Ported from
00004 * the nuelectronics Arduino code.
00005 *
00006 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
00007 *
00008 * This file is part of N3310LCD.
00009 *
00010 * N3310LCD is free software: you can redistribute it and/or modify
00011 * it under the terms of the GNU General Public License as published by
00012 * the Free Software Foundation, either version 3 of the License, or
00013 * (at your option) any later version.
00014 * 
00015 * N3310LCD is distributed in the hope that it will be useful,
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 * GNU General Public License for more details.
00019 *
00020 * You should have received a copy of the GNU General Public License
00021 * along with N3310LCD.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 
00024 #include <mbed.h>
00025 #include "Joystick.h"
00026 
00027 //keypad debounce parameter
00028 #define DEBOUNCE_MAX 15
00029 #define DEBOUNCE_ON  10
00030 #define DEBOUNCE_OFF 3
00031 
00032 // values correspond to use of a 3.3V supply for the LCD shield.
00033 const int Joystick::adcKeyVal[NUM_KEYS] = {50,     // LEFT
00034                                            200,    // CENTER DEPRESSED
00035                                            400,    // DOWN 
00036                                            600,    // UP
00037                                            800     // RIGHT
00038                                            // 1024 CENTER NOT DEPRESSED
00039                                            };
00040                                            
00041 Joystick::Joystick(PinName jstick) : joystick(jstick)
00042 {
00043     // reset button arrays
00044     for (int i = 0; i < NUM_KEYS; i++)
00045     {
00046         buttonCount[i] = 0;
00047         buttonStatus[i] = 0;
00048         buttonFlag[i] = 0;
00049     }
00050 }
00051 
00052 int Joystick::getKeyState(int i)
00053 {
00054     int retval = 0;
00055     
00056     if (i < NUM_KEYS)
00057     {
00058         retval = buttonFlag[i];
00059     }
00060     
00061     return retval;
00062 }
00063 
00064 void Joystick::resetKeyState(int i)
00065 {
00066     if (i < NUM_KEYS)
00067     {
00068         buttonFlag[i] = 0;
00069     }
00070 }
00071 
00072 void Joystick::updateADCKey()
00073 {
00074     // NOTE: the mbed analog in is 0 - 3.3V, represented as 0.0 - 1.0. It is important 
00075     // that the LCD shield is powered from a 3.3V supply in order for the 'right' joystick
00076     // key to function correctly.
00077     
00078     int adcKeyIn = joystick * 1024;    // scale this up so we can use int
00079     int keyIn = getKey(adcKeyIn);
00080     
00081     for (int i = 0; i < NUM_KEYS; i++)
00082     {
00083         if (keyIn == i)  //one key is pressed 
00084         { 
00085             if (buttonCount[i] < DEBOUNCE_MAX)
00086             {
00087                 buttonCount[i]++;
00088                 if (buttonCount[i] > DEBOUNCE_ON)
00089                 {
00090                     if (buttonStatus[i] == 0)
00091                     {
00092                         buttonFlag[i] = 1;
00093                         buttonStatus[i] = 1; //button debounced to 'pressed' status
00094                     }
00095                 }
00096             }
00097         }
00098         else // no button pressed
00099         {
00100             if (buttonCount[i] > 0)
00101             {  
00102                 buttonFlag[i] = 0;    
00103                 buttonCount[i]--;
00104                 if (buttonCount[i] < DEBOUNCE_OFF)
00105                 {
00106                     buttonStatus[i] = 0;   //button debounced to 'released' status
00107                 }
00108             }
00109         }
00110     }
00111 }
00112   
00113 // Convert ADC value to key number
00114 int Joystick::getKey(int input)
00115 {
00116     int k;
00117     
00118     for (k = 0; k < NUM_KEYS; k++)
00119     {
00120         if (input < adcKeyVal[k]) return k;
00121     }
00122     
00123     if (k >= NUM_KEYS) k = -1;     // No valid key pressed
00124     
00125     return k;
00126 }