PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 line
4.5KB

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