Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
keypad.h@0:372ef8c791a1, 2019-07-05 (annotated)
- Committer:
- irsanjul
- Date:
- Fri Jul 05 10:33:24 2019 +0000
- Revision:
- 0:372ef8c791a1
hhhh
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| irsanjul | 0:372ef8c791a1 | 1 | /* mbed Keypad library, using user-defined interrupt callback |
| irsanjul | 0:372ef8c791a1 | 2 | * Copyright (c) 2012 Yoong Hor Meng |
| irsanjul | 0:372ef8c791a1 | 3 | * |
| irsanjul | 0:372ef8c791a1 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| irsanjul | 0:372ef8c791a1 | 5 | * of this software and associated documentation files (the "Software"), to deal |
| irsanjul | 0:372ef8c791a1 | 6 | * in the Software without restriction, including without limitation the rights |
| irsanjul | 0:372ef8c791a1 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| irsanjul | 0:372ef8c791a1 | 8 | * copies of the Software, and to permit persons to whom the Software is |
| irsanjul | 0:372ef8c791a1 | 9 | * furnished to do so, subject to the following conditions: |
| irsanjul | 0:372ef8c791a1 | 10 | * |
| irsanjul | 0:372ef8c791a1 | 11 | * The above copyright notice and this permission notice shall be included in |
| irsanjul | 0:372ef8c791a1 | 12 | * all copies or substantial portions of the Software. |
| irsanjul | 0:372ef8c791a1 | 13 | * |
| irsanjul | 0:372ef8c791a1 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| irsanjul | 0:372ef8c791a1 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| irsanjul | 0:372ef8c791a1 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| irsanjul | 0:372ef8c791a1 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| irsanjul | 0:372ef8c791a1 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| irsanjul | 0:372ef8c791a1 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| irsanjul | 0:372ef8c791a1 | 20 | * THE SOFTWARE |
| irsanjul | 0:372ef8c791a1 | 21 | */ |
| irsanjul | 0:372ef8c791a1 | 22 | |
| irsanjul | 0:372ef8c791a1 | 23 | #ifndef KEYPAD_H |
| irsanjul | 0:372ef8c791a1 | 24 | #define KEYPAD_H |
| irsanjul | 0:372ef8c791a1 | 25 | |
| irsanjul | 0:372ef8c791a1 | 26 | #include "mbed.h" |
| irsanjul | 0:372ef8c791a1 | 27 | #include "FPointer.h" |
| irsanjul | 0:372ef8c791a1 | 28 | |
| irsanjul | 0:372ef8c791a1 | 29 | /** |
| irsanjul | 0:372ef8c791a1 | 30 | * An interrupt-based interface to 4x4 keypad. |
| irsanjul | 0:372ef8c791a1 | 31 | * |
| irsanjul | 0:372ef8c791a1 | 32 | * On each key pressed on a keypad, the index of the key is passed to a |
| irsanjul | 0:372ef8c791a1 | 33 | * user-defined function. User is free to define what to be done with the |
| irsanjul | 0:372ef8c791a1 | 34 | * input. |
| irsanjul | 0:372ef8c791a1 | 35 | * |
| irsanjul | 0:372ef8c791a1 | 36 | * This library makes use of |
| irsanjul | 0:372ef8c791a1 | 37 | * @see http://mbed.org/cookbook/FPointer by Andy Kirkham |
| irsanjul | 0:372ef8c791a1 | 38 | * |
| irsanjul | 0:372ef8c791a1 | 39 | * Example: |
| irsanjul | 0:372ef8c791a1 | 40 | * @code |
| irsanjul | 0:372ef8c791a1 | 41 | * #include "mbed.h" |
| irsanjul | 0:372ef8c791a1 | 42 | * #include "keypad.h" |
| irsanjul | 0:372ef8c791a1 | 43 | * |
| irsanjul | 0:372ef8c791a1 | 44 | * // Define your own keypad values |
| irsanjul | 0:372ef8c791a1 | 45 | * char Keytable[] = { '1', '2', '3', 'A', |
| irsanjul | 0:372ef8c791a1 | 46 | * '4', '5', '6', 'B', |
| irsanjul | 0:372ef8c791a1 | 47 | * '7', '8', '9', 'C', |
| irsanjul | 0:372ef8c791a1 | 48 | * '*', '0', '#', 'D' |
| irsanjul | 0:372ef8c791a1 | 49 | * }; |
| irsanjul | 0:372ef8c791a1 | 50 | * |
| irsanjul | 0:372ef8c791a1 | 51 | * uint32_t cbAfterInput(uint32_t index) { |
| irsanjul | 0:372ef8c791a1 | 52 | * printf("Index:%d => Key:%c\n", key, Keytable[index]); |
| irsanjul | 0:372ef8c791a1 | 53 | * return 0; |
| irsanjul | 0:372ef8c791a1 | 54 | * } |
| irsanjul | 0:372ef8c791a1 | 55 | * |
| irsanjul | 0:372ef8c791a1 | 56 | * int main() { |
| irsanjul | 0:372ef8c791a1 | 57 | * Keypad keypad(p25, p26, p27, p28, p21, p22, p23, p24); |
| irsanjul | 0:372ef8c791a1 | 58 | * keypad.CallAfterInput(&cbAfterInput); |
| irsanjul | 0:372ef8c791a1 | 59 | * keypad.Start(); |
| irsanjul | 0:372ef8c791a1 | 60 | * |
| irsanjul | 0:372ef8c791a1 | 61 | * while (1) { |
| irsanjul | 0:372ef8c791a1 | 62 | * wait_ms(100); |
| irsanjul | 0:372ef8c791a1 | 63 | * } |
| irsanjul | 0:372ef8c791a1 | 64 | * } |
| irsanjul | 0:372ef8c791a1 | 65 | * @endcode |
| irsanjul | 0:372ef8c791a1 | 66 | */ |
| irsanjul | 0:372ef8c791a1 | 67 | class Keypad |
| irsanjul | 0:372ef8c791a1 | 68 | { |
| irsanjul | 0:372ef8c791a1 | 69 | public: |
| irsanjul | 0:372ef8c791a1 | 70 | /** Create a Keypad interface |
| irsanjul | 0:372ef8c791a1 | 71 | * |
| irsanjul | 0:372ef8c791a1 | 72 | * @param row<3..0> Row data lines |
| irsanjul | 0:372ef8c791a1 | 73 | * @param col<3..0> Column data lines |
| irsanjul | 0:372ef8c791a1 | 74 | * @param debounce_ms Debounce in ms (Default to 20ms) |
| irsanjul | 0:372ef8c791a1 | 75 | */ |
| irsanjul | 0:372ef8c791a1 | 76 | Keypad(PinName row3, PinName row2, PinName row1, PinName row0, |
| irsanjul | 0:372ef8c791a1 | 77 | PinName col3, PinName col2, PinName col1, PinName col0, |
| irsanjul | 0:372ef8c791a1 | 78 | int debounce_ms = 20); |
| irsanjul | 0:372ef8c791a1 | 79 | |
| irsanjul | 0:372ef8c791a1 | 80 | /** Start the keypad interrupt routines |
| irsanjul | 0:372ef8c791a1 | 81 | */ |
| irsanjul | 0:372ef8c791a1 | 82 | void Start(void); |
| irsanjul | 0:372ef8c791a1 | 83 | |
| irsanjul | 0:372ef8c791a1 | 84 | /** Stop the keypad interrupt routines |
| irsanjul | 0:372ef8c791a1 | 85 | */ |
| irsanjul | 0:372ef8c791a1 | 86 | void Stop(void); |
| irsanjul | 0:372ef8c791a1 | 87 | |
| irsanjul | 0:372ef8c791a1 | 88 | /** Scan the keyboard for a debounced pressed key |
| irsanjul | 0:372ef8c791a1 | 89 | */ |
| irsanjul | 0:372ef8c791a1 | 90 | int DebouncedScan(void); |
| irsanjul | 0:372ef8c791a1 | 91 | |
| irsanjul | 0:372ef8c791a1 | 92 | /** Scan the keyboard for a pressed key |
| irsanjul | 0:372ef8c791a1 | 93 | */ |
| irsanjul | 0:372ef8c791a1 | 94 | int Scan(void); |
| irsanjul | 0:372ef8c791a1 | 95 | |
| irsanjul | 0:372ef8c791a1 | 96 | /** Scan the keyboard for multiple debounced pressed keys |
| irsanjul | 0:372ef8c791a1 | 97 | */ |
| irsanjul | 0:372ef8c791a1 | 98 | int DebouncedScanMultiple(void); |
| irsanjul | 0:372ef8c791a1 | 99 | |
| irsanjul | 0:372ef8c791a1 | 100 | /** Scan the keyboard for multiple pressed keys |
| irsanjul | 0:372ef8c791a1 | 101 | */ |
| irsanjul | 0:372ef8c791a1 | 102 | int ScanMultiple(void); |
| irsanjul | 0:372ef8c791a1 | 103 | |
| irsanjul | 0:372ef8c791a1 | 104 | /** User-defined function that to be called when a key is pressed |
| irsanjul | 0:372ef8c791a1 | 105 | * @param fptr A function pointer takes a uint32_t and |
| irsanjul | 0:372ef8c791a1 | 106 | * returns uint32_t |
| irsanjul | 0:372ef8c791a1 | 107 | */ |
| irsanjul | 0:372ef8c791a1 | 108 | void CallAfterInput(uint32_t (*fptr)(uint32_t)); |
| irsanjul | 0:372ef8c791a1 | 109 | |
| irsanjul | 0:372ef8c791a1 | 110 | protected: |
| irsanjul | 0:372ef8c791a1 | 111 | static const int row_count = 4; |
| irsanjul | 0:372ef8c791a1 | 112 | static const int col_count = 4; |
| irsanjul | 0:372ef8c791a1 | 113 | |
| irsanjul | 0:372ef8c791a1 | 114 | InterruptIn _row0; |
| irsanjul | 0:372ef8c791a1 | 115 | InterruptIn _row1; |
| irsanjul | 0:372ef8c791a1 | 116 | InterruptIn _row2; |
| irsanjul | 0:372ef8c791a1 | 117 | InterruptIn _row3; |
| irsanjul | 0:372ef8c791a1 | 118 | InterruptIn *_rows[row_count]; |
| irsanjul | 0:372ef8c791a1 | 119 | BusInOut _cols; // BusOut doesn't support mode() yet; need open drain to prevent short circuits... |
| irsanjul | 0:372ef8c791a1 | 120 | int _debounce; |
| irsanjul | 0:372ef8c791a1 | 121 | FPointer _input; // Called after each input |
| irsanjul | 0:372ef8c791a1 | 122 | |
| irsanjul | 0:372ef8c791a1 | 123 | void _callback(); |
| irsanjul | 0:372ef8c791a1 | 124 | void _setupFallTrigger(void); |
| irsanjul | 0:372ef8c791a1 | 125 | }; |
| irsanjul | 0:372ef8c791a1 | 126 | |
| irsanjul | 0:372ef8c791a1 | 127 | #endif // KEYPAD_H |