Guest User

TouchScreenPaintTest

a guest
Mar 5th, 2019
32
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***************************************************
  2. This is our touchscreen painting example for the Adafruit ILI9341 Shield
  3. ----> http://www.adafruit.com/products/1651
  4.  
  5. Check out the links above for our tutorials and wiring diagrams
  6. These displays use SPI to communicate, 4 or 5 pins are required to
  7. interface (RST is optional)
  8. Adafruit invests time and resources providing this open source code,
  9. please support Adafruit and open-source hardware by purchasing
  10. products from Adafruit!
  11.  
  12. Written by Limor Fried/Ladyada for Adafruit Industries.
  13. MIT license, all text above must be included in any redistribution
  14. ****************************************************/
  15.  
  16.  
  17. #include <Adafruit_GFX.h> // Core graphics library
  18. #include <SPI.h>
  19. #include <Wire.h> // this is needed even tho we aren't using it
  20. #include <Adafruit_ILI9341.h>
  21. #include <Adafruit_STMPE610.h>
  22.  
  23. // This is calibration data for the raw touch data to the screen coordinates
  24. #define TS_MINX 150
  25. #define TS_MINY 130
  26. #define TS_MAXX 3800
  27. #define TS_MAXY 4000
  28.  
  29. // The STMPE610 uses hardware SPI on the shield, and #8
  30. #define STMPE_CS 8
  31. Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
  32.  
  33. // The display also uses hardware SPI, plus #9 & #10
  34. #define TFT_CS 10
  35. #define TFT_DC 9
  36. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
  37.  
  38. // Size of the color selection boxes and the paintbrush size
  39. #define BOXSIZE 40
  40. #define PENRADIUS 3
  41. int oldcolor, currentcolor;
  42.  
  43. void setup(void) {
  44. // while (!Serial); // used for leonardo debugging
  45.  
  46. Serial.begin(9600);
  47. Serial.println(F("Touch Paint!"));
  48.  
  49. tft.begin();
  50.  
  51. if (!ts.begin()) {
  52. Serial.println("Couldn't start touchscreen controller");
  53. while (1);
  54. }
  55. Serial.println("Touchscreen started");
  56.  
  57. tft.fillScreen(ILI9341_BLACK);
  58.  
  59. // make the color selection boxes
  60. tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
  61. tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
  62. tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
  63. tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
  64. tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
  65. tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
  66.  
  67. // select the current color 'red'
  68. tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  69. currentcolor = ILI9341_RED;
  70. }
  71.  
  72.  
  73. void loop()
  74. {
  75. // See if there's any touch data for us
  76. if (ts.bufferEmpty()) {
  77. return;
  78. }
  79. /*
  80. // You can also wait for a touch
  81. if (! ts.touched()) {
  82. return;
  83. }
  84. */
  85.  
  86. // Retrieve a point
  87. TS_Point p = ts.getPoint();
  88.  
  89. /*
  90. Serial.print("X = "); Serial.print(p.x);
  91. Serial.print("\tY = "); Serial.print(p.y);
  92. Serial.print("\tPressure = "); Serial.println(p.z);
  93. */
  94.  
  95. // Scale from ~0->4000 to tft.width using the calibration #'s
  96. p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
  97. p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
  98.  
  99. /*
  100. Serial.print("("); Serial.print(p.x);
  101. Serial.print(", "); Serial.print(p.y);
  102. Serial.println(")");
  103. */
  104.  
  105. if (p.y < BOXSIZE) {
  106. oldcolor = currentcolor;
  107.  
  108. if (p.x < BOXSIZE) {
  109. currentcolor = ILI9341_RED;
  110. tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  111. } else if (p.x < BOXSIZE*2) {
  112. currentcolor = ILI9341_YELLOW;
  113. tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  114. } else if (p.x < BOXSIZE*3) {
  115. currentcolor = ILI9341_GREEN;
  116. tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  117. } else if (p.x < BOXSIZE*4) {
  118. currentcolor = ILI9341_CYAN;
  119. tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  120. } else if (p.x < BOXSIZE*5) {
  121. currentcolor = ILI9341_BLUE;
  122. tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  123. } else if (p.x < BOXSIZE*6) {
  124. currentcolor = ILI9341_MAGENTA;
  125. tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  126. }
  127.  
  128. if (oldcolor != currentcolor) {
  129. if (oldcolor == ILI9341_RED)
  130. tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
  131. if (oldcolor == ILI9341_YELLOW)
  132. tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
  133. if (oldcolor == ILI9341_GREEN)
  134. tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
  135. if (oldcolor == ILI9341_CYAN)
  136. tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
  137. if (oldcolor == ILI9341_BLUE)
  138. tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
  139. if (oldcolor == ILI9341_MAGENTA)
  140. tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
  141. }
  142. }
  143. if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {
  144. tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
  145. }
  146. }
RAW Paste Data