pt aliff

Fork of keypad by Dimiter K

Committer:
aliffhilmie93
Date:
Mon Jun 04 16:01:47 2018 +0000
Revision:
1:ced7b1445bc4
Parent:
0:1fa357ea3fcc
pt aliff

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aliffhilmie93 1:ced7b1445bc4 1 /*mbed simple 4x4 keypad library, using polling
aliffhilmie93 1:ced7b1445bc4 2
aliffhilmie93 1:ced7b1445bc4 3 Copyright (c) 2015 Rune Langøy
DimiterK 0:1fa357ea3fcc 4
DimiterK 0:1fa357ea3fcc 5 Permission is hereby granted, free of charge, to any person obtaining a copy
DimiterK 0:1fa357ea3fcc 6 of this software and associated documentation files (the "Software"), to deal
DimiterK 0:1fa357ea3fcc 7 in the Software without restriction, including without limitation the rights
DimiterK 0:1fa357ea3fcc 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
DimiterK 0:1fa357ea3fcc 9 copies of the Software, and to permit persons to whom the Software is
DimiterK 0:1fa357ea3fcc 10 furnished to do so, subject to the following conditions:
DimiterK 0:1fa357ea3fcc 11
DimiterK 0:1fa357ea3fcc 12 The above copyright notice and this permission notice shall be included in
DimiterK 0:1fa357ea3fcc 13 all copies or substantial portions of the Software.
DimiterK 0:1fa357ea3fcc 14
DimiterK 0:1fa357ea3fcc 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
DimiterK 0:1fa357ea3fcc 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
DimiterK 0:1fa357ea3fcc 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
DimiterK 0:1fa357ea3fcc 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
DimiterK 0:1fa357ea3fcc 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DimiterK 0:1fa357ea3fcc 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
DimiterK 0:1fa357ea3fcc 21 THE SOFTWARE.
DimiterK 0:1fa357ea3fcc 22 */
DimiterK 0:1fa357ea3fcc 23
DimiterK 0:1fa357ea3fcc 24 #ifndef KEYPAD_H
DimiterK 0:1fa357ea3fcc 25 #define KEYPAD_H
DimiterK 0:1fa357ea3fcc 26
DimiterK 0:1fa357ea3fcc 27 #include "DigitalIn.h"
DimiterK 0:1fa357ea3fcc 28 #include "BusOut.h"
DimiterK 0:1fa357ea3fcc 29
aliffhilmie93 1:ced7b1445bc4 30 namespace mbed
aliffhilmie93 1:ced7b1445bc4 31 {
DimiterK 0:1fa357ea3fcc 32
DimiterK 0:1fa357ea3fcc 33 const char NO_KEY = '\0';
DimiterK 0:1fa357ea3fcc 34 #define KEY_RELEASED NO_KEY
aliffhilmie93 1:ced7b1445bc4 35
aliffhilmie93 1:ced7b1445bc4 36 /** @brief table showing the printed layout of the keypad \n
aliffhilmie93 1:ced7b1445bc4 37 */
aliffhilmie93 1:ced7b1445bc4 38 const char keys[16] = {'1','2','3','F',
aliffhilmie93 1:ced7b1445bc4 39 '4','5','6','E',
aliffhilmie93 1:ced7b1445bc4 40 '7','8','9','D',
aliffhilmie93 1:ced7b1445bc4 41 'A','0','B','C'
aliffhilmie93 1:ced7b1445bc4 42 };
aliffhilmie93 1:ced7b1445bc4 43 /**
aliffhilmie93 1:ced7b1445bc4 44 * An simple polling-based interface to read a 4x4 keypad.
aliffhilmie93 1:ced7b1445bc4 45 *
aliffhilmie93 1:ced7b1445bc4 46 * The function getKey() reads the index of the pressed key
aliffhilmie93 1:ced7b1445bc4 47 * and returns the letter of the pressed key
aliffhilmie93 1:ced7b1445bc4 48 *
aliffhilmie93 1:ced7b1445bc4 49 * This work is a derivative of the works done by:
aliffhilmie93 1:ced7b1445bc4 50 * Dimiter Kentri in 2010 https://developer.mbed.org/users/DimiterK/code/keypad/
aliffhilmie93 1:ced7b1445bc4 51 * and
aliffhilmie93 1:ced7b1445bc4 52 * Yoong Hor Meng in 2012 https://developer.mbed.org/users/yoonghm/code/keypad/
aliffhilmie93 1:ced7b1445bc4 53 *
aliffhilmie93 1:ced7b1445bc4 54 * Example:
aliffhilmie93 1:ced7b1445bc4 55 * @code
aliffhilmie93 1:ced7b1445bc4 56 * #include "mbed.h"
aliffhilmie93 1:ced7b1445bc4 57 * #include "keypad.h"
aliffhilmie93 1:ced7b1445bc4 58 *
aliffhilmie93 1:ced7b1445bc4 59 * Serial pc(USBTX, USBRX);
aliffhilmie93 1:ced7b1445bc4 60 * Keypad keypad(D3,D4,D5,D6,D7,D8,D9,D10);
aliffhilmie93 1:ced7b1445bc4 61 * // Keypad keypad( PC_3,PC_2,PA_0,PA_1,PA_4,PB_0,PC_1,PC_0 ); // Tested on Nucleo303RE card
aliffhilmie93 1:ced7b1445bc4 62 *
aliffhilmie93 1:ced7b1445bc4 63 * int main(void)
aliffhilmie93 1:ced7b1445bc4 64 * {
aliffhilmie93 1:ced7b1445bc4 65 * keypad.enablePullUp();
aliffhilmie93 1:ced7b1445bc4 66 * char key;
aliffhilmie93 1:ced7b1445bc4 67 * pc.printf("Please touch a key on the keypad\r\n");
aliffhilmie93 1:ced7b1445bc4 68 * while(1)
aliffhilmie93 1:ced7b1445bc4 69 * {
aliffhilmie93 1:ced7b1445bc4 70 * key = keypad.getKey();
aliffhilmie93 1:ced7b1445bc4 71 * if(key != KEY_RELEASED)
aliffhilmie93 1:ced7b1445bc4 72 * {
aliffhilmie93 1:ced7b1445bc4 73 * pc.printf("%c\r\n",key);
aliffhilmie93 1:ced7b1445bc4 74 * wait(0.2);
aliffhilmie93 1:ced7b1445bc4 75 * }
aliffhilmie93 1:ced7b1445bc4 76 * }
aliffhilmie93 1:ced7b1445bc4 77 * }
aliffhilmie93 1:ced7b1445bc4 78 * @endcode
aliffhilmie93 1:ced7b1445bc4 79
aliffhilmie93 1:ced7b1445bc4 80 */
DimiterK 0:1fa357ea3fcc 81
aliffhilmie93 1:ced7b1445bc4 82 class Keypad
aliffhilmie93 1:ced7b1445bc4 83 {
aliffhilmie93 1:ced7b1445bc4 84 public:
aliffhilmie93 1:ced7b1445bc4 85 /** @brief Create a 4x4 (col, row) or 4x4 keypad interface\n
aliffhilmie93 1:ced7b1445bc4 86 * <pre>
aliffhilmie93 1:ced7b1445bc4 87 * | Col0 | Col1 | Col2 | Col3 \n
aliffhilmie93 1:ced7b1445bc4 88 * -------+------+------+------+----- \n
aliffhilmie93 1:ced7b1445bc4 89 * Row 0 | x | x | x | x \n
aliffhilmie93 1:ced7b1445bc4 90 * Row 1 | x | x | x | x \n
aliffhilmie93 1:ced7b1445bc4 91 * Row 2 | x | x | x | x \n
aliffhilmie93 1:ced7b1445bc4 92 * Row 3 | x | x | x | x \n
aliffhilmie93 1:ced7b1445bc4 93 * </pre>
aliffhilmie93 1:ced7b1445bc4 94 *
aliffhilmie93 1:ced7b1445bc4 95 * @param col<0..3> Row data lines
aliffhilmie93 1:ced7b1445bc4 96 * @param row<0..3> Column data lines
aliffhilmie93 1:ced7b1445bc4 97 */
aliffhilmie93 1:ced7b1445bc4 98 Keypad(PinName col0, PinName col1, PinName col2, PinName col3, PinName row0,PinName row1, PinName row2, PinName row3);
aliffhilmie93 1:ced7b1445bc4 99
aliffhilmie93 1:ced7b1445bc4 100 /** @brief Returns the letter of the pressed key \n
aliffhilmie93 1:ced7b1445bc4 101 *
aliffhilmie93 1:ced7b1445bc4 102 * @return char
aliffhilmie93 1:ced7b1445bc4 103 * @returns
aliffhilmie93 1:ced7b1445bc4 104 * The pressed character\n
aliffhilmie93 1:ced7b1445bc4 105 * '\0' or NO_KEY if no keys was pressed
aliffhilmie93 1:ced7b1445bc4 106 */
aliffhilmie93 1:ced7b1445bc4 107 char getKey();
DimiterK 0:1fa357ea3fcc 108
aliffhilmie93 1:ced7b1445bc4 109 /** @brief Detects if any key was pressed
aliffhilmie93 1:ced7b1445bc4 110 *
aliffhilmie93 1:ced7b1445bc4 111 * @return bool
aliffhilmie93 1:ced7b1445bc4 112 * @retval true a key is pressed
aliffhilmie93 1:ced7b1445bc4 113 * @retval false no keys was not pressed
aliffhilmie93 1:ced7b1445bc4 114 */
aliffhilmie93 1:ced7b1445bc4 115 bool getKeyPressed();
aliffhilmie93 1:ced7b1445bc4 116 /** Enables internal PullUp resistors on the coloums pins
aliffhilmie93 1:ced7b1445bc4 117 *
aliffhilmie93 1:ced7b1445bc4 118 * @return void
aliffhilmie93 1:ced7b1445bc4 119 */
aliffhilmie93 1:ced7b1445bc4 120 void enablePullUp();
aliffhilmie93 1:ced7b1445bc4 121
aliffhilmie93 1:ced7b1445bc4 122 protected:
aliffhilmie93 1:ced7b1445bc4 123 BusIn _cols;
aliffhilmie93 1:ced7b1445bc4 124 BusOut _rows;
aliffhilmie93 1:ced7b1445bc4 125 /** @brief return the index value
aliffhilmie93 1:ced7b1445bc4 126 * representating the pressed key \n
aliffhilmie93 1:ced7b1445bc4 127 *
aliffhilmie93 1:ced7b1445bc4 128 * @return int
aliffhilmie93 1:ced7b1445bc4 129 * @returns
aliffhilmie93 1:ced7b1445bc4 130 * The index representing the pushed key used in table keys\n
aliffhilmie93 1:ced7b1445bc4 131 * -1 if no key was pressed
aliffhilmie93 1:ced7b1445bc4 132 */
aliffhilmie93 1:ced7b1445bc4 133 int getKeyIndex();
aliffhilmie93 1:ced7b1445bc4 134
DimiterK 0:1fa357ea3fcc 135 };
DimiterK 0:1fa357ea3fcc 136
DimiterK 0:1fa357ea3fcc 137 }
DimiterK 0:1fa357ea3fcc 138 #endif