Guest User

Untitled

a guest
Dec 11th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.23 KB | None | 0 0
  1. // Paint example specifically for the TFTLCD breakout board.
  2. // If using the Arduino shield, use the tftpaint_shield.pde sketch instead!
  3. // DOES NOT CURRENTLY WORK ON ARDUINO LEONARDO
  4.  
  5. #include <Adafruit_GFX.h>    // Core graphics library
  6. #include <Adafruit_TFTLCD.h> // Hardware-specific library
  7. #include <TouchScreen.h>
  8.  
  9. #if defined(__SAM3X8E__)
  10.     #undef __FlashStringHelper::F(string_literal)
  11.     #define F(string_literal) string_literal
  12. #endif
  13.  
  14. // When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
  15. // For the Arduino Uno, Duemilanove, Diecimila, etc.:
  16. //   D0 connects to digital pin 8  (Notice these are
  17. //   D1 connects to digital pin 9   NOT in order!)
  18. //   D2 connects to digital pin 2
  19. //   D3 connects to digital pin 3
  20. //   D4 connects to digital pin 4
  21. //   D5 connects to digital pin 5
  22. //   D6 connects to digital pin 6
  23. //   D7 connects to digital pin 7
  24.  
  25. // For the Arduino Mega, use digital pins 22 through 29
  26. // (on the 2-row header at the end of the board).
  27. //   D0 connects to digital pin 22
  28. //   D1 connects to digital pin 23
  29. //   D2 connects to digital pin 24
  30. //   D3 connects to digital pin 25
  31. //   D4 connects to digital pin 26
  32. //   D5 connects to digital pin 27
  33. //   D6 connects to digital pin 28
  34. //   D7 connects to digital pin 29
  35.  
  36. // For the Arduino Due, use digital pins 33 through 40
  37. // (on the 2-row header at the end of the board).
  38. //   D0 connects to digital pin 33
  39. //   D1 connects to digital pin 34
  40. //   D2 connects to digital pin 35
  41. //   D3 connects to digital pin 36
  42. //   D4 connects to digital pin 37
  43. //   D5 connects to digital pin 38
  44. //   D6 connects to digital pin 39
  45. //   D7 connects to digital pin 40
  46.  
  47. #define YP A1  // must be an analog pin, use "An" notation!
  48. #define XM A2  // must be an analog pin, use "An" notation!
  49. #define YM 7   // can be a digital pin
  50. #define XP 6   // can be a digital pin
  51.  
  52. #define TS_MINX 150
  53. #define TS_MINY 120
  54. #define TS_MAXX 920
  55. #define TS_MAXY 940
  56.  
  57. // For better pressure precision, we need to know the resistance
  58. // between X+ and X- Use any multimeter to read it
  59. // For the one we're using, its 300 ohms across the X plate
  60. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  61.  
  62. #define LCD_CS A3
  63. #define LCD_CD A2
  64. #define LCD_WR A1
  65. #define LCD_RD A0
  66. // optional
  67. #define LCD_RESET A4
  68.  
  69. // Assign human-readable names to some common 16-bit color values:
  70. #define BLACK   0x0000
  71. #define BLUE    0x001F
  72. #define RED     0xF800
  73. #define GREEN   0x07E0
  74. #define CYAN    0x07FF
  75. #define MAGENTA 0xF81F
  76. #define YELLOW  0xFFE0
  77. #define WHITE   0xFFFF
  78.  
  79.  
  80. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  81.  
  82. #define BOXSIZE 40
  83. #define PENRADIUS 3
  84. int oldcolor, currentcolor;
  85.  
  86. void setup(void) {
  87.   Serial.begin(9600);
  88.   Serial.println(F("Paint!"));
  89.  
  90.   tft.reset();
  91.  
  92.   uint16_t identifier = tft.readID();
  93.  
  94.     if(identifier == 0x9325) {
  95.     Serial.println(F("Found ILI9325 LCD driver"));
  96.   } else if(identifier == 0x9327) {
  97.     Serial.println(F("Found ILI9327 LCD driver"));
  98.   } else if(identifier == 0x9328) {
  99.     Serial.println(F("Found ILI9328 LCD driver"));
  100.   } else if(identifier == 0x7575) {
  101.     Serial.println(F("Found HX8347G LCD driver"));
  102.   } else if(identifier == 0x9341) {
  103.     Serial.println(F("Found ILI9341 LCD driver"));
  104.   } else if(identifier == 0x8357) {
  105.     Serial.println(F("Found HX8357D LCD driver"));
  106.   } else if(identifier == 0x0154) {
  107.     Serial.println(F("Found S6D0154 LCD driver"));
  108.   } else if(identifier == 0x9488) {
  109.     Serial.println(F("Found ILI9488 LCD driver"));
  110.   } else {
  111.     Serial.print(F("Unknown LCD driver chip: "));
  112.     Serial.println(identifier, HEX);
  113.     Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
  114.     Serial.println(F("  #define USE_ADAFRUIT_SHIELD_PINOUT"));
  115.     Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
  116.     Serial.println(F("If using the breakout board, it should NOT be #defined!"));
  117.     Serial.println(F("Also if using the breakout, double-check that all wiring"));
  118.     Serial.println(F("matches the tutorial."));
  119.     return;
  120.   }
  121.  
  122.   tft.begin(identifier);
  123.  
  124.   tft.fillScreen(BLACK);
  125.  
  126.   tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  127.   tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
  128.   tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN);
  129.   tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN);
  130.   tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE);
  131.   tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA);
  132.   // tft.fillRect(BOXSIZE*6, 0, BOXSIZE, BOXSIZE, WHITE);
  133.  
  134.   tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  135.   currentcolor = RED;
  136.  
  137.   pinMode(13, OUTPUT);
  138. }
  139.  
  140. #define MINPRESSURE 10
  141. #define MAXPRESSURE 1000
  142.  
  143. void loop()
  144. {
  145.   digitalWrite(13, HIGH);
  146.   TSPoint p = ts.getPoint();
  147.   digitalWrite(13, LOW);
  148.  
  149.   // if sharing pins, you'll need to fix the directions of the touchscreen pins
  150.   //pinMode(XP, OUTPUT);
  151.   pinMode(XM, OUTPUT);
  152.   pinMode(YP, OUTPUT);
  153.   //pinMode(YM, OUTPUT);
  154.  
  155.   // we have some minimum pressure we consider 'valid'
  156.   // pressure of 0 means no pressing!
  157.  
  158.   if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
  159.     /*
  160.     Serial.print("X = "); Serial.print(p.x);
  161.     Serial.print("\tY = "); Serial.print(p.y);
  162.     Serial.print("\tPressure = "); Serial.println(p.z);
  163.     */
  164.    
  165.     if (p.y < (TS_MINY-5)) {
  166.       Serial.println("erase");
  167.       // press the bottom of the screen to erase
  168.       tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);
  169.     }
  170.     // scale from 0->1023 to tft.width
  171.     p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
  172.     p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
  173.     /*
  174.     Serial.print("("); Serial.print(p.x);
  175.     Serial.print(", "); Serial.print(p.y);
  176.     Serial.println(")");
  177.     */
  178.     if (p.y < BOXSIZE) {
  179.        oldcolor = currentcolor;
  180.  
  181.        if (p.x < BOXSIZE) {
  182.          currentcolor = RED;
  183.          tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE);
  184.        } else if (p.x < BOXSIZE*2) {
  185.          currentcolor = YELLOW;
  186.          tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE);
  187.        } else if (p.x < BOXSIZE*3) {
  188.          currentcolor = GREEN;
  189.          tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, WHITE);
  190.        } else if (p.x < BOXSIZE*4) {
  191.          currentcolor = CYAN;
  192.          tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, WHITE);
  193.        } else if (p.x < BOXSIZE*5) {
  194.          currentcolor = BLUE;
  195.          tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, WHITE);
  196.        } else if (p.x < BOXSIZE*6) {
  197.          currentcolor = MAGENTA;
  198.          tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, WHITE);
  199.        }
  200.  
  201.        if (oldcolor != currentcolor) {
  202.           if (oldcolor == RED) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, RED);
  203.           if (oldcolor == YELLOW) tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, YELLOW);
  204.           if (oldcolor == GREEN) tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, GREEN);
  205.           if (oldcolor == CYAN) tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, CYAN);
  206.           if (oldcolor == BLUE) tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, BLUE);
  207.           if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, MAGENTA);
  208.        }
  209.     }
  210.     if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {
  211.       tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
  212.     }
  213.   }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment