s

Fork of keypad by HM Yoong

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Keypad.h Source File

Keypad.h

00001 /* mbed Keypad library, using user-defined interrupt callback
00002  * Copyright (c) 2012 Yoong Hor Meng
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining
00005  * a copy of this software and associated documentation files (the
00006  * "Software"), to deal in the Software without restriction, including
00007  * without limitation the rights to use, copy, modify, merge, publish,
00008  * distribute, sublicense, and/or sell copies of the Software, and to
00009  * permit persons to whom the Software is furnished to do so, subject to
00010  * the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included
00013  * in all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
00019  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00020  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00021  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
00022  */
00023 
00024 #ifndef KEYPAD_H
00025 #define KEYPAD_H
00026 
00027 #include "mbed.h"
00028 #include "FPointer.h"
00029 
00030 
00031 /**
00032  * An interrupt-based interface to 4x4 keypad.
00033  *
00034  * On each key pressed on a keypad, the index of the key is passed to a
00035  * user-defined function. User is free to define what to be done with the
00036  * input.
00037  *
00038  * Example:
00039  * @code
00040  * #include "mbed.h"
00041  * #include "Keypad.h"
00042  *
00043  * // Define your own keypad values
00044  * char Keytable[] = { '1', '2', '3', 'A',   // r0
00045  *                     '4', '5', '6', 'B',   // r1
00046  *                     '7', '8', '9', 'C',   // r2
00047  *                     '*', '0', '#', 'D'    // r3
00048  *                   };
00049  *                  // c0   c1   c2   c3
00050  *
00051  * uint32_t Index;
00052  *
00053  * uint32_t cbAfterInput(uint32_t index) {
00054  *     Index = index;
00055  *     return 0;
00056  * }
00057  *
00058  * int main() {
00059  *                 // r0   r1   r2   r3   c0   c1   c2   c3
00060  *     Keypad keypad(p21, p22, p23, p24, p25, p26, p27, p28);
00061  *     keypad.attach(&cbAfterInput);
00062  *     keypad.start();  // energize the keypad via c0-c3
00063  *
00064  *     while (1) {
00065  *         __wfi();
00066  *         printf("Interrupted\r\n");
00067  *         printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]);
00068  *     }
00069  * }
00070  * @endcode
00071  */
00072 
00073 class Keypad {
00074 public:
00075     /** Create a 4x4 (row, col) or 4x3 keypad interface:
00076      *
00077      *          | Col0 | Col1 | Col2 | Col3
00078      *   -------+------+------+------+-----
00079      *   Row 0  |   x  |   x  |   x  |  x
00080      *   Row 1  |   x  |   x  |   x  |  x
00081      *   Row 2  |   x  |   x  |   x  |  x
00082      *   Row 3  |   x  |   x  |   x  |  x
00083      *
00084      *  @param row<0..3>     Row data lines
00085      *  @param col<0..3>     Column data lines
00086      *  @param debounce_ms   Debounce in ms (Default to 20ms)
00087      */
00088     Keypad(PinName r0, PinName r1, PinName r2, PinName r3,
00089            PinName c0, PinName c1, PinName c2, PinName c3,
00090            int debounce_ms = 20);
00091 
00092     /** Destructor
00093      */
00094     ~Keypad();
00095                     
00096     /** Start the keypad interrupt routines
00097      */
00098     void start(void);
00099 
00100     /** Stop the keypad interrupt routines
00101      */
00102     void stop(void);
00103 
00104     /** User-defined function that to be called when a key is pressed
00105      *  @param fptr           A function pointer takes a uint32_t and
00106      *                        returns uint32_t
00107      */
00108     void attach(uint32_t (*fptr)(uint32_t));
00109 
00110 protected:
00111     InterruptIn     *_rows[4];
00112     DigitalOut      *_cols[4];
00113     int              _debounce;  // miliseconds
00114     int              _nRow;
00115     int              _nCol;
00116     FPointer         _callback; // Called after each input
00117 
00118     void _checkIndex(int row, InterruptIn *therow);
00119     void _cbRow0Rise(void);
00120     void _cbRow1Rise(void);
00121     void _cbRow2Rise(void);
00122     void _cbRow3Rise(void);
00123     void _setupRiseTrigger(void);
00124     
00125 };
00126 
00127 #endif // KEYPAD_H
00128