Class library for a touchscreen-based keypad for the LCD display present on the DISCO_F429ZI board.
Dependents: WIRE-BANDING_FT810 WIRE-BANDING_copy
KEYPAD_DISCO_F429ZI.h@4:9d6ea6fbb67f, 2016-05-10 (annotated)
- Committer:
- grantphillips
- Date:
- Tue May 10 21:02:52 2016 +0000
- Revision:
- 4:9d6ea6fbb67f
- Parent:
- 3:5f2fa06a0492
v1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
grantphillips | 0:8cc22acc00d2 | 1 | /* KEYPAD_DISCO_F429ZI Library v1.0 |
grantphillips | 0:8cc22acc00d2 | 2 | * Copyright (c) 2016 Grant Phillips |
grantphillips | 0:8cc22acc00d2 | 3 | * grant.phillips@nmmu.ac.za |
grantphillips | 0:8cc22acc00d2 | 4 | * |
grantphillips | 0:8cc22acc00d2 | 5 | * |
grantphillips | 0:8cc22acc00d2 | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
grantphillips | 0:8cc22acc00d2 | 7 | * of this software and associated documentation files (the "Software"), to deal |
grantphillips | 0:8cc22acc00d2 | 8 | * in the Software without restriction, including without limitation the rights |
grantphillips | 0:8cc22acc00d2 | 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
grantphillips | 0:8cc22acc00d2 | 10 | * copies of the Software, and to permit persons to whom the Software is |
grantphillips | 0:8cc22acc00d2 | 11 | * furnished to do so, subject to the following conditions: |
grantphillips | 0:8cc22acc00d2 | 12 | * |
grantphillips | 0:8cc22acc00d2 | 13 | * The above copyright notice and this permission notice shall be included in |
grantphillips | 0:8cc22acc00d2 | 14 | * all copies or substantial portions of the Software. |
grantphillips | 0:8cc22acc00d2 | 15 | * |
grantphillips | 0:8cc22acc00d2 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
grantphillips | 0:8cc22acc00d2 | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
grantphillips | 0:8cc22acc00d2 | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
grantphillips | 0:8cc22acc00d2 | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
grantphillips | 0:8cc22acc00d2 | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
grantphillips | 0:8cc22acc00d2 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
grantphillips | 0:8cc22acc00d2 | 22 | * THE SOFTWARE. |
grantphillips | 0:8cc22acc00d2 | 23 | */ |
grantphillips | 0:8cc22acc00d2 | 24 | |
grantphillips | 0:8cc22acc00d2 | 25 | #ifndef KEYPAD_DISCO_F429ZI_H |
grantphillips | 0:8cc22acc00d2 | 26 | #define KEYPAD_DISCO_F429ZI_H |
grantphillips | 0:8cc22acc00d2 | 27 | |
grantphillips | 0:8cc22acc00d2 | 28 | #include "mbed.h" |
grantphillips | 0:8cc22acc00d2 | 29 | #include "TS_DISCO_F429ZI.h" |
grantphillips | 0:8cc22acc00d2 | 30 | #include "LCD_DISCO_F429ZI.h" |
grantphillips | 0:8cc22acc00d2 | 31 | |
grantphillips | 3:5f2fa06a0492 | 32 | /** Class library for a touchscreen-based 4x4 keypad for the LCD display present on the DISCO_F429ZI board. |
grantphillips | 2:51f454b7c9ab | 33 | * It requires the TS_DISCO_F429ZI and LCD_DISCO_F429ZI to also be imported into the program. |
grantphillips | 0:8cc22acc00d2 | 34 | * |
grantphillips | 0:8cc22acc00d2 | 35 | * Example: |
grantphillips | 0:8cc22acc00d2 | 36 | * @code |
grantphillips | 0:8cc22acc00d2 | 37 | * #include "mbed.h" |
grantphillips | 4:9d6ea6fbb67f | 38 | * #include "TS_DISCO_F429ZI.h" |
grantphillips | 4:9d6ea6fbb67f | 39 | * #include "LCD_DISCO_F429ZI.h" |
grantphillips | 4:9d6ea6fbb67f | 40 | * #include "KEYPAD_DISCO_F429ZI.h" |
grantphillips | 4:9d6ea6fbb67f | 41 | * |
grantphillips | 4:9d6ea6fbb67f | 42 | * KEYPAD_DISCO_F429ZI keypad; |
grantphillips | 4:9d6ea6fbb67f | 43 | * |
grantphillips | 0:8cc22acc00d2 | 44 | * int main() { |
grantphillips | 4:9d6ea6fbb67f | 45 | * char buf[20]; |
grantphillips | 4:9d6ea6fbb67f | 46 | * char k, key; |
grantphillips | 4:9d6ea6fbb67f | 47 | * |
grantphillips | 4:9d6ea6fbb67f | 48 | * keypad.Show(1, 1); //Display the keypad, enable the Message and Output Box areas |
grantphillips | 4:9d6ea6fbb67f | 49 | * keypad.WriteMsgBoxLine(0, "Press any key..."); //display a message in the |
grantphillips | 4:9d6ea6fbb67f | 50 | * keypad.WriteMsgBoxLine(1, "----------------"); //Message Box area of the keypad |
grantphillips | 4:9d6ea6fbb67f | 51 | * |
grantphillips | 4:9d6ea6fbb67f | 52 | * while(1) { |
grantphillips | 4:9d6ea6fbb67f | 53 | * k = keypad.ReadKey(); //read the current key pressed |
grantphillips | 4:9d6ea6fbb67f | 54 | * if(k != '\0') { //if a key is pressed |
grantphillips | 4:9d6ea6fbb67f | 55 | * key = k; //store the key in a variable |
grantphillips | 4:9d6ea6fbb67f | 56 | * do //read until key is released |
grantphillips | 4:9d6ea6fbb67f | 57 | * k = keypad.ReadKey(); |
grantphillips | 4:9d6ea6fbb67f | 58 | * while(k != '\0'); |
grantphillips | 4:9d6ea6fbb67f | 59 | * sprintf(buf, "%c was pressed", key); |
grantphillips | 4:9d6ea6fbb67f | 60 | * keypad.WriteOutBox(buf); //display result in Output Box area of keypad |
grantphillips | 4:9d6ea6fbb67f | 61 | * } |
grantphillips | 0:8cc22acc00d2 | 62 | * } |
grantphillips | 0:8cc22acc00d2 | 63 | * } |
grantphillips | 0:8cc22acc00d2 | 64 | * @endcode |
grantphillips | 0:8cc22acc00d2 | 65 | */ |
grantphillips | 0:8cc22acc00d2 | 66 | |
grantphillips | 0:8cc22acc00d2 | 67 | class KEYPAD_DISCO_F429ZI { |
grantphillips | 0:8cc22acc00d2 | 68 | public: |
grantphillips | 0:8cc22acc00d2 | 69 | /** Create a KEYPAD_DISCO_F429ZI object. |
grantphillips | 0:8cc22acc00d2 | 70 | */ |
grantphillips | 0:8cc22acc00d2 | 71 | KEYPAD_DISCO_F429ZI(); |
grantphillips | 0:8cc22acc00d2 | 72 | |
grantphillips | 1:1db4c6c394bb | 73 | /** Draws the keys for the keypad and the Message Box and Output Box areas if enabled. |
grantphillips | 1:1db4c6c394bb | 74 | * @param |
grantphillips | 1:1db4c6c394bb | 75 | * MessageBoxEnable Enable (=1) or disable(=0 the drawing of a Message Box area on the keypad screen. |
grantphillips | 1:1db4c6c394bb | 76 | * OutBoxEnable Enable (=1) or disable(=0 the drawing of a Output Box area on the keypad screen. |
grantphillips | 0:8cc22acc00d2 | 77 | */ |
grantphillips | 0:8cc22acc00d2 | 78 | void Show(int MessageBoxEnable, int OutBoxEnable); |
grantphillips | 0:8cc22acc00d2 | 79 | |
grantphillips | 0:8cc22acc00d2 | 80 | /** Return the current key pressed on the keypad. |
grantphillips | 0:8cc22acc00d2 | 81 | * @param |
grantphillips | 0:8cc22acc00d2 | 82 | * None |
grantphillips | 0:8cc22acc00d2 | 83 | * @return |
grantphillips | 0:8cc22acc00d2 | 84 | * ASCII character of the key pressed. Will be NULL ('/0') if no key is pressed. |
grantphillips | 0:8cc22acc00d2 | 85 | */ |
grantphillips | 0:8cc22acc00d2 | 86 | char ReadKey(); |
grantphillips | 0:8cc22acc00d2 | 87 | |
grantphillips | 2:51f454b7c9ab | 88 | /** Writes text (21 characters max) to one of the two lines of the Message Box area if it is enabled in Show(). |
grantphillips | 2:51f454b7c9ab | 89 | * @param |
grantphillips | 2:51f454b7c9ab | 90 | * LineNum The line number to to where the text must be displayed (0 or 1). |
grantphillips | 2:51f454b7c9ab | 91 | * text String to display. |
grantphillips | 2:51f454b7c9ab | 92 | */ |
grantphillips | 2:51f454b7c9ab | 93 | void WriteMsgBoxLine(int LineNum, char *text); |
grantphillips | 2:51f454b7c9ab | 94 | |
grantphillips | 2:51f454b7c9ab | 95 | /** Writes text (14 characters max) to the Output Box area if it is enabled in Show(). |
grantphillips | 2:51f454b7c9ab | 96 | * @param |
grantphillips | 2:51f454b7c9ab | 97 | * text String to display. |
grantphillips | 2:51f454b7c9ab | 98 | */ |
grantphillips | 2:51f454b7c9ab | 99 | void WriteOutBox(char *text); |
grantphillips | 2:51f454b7c9ab | 100 | |
grantphillips | 3:5f2fa06a0492 | 101 | /** Sets the ASCII characters for each key on the 4x4 keypad. |
grantphillips | 3:5f2fa06a0492 | 102 | * @param |
grantphillips | 4:9d6ea6fbb67f | 103 | * keymap Pointer to a two-dimensional array (array[4][4]) with ASCII characters for each key. |
grantphillips | 3:5f2fa06a0492 | 104 | */ |
grantphillips | 3:5f2fa06a0492 | 105 | void SetKeys(char (*keymap)[4]); |
grantphillips | 3:5f2fa06a0492 | 106 | |
grantphillips | 2:51f454b7c9ab | 107 | /** Sets the foreground color for the keypad keys. |
grantphillips | 2:51f454b7c9ab | 108 | * @param |
grantphillips | 2:51f454b7c9ab | 109 | * color The color constant or code ARGB(8-8-8-8). |
grantphillips | 2:51f454b7c9ab | 110 | */ |
grantphillips | 2:51f454b7c9ab | 111 | void SetKeyForeColor(uint32_t color); |
grantphillips | 2:51f454b7c9ab | 112 | |
grantphillips | 2:51f454b7c9ab | 113 | /** Sets the background color for the keypad keys. |
grantphillips | 2:51f454b7c9ab | 114 | * @param |
grantphillips | 2:51f454b7c9ab | 115 | * color The color constant or code ARGB(8-8-8-8). |
grantphillips | 2:51f454b7c9ab | 116 | */ |
grantphillips | 2:51f454b7c9ab | 117 | void SetKeyBackColor(uint32_t color); |
grantphillips | 2:51f454b7c9ab | 118 | |
grantphillips | 2:51f454b7c9ab | 119 | /** Sets the background color for the keypad keys when it is pressed. |
grantphillips | 2:51f454b7c9ab | 120 | * @param |
grantphillips | 2:51f454b7c9ab | 121 | * color The color constant or code ARGB(8-8-8-8). |
grantphillips | 2:51f454b7c9ab | 122 | */ |
grantphillips | 2:51f454b7c9ab | 123 | void SetKeyPressColor(uint32_t color); |
grantphillips | 2:51f454b7c9ab | 124 | |
grantphillips | 2:51f454b7c9ab | 125 | /** Sets the foreground color for the Message Box area. |
grantphillips | 2:51f454b7c9ab | 126 | * @param |
grantphillips | 2:51f454b7c9ab | 127 | * color The color constant or code ARGB(8-8-8-8). |
grantphillips | 2:51f454b7c9ab | 128 | */ |
grantphillips | 2:51f454b7c9ab | 129 | void SetMsgBoxForeColor(uint32_t color); |
grantphillips | 2:51f454b7c9ab | 130 | |
grantphillips | 2:51f454b7c9ab | 131 | /** Sets the background color for the Message Box area. |
grantphillips | 2:51f454b7c9ab | 132 | * @param |
grantphillips | 2:51f454b7c9ab | 133 | * color The color constant or code ARGB(8-8-8-8). |
grantphillips | 2:51f454b7c9ab | 134 | */ |
grantphillips | 2:51f454b7c9ab | 135 | void SetMsgBoxBackColor(uint32_t color); |
grantphillips | 2:51f454b7c9ab | 136 | |
grantphillips | 2:51f454b7c9ab | 137 | /** Sets the foreground color for the Output Box area. |
grantphillips | 2:51f454b7c9ab | 138 | * @param |
grantphillips | 2:51f454b7c9ab | 139 | * color The color constant or code ARGB(8-8-8-8). |
grantphillips | 2:51f454b7c9ab | 140 | */ |
grantphillips | 2:51f454b7c9ab | 141 | void SetOutBoxForeColor(uint32_t color); |
grantphillips | 2:51f454b7c9ab | 142 | |
grantphillips | 2:51f454b7c9ab | 143 | /** Sets the background color for the Output Box area. |
grantphillips | 2:51f454b7c9ab | 144 | * @param |
grantphillips | 2:51f454b7c9ab | 145 | * color The color constant or code ARGB(8-8-8-8). |
grantphillips | 2:51f454b7c9ab | 146 | */ |
grantphillips | 2:51f454b7c9ab | 147 | void SetOutBoxBackColor(uint32_t color); |
grantphillips | 2:51f454b7c9ab | 148 | |
grantphillips | 2:51f454b7c9ab | 149 | /** Sets the background color for the whole keypad area. |
grantphillips | 2:51f454b7c9ab | 150 | * @param |
grantphillips | 2:51f454b7c9ab | 151 | * color The color constant or code ARGB(8-8-8-8). |
grantphillips | 2:51f454b7c9ab | 152 | */ |
grantphillips | 2:51f454b7c9ab | 153 | void SetBackColor(uint32_t color); |
grantphillips | 0:8cc22acc00d2 | 154 | |
grantphillips | 0:8cc22acc00d2 | 155 | private: |
grantphillips | 0:8cc22acc00d2 | 156 | LCD_DISCO_F429ZI lcd; |
grantphillips | 0:8cc22acc00d2 | 157 | TS_DISCO_F429ZI ts; |
grantphillips | 0:8cc22acc00d2 | 158 | uint16_t LastkeyX, LastkeyY; |
grantphillips | 0:8cc22acc00d2 | 159 | uint8_t LastKey; |
grantphillips | 0:8cc22acc00d2 | 160 | uint8_t ReadKeyState; |
grantphillips | 0:8cc22acc00d2 | 161 | uint16_t KeysTopY; |
grantphillips | 0:8cc22acc00d2 | 162 | uint8_t row, col; |
grantphillips | 3:5f2fa06a0492 | 163 | char Keys[4][4]; |
grantphillips | 0:8cc22acc00d2 | 164 | uint32_t MsgBColor, MsgFColor; |
grantphillips | 0:8cc22acc00d2 | 165 | uint32_t OutBColor, OutFColor; |
grantphillips | 0:8cc22acc00d2 | 166 | uint32_t KeyBColor, KeyFColor, KeyPressColor; |
grantphillips | 0:8cc22acc00d2 | 167 | uint32_t BackColor; |
grantphillips | 0:8cc22acc00d2 | 168 | int MessageBoxEnabledFlag, OutBoxEnabledFlag; |
grantphillips | 0:8cc22acc00d2 | 169 | }; |
grantphillips | 0:8cc22acc00d2 | 170 | |
grantphillips | 0:8cc22acc00d2 | 171 | #endif |