4x4 Keypad easy to use library that pollis the interface width pullups

Dependents:   4x4KeyBoardExample xoxokey 4x4KeyBoardExample ProgettoCassaforte ... more

Fork of keypad by Dimiter K

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers keypad.h Source File

keypad.h

00001 /*mbed simple 4x4 keypad library, using polling
00002 
00003      Copyright (c) 2015 Rune Langøy
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011 
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #ifndef KEYPAD_H
00025 #define KEYPAD_H
00026 
00027 #include "DigitalIn.h"
00028 #include "BusOut.h"
00029 
00030 namespace mbed
00031 {
00032 
00033 const char NO_KEY = '\0';
00034 #define KEY_RELEASED NO_KEY
00035 
00036 /** @brief table showing the printed layout of the keypad \n
00037  */
00038 const char keys[16] = {'1','2','3','A',
00039                        '4','5','6','B',
00040                        '7','8','9','C',
00041                        '*','0','#','D'
00042                       };
00043 /**
00044 * An simple polling-based interface to read a 4x4 keypad.
00045 *
00046 * The function getKey() reads  the index of the pressed key
00047 * and returns the letter of the pressed key
00048 *
00049 *  This work is a derivative of the works done by:
00050 *    Dimiter Kentri in 2010 https://developer.mbed.org/users/DimiterK/code/keypad/
00051 *  and
00052 *    Yoong Hor Meng in 2012 https://developer.mbed.org/users/yoonghm/code/keypad/
00053 *
00054 * Example:
00055 * @code
00056 * #include "mbed.h"
00057 * #include "keypad.h"
00058 *
00059 * Serial pc(USBTX, USBRX);
00060 * Keypad keypad(D3,D4,D5,D6,D7,D8,D9,D10);
00061 * // Keypad keypad( PC_3,PC_2,PA_0,PA_1,PA_4,PB_0,PC_1,PC_0 ); // Tested on  Nucleo303RE card
00062 *
00063 * int main(void)
00064 * {
00065 *  keypad.enablePullUp();
00066 *  char key;
00067 *  pc.printf("Please touch a key on the keypad\r\n");
00068 *  while(1)
00069 *  {
00070 *   key = keypad.getKey();
00071 *   if(key != KEY_RELEASED)
00072 *   {
00073 *      pc.printf("%c\r\n",key);
00074 *      wait(0.2);
00075 *    }
00076 *  }
00077 * }
00078 * @endcode
00079 
00080 */
00081 
00082 class Keypad
00083 {
00084 public:
00085     /**  @brief Create a 4x4 (col, row) or 4x4 keypad interface\n
00086      *  <pre>
00087      *          | Col0 | Col1 | Col2 | Col3   \n
00088      *   -------+------+------+------+-----   \n
00089      *   Row 0  |   x  |   x  |   x  |  x     \n
00090      *   Row 1  |   x  |   x  |   x  |  x     \n
00091      *   Row 2  |   x  |   x  |   x  |  x     \n
00092      *   Row 3  |   x  |   x  |   x  |  x     \n
00093      *  </pre>
00094      *
00095      *  @param col<0..3>     Row data lines
00096      *  @param row<0..3>     Column data lines
00097      */
00098     Keypad(PinName col0, PinName col1, PinName col2, PinName col3, PinName row0,PinName row1, PinName row2, PinName row3);
00099 
00100     /** @brief  Returns the letter of the pressed key \n
00101      *
00102      *  @return char
00103      *  @returns
00104      *     The pressed character\n
00105      *     '\0' or NO_KEY if no keys was pressed
00106      */
00107     char getKey();
00108 
00109     /** @brief Detects if any key was pressed
00110     *
00111     *   @return bool
00112     *   @retval true   a key is pressed
00113     *   @retval false  no keys was not pressed
00114     */
00115     bool getKeyPressed();
00116     /**  Enables internal PullUp resistors on the coloums pins
00117     *
00118     *  @return void
00119     */
00120     void enablePullUp();
00121 
00122 protected:
00123     BusIn  _cols;
00124     BusOut _rows;
00125     /** @brief return the index value
00126     *      representating the pressed key \n
00127     *
00128     *  @return int
00129     *  @returns
00130     *    The index representing the pushed key used in table keys\n
00131     *    -1 if no key was pressed
00132     */
00133     int getKeyIndex();
00134 
00135 };
00136 
00137 }
00138 #endif