jdlfkjasdf
Dependents: electrocoagulador_teclado
keypad.h@10:d66b1727615d, 2015-09-19 (annotated)
- Committer:
- rlanghbv
- Date:
- Sat Sep 19 17:06:05 2015 +0000
- Revision:
- 10:d66b1727615d
- Parent:
- 9:1f7793af2f64
- Child:
- 11:2176a5cf72b2
Doc Update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rlanghbv | 1:fa689a04c361 | 1 | /*mbed simple keypad library, using polling |
rlanghbv | 1:fa689a04c361 | 2 | |
rlanghbv | 1:fa689a04c361 | 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 | |
DimiterK | 0:1fa357ea3fcc | 30 | namespace mbed{ |
DimiterK | 0:1fa357ea3fcc | 31 | |
DimiterK | 0:1fa357ea3fcc | 32 | const char NO_KEY = '\0'; |
DimiterK | 0:1fa357ea3fcc | 33 | #define KEY_RELEASED NO_KEY |
rlanghbv | 1:fa689a04c361 | 34 | |
rlanghbv | 1:fa689a04c361 | 35 | const char keys[16] = {'1','2','3','A', |
DimiterK | 0:1fa357ea3fcc | 36 | '4','5','6','B', |
DimiterK | 0:1fa357ea3fcc | 37 | '7','8','9','C', |
DimiterK | 0:1fa357ea3fcc | 38 | '*','0','#','D'}; |
rlanghbv | 1:fa689a04c361 | 39 | /** |
rlanghbv | 1:fa689a04c361 | 40 | * An simple polling-based interface to read a 4x4 keypad. |
rlanghbv | 1:fa689a04c361 | 41 | * |
rlanghbv | 1:fa689a04c361 | 42 | * The function getKey() reads the index of the pressed key |
rlanghbv | 1:fa689a04c361 | 43 | * and returns the letter of the pressed key |
rlanghbv | 1:fa689a04c361 | 44 | * |
rlanghbv | 1:fa689a04c361 | 45 | * This work is a derivative of the works done by: |
rlanghbv | 1:fa689a04c361 | 46 | * Dimiter Kentri in 2010 https://developer.mbed.org/users/DimiterK/code/keypad/ |
rlanghbv | 1:fa689a04c361 | 47 | * and |
rlanghbv | 1:fa689a04c361 | 48 | * Yoong Hor Meng in 2012 https://developer.mbed.org/users/yoonghm/code/keypad/ |
rlanghbv | 1:fa689a04c361 | 49 | * |
rlanghbv | 2:cf9e1e315e74 | 50 | * Example: |
rlanghbv | 2:cf9e1e315e74 | 51 | * @code |
rlanghbv | 1:fa689a04c361 | 52 | * #include "mbed.h" |
rlanghbv | 1:fa689a04c361 | 53 | * #include "keypad.h" |
rlanghbv | 1:fa689a04c361 | 54 | * |
rlanghbv | 1:fa689a04c361 | 55 | * Serial pc(USBTX, USBRX); |
rlanghbv | 1:fa689a04c361 | 56 | * Keypad keypad(D3,D4,D5,D6,D7,D8,D9,D10); |
rlanghbv | 1:fa689a04c361 | 57 | * // Keypad keypad( PC_3,PC_2,PA_0,PA_1,PA_4,PB_0,PC_1,PC_0 ); // Tested on Nucleo303RE card |
rlanghbv | 1:fa689a04c361 | 58 | * |
rlanghbv | 1:fa689a04c361 | 59 | * int main(void) |
rlanghbv | 1:fa689a04c361 | 60 | * { |
rlanghbv | 1:fa689a04c361 | 61 | * keypad.enablePullUp(); |
rlanghbv | 1:fa689a04c361 | 62 | * char key; |
rlanghbv | 9:1f7793af2f64 | 63 | * pc.printf("Please touch a key on the keypad\r\n"); |
rlanghbv | 1:fa689a04c361 | 64 | * while(1) |
rlanghbv | 1:fa689a04c361 | 65 | * { |
rlanghbv | 1:fa689a04c361 | 66 | * key = keypad.getKey(); |
rlanghbv | 1:fa689a04c361 | 67 | * if(key != KEY_RELEASED) |
rlanghbv | 1:fa689a04c361 | 68 | * { |
rlanghbv | 9:1f7793af2f64 | 69 | * pc.printf("%c\r\n",key); |
rlanghbv | 1:fa689a04c361 | 70 | * wait(0.2); |
rlanghbv | 1:fa689a04c361 | 71 | * } |
rlanghbv | 1:fa689a04c361 | 72 | * } |
rlanghbv | 1:fa689a04c361 | 73 | * } |
rlanghbv | 1:fa689a04c361 | 74 | * @endcode |
rlanghbv | 1:fa689a04c361 | 75 | |
rlanghbv | 1:fa689a04c361 | 76 | */ |
rlanghbv | 1:fa689a04c361 | 77 | |
DimiterK | 0:1fa357ea3fcc | 78 | class Keypad{ |
DimiterK | 0:1fa357ea3fcc | 79 | public: |
rlanghbv | 8:862b0a362754 | 80 | /** @brief Create a 4x4 (col, row) or 4x4 keypad interface\n |
rlanghbv | 8:862b0a362754 | 81 | * \n |
rlanghbv | 10:d66b1727615d | 82 | * @verbatim |
rlanghbv | 3:d3772b2d763c | 83 | * | Col0 | Col1 | Col2 | Col3 \n |
rlanghbv | 3:d3772b2d763c | 84 | * -------+------+------+------+----- \n |
rlanghbv | 3:d3772b2d763c | 85 | * Row 0 | x | x | x | x \n |
rlanghbv | 3:d3772b2d763c | 86 | * Row 1 | x | x | x | x \n |
rlanghbv | 3:d3772b2d763c | 87 | * Row 2 | x | x | x | x \n |
rlanghbv | 3:d3772b2d763c | 88 | * Row 3 | x | x | x | x \n |
rlanghbv | 10:d66b1727615d | 89 | * @endverbatim |
rlanghbv | 6:d0a45d163653 | 90 | * |
rlanghbv | 1:fa689a04c361 | 91 | * @param col<0..3> Row data lines |
rlanghbv | 1:fa689a04c361 | 92 | * @param row<0..3> Column data lines |
rlanghbv | 1:fa689a04c361 | 93 | */ |
rlanghbv | 3:d3772b2d763c | 94 | Keypad(PinName col0, PinName col1, PinName col2, PinName col3, PinName row0,PinName row1, PinName row2, PinName row3); |
rlanghbv | 1:fa689a04c361 | 95 | |
rlanghbv | 8:862b0a362754 | 96 | /** @brief Returns the letter of the pressed key \n |
rlanghbv | 8:862b0a362754 | 97 | * if no walid key was pressed\n |
rlanghbv | 8:862b0a362754 | 98 | * the value -1 is returned\n |
rlanghbv | 1:fa689a04c361 | 99 | * |
rlanghbv | 8:862b0a362754 | 100 | * @return char - The pressed character or -1 if no char was pressed |
rlanghbv | 1:fa689a04c361 | 101 | */ |
DimiterK | 0:1fa357ea3fcc | 102 | char getKey(); |
rlanghbv | 1:fa689a04c361 | 103 | |
rlanghbv | 8:862b0a362754 | 104 | /** @brief Detects if any key was pressed |
rlanghbv | 1:fa689a04c361 | 105 | * |
rlanghbv | 8:862b0a362754 | 106 | * @return bool - true is returned if any key is pressed |
rlanghbv | 1:fa689a04c361 | 107 | */ |
rlanghbv | 1:fa689a04c361 | 108 | bool getKeyPressed(); |
rlanghbv | 8:862b0a362754 | 109 | /** Enables internal PullUp resistors on the coloums pins |
rlanghbv | 1:fa689a04c361 | 110 | * |
rlanghbv | 8:862b0a362754 | 111 | * @return void |
rlanghbv | 1:fa689a04c361 | 112 | */ |
rlanghbv | 1:fa689a04c361 | 113 | void enablePullUp(); |
DimiterK | 0:1fa357ea3fcc | 114 | |
DimiterK | 0:1fa357ea3fcc | 115 | protected: |
rlanghbv | 1:fa689a04c361 | 116 | BusIn _cols; |
DimiterK | 0:1fa357ea3fcc | 117 | BusOut _rows; |
rlanghbv | 8:862b0a362754 | 118 | /** @brief return the index value \n |
rlanghbv | 8:862b0a362754 | 119 | * representating the pressed key \n |
rlanghbv | 8:862b0a362754 | 120 | * if no walid key was pressed \n |
rlanghbv | 8:862b0a362754 | 121 | * the value -1 is returned |
rlanghbv | 1:fa689a04c361 | 122 | * |
rlanghbv | 8:862b0a362754 | 123 | * @return int |
rlanghbv | 1:fa689a04c361 | 124 | */ |
rlanghbv | 1:fa689a04c361 | 125 | int getKeyIndex(); |
rlanghbv | 1:fa689a04c361 | 126 | |
DimiterK | 0:1fa357ea3fcc | 127 | }; |
DimiterK | 0:1fa357ea3fcc | 128 | |
DimiterK | 0:1fa357ea3fcc | 129 | } |
DimiterK | 0:1fa357ea3fcc | 130 | #endif |