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