Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.95 KB | None | 0 0
  1. // the regular Adafruit "TouchScreen.h" library only works on AVRs
  2.  
  3. // different mcufriend shields have Touchscreen on different pins
  4. // and rotation.
  5. // Run the UTouch_calibr_kbv sketch for calibration of your shield
  6.  
  7. #include <Adafruit_GFX.h> // Core graphics library
  8. //#include <Adafruit_TFTLCD.h> // Hardware-specific library
  9. //Adafruit_TFTLCD tft(A3, A2, A1, A0, A4);
  10. #include <MCUFRIEND_kbv.h>
  11. MCUFRIEND_kbv tft; // hard-wired for UNO shields anyway.
  12. #include <TouchScreen.h>
  13.  
  14. #if defined(__SAM3X8E__)
  15. #undef __FlashStringHelper::F(string_literal)
  16. #define F(string_literal) string_literal
  17. #endif
  18.  
  19. // These are the pins for some typical shields!
  20. // S6D0154: YP=A1, XM=A2, YM=7, XP=6
  21. // ST7783 : YP=A2, XM=A1, YM=6, XP=7
  22. // ILI9320: YP=A2, XM=A3, YM=8, XP=9
  23. // ILI9325: YP=A1, XM=A2, YM=7, XP=6
  24. // ILI9325BG: YP=A2, XM=A1, YM=6, XP=7
  25. // ILI9341: YP=A2, XM=A1, YM=7, XP=6
  26. // ILI9488: YP=A1, XM=A2, YM=7, XP=6
  27. // R65109V: YP=A2, XM=A1, YM=6, XP=7
  28.  
  29. // most mcufriend shields use these pins and Portrait mode:
  30. uint8_t YP = A1; // must be an analog pin, use "An" notation!
  31. uint8_t XM = A2; // must be an analog pin, use "An" notation!
  32. uint8_t YM = 7; // can be a digital pin
  33. uint8_t XP = 6; // can be a digital pin
  34. uint8_t SwapXY = 0;
  35.  
  36. uint16_t TS_LEFT = 920;
  37. uint16_t TS_RT = 150;
  38. uint16_t TS_TOP = 940;
  39. uint16_t TS_BOT = 120;
  40. char *name = "Unknown controller";
  41.  
  42. // For better pressure precision, we need to know the resistance
  43. // between X+ and X- Use any multimeter to read it
  44. // For the one we're using, its 300 ohms across the X plate
  45. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  46. TSPoint tp;
  47.  
  48. #define MINPRESSURE 20
  49. #define MAXPRESSURE 1000
  50.  
  51. #define SWAP(a, b) {uint16_t tmp = a; a = b; b = tmp;}
  52.  
  53. int16_t BOXSIZE;
  54. int16_t PENRADIUS = 3;
  55. uint16_t identifier, oldcolor, currentcolor;
  56. uint8_t Orientation = 0; //PORTRAIT
  57.  
  58. // Assign human-readable names to some common 16-bit color values:
  59. #define BLACK 0x0000
  60. #define BLUE 0x001F
  61. #define RED 0xF800
  62. #define GREEN 0x07E0
  63. #define CYAN 0x07FF
  64. #define MAGENTA 0xF81F
  65. #define YELLOW 0xFFE0
  66. #define WHITE 0xFFFF
  67.  
  68. void show_Serial(void)
  69. {
  70. Serial.print(F("Found "));
  71. Serial.print(name);
  72. Serial.println(F(" LCD driver"));
  73. Serial.print(F("ID=0x"));
  74. Serial.println(identifier, HEX);
  75. Serial.println("Screen is " + String(tft.width()) + "x" + String(tft.height()));
  76. Serial.println("Calibration is: ");
  77. Serial.println("LEFT = " + String(TS_LEFT) + " RT = " + String(TS_RT));
  78. Serial.println("TOP = " + String(TS_TOP) + " BOT = " + String(TS_BOT));
  79. Serial.print("Wiring is: ");
  80. Serial.println(SwapXY ? "SWAPXY" : "PORTRAIT");
  81. Serial.println("YP=" + String(YP) + " XM=" + String(XM));
  82. Serial.println("YM=" + String(YM) + " XP=" + String(XP));
  83. }
  84.  
  85. void show_tft(void)
  86. {
  87. tft.setCursor(0, 0);
  88. tft.setTextSize(2);
  89. tft.print(F("Found "));
  90. tft.print(name);
  91. tft.println(F(" LCD"));
  92. tft.setTextSize(1);
  93. tft.print(F("ID=0x"));
  94. tft.println(identifier, HEX);
  95. tft.println("Screen is " + String(tft.width()) + "x" + String(tft.height()));
  96. tft.println("Calibration is: ");
  97. tft.println("LEFT = " + String(TS_LEFT) + " RT = " + String(TS_RT));
  98. tft.println("TOP = " + String(TS_TOP) + " BOT = " + String(TS_BOT));
  99. tft.print("\nWiring is: ");
  100. if (SwapXY) {
  101. tft.setTextColor(CYAN);
  102. tft.setTextSize(2);
  103. }
  104. tft.println(SwapXY ? "SWAPXY" : "PORTRAIT");
  105. tft.println("YP=" + String(YP) + " XM=" + String(XM));
  106. tft.println("YM=" + String(YM) + " XP=" + String(XP));
  107. tft.setTextSize(2);
  108. tft.setTextColor(RED);
  109. tft.setCursor((tft.width() - 48) / 2, (tft.height() * 2) / 4);
  110. tft.print("EXIT");
  111. tft.setTextColor(YELLOW, BLACK);
  112. tft.setCursor(0, (tft.height() * 6) / 8);
  113. tft.print("Touch screen for loc");
  114. while (1) {
  115. tp = ts.getPoint();
  116. pinMode(XM, OUTPUT);
  117. pinMode(YP, OUTPUT);
  118. pinMode(XP, OUTPUT);
  119. pinMode(YM, OUTPUT);
  120. if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) continue;
  121. if (tp.x > 450 && tp.x < 570 && tp.y > 450 && tp.y < 570) break;
  122. tft.setCursor(0, (tft.height() * 3) / 4);
  123. tft.print("tp.x=" + String(tp.x) + " tp.y=" + String(tp.y) + " ");
  124. }
  125. }
  126.  
  127.  
  128. void setup(void)
  129. {
  130. uint16_t tmp;
  131. tft.begin(9600);
  132.  
  133. tft.reset();
  134. identifier = tft.readID();
  135. // if (identifier == 0) identifier = 0x9341;
  136. if (0) {
  137. } else if (identifier == 0x0154) {
  138. name = "S6D0154";
  139. TS_LEFT = 914; TS_RT = 181; TS_TOP = 957; TS_BOT = 208;
  140. } else if (identifier == 0x7783) {
  141. name = "ST7781";
  142. TS_LEFT = 865; TS_RT = 155; TS_TOP = 942; TS_BOT = 153;
  143. SwapXY = 1;
  144. } else if (identifier == 0x7789) {
  145. name = "ST7789V";
  146. YP = A2; XM = A1; YM = 7; XP = 6;
  147. TS_LEFT = 906; TS_RT = 169; TS_TOP = 161; TS_BOT = 919;
  148. } else if (identifier == 0x9320) {
  149. name = "ILI9320";
  150. YP = A3; XM = A2; YM = 9; XP = 8;
  151. TS_LEFT = 902; TS_RT = 137; TS_TOP = 941; TS_BOT = 134;
  152. } else if (identifier == 0x9325) {
  153. name = "ILI9325";
  154. TS_LEFT = 900; TS_RT = 103; TS_TOP = 96; TS_BOT = 904;
  155. } else if (identifier == 0x9325) {
  156. name = "ILI9325 Green Dog";
  157. TS_LEFT = 900; TS_RT = 130; TS_TOP = 940; TS_BOT = 130;
  158. } else if (identifier == 0x9327) {
  159. name = "ILI9327";
  160. TS_LEFT = 899; TS_RT = 135; TS_TOP = 935; TS_BOT = 79;
  161. SwapXY = 1;
  162. } else if (identifier == 0x9329) {
  163. name = "ILI9329";
  164. TS_LEFT = 143; TS_RT = 885; TS_TOP = 941; TS_BOT = 131;
  165. SwapXY = 1;
  166. } else if (identifier == 0x9341) {
  167. name = "ILI9341 BLUE";
  168. TS_LEFT = 920; TS_RT = 139; TS_TOP = 944; TS_BOT = 150;
  169. SwapXY = 0;
  170. } else if (identifier == 0) {
  171. name = "ILI9341 DealExtreme";
  172. TS_LEFT = 893; TS_RT = 145; TS_TOP = 930; TS_BOT = 135;
  173. SwapXY = 1;
  174. } else if (identifier == 0 || identifier == 0x9341) {
  175. name = "ILI9341";
  176. TS_LEFT = 128; TS_RT = 911; TS_TOP = 105; TS_BOT = 908;
  177. SwapXY = 1;
  178. } else if (identifier == 0x9486) {
  179. name = "ILI9486";
  180. TS_LEFT = 904; TS_RT = 170; TS_TOP = 950; TS_BOT = 158;
  181. } else if (identifier == 0x9488) {
  182. name = "ILI9488";
  183. TS_LEFT = 904; TS_RT = 170; TS_TOP = 950; TS_BOT = 158;
  184. } else if (identifier == 0xB509) {
  185. name = "R61509V";
  186. TS_LEFT = 889; TS_RT = 149; TS_TOP = 106; TS_BOT = 975;
  187. SwapXY = 1;
  188. } else {
  189. name = "unknown";
  190. }
  191. switch (Orientation) { // adjust for different aspects
  192. case 0: break; //no change, calibrated for PORTRAIT
  193. case 1: tmp = TS_LEFT, TS_LEFT = TS_BOT, TS_BOT = TS_RT, TS_RT = TS_TOP, TS_TOP = tmp; break;
  194. case 2: SWAP(TS_LEFT, TS_RT); SWAP(TS_TOP, TS_BOT); break;
  195. case 3: tmp = TS_LEFT, TS_LEFT = TS_TOP, TS_TOP = TS_RT, TS_RT = TS_BOT, TS_BOT = tmp; break;
  196. }
  197.  
  198. Serial.begin(9600);
  199. ts = TouchScreen(XP, YP, XM, YM, 300); //call the constructor AGAIN with new values.
  200. tft.begin(identifier);
  201. show_Serial();
  202. tft.setRotation(Orientation);
  203. tft.fillScreen(BLACK);
  204. show_tft();
  205.  
  206. BOXSIZE = tft.width() / 6;
  207. tft.fillScreen(BLACK);
  208.  
  209. tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  210. tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
  211. tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN);
  212. tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN);
  213. tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE);
  214. tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);
  215.  
  216. tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  217. currentcolor = RED;
  218. delay(1000);
  219. }
  220.  
  221. void loop()
  222. {
  223. uint16_t xpos, ypos; //screen coordinates
  224. tp = ts.getPoint(); //tp.x, tp.y are ADC values
  225.  
  226. // if sharing pins, you'll need to fix the directions of the touchscreen pins
  227. pinMode(XM, OUTPUT);
  228. pinMode(YP, OUTPUT);
  229. pinMode(XP, OUTPUT);
  230. pinMode(YM, OUTPUT);
  231. // digitalWrite(XM, HIGH);
  232. // digitalWrite(YP, HIGH);
  233. // we have some minimum pressure we consider 'valid'
  234. // pressure of 0 means no pressing!
  235.  
  236. if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) {
  237. // is controller wired for Landscape ? or are we oriented in Landscape?
  238. if (SwapXY != (Orientation & 1)) SWAP(tp.x, tp.y);
  239. // scale from 0->1023 to tft.width i.e. left = 0, rt = width
  240. // most mcufriend have touch (with icons) that extends below the TFT
  241. // screens without icons need to reserve a space for "erase"
  242. // scale the ADC values from ts.getPoint() to screen values e.g. 0-239
  243. xpos = map(tp.x, TS_LEFT, TS_RT, 0, tft.width());
  244. ypos = map(tp.y, TS_TOP, TS_BOT, 0, tft.height());
  245.  
  246. // are we in top color box area ?
  247. if (ypos < BOXSIZE) { //draw white border on selected color box
  248. oldcolor = currentcolor;
  249.  
  250. if (xpos < BOXSIZE) {
  251. currentcolor = RED;
  252. tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  253. } else if (xpos < BOXSIZE * 2) {
  254. currentcolor = YELLOW;
  255. tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
  256. } else if (xpos < BOXSIZE * 3) {
  257. currentcolor = GREEN;
  258. tft.drawRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, WHITE);
  259. } else if (xpos < BOXSIZE * 4) {
  260. currentcolor = CYAN;
  261. tft.drawRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, WHITE);
  262. } else if (xpos < BOXSIZE * 5) {
  263. currentcolor = BLUE;
  264. tft.drawRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, WHITE);
  265. } else if (xpos < BOXSIZE * 6) {
  266. currentcolor = MAGENTA;
  267. tft.drawRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, WHITE);
  268. }
  269.  
  270. if (oldcolor != currentcolor) { //rub out the previous white border
  271. if (oldcolor == RED) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  272. if (oldcolor == YELLOW) tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
  273. if (oldcolor == GREEN) tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN);
  274. if (oldcolor == CYAN) tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN);
  275. if (oldcolor == BLUE) tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE);
  276. if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);
  277. }
  278. }
  279. // are we in drawing area ?
  280. if (((ypos - PENRADIUS) > BOXSIZE) && ((ypos + PENRADIUS) < tft.height())) {
  281. tft.fillCircle(xpos, ypos, PENRADIUS, currentcolor);
  282. }
  283. // are we in erase area ?
  284. if (ypos > tft.height() - 10) {
  285. // press the bottom of the screen to erase
  286. tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, BLACK);
  287. }
  288. }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement