Advertisement
Guest User

BarSketchTFT

a guest
Mar 22nd, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
  2. // CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
  3. // SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.
  4. //Technical support:goodtft@163.com
  5.  
  6. #include <Adafruit_GFX.h>    // Core graphics library
  7. #include <Adafruit_TFTLCD.h> // Hardware-specific library
  8. #include <TouchScreen.h>
  9.  
  10. // The control pins for the LCD can be assigned to any digital or
  11. // analog pins...but we'll use the analog pins as this allows us to
  12. // double up the pins with the touch screen (see the TFT paint example).
  13. #define LCD_CS A3 // Chip Select goes to Analog 3
  14. #define LCD_CD A2 // Command/Data goes to Analog 2
  15. #define LCD_WR A1 // LCD Write goes to Analog 1
  16. #define LCD_RD A0 // LCD Read goes to Analog 0
  17.  
  18. #define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
  19.  
  20. // When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
  21. // For the Arduino Uno, Duemilanove, Diecimila, etc.:
  22. //   D0 connects to digital pin 8  (Notice these are
  23. //   D1 connects to digital pin 9   NOT in order!)
  24. //   D2 connects to digital pin 2
  25. //   D3 connects to digital pin 3
  26. //   D4 connects to digital pin 4
  27. //   D5 connects to digital pin 5
  28. //   D6 connects to digital pin 6
  29. //   D7 connects to digital pin 7
  30. // For the Arduino Mega, use digital pins 22 through 29
  31. // (on the 2-row header at the end of the board).
  32.  
  33. // Assign human-readable names to some common 16-bit color values:
  34. #define BLACK   0x0000
  35. #define BLUE    0x001F
  36. #define RED     0xF800
  37. #define GREEN   0x07E0
  38. #define CYAN    0x07FF
  39. #define MAGENTA 0xF81F
  40. #define YELLOW  0xFFE0
  41. #define WHITE   0xFFFF
  42.  
  43. // Color definitions
  44. #define ILI9341_BLACK       0x0000      /*   0,   0,   0 */
  45. #define ILI9341_NAVY        0x000F      /*   0,   0, 128 */
  46. #define ILI9341_DARKGREEN   0x03E0      /*   0, 128,   0 */
  47. #define ILI9341_DARKCYAN    0x03EF      /*   0, 128, 128 */
  48. #define ILI9341_MAROON      0x7800      /* 128,   0,   0 */
  49. #define ILI9341_PURPLE      0x780F      /* 128,   0, 128 */
  50. #define ILI9341_OLIVE       0x7BE0      /* 128, 128,   0 */
  51. #define ILI9341_LIGHTGREY   0xC618      /* 192, 192, 192 */
  52. #define ILI9341_DARKGREY    0x7BEF      /* 128, 128, 128 */
  53. #define ILI9341_BLUE        0x001F      /*   0,   0, 255 */
  54. #define ILI9341_GREEN       0x07E0      /*   0, 255,   0 */
  55. #define ILI9341_CYAN        0x07FF      /*   0, 255, 255 */
  56. #define ILI9341_RED         0xF800      /* 255,   0,   0 */
  57. #define ILI9341_MAGENTA     0xF81F      /* 255,   0, 255 */
  58. #define ILI9341_YELLOW      0xFFE0      /* 255, 255,   0 */
  59. #define ILI9341_WHITE       0xFFFF      /* 255, 255, 255 */
  60. #define ILI9341_ORANGE      0xFD20      /* 255, 165,   0 */
  61. #define ILI9341_GREENYELLOW 0xAFE5      /* 173, 255,  47 */
  62. #define ILI9341_PINK        0xF81F
  63.  
  64. /******************* UI details */
  65. #define BUTTON_X 80
  66. #define BUTTON_Y 40
  67. #define BUTTON_W 150
  68. #define BUTTON_H 55
  69. #define BUTTON_SPACING_X 10
  70. #define BUTTON_SPACING_Y 25
  71. #define BUTTON_TEXTSIZE 2
  72. #define BUTTON_TEXT_LEN 15
  73.  
  74. #define YP A2  // must be an analog pin, use "An" notation!
  75. #define XM A3  // must be an analog pin, use "An" notation!
  76. #define YM 8   // can be a digital pin
  77. #define XP 9   // can be a digital pin
  78.  
  79. #define TS_MINY 100                        
  80. #define TS_MAXY 900
  81.  
  82. #define TS_MINX 68
  83. #define TS_MAXX 945
  84.  
  85.  
  86.  
  87.  
  88. Adafruit_GFX_Button buttons[12];
  89. /* create 12 buttons, in classic candybar phone style */
  90. char buttonlabels[12][12] = {
  91. "Lagoon","Touchdown","Hurricane",
  92. "M.Sunrise", "Zombie", "Baha.Mama",
  93. "Pla.Punch", "S.o.Beach", "Paradise",
  94. "Cocoskiss","BlueNight","Gin 2cl"};
  95.  
  96.  
  97. uint16_t buttoncolors[12] = {
  98. ILI9341_BLACK, ILI9341_BLACK, ILI9341_BLACK,
  99. ILI9341_BLACK, ILI9341_BLACK, ILI9341_BLACK,
  100. ILI9341_BLACK, ILI9341_BLACK, ILI9341_BLACK,
  101. ILI9341_BLACK, ILI9341_BLACK, ILI9341_BLACK};
  102.  
  103.                              
  104.                              
  105. #include <MCUFRIEND_kbv.h>
  106. MCUFRIEND_kbv tft;
  107. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  108. // If using the shield, all control and data lines are fixed, and
  109. // a simpler declaration can optionally be used:
  110. // Adafruit_TFTLCD tft;
  111.  
  112. void setup(void) {
  113.   Serial.begin(9600);
  114.   Serial.println(F("TFT LCD test"));
  115.  
  116.   tft.reset();
  117.  
  118.   uint16_t identifier = tft.readID();
  119.   if(identifier == 0x9325) {
  120.     Serial.println(F("Found ILI9325 LCD driver"));
  121.   } else if(identifier == 0x9328) {
  122.     Serial.println(F("Found ILI9328 LCD driver"));
  123.   } else if(identifier == 0x4535) {
  124.     Serial.println(F("Found LGDP4535 LCD driver"));
  125.   }else if(identifier == 0x7575) {
  126.     Serial.println(F("Found HX8347G LCD driver"));
  127.   } else if(identifier == 0x9341) {
  128.     Serial.println(F("Found ILI9341 LCD driver"));
  129.   }else if(identifier == 0x7783) {
  130.     Serial.println(F("Found ST7781 LCD driver"));
  131.   }else if(identifier == 0x8230) {
  132.     Serial.println(F("Found UC8230 LCD driver"));  
  133.   }
  134.   else if(identifier == 0x8357) {
  135.     Serial.println(F("Found HX8357D LCD driver"));
  136.   } else if(identifier==0x0101)
  137.   {    
  138.       identifier=0x9341;
  139.        Serial.println(F("Found 0x9341 LCD driver"));
  140.   }else if(identifier==0x9481)
  141.   {    
  142.        Serial.println(F("Found 0x9481 LCD driver"));
  143.   }
  144.   else if(identifier==0x9486)
  145.   {    
  146.        Serial.println(F("Found 0x9486 LCD driver"));
  147.   }
  148.   else {
  149.     Serial.print(F("Unknown LCD driver chip: "));
  150.     Serial.println(identifier, HEX);
  151.     Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
  152.     Serial.println(F("  #define USE_ADAFRUIT_SHIELD_PINOUT"));
  153.     Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
  154.     Serial.println(F("If using the breakout board, it should NOT be #defined!"));
  155.     Serial.println(F("Also if using the breakout, double-check that all wiring"));
  156.     Serial.println(F("matches the tutorial."));
  157.     identifier=0x9486;
  158.    
  159.   }
  160.  
  161.   tft.begin(identifier);
  162.   Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
  163.   tft.setRotation(1);
  164.   tft.fillScreen(BLACK);
  165.  
  166.   // create buttons
  167. for (uint16_t row=0; row<4; row++) {
  168. for (uint16_t col=0; col<3; col++) {
  169.  buttons[col + row*3].initButton(&tft,BUTTON_X+col (BUTTON_W+BUTTON_SPACING_X),
  170. BUTTON_Y+row*(BUTTON_H+BUTTON_SPACING_Y),    // x, y, w,h, outline, fill, text
  171. BUTTON_W, BUTTON_H, ILI9341_GREENYELLOW, buttoncolors[col +row*3], ILI9341_WHITE,
  172. buttonlabels[col + row*3], BUTTON_TEXTSIZE);
  173.       buttons[col + row*3].drawButton();
  174.      
  175.     }
  176.   }
  177.  
  178.  
  179. }
  180. #define MINPRESSURE 10
  181. #define MAXPRESSURE 1000
  182. void loop(void) {
  183.   /*TSPoint p;
  184.   p = ts.getPoint();
  185.   */
  186.   digitalWrite(13, HIGH);
  187.   TSPoint p = ts.getPoint();
  188.   digitalWrite(13, LOW);
  189.  
  190.   // if sharing pins, you'll need to fix the directions of the touchscreen pins
  191.   //pinMode(XP, OUTPUT);
  192.   pinMode(XM, OUTPUT);
  193.   pinMode(YP, OUTPUT);
  194.   //pinMode(YM, OUTPUT);
  195.  
  196.   // we have some minimum pressure we consider 'valid'
  197.   // pressure of 0 means no pressing!
  198.  
  199.  // p = ts.getPoint();
  200.   /*
  201.   if (ts.bufferSize()) {
  202.    
  203.   } else {
  204.     // this is our way of tracking touch 'release'!
  205.     p.x = p.y = p.z = -1;
  206.   }*/
  207.  
  208.   // Scale from ~0->4000 to tft.width using the calibration #'s
  209.   /*
  210.   if (p.z != -1) {
  211.     p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
  212.     p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
  213.     Serial.print("("); Serial.print(p.x); Serial.print(", ");
  214.     Serial.print(p.y); Serial.print(", ");
  215.     Serial.print(p.z); Serial.println(") ");
  216.   }*/
  217.    if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
  218.    {
  219.     p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
  220.     p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
  221.     Serial.print("("); Serial.print(p.x); Serial.print(", ");
  222.     Serial.print(p.y); Serial.print(", ");
  223.     Serial.print(p.z); Serial.println(") ");
  224.  
  225.    }
  226.  
  227.  
  228.  
  229.    
  230.   // go thru all the buttons, checking if they were pressed
  231.   for (uint8_t b=0; b<12; b++) {
  232.     if ((buttons[b].contains(p.x, p.y)))
  233.     {
  234.       Serial.print("Pressing: "); Serial.println(b);
  235.       buttons[b].press(true);  // tell the button it is pressed
  236.     } else {
  237.       buttons[b].press(false);  // tell the button it is NOT pressed
  238.     }
  239.   }
  240.  
  241.   // now we can ask the buttons if their state has changed
  242.   for (uint8_t b=0; b<12; b++) {
  243.     if (buttons[b].justReleased()) {
  244.        Serial.print("Released: "); Serial.println(b);
  245.       buttons[b].drawButton();  // draw normal
  246.     }
  247.    
  248.     if (buttons[b].justPressed()) {
  249.         buttons[b].drawButton(true);  // draw invert!
  250.        
  251.           }
  252.         }
  253.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement