s

Fork of keypad by HM Yoong

Committer:
felipe_r_andreis
Date:
Fri Aug 05 13:20:41 2016 +0000
Revision:
11:1d35d1df6c60
Parent:
10:da060f8c03e8
No changes

Who changed what in which revision?

UserRevisionLine numberNew 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
felipe_r_andreis 11:1d35d1df6c60 30
yoonghm 10:da060f8c03e8 31 /**
yoonghm 10:da060f8c03e8 32 * An interrupt-based interface to 4x4 keypad.
yoonghm 10:da060f8c03e8 33 *
yoonghm 10:da060f8c03e8 34 * On each key pressed on a keypad, the index of the key is passed to a
yoonghm 10:da060f8c03e8 35 * user-defined function. User is free to define what to be done with the
yoonghm 10:da060f8c03e8 36 * input.
yoonghm 10:da060f8c03e8 37 *
yoonghm 10:da060f8c03e8 38 * Example:
yoonghm 10:da060f8c03e8 39 * @code
yoonghm 10:da060f8c03e8 40 * #include "mbed.h"
yoonghm 10:da060f8c03e8 41 * #include "Keypad.h"
yoonghm 10:da060f8c03e8 42 *
yoonghm 10:da060f8c03e8 43 * // Define your own keypad values
yoonghm 10:da060f8c03e8 44 * char Keytable[] = { '1', '2', '3', 'A', // r0
yoonghm 10:da060f8c03e8 45 * '4', '5', '6', 'B', // r1
yoonghm 10:da060f8c03e8 46 * '7', '8', '9', 'C', // r2
yoonghm 10:da060f8c03e8 47 * '*', '0', '#', 'D' // r3
yoonghm 10:da060f8c03e8 48 * };
yoonghm 10:da060f8c03e8 49 * // c0 c1 c2 c3
yoonghm 10:da060f8c03e8 50 *
yoonghm 10:da060f8c03e8 51 * uint32_t Index;
yoonghm 10:da060f8c03e8 52 *
yoonghm 10:da060f8c03e8 53 * uint32_t cbAfterInput(uint32_t index) {
yoonghm 10:da060f8c03e8 54 * Index = index;
yoonghm 10:da060f8c03e8 55 * return 0;
yoonghm 10:da060f8c03e8 56 * }
yoonghm 10:da060f8c03e8 57 *
yoonghm 10:da060f8c03e8 58 * int main() {
yoonghm 10:da060f8c03e8 59 * // r0 r1 r2 r3 c0 c1 c2 c3
yoonghm 10:da060f8c03e8 60 * Keypad keypad(p21, p22, p23, p24, p25, p26, p27, p28);
yoonghm 10:da060f8c03e8 61 * keypad.attach(&cbAfterInput);
yoonghm 10:da060f8c03e8 62 * keypad.start(); // energize the keypad via c0-c3
yoonghm 10:da060f8c03e8 63 *
yoonghm 10:da060f8c03e8 64 * while (1) {
yoonghm 10:da060f8c03e8 65 * __wfi();
yoonghm 10:da060f8c03e8 66 * printf("Interrupted\r\n");
yoonghm 10:da060f8c03e8 67 * printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]);
yoonghm 10:da060f8c03e8 68 * }
yoonghm 10:da060f8c03e8 69 * }
yoonghm 10:da060f8c03e8 70 * @endcode
yoonghm 10:da060f8c03e8 71 */
yoonghm 10:da060f8c03e8 72
yoonghm 10:da060f8c03e8 73 class Keypad {
yoonghm 10:da060f8c03e8 74 public:
yoonghm 10:da060f8c03e8 75 /** Create a 4x4 (row, col) or 4x3 keypad interface:
yoonghm 10:da060f8c03e8 76 *
yoonghm 10:da060f8c03e8 77 * | Col0 | Col1 | Col2 | Col3
yoonghm 10:da060f8c03e8 78 * -------+------+------+------+-----
yoonghm 10:da060f8c03e8 79 * Row 0 | x | x | x | x
yoonghm 10:da060f8c03e8 80 * Row 1 | x | x | x | x
yoonghm 10:da060f8c03e8 81 * Row 2 | x | x | x | x
yoonghm 10:da060f8c03e8 82 * Row 3 | x | x | x | x
yoonghm 10:da060f8c03e8 83 *
yoonghm 10:da060f8c03e8 84 * @param row<0..3> Row data lines
yoonghm 10:da060f8c03e8 85 * @param col<0..3> Column data lines
yoonghm 10:da060f8c03e8 86 * @param debounce_ms Debounce in ms (Default to 20ms)
yoonghm 10:da060f8c03e8 87 */
yoonghm 10:da060f8c03e8 88 Keypad(PinName r0, PinName r1, PinName r2, PinName r3,
yoonghm 10:da060f8c03e8 89 PinName c0, PinName c1, PinName c2, PinName c3,
yoonghm 10:da060f8c03e8 90 int debounce_ms = 20);
yoonghm 10:da060f8c03e8 91
yoonghm 10:da060f8c03e8 92 /** Destructor
yoonghm 10:da060f8c03e8 93 */
yoonghm 10:da060f8c03e8 94 ~Keypad();
yoonghm 10:da060f8c03e8 95
yoonghm 10:da060f8c03e8 96 /** Start the keypad interrupt routines
yoonghm 10:da060f8c03e8 97 */
yoonghm 10:da060f8c03e8 98 void start(void);
yoonghm 10:da060f8c03e8 99
yoonghm 10:da060f8c03e8 100 /** Stop the keypad interrupt routines
yoonghm 10:da060f8c03e8 101 */
yoonghm 10:da060f8c03e8 102 void stop(void);
yoonghm 10:da060f8c03e8 103
yoonghm 10:da060f8c03e8 104 /** User-defined function that to be called when a key is pressed
yoonghm 10:da060f8c03e8 105 * @param fptr A function pointer takes a uint32_t and
yoonghm 10:da060f8c03e8 106 * returns uint32_t
yoonghm 10:da060f8c03e8 107 */
yoonghm 10:da060f8c03e8 108 void attach(uint32_t (*fptr)(uint32_t));
yoonghm 10:da060f8c03e8 109
yoonghm 10:da060f8c03e8 110 protected:
yoonghm 10:da060f8c03e8 111 InterruptIn *_rows[4];
yoonghm 10:da060f8c03e8 112 DigitalOut *_cols[4];
yoonghm 10:da060f8c03e8 113 int _debounce; // miliseconds
yoonghm 10:da060f8c03e8 114 int _nRow;
yoonghm 10:da060f8c03e8 115 int _nCol;
yoonghm 10:da060f8c03e8 116 FPointer _callback; // Called after each input
yoonghm 10:da060f8c03e8 117
yoonghm 10:da060f8c03e8 118 void _checkIndex(int row, InterruptIn *therow);
yoonghm 10:da060f8c03e8 119 void _cbRow0Rise(void);
yoonghm 10:da060f8c03e8 120 void _cbRow1Rise(void);
yoonghm 10:da060f8c03e8 121 void _cbRow2Rise(void);
yoonghm 10:da060f8c03e8 122 void _cbRow3Rise(void);
yoonghm 10:da060f8c03e8 123 void _setupRiseTrigger(void);
felipe_r_andreis 11:1d35d1df6c60 124
yoonghm 10:da060f8c03e8 125 };
yoonghm 10:da060f8c03e8 126
yoonghm 10:da060f8c03e8 127 #endif // KEYPAD_H
yoonghm 10:da060f8c03e8 128