library keypad
Fork of keypad by
Keypad.h@10:da060f8c03e8, 2014-01-01 (annotated)
- Committer:
- yoonghm
- Date:
- Wed Jan 01 17:45:53 2014 +0000
- Revision:
- 10:da060f8c03e8
Update Keypad library to be used with RTOS.; Support any combination of keypad size below 4x4.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yoonghm | 10:da060f8c03e8 | 1 | /* mbed Keypad library, using user-defined interrupt callback |
yoonghm | 10:da060f8c03e8 | 2 | * Copyright (c) 2012 Yoong Hor Meng |
yoonghm | 10:da060f8c03e8 | 3 | * |
yoonghm | 10:da060f8c03e8 | 4 | * Permission is hereby granted, free of charge, to any person obtaining |
yoonghm | 10:da060f8c03e8 | 5 | * a copy of this software and associated documentation files (the |
yoonghm | 10:da060f8c03e8 | 6 | * "Software"), to deal in the Software without restriction, including |
yoonghm | 10:da060f8c03e8 | 7 | * without limitation the rights to use, copy, modify, merge, publish, |
yoonghm | 10:da060f8c03e8 | 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
yoonghm | 10:da060f8c03e8 | 9 | * permit persons to whom the Software is furnished to do so, subject to |
yoonghm | 10:da060f8c03e8 | 10 | * the following conditions: |
yoonghm | 10:da060f8c03e8 | 11 | * |
yoonghm | 10:da060f8c03e8 | 12 | * The above copyright notice and this permission notice shall be included |
yoonghm | 10:da060f8c03e8 | 13 | * in all copies or substantial portions of the Software. |
yoonghm | 10:da060f8c03e8 | 14 | * |
yoonghm | 10:da060f8c03e8 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
yoonghm | 10:da060f8c03e8 | 16 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
yoonghm | 10:da060f8c03e8 | 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
yoonghm | 10:da060f8c03e8 | 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
yoonghm | 10:da060f8c03e8 | 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
yoonghm | 10:da060f8c03e8 | 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
yoonghm | 10:da060f8c03e8 | 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE |
yoonghm | 10:da060f8c03e8 | 22 | */ |
yoonghm | 10:da060f8c03e8 | 23 | |
yoonghm | 10:da060f8c03e8 | 24 | #ifndef KEYPAD_H |
yoonghm | 10:da060f8c03e8 | 25 | #define KEYPAD_H |
yoonghm | 10:da060f8c03e8 | 26 | |
yoonghm | 10:da060f8c03e8 | 27 | #include "mbed.h" |
yoonghm | 10:da060f8c03e8 | 28 | #include "FPointer.h" |
yoonghm | 10:da060f8c03e8 | 29 | |
yoonghm | 10:da060f8c03e8 | 30 | /** |
yoonghm | 10:da060f8c03e8 | 31 | * An interrupt-based interface to 4x4 keypad. |
yoonghm | 10:da060f8c03e8 | 32 | * |
yoonghm | 10:da060f8c03e8 | 33 | * On each key pressed on a keypad, the index of the key is passed to a |
yoonghm | 10:da060f8c03e8 | 34 | * user-defined function. User is free to define what to be done with the |
yoonghm | 10:da060f8c03e8 | 35 | * input. |
yoonghm | 10:da060f8c03e8 | 36 | * |
yoonghm | 10:da060f8c03e8 | 37 | * Example: |
yoonghm | 10:da060f8c03e8 | 38 | * @code |
yoonghm | 10:da060f8c03e8 | 39 | * #include "mbed.h" |
yoonghm | 10:da060f8c03e8 | 40 | * #include "Keypad.h" |
yoonghm | 10:da060f8c03e8 | 41 | * |
yoonghm | 10:da060f8c03e8 | 42 | * // Define your own keypad values |
yoonghm | 10:da060f8c03e8 | 43 | * char Keytable[] = { '1', '2', '3', 'A', // r0 |
yoonghm | 10:da060f8c03e8 | 44 | * '4', '5', '6', 'B', // r1 |
yoonghm | 10:da060f8c03e8 | 45 | * '7', '8', '9', 'C', // r2 |
yoonghm | 10:da060f8c03e8 | 46 | * '*', '0', '#', 'D' // r3 |
yoonghm | 10:da060f8c03e8 | 47 | * }; |
yoonghm | 10:da060f8c03e8 | 48 | * // c0 c1 c2 c3 |
yoonghm | 10:da060f8c03e8 | 49 | * |
yoonghm | 10:da060f8c03e8 | 50 | * uint32_t Index; |
yoonghm | 10:da060f8c03e8 | 51 | * |
yoonghm | 10:da060f8c03e8 | 52 | * uint32_t cbAfterInput(uint32_t index) { |
yoonghm | 10:da060f8c03e8 | 53 | * Index = index; |
yoonghm | 10:da060f8c03e8 | 54 | * return 0; |
yoonghm | 10:da060f8c03e8 | 55 | * } |
yoonghm | 10:da060f8c03e8 | 56 | * |
yoonghm | 10:da060f8c03e8 | 57 | * int main() { |
yoonghm | 10:da060f8c03e8 | 58 | * // r0 r1 r2 r3 c0 c1 c2 c3 |
yoonghm | 10:da060f8c03e8 | 59 | * Keypad keypad(p21, p22, p23, p24, p25, p26, p27, p28); |
yoonghm | 10:da060f8c03e8 | 60 | * keypad.attach(&cbAfterInput); |
yoonghm | 10:da060f8c03e8 | 61 | * keypad.start(); // energize the keypad via c0-c3 |
yoonghm | 10:da060f8c03e8 | 62 | * |
yoonghm | 10:da060f8c03e8 | 63 | * while (1) { |
yoonghm | 10:da060f8c03e8 | 64 | * __wfi(); |
yoonghm | 10:da060f8c03e8 | 65 | * printf("Interrupted\r\n"); |
yoonghm | 10:da060f8c03e8 | 66 | * printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]); |
yoonghm | 10:da060f8c03e8 | 67 | * } |
yoonghm | 10:da060f8c03e8 | 68 | * } |
yoonghm | 10:da060f8c03e8 | 69 | * @endcode |
yoonghm | 10:da060f8c03e8 | 70 | */ |
yoonghm | 10:da060f8c03e8 | 71 | |
yoonghm | 10:da060f8c03e8 | 72 | class Keypad { |
yoonghm | 10:da060f8c03e8 | 73 | public: |
yoonghm | 10:da060f8c03e8 | 74 | /** Create a 4x4 (row, col) or 4x3 keypad interface: |
yoonghm | 10:da060f8c03e8 | 75 | * |
yoonghm | 10:da060f8c03e8 | 76 | * | Col0 | Col1 | Col2 | Col3 |
yoonghm | 10:da060f8c03e8 | 77 | * -------+------+------+------+----- |
yoonghm | 10:da060f8c03e8 | 78 | * Row 0 | x | x | x | x |
yoonghm | 10:da060f8c03e8 | 79 | * Row 1 | x | x | x | x |
yoonghm | 10:da060f8c03e8 | 80 | * Row 2 | x | x | x | x |
yoonghm | 10:da060f8c03e8 | 81 | * Row 3 | x | x | x | x |
yoonghm | 10:da060f8c03e8 | 82 | * |
yoonghm | 10:da060f8c03e8 | 83 | * @param row<0..3> Row data lines |
yoonghm | 10:da060f8c03e8 | 84 | * @param col<0..3> Column data lines |
yoonghm | 10:da060f8c03e8 | 85 | * @param debounce_ms Debounce in ms (Default to 20ms) |
yoonghm | 10:da060f8c03e8 | 86 | */ |
yoonghm | 10:da060f8c03e8 | 87 | Keypad(PinName r0, PinName r1, PinName r2, PinName r3, |
yoonghm | 10:da060f8c03e8 | 88 | PinName c0, PinName c1, PinName c2, PinName c3, |
yoonghm | 10:da060f8c03e8 | 89 | int debounce_ms = 20); |
yoonghm | 10:da060f8c03e8 | 90 | |
yoonghm | 10:da060f8c03e8 | 91 | /** Destructor |
yoonghm | 10:da060f8c03e8 | 92 | */ |
yoonghm | 10:da060f8c03e8 | 93 | ~Keypad(); |
yoonghm | 10:da060f8c03e8 | 94 | |
yoonghm | 10:da060f8c03e8 | 95 | /** Start the keypad interrupt routines |
yoonghm | 10:da060f8c03e8 | 96 | */ |
yoonghm | 10:da060f8c03e8 | 97 | void start(void); |
yoonghm | 10:da060f8c03e8 | 98 | |
yoonghm | 10:da060f8c03e8 | 99 | /** Stop the keypad interrupt routines |
yoonghm | 10:da060f8c03e8 | 100 | */ |
yoonghm | 10:da060f8c03e8 | 101 | void stop(void); |
yoonghm | 10:da060f8c03e8 | 102 | |
yoonghm | 10:da060f8c03e8 | 103 | /** User-defined function that to be called when a key is pressed |
yoonghm | 10:da060f8c03e8 | 104 | * @param fptr A function pointer takes a uint32_t and |
yoonghm | 10:da060f8c03e8 | 105 | * returns uint32_t |
yoonghm | 10:da060f8c03e8 | 106 | */ |
yoonghm | 10:da060f8c03e8 | 107 | void attach(uint32_t (*fptr)(uint32_t)); |
yoonghm | 10:da060f8c03e8 | 108 | |
yoonghm | 10:da060f8c03e8 | 109 | protected: |
yoonghm | 10:da060f8c03e8 | 110 | InterruptIn *_rows[4]; |
yoonghm | 10:da060f8c03e8 | 111 | DigitalOut *_cols[4]; |
yoonghm | 10:da060f8c03e8 | 112 | int _debounce; // miliseconds |
yoonghm | 10:da060f8c03e8 | 113 | int _nRow; |
yoonghm | 10:da060f8c03e8 | 114 | int _nCol; |
yoonghm | 10:da060f8c03e8 | 115 | FPointer _callback; // Called after each input |
yoonghm | 10:da060f8c03e8 | 116 | |
yoonghm | 10:da060f8c03e8 | 117 | void _checkIndex(int row, InterruptIn *therow); |
yoonghm | 10:da060f8c03e8 | 118 | void _cbRow0Rise(void); |
yoonghm | 10:da060f8c03e8 | 119 | void _cbRow1Rise(void); |
yoonghm | 10:da060f8c03e8 | 120 | void _cbRow2Rise(void); |
yoonghm | 10:da060f8c03e8 | 121 | void _cbRow3Rise(void); |
yoonghm | 10:da060f8c03e8 | 122 | void _setupRiseTrigger(void); |
yoonghm | 10:da060f8c03e8 | 123 | }; |
yoonghm | 10:da060f8c03e8 | 124 | |
yoonghm | 10:da060f8c03e8 | 125 | #endif // KEYPAD_H |
yoonghm | 10:da060f8c03e8 | 126 |