touchpaint.ino 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /***************************************************
  2. This is our touchscreen painting example for the Adafruit ILI9341 Shield
  3. ----> http://www.adafruit.com/products/1651
  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. #include <Adafruit_GFX.h> // Core graphics library
  14. #include <SPI.h>
  15. #include <Wire.h> // this is needed even tho we aren't using it
  16. #include <Adafruit_ILI9341.h>
  17. #include <Adafruit_STMPE610.h>
  18. // This is calibration data for the raw touch data to the screen coordinates
  19. #define TS_MINX 150
  20. #define TS_MINY 130
  21. #define TS_MAXX 3800
  22. #define TS_MAXY 4000
  23. // The STMPE610 uses hardware SPI on the shield, and #8
  24. #define STMPE_CS 8
  25. Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
  26. // The display also uses hardware SPI, plus #9 & #10
  27. #define TFT_CS 10
  28. #define TFT_DC 9
  29. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
  30. // Size of the color selection boxes and the paintbrush size
  31. #define BOXSIZE 40
  32. #define PENRADIUS 3
  33. int oldcolor, currentcolor;
  34. void setup(void) {
  35. // while (!Serial); // used for leonardo debugging
  36. Serial.begin(9600);
  37. Serial.println(F("Touch Paint!"));
  38. tft.begin();
  39. if (!ts.begin()) {
  40. Serial.println("Couldn't start touchscreen controller");
  41. while (1);
  42. }
  43. Serial.println("Touchscreen started");
  44. tft.fillScreen(ILI9341_BLACK);
  45. // make the color selection boxes
  46. tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
  47. tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
  48. tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
  49. tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
  50. tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
  51. tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
  52. // select the current color 'red'
  53. tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  54. currentcolor = ILI9341_RED;
  55. }
  56. void loop()
  57. {
  58. // See if there's any touch data for us
  59. if (ts.bufferEmpty()) {
  60. return;
  61. }
  62. /*
  63. // You can also wait for a touch
  64. if (! ts.touched()) {
  65. return;
  66. }
  67. */
  68. // Retrieve a point
  69. TS_Point p = ts.getPoint();
  70. /*
  71. Serial.print("X = "); Serial.print(p.x);
  72. Serial.print("\tY = "); Serial.print(p.y);
  73. Serial.print("\tPressure = "); Serial.println(p.z);
  74. */
  75. // Scale from ~0->4000 to tft.width using the calibration #'s
  76. p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
  77. p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
  78. /*
  79. Serial.print("("); Serial.print(p.x);
  80. Serial.print(", "); Serial.print(p.y);
  81. Serial.println(")");
  82. */
  83. if (p.y < BOXSIZE) {
  84. oldcolor = currentcolor;
  85. if (p.x < BOXSIZE) {
  86. currentcolor = ILI9341_RED;
  87. tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  88. } else if (p.x < BOXSIZE*2) {
  89. currentcolor = ILI9341_YELLOW;
  90. tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  91. } else if (p.x < BOXSIZE*3) {
  92. currentcolor = ILI9341_GREEN;
  93. tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  94. } else if (p.x < BOXSIZE*4) {
  95. currentcolor = ILI9341_CYAN;
  96. tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  97. } else if (p.x < BOXSIZE*5) {
  98. currentcolor = ILI9341_BLUE;
  99. tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  100. } else if (p.x < BOXSIZE*6) {
  101. currentcolor = ILI9341_MAGENTA;
  102. tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  103. }
  104. if (oldcolor != currentcolor) {
  105. if (oldcolor == ILI9341_RED)
  106. tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
  107. if (oldcolor == ILI9341_YELLOW)
  108. tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
  109. if (oldcolor == ILI9341_GREEN)
  110. tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
  111. if (oldcolor == ILI9341_CYAN)
  112. tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
  113. if (oldcolor == ILI9341_BLUE)
  114. tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
  115. if (oldcolor == ILI9341_MAGENTA)
  116. tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
  117. }
  118. }
  119. if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {
  120. tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
  121. }
  122. }