Fork without short circuits

Dependents:   SaveKeypad

Fork of keypad by HM Yoong

No extra hardware is needed besides the wires and switches. The columns are outputs configured with open drain. The rows are inputs configured with pull up resistors. A key press pulls down its row. With scanning the column is determined thereafter.

See SaveKeypad for an example usage.

Committer:
gj_schoneveld
Date:
Thu Nov 08 19:04:08 2012 +0000
Revision:
16:a9feaa7ac039
Parent:
13:fb6929fac0db
Updated Debounce

Who changed what in which revision?

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