Advertisement
Pouknouki

Adafruit Screen Code

Dec 20th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.38 KB | None | 0 0
  1. /***************************************************
  2.   This is an example sketch for the Adafruit 2.2" SPI display.
  3.   This library works with the Adafruit 2.2" TFT Breakout w/SD card
  4.   ----> http://www.adafruit.com/products/1480
  5.  
  6.   Check out the links above for our tutorials and wiring diagrams
  7.   These displays use SPI to communicate, 4 or 5 pins are required to
  8.   interface (RST is optional)
  9.   Adafruit invests time and resources providing this open source code,
  10.   please support Adafruit and open-source hardware by purchasing
  11.   products from Adafruit!
  12.  
  13.   Written by Limor Fried/Ladyada for Adafruit Industries.
  14.   MIT license, all text above must be included in any redistribution
  15.  ****************************************************/
  16.  
  17. #include "SPI.h"
  18. #include "Adafruit_GFX.h"
  19. #include "Adafruit_ILI9340.h"
  20.  
  21. #if defined(__SAM3X8E__)
  22.     #undef __FlashStringHelper::F(string_literal)
  23.     #define F(string_literal) string_literal
  24. #endif
  25.  
  26. // These are the pins used for the UNO
  27. // for Due/Mega/Leonardo use the hardware SPI pins (which are different)
  28. #define _sclk 3
  29. #define _miso 1
  30. #define _mosi 4
  31. #define _cs 10
  32. #define _rst 5
  33. #define _dc 2
  34.  
  35. // Using software SPI is really not suggested, its incredibly slow
  36. //Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _mosi, _sclk, _rst, _miso);
  37. // Use hardware SPI
  38. Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _rst);
  39.  
  40. void setup() {
  41.   Serial.begin(9600);
  42.   Serial.println("Adafruit 2.2\" SPI TFT Test!");
  43.  
  44.   tft.begin();
  45.  
  46.   Serial.println(F("Benchmark                Time (microseconds)"));
  47.   Serial.print(F("Screen fill              "));
  48.   Serial.println(testFillScreen());
  49.   delay(500);
  50.  
  51.   Serial.print(F("Text                     "));
  52.   Serial.println(testText());
  53.   delay(3000);
  54.  
  55.   Serial.print(F("Lines                    "));
  56.   Serial.println(testLines(ILI9340_CYAN));
  57.   delay(500);
  58.  
  59.   Serial.print(F("Horiz/Vert Lines         "));
  60.   Serial.println(testFastLines(ILI9340_RED, ILI9340_BLUE));
  61.   delay(500);
  62.  
  63.   Serial.print(F("Rectangles (outline)     "));
  64.   Serial.println(testRects(ILI9340_GREEN));
  65.   delay(500);
  66.  
  67.   Serial.print(F("Rectangles (filled)      "));
  68.   Serial.println(testFilledRects(ILI9340_YELLOW, ILI9340_MAGENTA));
  69.   delay(500);
  70.  
  71.   Serial.print(F("Circles (filled)         "));
  72.   Serial.println(testFilledCircles(10, ILI9340_MAGENTA));
  73.  
  74.   Serial.print(F("Circles (outline)        "));
  75.   Serial.println(testCircles(10, ILI9340_WHITE));
  76.   delay(500);
  77.  
  78.   Serial.print(F("Triangles (outline)      "));
  79.   Serial.println(testTriangles());
  80.   delay(500);
  81.  
  82.   Serial.print(F("Triangles (filled)       "));
  83.   Serial.println(testFilledTriangles());
  84.   delay(500);
  85.  
  86.   Serial.print(F("Rounded rects (outline)  "));
  87.   Serial.println(testRoundRects());
  88.   delay(500);
  89.  
  90.   Serial.print(F("Rounded rects (filled)   "));
  91.   Serial.println(testFilledRoundRects());
  92.   delay(500);
  93.  
  94.   Serial.println(F("Done!"));
  95. }
  96.  
  97. void loop(void) {
  98.   for(uint8_t rotation=0; rotation<4; rotation++) {
  99.     tft.setRotation(rotation);
  100.     testText();
  101.     delay(2000);
  102.   }
  103. }
  104.  
  105.  
  106. unsigned long testFillScreen() {
  107.   unsigned long start = micros();
  108.   tft.fillScreen(ILI9340_BLACK);
  109.   tft.fillScreen(ILI9340_RED);
  110.   tft.fillScreen(ILI9340_GREEN);
  111.   tft.fillScreen(ILI9340_BLUE);
  112.   tft.fillScreen(ILI9340_BLACK);
  113.   return micros() - start;
  114. }
  115.  
  116. unsigned long testText() {
  117.   tft.fillScreen(ILI9340_BLACK);
  118.   unsigned long start = micros();
  119.   tft.setCursor(0, 0);
  120.   tft.setTextColor(ILI9340_WHITE);  tft.setTextSize(1);
  121.   tft.println("Hello World!");
  122.   tft.setTextColor(ILI9340_YELLOW); tft.setTextSize(2);
  123.   tft.println(1234.56);
  124.   tft.setTextColor(ILI9340_RED);    tft.setTextSize(3);
  125.   tft.println(0xDEADBEEF, HEX);
  126.   tft.println();
  127.   tft.setTextColor(ILI9340_GREEN);
  128.   tft.setTextSize(5);
  129.   tft.println("Groop");
  130.   tft.setTextSize(2);
  131.   tft.println("I implore thee,");
  132.   tft.setTextSize(1);
  133.   tft.println("my foonting turlingdromes.");
  134.   tft.println("And hooptiously drangle me");
  135.   tft.println("with crinkly bindlewurdles,");
  136.   tft.println("Or I will rend thee");
  137.   tft.println("in the gobberwarts");
  138.   tft.println("with my blurglecruncheon,");
  139.   tft.println("see if I don't!");
  140.   return micros() - start;
  141. }
  142.  
  143. unsigned long testLines(uint16_t color) {
  144.   unsigned long start, t;
  145.   int           x1, y1, x2, y2,
  146.                 w = tft.width(),
  147.                 h = tft.height();
  148.  
  149.   tft.fillScreen(ILI9340_BLACK);
  150.  
  151.   x1 = y1 = 0;
  152.   y2    = h - 1;
  153.   start = micros();
  154.   for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  155.   x2    = w - 1;
  156.   for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  157.   t     = micros() - start; // fillScreen doesn't count against timing
  158.  
  159.   tft.fillScreen(ILI9340_BLACK);
  160.  
  161.   x1    = w - 1;
  162.   y1    = 0;
  163.   y2    = h - 1;
  164.   start = micros();
  165.   for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  166.   x2    = 0;
  167.   for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  168.   t    += micros() - start;
  169.  
  170.   tft.fillScreen(ILI9340_BLACK);
  171.  
  172.   x1    = 0;
  173.   y1    = h - 1;
  174.   y2    = 0;
  175.   start = micros();
  176.   for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  177.   x2    = w - 1;
  178.   for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  179.   t    += micros() - start;
  180.  
  181.   tft.fillScreen(ILI9340_BLACK);
  182.  
  183.   x1    = w - 1;
  184.   y1    = h - 1;
  185.   y2    = 0;
  186.   start = micros();
  187.   for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  188.   x2    = 0;
  189.   for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  190.  
  191.   return micros() - start;
  192. }
  193.  
  194. unsigned long testFastLines(uint16_t color1, uint16_t color2) {
  195.   unsigned long start;
  196.   int           x, y, w = tft.width(), h = tft.height();
  197.  
  198.   tft.fillScreen(ILI9340_BLACK);
  199.   start = micros();
  200.   for(y=0; y<h; y+=5) tft.drawFastHLine(0, y, w, color1);
  201.   for(x=0; x<w; x+=5) tft.drawFastVLine(x, 0, h, color2);
  202.  
  203.   return micros() - start;
  204. }
  205.  
  206. unsigned long testRects(uint16_t color) {
  207.   unsigned long start;
  208.   int           n, i, i2,
  209.                 cx = tft.width()  / 2,
  210.                 cy = tft.height() / 2;
  211.  
  212.   tft.fillScreen(ILI9340_BLACK);
  213.   n     = min(tft.width(), tft.height());
  214.   start = micros();
  215.   for(i=2; i<n; i+=6) {
  216.     i2 = i / 2;
  217.     tft.drawRect(cx-i2, cy-i2, i, i, color);
  218.   }
  219.  
  220.   return micros() - start;
  221. }
  222.  
  223. unsigned long testFilledRects(uint16_t color1, uint16_t color2) {
  224.   unsigned long start, t = 0;
  225.   int           n, i, i2,
  226.                 cx = tft.width()  / 2 - 1,
  227.                 cy = tft.height() / 2 - 1;
  228.  
  229.   tft.fillScreen(ILI9340_BLACK);
  230.   n = min(tft.width(), tft.height());
  231.   for(i=n; i>0; i-=6) {
  232.     i2    = i / 2;
  233.     start = micros();
  234.     tft.fillRect(cx-i2, cy-i2, i, i, color1);
  235.     t    += micros() - start;
  236.     // Outlines are not included in timing results
  237.     tft.drawRect(cx-i2, cy-i2, i, i, color2);
  238.   }
  239.  
  240.   return t;
  241. }
  242.  
  243. unsigned long testFilledCircles(uint8_t radius, uint16_t color) {
  244.   unsigned long start;
  245.   int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;
  246.  
  247.   tft.fillScreen(ILI9340_BLACK);
  248.   start = micros();
  249.   for(x=radius; x<w; x+=r2) {
  250.     for(y=radius; y<h; y+=r2) {
  251.       tft.fillCircle(x, y, radius, color);
  252.     }
  253.   }
  254.  
  255.   return micros() - start;
  256. }
  257.  
  258. unsigned long testCircles(uint8_t radius, uint16_t color) {
  259.   unsigned long start;
  260.   int           x, y, r2 = radius * 2,
  261.                 w = tft.width()  + radius,
  262.                 h = tft.height() + radius;
  263.  
  264.   // Screen is not cleared for this one -- this is
  265.   // intentional and does not affect the reported time.
  266.   start = micros();
  267.   for(x=0; x<w; x+=r2) {
  268.     for(y=0; y<h; y+=r2) {
  269.       tft.drawCircle(x, y, radius, color);
  270.     }
  271.   }
  272.  
  273.   return micros() - start;
  274. }
  275.  
  276. unsigned long testTriangles() {
  277.   unsigned long start;
  278.   int           n, i, cx = tft.width()  / 2 - 1,
  279.                       cy = tft.height() / 2 - 1;
  280.  
  281.   tft.fillScreen(ILI9340_BLACK);
  282.   n     = min(cx, cy);
  283.   start = micros();
  284.   for(i=0; i<n; i+=5) {
  285.     tft.drawTriangle(
  286.       cx    , cy - i, // peak
  287.       cx - i, cy + i, // bottom left
  288.       cx + i, cy + i, // bottom right
  289.       tft.Color565(0, 0, i));
  290.   }
  291.  
  292.   return micros() - start;
  293. }
  294.  
  295. unsigned long testFilledTriangles() {
  296.   unsigned long start, t = 0;
  297.   int           i, cx = tft.width()  / 2 - 1,
  298.                    cy = tft.height() / 2 - 1;
  299.  
  300.   tft.fillScreen(ILI9340_BLACK);
  301.   start = micros();
  302.   for(i=min(cx,cy); i>10; i-=5) {
  303.     start = micros();
  304.     tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
  305.       tft.Color565(0, i, i));
  306.     t += micros() - start;
  307.     tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
  308.       tft.Color565(i, i, 0));
  309.   }
  310.  
  311.   return t;
  312. }
  313.  
  314. unsigned long testRoundRects() {
  315.   unsigned long start;
  316.   int           w, i, i2,
  317.                 cx = tft.width()  / 2 - 1,
  318.                 cy = tft.height() / 2 - 1;
  319.  
  320.   tft.fillScreen(ILI9340_BLACK);
  321.   w     = min(tft.width(), tft.height());
  322.   start = micros();
  323.   for(i=0; i<w; i+=6) {
  324.     i2 = i / 2;
  325.     tft.drawRoundRect(cx-i2, cy-i2, i, i, i/8, tft.Color565(i, 0, 0));
  326.   }
  327.  
  328.   return micros() - start;
  329. }
  330.  
  331. unsigned long testFilledRoundRects() {
  332.   unsigned long start;
  333.   int           i, i2,
  334.                 cx = tft.width()  / 2 - 1,
  335.                 cy = tft.height() / 2 - 1;
  336.  
  337.   tft.fillScreen(ILI9340_BLACK);
  338.   start = micros();
  339.   for(i=min(tft.width(), tft.height()); i>20; i-=6) {
  340.     i2 = i / 2;
  341.     tft.fillRoundRect(cx-i2, cy-i2, i, i, i/8, tft.Color565(0, i, 0));
  342.   }
  343.  
  344.   return micros() - start;
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement