Fork without short circuits

Dependents:   SaveKeypad

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 a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE
00021  */
00022 
00023 #ifndef KEYPAD_H
00024 #define KEYPAD_H
00025 
00026 #include "mbed.h"
00027 #include "FPointer.h"
00028 
00029 /**
00030  * An interrupt-based interface to 4x4 keypad.
00031  *
00032  * On each key pressed on a keypad, the index of the key is passed to a
00033  * user-defined function. User is free to define what to be done with the
00034  * input.
00035  *
00036  * This library makes use of
00037  * @see http://mbed.org/cookbook/FPointer by Andy Kirkham
00038  *
00039  * Example:
00040  * @code
00041  * #include "mbed.h"
00042  * #include "keypad.h"
00043  *
00044  * // Define your own keypad values
00045  * char Keytable[] = { '1', '2', '3', 'A',
00046  *                     '4', '5', '6', 'B',
00047  *                     '7', '8', '9', 'C',
00048  *                     '*', '0', '#', 'D'
00049  *                   };
00050  *
00051  * uint32_t cbAfterInput(uint32_t index) {
00052  *     printf("Index:%d => Key:%c\n", key, Keytable[index]);
00053  *     return 0;
00054  * }
00055  *
00056  * int main() {
00057  *     Keypad keypad(p25, p26, p27, p28, p21, p22, p23, p24);
00058  *     keypad.CallAfterInput(&cbAfterInput);
00059  *     keypad.Start();
00060  *
00061  *     while (1) {
00062  *         wait_ms(100);
00063  *     }
00064  * }
00065  * @endcode
00066  */
00067 class Keypad
00068 {
00069 public:
00070     /** Create a Keypad interface
00071      *
00072      *  @param row<3..0>     Row data lines
00073      *  @param col<3..0>     Column data lines
00074      *  @param debounce_ms   Debounce in ms (Default to 20ms)
00075      */
00076     Keypad(PinName row3, PinName row2, PinName row1, PinName row0,
00077            PinName col3, PinName col2, PinName col1, PinName col0,
00078            int debounce_ms = 20);
00079 
00080     /** Start the keypad interrupt routines
00081      */
00082     void Start(void);
00083 
00084     /** Stop the keypad interrupt routines
00085      */
00086     void Stop(void);
00087 
00088     /** Scan the keyboard for a debounced pressed key
00089      */
00090     int DebouncedScan(void);
00091 
00092     /** Scan the keyboard for a pressed key
00093      */
00094     int Scan(void);
00095 
00096     /** Scan the keyboard for multiple debounced pressed keys
00097      */
00098     int DebouncedScanMultiple(void);
00099 
00100     /** Scan the keyboard for multiple pressed keys
00101      */
00102     int ScanMultiple(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 CallAfterInput(uint32_t (*fptr)(uint32_t));
00109 
00110 protected:
00111     static const int row_count = 4;
00112     static const int col_count = 4;
00113 
00114     InterruptIn      _row0;
00115     InterruptIn      _row1;
00116     InterruptIn      _row2;
00117     InterruptIn      _row3;
00118     InterruptIn      *_rows[row_count];
00119     BusInOut         _cols;  // BusOut doesn't support mode() yet; need open drain to prevent short circuits...
00120     int              _debounce;
00121     FPointer         _input; // Called after each input
00122 
00123     void _callback();
00124     void _setupFallTrigger(void);
00125 };
00126 
00127 #endif // KEYPAD_H