![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TFT_Touch_Menu_Encoder
Dependencies: mbed TouchScreen_kbv_mbed MCUFRIEND_kbv_R299 GLUE_STUFF_kbv EncoderAli ADA_GFX_kbv_R1107
TFT_MENU.cpp@5:64202050310c, 2022-04-18 (annotated)
- Committer:
- henriquer
- Date:
- Mon Apr 18 18:07:23 2022 +0000
- Revision:
- 5:64202050310c
- Child:
- 6:1be71a3e54fe
TFT_ Menu_ KY-040
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
henriquer | 5:64202050310c | 1 | |
henriquer | 5:64202050310c | 2 | |
henriquer | 5:64202050310c | 3 | //******************************Henrique**************************************// |
henriquer | 5:64202050310c | 4 | |
henriquer | 5:64202050310c | 5 | //********************Programa Exemplo Menu com botões***********************// |
henriquer | 5:64202050310c | 6 | |
henriquer | 5:64202050310c | 7 | |
henriquer | 5:64202050310c | 8 | //***********************Display TFT-ILI9341 Toutch***************************// |
henriquer | 5:64202050310c | 9 | |
henriquer | 5:64202050310c | 10 | |
henriquer | 5:64202050310c | 11 | //*****************************Biblioteca*************************************// |
henriquer | 5:64202050310c | 12 | |
henriquer | 5:64202050310c | 13 | |
henriquer | 5:64202050310c | 14 | #include "mbed.h" |
henriquer | 5:64202050310c | 15 | #include "Arduino.h" |
henriquer | 5:64202050310c | 16 | #include <MCUFRIEND_kbv.h> |
henriquer | 5:64202050310c | 17 | MCUFRIEND_kbv tft; |
henriquer | 5:64202050310c | 18 | #include "TouchScreen_kbv_mbed.h" |
henriquer | 5:64202050310c | 19 | #include "Encoder.h" |
henriquer | 5:64202050310c | 20 | |
henriquer | 5:64202050310c | 21 | //************************Configuração do Display*****************************// |
henriquer | 5:64202050310c | 22 | |
henriquer | 5:64202050310c | 23 | //****************************************************************************// |
henriquer | 5:64202050310c | 24 | |
henriquer | 5:64202050310c | 25 | const int TS_LEFT=121,TS_RT=922,TS_TOP=82,TS_BOT=890; |
henriquer | 5:64202050310c | 26 | const PinName XP = D8, YP = A3, XM = A2, YM = D9; //next common configuration |
henriquer | 5:64202050310c | 27 | DigitalInOut YPout(YP); |
henriquer | 5:64202050310c | 28 | DigitalInOut XMout(XM); |
henriquer | 5:64202050310c | 29 | |
henriquer | 5:64202050310c | 30 | |
henriquer | 5:64202050310c | 31 | long map(long x, long in_min, long in_max, long out_min, long out_max) |
henriquer | 5:64202050310c | 32 | { |
henriquer | 5:64202050310c | 33 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
henriquer | 5:64202050310c | 34 | } |
henriquer | 5:64202050310c | 35 | |
henriquer | 5:64202050310c | 36 | TouchScreen_kbv ts = TouchScreen_kbv(XP, YP, XM, YM, 300); |
henriquer | 5:64202050310c | 37 | TSPoint_kbv tp; |
henriquer | 5:64202050310c | 38 | |
henriquer | 5:64202050310c | 39 | // Valores para detectar a pressão do toque |
henriquer | 5:64202050310c | 40 | |
henriquer | 5:64202050310c | 41 | #define MINPRESSURE 10 |
henriquer | 5:64202050310c | 42 | #define MAXPRESSURE 1000 |
henriquer | 5:64202050310c | 43 | |
henriquer | 5:64202050310c | 44 | //****************************************************************************// |
henriquer | 5:64202050310c | 45 | |
henriquer | 5:64202050310c | 46 | //****************************************************************************// |
henriquer | 5:64202050310c | 47 | |
henriquer | 5:64202050310c | 48 | //***********************Orientação Display***********************************// |
henriquer | 5:64202050310c | 49 | |
henriquer | 5:64202050310c | 50 | uint8_t Orientation = 0; |
henriquer | 5:64202050310c | 51 | |
henriquer | 5:64202050310c | 52 | //****************************************************************************// |
henriquer | 5:64202050310c | 53 | |
henriquer | 5:64202050310c | 54 | //****************************************************************************// |
henriquer | 5:64202050310c | 55 | |
henriquer | 5:64202050310c | 56 | |
henriquer | 5:64202050310c | 57 | bool botao_1 = 0; |
henriquer | 5:64202050310c | 58 | bool botao_2 = 0; |
henriquer | 5:64202050310c | 59 | bool botao_3 = 0; |
henriquer | 5:64202050310c | 60 | int pulse; |
henriquer | 5:64202050310c | 61 | bool sw; |
henriquer | 5:64202050310c | 62 | |
henriquer | 5:64202050310c | 63 | //***********************Tabela de Cores**************************************// |
henriquer | 5:64202050310c | 64 | |
henriquer | 5:64202050310c | 65 | #define BLACK 0x0000 |
henriquer | 5:64202050310c | 66 | #define BLUE 0x001F |
henriquer | 5:64202050310c | 67 | #define RED 0xF800 |
henriquer | 5:64202050310c | 68 | #define GREEN 0x07E0 |
henriquer | 5:64202050310c | 69 | #define CYAN 0x07FF |
henriquer | 5:64202050310c | 70 | #define MAGENTA 0xF81F |
henriquer | 5:64202050310c | 71 | #define YELLOW 0xFFE0 |
henriquer | 5:64202050310c | 72 | #define WHITE 0xFFFF |
henriquer | 5:64202050310c | 73 | |
henriquer | 5:64202050310c | 74 | //****************************************************************************// |
henriquer | 5:64202050310c | 75 | |
henriquer | 5:64202050310c | 76 | //****************************************************************************// |
henriquer | 5:64202050310c | 77 | |
henriquer | 5:64202050310c | 78 | |
henriquer | 5:64202050310c | 79 | void draw() |
henriquer | 5:64202050310c | 80 | |
henriquer | 5:64202050310c | 81 | { |
henriquer | 5:64202050310c | 82 | |
henriquer | 5:64202050310c | 83 | tft.drawRoundRect(5, 15, 154, 50, 5, WHITE); |
henriquer | 5:64202050310c | 84 | tft.setTextColor(GREEN); |
henriquer | 5:64202050310c | 85 | tft.setTextSize(3); |
henriquer | 5:64202050310c | 86 | tft.setCursor(15, 30); |
henriquer | 5:64202050310c | 87 | tft.println("Button 1"); |
henriquer | 5:64202050310c | 88 | |
henriquer | 5:64202050310c | 89 | tft.drawRoundRect(5, 70, 154, 50, 5, WHITE); |
henriquer | 5:64202050310c | 90 | tft.setTextColor(GREEN); |
henriquer | 5:64202050310c | 91 | tft.setTextSize(3); |
henriquer | 5:64202050310c | 92 | tft.setCursor(15,85); |
henriquer | 5:64202050310c | 93 | tft.println("Button 2"); |
henriquer | 5:64202050310c | 94 | |
henriquer | 5:64202050310c | 95 | tft.drawRoundRect(5, 125, 154, 50, 5, WHITE); |
henriquer | 5:64202050310c | 96 | tft.setTextColor(GREEN); |
henriquer | 5:64202050310c | 97 | tft.setTextSize(3); |
henriquer | 5:64202050310c | 98 | tft.setCursor(15,140); |
henriquer | 5:64202050310c | 99 | tft.println("Button 3"); |
henriquer | 5:64202050310c | 100 | |
henriquer | 5:64202050310c | 101 | } |
henriquer | 5:64202050310c | 102 | |
henriquer | 5:64202050310c | 103 | |
henriquer | 5:64202050310c | 104 | void show_tft(void) |
henriquer | 5:64202050310c | 105 | { |
henriquer | 5:64202050310c | 106 | |
henriquer | 5:64202050310c | 107 | EncoderAli Enc(PB_13,PB_14,PB_15); //DT, CLK, SW |
henriquer | 5:64202050310c | 108 | Enc.setRange(1,16); |
henriquer | 5:64202050310c | 109 | |
henriquer | 5:64202050310c | 110 | |
henriquer | 5:64202050310c | 111 | tft.setTextSize(2); |
henriquer | 5:64202050310c | 112 | |
henriquer | 5:64202050310c | 113 | |
henriquer | 5:64202050310c | 114 | tft.setTextColor(MAGENTA,BLUE); |
henriquer | 5:64202050310c | 115 | |
henriquer | 5:64202050310c | 116 | while (1) { |
henriquer | 5:64202050310c | 117 | |
henriquer | 5:64202050310c | 118 | pulse = Enc.getState(); |
henriquer | 5:64202050310c | 119 | sw = Enc.getButtonState(); |
henriquer | 5:64202050310c | 120 | printf("\n\r PULSOS: %d; ",Enc.getState()); |
henriquer | 5:64202050310c | 121 | tp = ts.getPoint(); |
henriquer | 5:64202050310c | 122 | YPout.output(); |
henriquer | 5:64202050310c | 123 | XMout.output(); |
henriquer | 5:64202050310c | 124 | |
henriquer | 5:64202050310c | 125 | if (tp.z < MINPRESSURE && tp.z > MAXPRESSURE) |
henriquer | 5:64202050310c | 126 | |
henriquer | 5:64202050310c | 127 | tp.x = tft.width() - (map(tp.x, TS_RT, TS_LEFT, tft.width(), 0)); |
henriquer | 5:64202050310c | 128 | tp.y = tft.height() - (map(tp.y, TS_BOT, TS_TOP, tft.height(), 0)); |
henriquer | 5:64202050310c | 129 | |
henriquer | 5:64202050310c | 130 | |
henriquer | 5:64202050310c | 131 | |
henriquer | 5:64202050310c | 132 | if (pulse>=2 && pulse<=5) { |
henriquer | 5:64202050310c | 133 | |
henriquer | 5:64202050310c | 134 | if(sw==true) { |
henriquer | 5:64202050310c | 135 | |
henriquer | 5:64202050310c | 136 | tft.fillRoundRect(5, 15, 154, 50, 5, RED); |
henriquer | 5:64202050310c | 137 | tft.setCursor(15, 30); |
henriquer | 5:64202050310c | 138 | tft.println("Button 1"); |
henriquer | 5:64202050310c | 139 | //tft.fillScreen(BLUE); |
henriquer | 5:64202050310c | 140 | // sw =!sw; |
henriquer | 5:64202050310c | 141 | } |
henriquer | 5:64202050310c | 142 | |
henriquer | 5:64202050310c | 143 | else { |
henriquer | 5:64202050310c | 144 | |
henriquer | 5:64202050310c | 145 | tft.fillRoundRect(5, 15, 154, 50, 5, BLACK); |
henriquer | 5:64202050310c | 146 | tft.drawRoundRect(5, 15, 154, 50, 5, WHITE); |
henriquer | 5:64202050310c | 147 | tft.setTextColor(GREEN); |
henriquer | 5:64202050310c | 148 | tft.setTextSize(3); |
henriquer | 5:64202050310c | 149 | tft.setCursor(15, 30); |
henriquer | 5:64202050310c | 150 | tft.println("Button 1"); |
henriquer | 5:64202050310c | 151 | //tft.fillScreen(BLACK); |
henriquer | 5:64202050310c | 152 | |
henriquer | 5:64202050310c | 153 | //sw =!sw; |
henriquer | 5:64202050310c | 154 | |
henriquer | 5:64202050310c | 155 | } |
henriquer | 5:64202050310c | 156 | } |
henriquer | 5:64202050310c | 157 | |
henriquer | 5:64202050310c | 158 | |
henriquer | 5:64202050310c | 159 | if (pulse>=6 && pulse<=10) { |
henriquer | 5:64202050310c | 160 | |
henriquer | 5:64202050310c | 161 | if(sw==true) { |
henriquer | 5:64202050310c | 162 | |
henriquer | 5:64202050310c | 163 | tft.fillRoundRect(5, 70, 154, 50, 5, BLUE); |
henriquer | 5:64202050310c | 164 | tft.setTextColor(GREEN); |
henriquer | 5:64202050310c | 165 | tft.setTextSize(3); |
henriquer | 5:64202050310c | 166 | tft.setCursor(15, 85); |
henriquer | 5:64202050310c | 167 | tft.println("Button 2"); |
henriquer | 5:64202050310c | 168 | botao_2 =!botao_2; |
henriquer | 5:64202050310c | 169 | } |
henriquer | 5:64202050310c | 170 | |
henriquer | 5:64202050310c | 171 | else { |
henriquer | 5:64202050310c | 172 | |
henriquer | 5:64202050310c | 173 | |
henriquer | 5:64202050310c | 174 | tft.fillRoundRect(5, 70, 154, 50, 5, BLACK); |
henriquer | 5:64202050310c | 175 | tft.drawRoundRect(5, 70, 154, 50, 5, WHITE); |
henriquer | 5:64202050310c | 176 | tft.setTextColor(GREEN); |
henriquer | 5:64202050310c | 177 | tft.setTextSize(3); |
henriquer | 5:64202050310c | 178 | tft.setCursor(15,85); |
henriquer | 5:64202050310c | 179 | tft.println("Button 2"); |
henriquer | 5:64202050310c | 180 | botao_2 =!botao_2; |
henriquer | 5:64202050310c | 181 | } |
henriquer | 5:64202050310c | 182 | } |
henriquer | 5:64202050310c | 183 | |
henriquer | 5:64202050310c | 184 | if (pulse>11 && pulse<=14) { |
henriquer | 5:64202050310c | 185 | |
henriquer | 5:64202050310c | 186 | if(sw==true) { |
henriquer | 5:64202050310c | 187 | |
henriquer | 5:64202050310c | 188 | tft.fillRoundRect(5, 125, 154, 50, 5, MAGENTA); |
henriquer | 5:64202050310c | 189 | tft.setTextColor(GREEN); |
henriquer | 5:64202050310c | 190 | tft.setTextSize(3); |
henriquer | 5:64202050310c | 191 | tft.setCursor(15, 140); |
henriquer | 5:64202050310c | 192 | tft.println("Button 3"); |
henriquer | 5:64202050310c | 193 | botao_3 =!botao_3; |
henriquer | 5:64202050310c | 194 | } |
henriquer | 5:64202050310c | 195 | |
henriquer | 5:64202050310c | 196 | else { |
henriquer | 5:64202050310c | 197 | |
henriquer | 5:64202050310c | 198 | tft.fillRoundRect(5, 125, 154, 50, 5, BLACK); |
henriquer | 5:64202050310c | 199 | tft.drawRoundRect(5, 125, 154, 50, 5, WHITE); |
henriquer | 5:64202050310c | 200 | tft.setTextColor(GREEN); |
henriquer | 5:64202050310c | 201 | tft.setTextSize(3); |
henriquer | 5:64202050310c | 202 | tft.setCursor(15,140); |
henriquer | 5:64202050310c | 203 | tft.println("Button 3"); |
henriquer | 5:64202050310c | 204 | botao_3 =!botao_3; |
henriquer | 5:64202050310c | 205 | } |
henriquer | 5:64202050310c | 206 | } |
henriquer | 5:64202050310c | 207 | |
henriquer | 5:64202050310c | 208 | } |
henriquer | 5:64202050310c | 209 | } |
henriquer | 5:64202050310c | 210 | |
henriquer | 5:64202050310c | 211 | |
henriquer | 5:64202050310c | 212 | |
henriquer | 5:64202050310c | 213 | |
henriquer | 5:64202050310c | 214 | |
henriquer | 5:64202050310c | 215 | void setup(void) |
henriquer | 5:64202050310c | 216 | { |
henriquer | 5:64202050310c | 217 | |
henriquer | 5:64202050310c | 218 | |
henriquer | 5:64202050310c | 219 | |
henriquer | 5:64202050310c | 220 | tft.reset(); |
henriquer | 5:64202050310c | 221 | tft.begin(); |
henriquer | 5:64202050310c | 222 | tft.setRotation(Orientation); |
henriquer | 5:64202050310c | 223 | tft.fillScreen(BLACK); |
henriquer | 5:64202050310c | 224 | draw(); |
henriquer | 5:64202050310c | 225 | show_tft(); |
henriquer | 5:64202050310c | 226 | |
henriquer | 5:64202050310c | 227 | |
henriquer | 5:64202050310c | 228 | delay(1000); |
henriquer | 5:64202050310c | 229 | } |
henriquer | 5:64202050310c | 230 | |
henriquer | 5:64202050310c | 231 | void loop() |
henriquer | 5:64202050310c | 232 | { |
henriquer | 5:64202050310c | 233 | |
henriquer | 5:64202050310c | 234 | |
henriquer | 5:64202050310c | 235 | } |
henriquer | 5:64202050310c | 236 | |
henriquer | 5:64202050310c | 237 | //****************************************************************************// |
henriquer | 5:64202050310c | 238 | |
henriquer | 5:64202050310c | 239 | //****************************************************************************// |