breakouttouchpaint.ino 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /***************************************************
  2. This is our touchscreen painting example for the Adafruit ILI9341 Breakout
  3. ----> http://www.adafruit.com/products/1770
  4. Check out the links above for our tutorials and wiring diagrams
  5. These displays use SPI to communicate, 4 or 5 pins are required to
  6. interface (RST is optional)
  7. Adafruit invests time and resources providing this open source code,
  8. please support Adafruit and open-source hardware by purchasing
  9. products from Adafruit!
  10. Written by Limor Fried/Ladyada for Adafruit Industries.
  11. MIT license, all text above must be included in any redistribution
  12. ****************************************************/
  13. /** NOT FOR USE WITH THE TOUCH SHIELD, ONLY FOR THE BREAKOUT! **/
  14. #include <Adafruit_GFX.h> // Core graphics library
  15. #include <SPI.h>
  16. #include <Adafruit_ILI9341.h>
  17. #include "TouchScreen.h"
  18. // These are the four touchscreen analog pins
  19. #define YP A2 // must be an analog pin, use "An" notation!
  20. #define XM A3 // must be an analog pin, use "An" notation!
  21. #define YM 5 // can be a digital pin
  22. #define XP 4 // can be a digital pin
  23. // This is calibration data for the raw touch data to the screen coordinates
  24. #define TS_MINX 150
  25. #define TS_MINY 120
  26. #define TS_MAXX 920
  27. #define TS_MAXY 940
  28. #define MINPRESSURE 10
  29. #define MAXPRESSURE 1000
  30. // The display uses hardware SPI, plus #9 & #10
  31. #define TFT_CS 10
  32. #define TFT_DC 9
  33. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
  34. // For better pressure precision, we need to know the resistance
  35. // between X+ and X- Use any multimeter to read it
  36. // For the one we're using, its 300 ohms across the X plate
  37. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  38. // Size of the color selection boxes and the paintbrush size
  39. #define BOXSIZE 40
  40. #define PENRADIUS 3
  41. int oldcolor, currentcolor;
  42. void setup(void) {
  43. // while (!Serial); // used for leonardo debugging
  44. Serial.begin(9600);
  45. Serial.println(F("Touch Paint!"));
  46. tft.begin();
  47. tft.fillScreen(ILI9341_BLACK);
  48. // make the color selection boxes
  49. tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
  50. tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
  51. tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
  52. tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
  53. tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
  54. tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
  55. // select the current color 'red'
  56. tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  57. currentcolor = ILI9341_RED;
  58. }
  59. void loop()
  60. {
  61. // Retrieve a point
  62. TSPoint p = ts.getPoint();
  63. /*
  64. Serial.print("X = "); Serial.print(p.x);
  65. Serial.print("\tY = "); Serial.print(p.y);
  66. Serial.print("\tPressure = "); Serial.println(p.z);
  67. */
  68. // we have some minimum pressure we consider 'valid'
  69. // pressure of 0 means no pressing!
  70. if (p.z < MINPRESSURE || p.z > MAXPRESSURE) {
  71. return;
  72. }
  73. // Scale from ~0->1000 to tft.width using the calibration #'s
  74. p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
  75. p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
  76. /*
  77. Serial.print("("); Serial.print(p.x);
  78. Serial.print(", "); Serial.print(p.y);
  79. Serial.println(")");
  80. */
  81. if (p.y < BOXSIZE) {
  82. oldcolor = currentcolor;
  83. if (p.x < BOXSIZE) {
  84. currentcolor = ILI9341_RED;
  85. tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  86. } else if (p.x < BOXSIZE*2) {
  87. currentcolor = ILI9341_YELLOW;
  88. tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  89. } else if (p.x < BOXSIZE*3) {
  90. currentcolor = ILI9341_GREEN;
  91. tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  92. } else if (p.x < BOXSIZE*4) {
  93. currentcolor = ILI9341_CYAN;
  94. tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  95. } else if (p.x < BOXSIZE*5) {
  96. currentcolor = ILI9341_BLUE;
  97. tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  98. } else if (p.x < BOXSIZE*6) {
  99. currentcolor = ILI9341_MAGENTA;
  100. tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  101. }
  102. if (oldcolor != currentcolor) {
  103. if (oldcolor == ILI9341_RED)
  104. tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
  105. if (oldcolor == ILI9341_YELLOW)
  106. tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
  107. if (oldcolor == ILI9341_GREEN)
  108. tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
  109. if (oldcolor == ILI9341_CYAN)
  110. tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
  111. if (oldcolor == ILI9341_BLUE)
  112. tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
  113. if (oldcolor == ILI9341_MAGENTA)
  114. tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
  115. }
  116. }
  117. if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {
  118. tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
  119. }
  120. }