Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 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. //Store: http://dx.com
  7. // https://open-smart.aliexpress.com/store/1199788
  8. // Board:OPEN-SMART UNO R3 5V / 3.3V, Arduino UNO R3, Arduino Mega2560
  9. //3.2INCH TFT:
  10. // https://www.aliexpress.com/store/product/3-2-TFT-LCD-Display-module-Touch-Screen-Shield-board-onboard-temperature-sensor-w-Touch-Pen/1199788_32755473754.html?spm=2114.12010615.0.0.bXDdc3
  11. //OPEN-SMART UNO R3 5V / 3.3V:
  12. // https://www.aliexpress.com/store/product/OPEN-SMART-5V-3-3V-Compatible-UNO-R3-CH340G-ATMEGA328P-Development-Board-with-USB-Cable-for/1199788_32758607490.html?spm=2114.12010615.0.0.ckMTaN
  13.  
  14.  
  15. #include <Adafruit_GFX.h> // Core graphics library
  16. //#include <Adafruit_TFTLCD.h> // Hardware-specific library
  17. //Adafruit_TFTLCD tft(A3, A2, A1, A0, A4);
  18. #include <MCUFRIEND_kbv.h>
  19. MCUFRIEND_kbv tft; // hard-wired for UNO shields anyway.
  20. #include <TouchScreen.h>
  21.  
  22. #if defined(__SAM3X8E__)
  23. #undef __FlashStringHelper::F(string_literal)
  24. #define F(string_literal) string_literal
  25. #endif
  26.  
  27. //----------------------------------------|
  28. // TFT Breakout -- Arduino UNO / Mega2560 / OPEN-SMART UNO Black
  29. // GND -- GND
  30. // 3V3 -- 3.3V
  31. // CS -- A3
  32. // RS -- A2
  33. // WR -- A1
  34. // RD -- A0
  35. // RST -- RESET
  36. // LED -- GND
  37. // DB0 -- 8
  38. // DB1 -- 9
  39. // DB2 -- 10
  40. // DB3 -- 11
  41. // DB4 -- 4
  42. // DB5 -- 13
  43. // DB6 -- 6
  44. // DB7 -- 7
  45. // most mcufriend shields use these pins and Portrait mode:
  46. uint8_t YP = A1; // must be an analog pin, use "An" notation!
  47. uint8_t XM = A2; // must be an analog pin, use "An" notation!
  48. uint8_t YM = 7; // can be a digital pin
  49. uint8_t XP = 6; // can be a digital pin
  50. uint8_t SwapXY = 0;
  51.  
  52. uint16_t TS_LEFT = 880;
  53. uint16_t TS_RT = 170;
  54. uint16_t TS_TOP = 950;
  55. uint16_t TS_BOT = 180;
  56. char *name = "Unknown controller";
  57.  
  58. // For better pressure precision, we need to know the resistance
  59. // between X+ and X- Use any multimeter to read it
  60. // For the one we're using, its 260 ohms across the X plate
  61. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 260);
  62. TSPoint tp;
  63.  
  64. #define MINPRESSURE 20
  65. #define MAXPRESSURE 1000
  66.  
  67. #define SWAP(a, b) {uint16_t tmp = a; a = b; b = tmp;}
  68.  
  69. int16_t BOXSIZE;
  70. int16_t PENRADIUS = 3;
  71. uint16_t identifier, oldcolor, currentcolor;
  72. uint8_t Orientation = 2; //PORTRAIT
  73.  
  74. // Assign human-readable names to some common 16-bit color values:
  75. #define BLACK 0x0000
  76. #define BLUE 0x001F
  77. #define RED 0xF800
  78. #define GREEN 0x07E0
  79. #define CYAN 0x07FF
  80. #define MAGENTA 0xF81F
  81. #define YELLOW 0xFFE0
  82. #define WHITE 0xFFFF
  83.  
  84. void show_Serial(void)
  85. {
  86. Serial.print(F("Found "));
  87. Serial.print(name);
  88. Serial.println(F(" LCD driver"));
  89. Serial.print(F("ID=0x"));
  90. Serial.println(identifier, HEX);
  91. Serial.println("Screen is " + String(tft.width()) + "x" + String(tft.height()));
  92. Serial.println("Calibration is: ");
  93. Serial.println("LEFT = " + String(TS_LEFT) + " RT = " + String(TS_RT));
  94. Serial.println("TOP = " + String(TS_TOP) + " BOT = " + String(TS_BOT));
  95. Serial.print("Wiring is: ");
  96. Serial.println(SwapXY ? "SWAPXY" : "PORTRAIT");
  97. Serial.println("YP=" + String(YP) + " XM=" + String(XM));
  98. Serial.println("YM=" + String(YM) + " XP=" + String(XP));
  99. }
  100.  
  101. void show_tft(void)
  102. {
  103. tft.setCursor(0, 0);
  104. tft.setTextSize(2);
  105. tft.print(F("Found "));
  106. tft.print(name);
  107. tft.println(F(" LCD"));
  108. tft.setTextSize(1);
  109. tft.print(F("ID=0x"));
  110. tft.println(identifier, HEX);
  111. tft.println("Screen is " + String(tft.width()) + "x" + String(tft.height()));
  112. tft.println("Calibration is: ");
  113. tft.println("LEFT = " + String(TS_LEFT) + " RT = " + String(TS_RT));
  114. tft.println("TOP = " + String(TS_TOP) + " BOT = " + String(TS_BOT));
  115. tft.print("\nWiring is: ");
  116. if (SwapXY) {
  117. tft.setTextColor(CYAN);
  118. tft.setTextSize(2);
  119. }
  120. tft.println(SwapXY ? "SWAPXY" : "PORTRAIT");
  121. tft.println("YP=" + String(YP) + " XM=" + String(XM));
  122. tft.println("YM=" + String(YM) + " XP=" + String(XP));
  123. tft.setTextSize(2);
  124. tft.setTextColor(RED);
  125. tft.setCursor((tft.width() - 48) / 2, (tft.height() * 2) / 4);
  126. tft.print("EXIT");
  127. tft.setTextColor(YELLOW, BLACK);
  128. tft.setCursor(0, (tft.height() * 6) / 8);
  129. tft.print("Touch screen for loc");
  130. while (1) {
  131. tp = ts.getPoint();
  132. pinMode(XM, OUTPUT);
  133. pinMode(YP, OUTPUT);
  134. pinMode(XP, OUTPUT);
  135. pinMode(YM, OUTPUT);
  136. if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) continue;
  137. if (tp.x > 450 && tp.x < 570 && tp.y > 450 && tp.y < 570) break;
  138. tft.setCursor(0, (tft.height() * 3) / 4);
  139. tft.print("tp.x=" + String(tp.x) + " tp.y=" + String(tp.y) + " ");
  140. }
  141. }
  142.  
  143.  
  144. void setup(void)
  145. {
  146. uint16_t tmp;
  147. tft.begin(9600);
  148.  
  149. tft.reset();
  150. identifier = tft.readID();
  151.  
  152. switch (Orientation) { // adjust for different aspects
  153. case 0: break; //no change, calibrated for PORTRAIT
  154. case 1: tmp = TS_LEFT, TS_LEFT = TS_BOT, TS_BOT = TS_RT, TS_RT = TS_TOP, TS_TOP = tmp; break;
  155. case 2: SWAP(TS_LEFT, TS_RT); SWAP(TS_TOP, TS_BOT); break;
  156. case 3: tmp = TS_LEFT, TS_LEFT = TS_TOP, TS_TOP = TS_RT, TS_RT = TS_BOT, TS_BOT = tmp; break;
  157. }
  158.  
  159. Serial.begin(9600);
  160. ts = TouchScreen(XP, YP, XM, YM, 260); //call the constructor AGAIN with new values.
  161. tft.begin(identifier);
  162. show_Serial();
  163. tft.setRotation(Orientation);
  164. tft.fillScreen(BLACK);
  165. show_tft();
  166.  
  167. BOXSIZE = tft.width() / 6;
  168. tft.fillScreen(BLACK);
  169.  
  170. tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  171. tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
  172. tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN);
  173. tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN);
  174. tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE);
  175. tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);
  176.  
  177. tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  178. currentcolor = RED;
  179. delay(1000);
  180. }
  181.  
  182. void loop()
  183. {
  184. uint16_t xpos, ypos; //screen coordinates
  185. tp = ts.getPoint(); //tp.x, tp.y are ADC values
  186.  
  187. // if sharing pins, you'll need to fix the directions of the touchscreen pins
  188. pinMode(XM, OUTPUT);
  189. pinMode(YP, OUTPUT);
  190. pinMode(XP, OUTPUT);
  191. pinMode(YM, OUTPUT);
  192. // digitalWrite(XM, HIGH);
  193. // digitalWrite(YP, HIGH);
  194. // we have some minimum pressure we consider 'valid'
  195. // pressure of 0 means no pressing!
  196.  
  197. if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) {
  198. // is controller wired for Landscape ? or are we oriented in Landscape?
  199. if (SwapXY != (Orientation & 1)) SWAP(tp.x, tp.y);
  200. // scale from 0->1023 to tft.width i.e. left = 0, rt = width
  201. // most mcufriend have touch (with icons) that extends below the TFT
  202. // screens without icons need to reserve a space for "erase"
  203. // scale the ADC values from ts.getPoint() to screen values e.g. 0-239
  204. xpos = map(tp.x, TS_LEFT, TS_RT, tft.width(), 0);
  205. ypos = map(tp.y, TS_TOP, TS_BOT, tft.height(), 0);
  206.  
  207. // are we in top color box area ?
  208. if (ypos < BOXSIZE) { //draw white border on selected color box
  209. oldcolor = currentcolor;
  210.  
  211. if (xpos < BOXSIZE) {
  212. currentcolor = RED;
  213. tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  214. } else if (xpos < BOXSIZE * 2) {
  215. currentcolor = YELLOW;
  216. tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
  217. } else if (xpos < BOXSIZE * 3) {
  218. currentcolor = GREEN;
  219. tft.drawRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, WHITE);
  220. } else if (xpos < BOXSIZE * 4) {
  221. currentcolor = CYAN;
  222. tft.drawRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, WHITE);
  223. } else if (xpos < BOXSIZE * 5) {
  224. currentcolor = BLUE;
  225. tft.drawRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, WHITE);
  226. } else if (xpos < BOXSIZE * 6) {
  227. currentcolor = MAGENTA;
  228. tft.drawRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, WHITE);
  229. }
  230.  
  231. if (oldcolor != currentcolor) { //rub out the previous white border
  232. if (oldcolor == RED) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  233. if (oldcolor == YELLOW) tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
  234. if (oldcolor == GREEN) tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, GREEN);
  235. if (oldcolor == CYAN) tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, CYAN);
  236. if (oldcolor == BLUE) tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, BLUE);
  237. if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, MAGENTA);
  238. }
  239. }
  240. // are we in drawing area ?
  241. if (((ypos - PENRADIUS) > BOXSIZE) && ((ypos + PENRADIUS) < tft.height())) {
  242. tft.fillCircle(xpos, ypos, PENRADIUS, currentcolor);
  243. }
  244. // are we in erase area ?
  245. if (ypos > tft.height() - 10) {
  246. // press the bottom of the screen to erase
  247. tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, BLACK);
  248. }
  249. }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement