PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

145 rindas
4.8KB

  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 <SPI.h>
  15. #include <ILI9341_t3.h>
  16. #include "TouchScreen.h"
  17. // These are the four touchscreen analog pins
  18. #define YP A2 // must be an analog pin, use "An" notation!
  19. #define XM A3 // must be an analog pin, use "An" notation!
  20. #define YM 5 // can be a digital pin
  21. #define XP 4 // can be a digital pin
  22. // This is calibration data for the raw touch data to the screen coordinates
  23. #define TS_MINX 150
  24. #define TS_MINY 120
  25. #define TS_MAXX 920
  26. #define TS_MAXY 940
  27. #define MINPRESSURE 10
  28. #define MAXPRESSURE 1000
  29. // The display uses hardware SPI, plus #9 & #10
  30. #define TFT_CS 10
  31. #define TFT_DC 9
  32. ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC);
  33. // For better pressure precision, we need to know the resistance
  34. // between X+ and X- Use any multimeter to read it
  35. // For the one we're using, its 300 ohms across the X plate
  36. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  37. // Size of the color selection boxes and the paintbrush size
  38. #define BOXSIZE 40
  39. #define PENRADIUS 3
  40. int oldcolor, currentcolor;
  41. void setup(void) {
  42. // while (!Serial); // used for leonardo debugging
  43. Serial.begin(9600);
  44. Serial.println(F("Touch Paint!"));
  45. tft.begin();
  46. tft.fillScreen(ILI9341_BLACK);
  47. // make the color selection boxes
  48. tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
  49. tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
  50. tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
  51. tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
  52. tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
  53. tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
  54. // select the current color 'red'
  55. tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  56. currentcolor = ILI9341_RED;
  57. }
  58. void loop()
  59. {
  60. // Retrieve a point
  61. TSPoint p = ts.getPoint();
  62. /*
  63. Serial.print("X = "); Serial.print(p.x);
  64. Serial.print("\tY = "); Serial.print(p.y);
  65. Serial.print("\tPressure = "); Serial.println(p.z);
  66. */
  67. // we have some minimum pressure we consider 'valid'
  68. // pressure of 0 means no pressing!
  69. if (p.z < MINPRESSURE || p.z > MAXPRESSURE) {
  70. return;
  71. }
  72. // Scale from ~0->1000 to tft.width using the calibration #'s
  73. p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
  74. p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
  75. /*
  76. Serial.print("("); Serial.print(p.x);
  77. Serial.print(", "); Serial.print(p.y);
  78. Serial.println(")");
  79. */
  80. if (p.y < BOXSIZE) {
  81. oldcolor = currentcolor;
  82. if (p.x < BOXSIZE) {
  83. currentcolor = ILI9341_RED;
  84. tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  85. } else if (p.x < BOXSIZE*2) {
  86. currentcolor = ILI9341_YELLOW;
  87. tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  88. } else if (p.x < BOXSIZE*3) {
  89. currentcolor = ILI9341_GREEN;
  90. tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  91. } else if (p.x < BOXSIZE*4) {
  92. currentcolor = ILI9341_CYAN;
  93. tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  94. } else if (p.x < BOXSIZE*5) {
  95. currentcolor = ILI9341_BLUE;
  96. tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  97. } else if (p.x < BOXSIZE*6) {
  98. currentcolor = ILI9341_MAGENTA;
  99. tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
  100. }
  101. if (oldcolor != currentcolor) {
  102. if (oldcolor == ILI9341_RED)
  103. tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
  104. if (oldcolor == ILI9341_YELLOW)
  105. tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
  106. if (oldcolor == ILI9341_GREEN)
  107. tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
  108. if (oldcolor == ILI9341_CYAN)
  109. tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
  110. if (oldcolor == ILI9341_BLUE)
  111. tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
  112. if (oldcolor == ILI9341_MAGENTA)
  113. tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
  114. }
  115. }
  116. if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {
  117. tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
  118. }
  119. }