Components and supplies
Arduino Uno
TFT ILI9486 SHEILD
Apps and platforms
Arduino IDE
Project description
Code
CODE
c_cpp
1#include <Adafruit_GFX.h> // Core graphics library 2#include <Adafruit_TFTLCD.h> // Hardware-specific library 3#include <TouchScreen.h> 4 5// The control pins for the LCD can be assigned to any digital or 6// analog pins...but we'll use the analog pins as this allows us to 7// double up the pins with the touch screen (see the TFT paint example). 8#define LCD_CS A3 // Chip Select goes to Analog 3 9#define LCD_CD A2 // Command/Data goes to Analog 2 10#define LCD_WR A1 // LCD Write goes to Analog 1 11#define LCD_RD A0 // LCD Read goes to Analog 0 12 13#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin 14 15// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD: 16// For the Arduino Uno, Duemilanove, Diecimila, etc.: 17// D0 connects to digital pin 8 (Notice these are 18// D1 connects to digital pin 9 NOT in order!) 19// D2 connects to digital pin 2 20// D3 connects to digital pin 3 21// D4 connects to digital pin 4 22// D5 connects to digital pin 5 23// D6 connects to digital pin 6 24// D7 connects to digital pin 7 25// For the Arduino Mega, use digital pins 22 through 29 26// (on the 2-row header at the end of the board). 27 28// Assign human-readable names to some common 16-bit color values: 29#define BLACK 0x0000 30#define BLUE 0x001F 31#define RED 0xF800 32#define GREEN 0x07E0 33#define CYAN 0x07FF 34#define MAGENTA 0xF81F 35#define YELLOW 0xFFE0 36#define WHITE 0xFFFF 37 38// Color definitions 39#define ILI9341_BLACK 0x0000 /* 0, 0, 0 */ 40#define ILI9341_NAVY 0x000F /* 0, 0, 128 */ 41#define ILI9341_DARKGREEN 0x03E0 /* 0, 128, 0 */ 42#define ILI9341_DARKCYAN 0x03EF /* 0, 128, 128 */ 43#define ILI9341_MAROON 0x7800 /* 128, 0, 0 */ 44#define ILI9341_PURPLE 0x780F /* 128, 0, 128 */ 45#define ILI9341_OLIVE 0x7BE0 /* 128, 128, 0 */ 46#define ILI9341_LIGHTGREY 0xC618 /* 192, 192, 192 */ 47#define ILI9341_DARKGREY 0x7BEF /* 128, 128, 128 */ 48#define ILI9341_BLUE 0x001F /* 0, 0, 255 */ 49#define ILI9341_GREEN 0x07E0 /* 0, 255, 0 */ 50#define ILI9341_CYAN 0x07FF /* 0, 255, 255 */ 51#define ILI9341_RED 0xF800 /* 255, 0, 0 */ 52#define ILI9341_MAGENTA 0xF81F /* 255, 0, 255 */ 53#define ILI9341_YELLOW 0xFFE0 /* 255, 255, 0 */ 54#define ILI9341_WHITE 0xFFFF /* 255, 255, 255 */ 55#define ILI9341_ORANGE 0xFD20 /* 255, 165, 0 */ 56#define ILI9341_GREENYELLOW 0xAFE5 /* 173, 255, 47 */ 57#define ILI9341_PINK 0xF81F 58 59/******************* UI details */ 60#define BUTTON_X 52 61#define BUTTON_Y 150 62#define BUTTON_W 80 63#define BUTTON_H 45 64#define BUTTON_SPACING_X 26 65#define BUTTON_SPACING_Y 30 66#define BUTTON_TEXTSIZE 3 67 68// text box where numbers go 69#define TEXT_X 10 70#define TEXT_Y 10 71#define TEXT_W 300 72#define TEXT_H 50 73#define TEXT_TSIZE 3 74#define TEXT_TCOLOR ILI9341_MAGENTA 75// the data (phone #) we store in the textfield 76#define TEXT_LEN 16 77char textfield[TEXT_LEN+1] = ""; 78uint8_t textfield_i=0; 79 80#define YP A2 // must be an analog pin, use "An" notation! 81#define XM A3 // must be an analog pin, use "An" notation! 82#define YM 8 // can be a digital pin 83#define XP 9 // can be a digital pin 84 85#define TS_MINX 130 86#define TS_MAXX 905 87 88#define TS_MINY 75 89#define TS_MAXY 930 90// We have a status line for like, is FONA working 91#define STATUS_X 10 92#define STATUS_Y 65 93 94 95 96 97Adafruit_GFX_Button buttons[15]; 98/* create 15 buttons, in classic candybar phone style */ 99char buttonlabels[15][5] = {"Send", "Clr", "End", "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#" }; 100uint16_t buttoncolors[15] = {ILI9341_DARKGREEN, ILI9341_DARKGREY, ILI9341_RED, 101 ILI9341_BLUE, ILI9341_BLUE, ILI9341_BLUE, 102 ILI9341_BLUE, ILI9341_BLUE, ILI9341_BLUE, 103 ILI9341_BLUE, ILI9341_BLUE, ILI9341_BLUE, 104 ILI9341_ORANGE, ILI9341_BLUE, ILI9341_ORANGE}; 105 106#include <MCUFRIEND_kbv.h> 107MCUFRIEND_kbv tft; 108TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); 109// If using the shield, all control and data lines are fixed, and 110// a simpler declaration can optionally be used: 111// Adafruit_TFTLCD tft; 112 113void setup(void) { 114 Serial.begin(9600); 115 Serial.println(F("TFT LCD test")); 116 117 tft.reset(); 118 119 uint16_t identifier = tft.readID(); 120 if(identifier == 0x9325) { 121 Serial.println(F("Found ILI9325 LCD driver")); 122 } else if(identifier == 0x9328) { 123 Serial.println(F("Found ILI9328 LCD driver")); 124 } else if(identifier == 0x4535) { 125 Serial.println(F("Found LGDP4535 LCD driver")); 126 }else if(identifier == 0x7575) { 127 Serial.println(F("Found HX8347G LCD driver")); 128 } else if(identifier == 0x9341) { 129 Serial.println(F("Found ILI9341 LCD driver")); 130 }else if(identifier == 0x7783) { 131 Serial.println(F("Found ST7781 LCD driver")); 132 }else if(identifier == 0x8230) { 133 Serial.println(F("Found UC8230 LCD driver")); 134 } 135 else if(identifier == 0x8357) { 136 Serial.println(F("Found HX8357D LCD driver")); 137 } else if(identifier==0x0101) 138 { 139 identifier=0x9341; 140 Serial.println(F("Found 0x9341 LCD driver")); 141 }else if(identifier==0x9481) 142 { 143 Serial.println(F("Found 0x9481 LCD driver")); 144 } 145 else if(identifier==0x9486) 146 { 147 Serial.println(F("Found 0x9486 LCD driver")); 148 } 149 else { 150 Serial.print(F("Unknown LCD driver chip: ")); 151 Serial.println(identifier, HEX); 152 Serial.println(F("If using the Adafruit 2.8\\" TFT Arduino shield, the line:")); 153 Serial.println(F(" #define USE_ADAFRUIT_SHIELD_PINOUT")); 154 Serial.println(F("should appear in the library header (Adafruit_TFT.h).")); 155 Serial.println(F("If using the breakout board, it should NOT be #defined!")); 156 Serial.println(F("Also if using the breakout, double-check that all wiring")); 157 Serial.println(F("matches the tutorial.")); 158 identifier=0x9486; 159 160 } 161 162 tft.begin(identifier); 163 Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height()); 164 tft.setRotation(0); 165 tft.fillScreen(BLACK); 166 167 // create buttons 168 for (uint8_t row=0; row<5; row++) { 169 for (uint8_t col=0; col<3; col++) { 170 buttons[col + row*3].initButton(&tft, BUTTON_X+col*(BUTTON_W+BUTTON_SPACING_X), 171 BUTTON_Y+row*(BUTTON_H+BUTTON_SPACING_Y), // x, y, w, h, outline, fill, text 172 BUTTON_W, BUTTON_H, ILI9341_WHITE, buttoncolors[col+row*3], ILI9341_WHITE, 173 buttonlabels[col + row*3], BUTTON_TEXTSIZE); 174 buttons[col + row*3].drawButton(); 175 } 176 } 177 178 // create 'text field' 179 tft.drawRect(TEXT_X, TEXT_Y, TEXT_W, TEXT_H, ILI9341_WHITE); 180 181 182} 183// Print something in the mini status bar with either flashstring 184void status(const __FlashStringHelper *msg) { 185 tft.fillRect(STATUS_X, STATUS_Y, 240, 8, ILI9341_BLACK); 186 tft.setCursor(STATUS_X, STATUS_Y); 187 tft.setTextColor(ILI9341_WHITE); 188 tft.setTextSize(1); 189 tft.print(msg); 190} 191// or charstring 192void status(char *msg) { 193 tft.fillRect(STATUS_X, STATUS_Y, 240, 8, ILI9341_BLACK); 194 tft.setCursor(STATUS_X, STATUS_Y); 195 tft.setTextColor(ILI9341_WHITE); 196 tft.setTextSize(1); 197 tft.print(msg); 198} 199#define MINPRESSURE 10 200#define MAXPRESSURE 1000 201void loop(void) { 202 /*TSPoint p; 203 p = ts.getPoint(); 204 */ 205 digitalWrite(13, HIGH); 206 TSPoint p = ts.getPoint(); 207 digitalWrite(13, LOW); 208 209 // if sharing pins, you'll need to fix the directions of the touchscreen pins 210 //pinMode(XP, OUTPUT); 211 pinMode(XM, OUTPUT); 212 pinMode(YP, OUTPUT); 213 //pinMode(YM, OUTPUT); 214 215 // we have some minimum pressure we consider 'valid' 216 // pressure of 0 means no pressing! 217 218 // p = ts.getPoint(); 219 /* 220 if (ts.bufferSize()) { 221 222 } else { 223 // this is our way of tracking touch 'release'! 224 p.x = p.y = p.z = -1; 225 }*/ 226 227 // Scale from ~0->4000 to tft.width using the calibration #'s 228 /* 229 if (p.z != -1) { 230 p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width()); 231 p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height()); 232 Serial.print("("); Serial.print(p.x); Serial.print(", "); 233 Serial.print(p.y); Serial.print(", "); 234 Serial.print(p.z); Serial.println(") "); 235 }*/ 236 if (p.z > MINPRESSURE && p.z < MAXPRESSURE) 237 { 238 239 p.x = p.x + p.y; 240 p.y = p.x - p.y; 241 p.x = p.x - p.y; 242 p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0); 243 p.y = tft.height()-(map(p.y, TS_MINY, TS_MAXY, tft.height(), 0)); 244 245 246 } 247 248 249 250 // go thru all the buttons, checking if they were pressed 251 for (uint8_t b=0; b<15; b++) { 252 if ((buttons[b].contains(p.x, p.y))&&p.x>10) 253 { 254 Serial.print("Pressing: "); Serial.println(b); 255 buttons[b].press(true); // tell the button it is pressed 256 } else { 257 buttons[b].press(false); // tell the button it is NOT pressed 258 } 259 } 260 261 // now we can ask the buttons if their state has changed 262 for (uint8_t b=0; b<15; b++) { 263 if (buttons[b].justReleased()) { 264 Serial.print("Released: "); Serial.println(b); 265 buttons[b].drawButton(); // draw normal 266 } 267 268 if (buttons[b].justPressed()) { 269 buttons[b].drawButton(true); // draw invert! 270 271 // if a numberpad button, append the relevant # to the textfield 272 if (b >= 3) { 273 if (textfield_i < TEXT_LEN) { 274 textfield[textfield_i] = buttonlabels[b][0]; 275 textfield_i++; 276 textfield[textfield_i] = 0; // zero terminate 277 278 // fona.playDTMF(buttonlabels[b][0]); 279 } 280 } 281 282 // clr button! delete char 283 if (b == 1) { 284 285 textfield[textfield_i] = 0; 286 if (textfield > 0) { 287 textfield_i--; 288 textfield[textfield_i] = ' '; 289 } 290 } 291 292 // update the current text field 293 Serial.println(textfield); 294 tft.setCursor(TEXT_X + 2, TEXT_Y+10); 295 tft.setTextColor(TEXT_TCOLOR, ILI9341_BLACK); 296 tft.setTextSize(TEXT_TSIZE); 297 tft.print(textfield); 298 299 // its always OK to just hang up 300 if (b == 2) { 301 status(F("Hanging up")); 302 //fona.hangUp(); 303 } 304 // we dont really check that the text field makes sense 305 // just try to call 306 if (b == 0) { 307 status(F("Calling")); 308 Serial.print("Calling "); Serial.print(textfield); 309 310 //fona.callPhone(textfield); 311 } 312 313 delay(100); // UI debouncing 314 } 315 } 316 317}
Comments
Only logged in users can leave comments
2 Team members on this project
0