Advertisement
Guest User

TFT Arduino

a guest
Apr 21st, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.83 KB | None | 0 0
  1. // IMPORTANT: TftSpfd5408 LIBRARY MUST BE SPECIFICALLY
  2. // CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
  3. // SEE RELEVANT COMMENTS IN TftSpfd5408.h FOR SETUP.
  4.  
  5. #include <Adafruit_GFX.h>    // Core graphics library
  6. #include <TftSpfd5408.h> // Hardware-specific library
  7.  
  8. // The control pins for the LCD can be assigned to any digital or
  9. // analog pins...but we'll use the analog pins as this allows us to
  10. // double up the pins with the touch screen (see the TFT paint example).
  11. #define LCD_CS A3 // Chip Select goes to Analog 3
  12. #define LCD_CD A2 // Command/Data goes to Analog 2
  13. #define LCD_WR A1 // LCD Write goes to Analog 1
  14. #define LCD_RD A0 // LCD Read goes to Analog 0
  15.  
  16. #define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
  17.  
  18. // When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
  19. // For the Arduino Uno, Duemilanove, Diecimila, etc.:
  20. //   D0 connects to digital pin 8  (Notice these are
  21. //   D1 connects to digital pin 9   NOT in order!)
  22. //   D2 connects to digital pin 2
  23. //   D3 connects to digital pin 3
  24. //   D4 connects to digital pin 4
  25. //   D5 connects to digital pin 5
  26. //   D6 connects to digital pin 6
  27. //   D7 connects to digital pin 7
  28. // For the Arduino Mega, use digital pins 22 through 29
  29. // (on the 2-row header at the end of the board).
  30.  
  31. // Assign human-readable names to some common 16-bit color values:
  32. #define BLACK   0x0000
  33. #define BLUE    0x001F
  34. #define RED     0xF800
  35. #define GREEN   0x07E0
  36. #define CYAN    0x07FF
  37. #define MAGENTA 0xF81F
  38. #define YELLOW  0xFFE0
  39. #define WHITE   0xFFFF
  40.  
  41. TftSpfd5408 tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  42. // If using the shield, all control and data lines are fixed, and
  43. // a simpler declaration can optionally be used:
  44. // TftSpfd5408 tft;
  45.  
  46. void setup(void) {
  47.   Serial.begin(9600);
  48.   Serial.println(F("TFT LCD test"));
  49.  
  50. #ifdef USE_ADAFRUIT_SHIELD_PINOUT
  51.   Serial.println(F("Using Adafruit 2.8\" TFT Arduino Shield Pinout"));
  52. #else
  53.   Serial.println(F("Using Adafruit 2.8\" TFT Breakout Board Pinout"));
  54. #endif
  55.  
  56.   Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
  57.  
  58.   tft.reset();
  59.  
  60.   tft.begin();
  61.  
  62.   Serial.println(F("Benchmark                Time (microseconds)"));
  63.  
  64.   Serial.print(F("Screen fill              "));
  65.   Serial.println(testFillScreen());
  66.   delay(500);
  67.  
  68.   Serial.print(F("Text                     "));
  69.   Serial.println(testText());
  70.   delay(3000);
  71.  
  72.   Serial.print(F("Lines                    "));
  73.   Serial.println(testLines(CYAN));
  74.   delay(500);
  75.  
  76.   Serial.print(F("Horiz/Vert Lines         "));
  77.   Serial.println(testFastLines(RED, BLUE));
  78.   delay(500);
  79.  
  80.   Serial.print(F("Rectangles (outline)     "));
  81.   Serial.println(testRects(GREEN));
  82.   delay(500);
  83.  
  84.   Serial.print(F("Rectangles (filled)      "));
  85.   Serial.println(testFilledRects(YELLOW, MAGENTA));
  86.   delay(500);
  87.  
  88.   Serial.print(F("Circles (filled)         "));
  89.   Serial.println(testFilledCircles(10, MAGENTA));
  90.  
  91.   Serial.print(F("Circles (outline)        "));
  92.   Serial.println(testCircles(10, WHITE));
  93.   delay(500);
  94.  
  95.   Serial.print(F("Triangles (outline)      "));
  96.   Serial.println(testTriangles());
  97.   delay(500);
  98.  
  99.   Serial.print(F("Triangles (filled)       "));
  100.   Serial.println(testFilledTriangles());
  101.   delay(500);
  102.  
  103.   Serial.print(F("Rounded rects (outline)  "));
  104.   Serial.println(testRoundRects());
  105.   delay(500);
  106.  
  107.   Serial.print(F("Rounded rects (filled)   "));
  108.   Serial.println(testFilledRoundRects());
  109.   delay(500);
  110.  
  111.   Serial.println(F("Done!"));
  112. }
  113.  
  114. void loop(void) {
  115.   for(uint8_t rotation=0; rotation<4; rotation++) {
  116.     tft.setRotation(rotation);
  117.     testText();
  118.     delay(2000);
  119.   }
  120. }
  121.  
  122. unsigned long testFillScreen() {
  123.   unsigned long start = micros();
  124.   tft.fillScreen(BLACK);
  125.   tft.fillScreen(RED);
  126.   tft.fillScreen(GREEN);
  127.   tft.fillScreen(BLUE);
  128.   tft.fillScreen(BLACK);
  129.   return micros() - start;
  130. }
  131.  
  132. unsigned long testText() {
  133.   tft.fillScreen(BLACK);
  134.   unsigned long start = micros();
  135.   tft.setCursor(0, 0);
  136.   tft.setTextColor(WHITE);  tft.setTextSize(1);
  137.   tft.println("Hello World!");
  138.   tft.setTextColor(YELLOW); tft.setTextSize(2);
  139.   tft.println(1234.56);
  140.   tft.setTextColor(RED);    tft.setTextSize(3);
  141.   tft.println(0xDEADBEEF, HEX);
  142.   tft.println();
  143.   tft.setTextColor(GREEN);
  144.   tft.setTextSize(5);
  145.   tft.println("Groop");
  146.   tft.setTextSize(2);
  147.   tft.println("I implore thee,");
  148.   tft.setTextSize(1);
  149.   tft.println("my foonting turlingdromes.");
  150.   tft.println("And hooptiously drangle me");
  151.   tft.println("with crinkly bindlewurdles,");
  152.   tft.println("Or I will rend thee");
  153.   tft.println("in the gobberwarts");
  154.   tft.println("with my blurglecruncheon,");
  155.   tft.println("see if I don't!");
  156.   return micros() - start;
  157. }
  158.  
  159. unsigned long testLines(uint16_t color) {
  160.   unsigned long start, t;
  161.   int           x1, y1, x2, y2,
  162.                 w = tft.width(),
  163.                 h = tft.height();
  164.  
  165.   tft.fillScreen(BLACK);
  166.  
  167.   x1 = y1 = 0;
  168.   y2    = h - 1;
  169.   start = micros();
  170.   for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  171.   x2    = w - 1;
  172.   for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  173.   t     = micros() - start; // fillScreen doesn't count against timing
  174.  
  175.   tft.fillScreen(BLACK);
  176.  
  177.   x1    = w - 1;
  178.   y1    = 0;
  179.   y2    = h - 1;
  180.   start = micros();
  181.   for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  182.   x2    = 0;
  183.   for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  184.   t    += micros() - start;
  185.  
  186.   tft.fillScreen(BLACK);
  187.  
  188.   x1    = 0;
  189.   y1    = h - 1;
  190.   y2    = 0;
  191.   start = micros();
  192.   for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  193.   x2    = w - 1;
  194.   for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  195.   t    += micros() - start;
  196.  
  197.   tft.fillScreen(BLACK);
  198.  
  199.   x1    = w - 1;
  200.   y1    = h - 1;
  201.   y2    = 0;
  202.   start = micros();
  203.   for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  204.   x2    = 0;
  205.   for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  206.  
  207.   return micros() - start;
  208. }
  209.  
  210. unsigned long testFastLines(uint16_t color1, uint16_t color2) {
  211.   unsigned long start;
  212.   int           x, y, w = tft.width(), h = tft.height();
  213.  
  214.   tft.fillScreen(BLACK);
  215.   start = micros();
  216.   for(y=0; y<h; y+=5) tft.drawFastHLine(0, y, w, color1);
  217.   for(x=0; x<w; x+=5) tft.drawFastVLine(x, 0, h, color2);
  218.  
  219.   return micros() - start;
  220. }
  221.  
  222. unsigned long testRects(uint16_t color) {
  223.   unsigned long start;
  224.   int           n, i, i2,
  225.                 cx = tft.width()  / 2,
  226.                 cy = tft.height() / 2;
  227.  
  228.   tft.fillScreen(BLACK);
  229.   n     = min(tft.width(), tft.height());
  230.   start = micros();
  231.   for(i=2; i<n; i+=6) {
  232.     i2 = i / 2;
  233.     tft.drawRect(cx-i2, cy-i2, i, i, color);
  234.   }
  235.  
  236.   return micros() - start;
  237. }
  238.  
  239. unsigned long testFilledRects(uint16_t color1, uint16_t color2) {
  240.   unsigned long start, t = 0;
  241.   int           n, i, i2,
  242.                 cx = tft.width()  / 2 - 1,
  243.                 cy = tft.height() / 2 - 1;
  244.  
  245.   tft.fillScreen(BLACK);
  246.   n = min(tft.width(), tft.height());
  247.   for(i=n; i>0; i-=6) {
  248.     i2    = i / 2;
  249.     start = micros();
  250.     tft.fillRect(cx-i2, cy-i2, i, i, color1);
  251.     t    += micros() - start;
  252.     // Outlines are not included in timing results
  253.     tft.drawRect(cx-i2, cy-i2, i, i, color2);
  254.   }
  255.  
  256.   return t;
  257. }
  258.  
  259. unsigned long testFilledCircles(uint8_t radius, uint16_t color) {
  260.   unsigned long start;
  261.   int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;
  262.  
  263.   tft.fillScreen(BLACK);
  264.   start = micros();
  265.   for(x=radius; x<w; x+=r2) {
  266.     for(y=radius; y<h; y+=r2) {
  267.       tft.fillCircle(x, y, radius, color);
  268.     }
  269.   }
  270.  
  271.   return micros() - start;
  272. }
  273.  
  274. unsigned long testCircles(uint8_t radius, uint16_t color) {
  275.   unsigned long start;
  276.   int           x, y, r2 = radius * 2,
  277.                 w = tft.width()  + radius,
  278.                 h = tft.height() + radius;
  279.  
  280.   // Screen is not cleared for this one -- this is
  281.   // intentional and does not affect the reported time.
  282.   start = micros();
  283.   for(x=0; x<w; x+=r2) {
  284.     for(y=0; y<h; y+=r2) {
  285.       tft.drawCircle(x, y, radius, color);
  286.     }
  287.   }
  288.  
  289.   return micros() - start;
  290. }
  291.  
  292. unsigned long testTriangles() {
  293.   unsigned long start;
  294.   int           n, i, cx = tft.width()  / 2 - 1,
  295.                       cy = tft.height() / 2 - 1;
  296.  
  297.   tft.fillScreen(BLACK);
  298.   n     = min(cx, cy);
  299.   start = micros();
  300.   for(i=0; i<n; i+=5) {
  301.     tft.drawTriangle(
  302.       cx    , cy - i, // peak
  303.       cx - i, cy + i, // bottom left
  304.       cx + i, cy + i, // bottom right
  305.       tft.color565(0, 0, i));
  306.   }
  307.  
  308.   return micros() - start;
  309. }
  310.  
  311. unsigned long testFilledTriangles() {
  312.   unsigned long start, t = 0;
  313.   int           i, cx = tft.width()  / 2 - 1,
  314.                    cy = tft.height() / 2 - 1;
  315.  
  316.   tft.fillScreen(BLACK);
  317.   start = micros();
  318.   for(i=min(cx,cy); i>10; i-=5) {
  319.     start = micros();
  320.     tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
  321.       tft.color565(0, i, i));
  322.     t += micros() - start;
  323.     tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
  324.       tft.color565(i, i, 0));
  325.   }
  326.  
  327.   return t;
  328. }
  329.  
  330. unsigned long testRoundRects() {
  331.   unsigned long start;
  332.   int           w, i, i2,
  333.                 cx = tft.width()  / 2 - 1,
  334.                 cy = tft.height() / 2 - 1;
  335.  
  336.   tft.fillScreen(BLACK);
  337.   w     = min(tft.width(), tft.height());
  338.   start = micros();
  339.   for(i=0; i<w; i+=6) {
  340.     i2 = i / 2;
  341.     tft.drawRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(i, 0, 0));
  342.   }
  343.  
  344.   return micros() - start;
  345. }
  346.  
  347. unsigned long testFilledRoundRects() {
  348.   unsigned long start;
  349.   int           i, i2,
  350.                 cx = tft.width()  / 2 - 1,
  351.                 cy = tft.height() / 2 - 1;
  352.  
  353.   tft.fillScreen(BLACK);
  354.   start = micros();
  355.   for(i=min(tft.width(), tft.height()); i>20; i-=6) {
  356.     i2 = i / 2;
  357.     tft.fillRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(0, i, 0));
  358.   }
  359.  
  360.   return micros() - start;
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement