Advertisement
Guest User

Raspberry Pi Pico Arduino Adafruit_ILI9341 example graphics_test

a guest
May 18th, 2021
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.13 KB | None | 0 0
  1. /*
  2.     require
  3.         https://github.com/adafruit/Adafruit-GFX-Library
  4.         https://github.com/adafruit/Adafruit_ILI9341
  5. */
  6. /***************************************************
  7.   This is our GFX example for the Adafruit ILI9341 Breakout and Shield
  8.   ----> http://www.adafruit.com/products/1651
  9.  
  10.   Check out the links above for our tutorials and wiring diagrams
  11.   These displays use SPI to communicate, 4 or 5 pins are required to
  12.   interface (RST is optional)
  13.   Adafruit invests time and resources providing this open source code,
  14.   please support Adafruit and open-source hardware by purchasing
  15.   products from Adafruit!
  16.  
  17.   Written by Limor Fried/Ladyada for Adafruit Industries.
  18.   MIT license, all text above must be included in any redistribution
  19.  ****************************************************/
  20. #include "SPI.h"
  21. #include "Adafruit_GFX.h"
  22. #include "Adafruit_ILI9341.h"
  23.  
  24.  
  25. // For the Adafruit shield, these are the default.
  26. //#define TFT_DC    9
  27. //#define TFT_RST  14
  28. //#define TFT_CS   10
  29. //#define TFT_MOSI 11
  30. //#define TFT_MISO 12
  31. //#define TFT_CLK  13
  32.  
  33. #define TFT_DC   20  // (GP20ピン番号26)
  34. #define TFT_RST  22  // (GP22ピン番号29)
  35. #define TFT_CS   17  // (GP17ピン番号22)
  36. #define TFT_MOSI 19  // (GP19ピン番号25)
  37. #define TFT_MISO 16  // (GP16ピン番号21)
  38. #define TFT_CLK  18  // (GP18ピン番号24)
  39.  
  40.  
  41. // SPIがMISO=GP4,MOSI=GP3,SCK=GP2,CS=GP5固定なので新しくSPIを作成
  42. arduino::MbedSPI SPI2(digitalPinToPinName(TFT_MISO), digitalPinToPinName(TFT_MOSI), digitalPinToPinName(TFT_CLK));
  43.  
  44. // Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
  45. Adafruit_ILI9341 tft = Adafruit_ILI9341(&SPI2, TFT_DC, TFT_CS, TFT_RST);
  46. // If using the breakout, change pins as desired
  47. //Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
  48.  
  49. void setup() {
  50.   Serial.begin(115200);
  51.   Serial.println("ILI9341 Test!");
  52.  
  53.   // 2MHz以上に設定してもPicoのmbedのSPIの機能で2MHzに制限されてる模様
  54.   tft.begin(40000000);
  55.  
  56.   tft.fillScreen(ILI9341_BLACK);
  57.   for (int i = 10; i >= 0; i--) {
  58.     tft.setCursor(100, 100);
  59.     tft.setTextColor(ILI9341_GREEN);
  60.     tft.setTextSize(5);
  61.     tft.println(i);
  62.     delay(1000);
  63.     tft.fillScreen(ILI9341_BLACK);
  64.   }
  65.   tft.setTextSize(1);
  66. }
  67.  
  68. void loop(void) {
  69.  
  70.   tft.fillScreen(ILI9341_BLACK);
  71.   loop01();
  72.   for (int i = 0; i < 5; i++) {
  73.     loop02();
  74.   }
  75. }
  76.  
  77. void loop01(void) {
  78.  
  79.   // read diagnostics (optional but can help debug problems)
  80.   uint8_t x = tft.readcommand8(ILI9341_RDMODE);
  81.   Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
  82.   x = tft.readcommand8(ILI9341_RDMADCTL);
  83.   Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
  84.   x = tft.readcommand8(ILI9341_RDPIXFMT);
  85.   Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
  86.   x = tft.readcommand8(ILI9341_RDIMGFMT);
  87.   Serial.print("Image Format: 0x"); Serial.println(x, HEX);
  88.   x = tft.readcommand8(ILI9341_RDSELFDIAG);
  89.   Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
  90.  
  91.   Serial.println(F("Benchmark                Time (microseconds)"));
  92.   delay(10);
  93.   Serial.print(F("Screen fill              "));
  94.   Serial.println(testFillScreen());
  95.   delay(500);
  96.  
  97.   Serial.print(F("Text                     "));
  98.   Serial.println(testText());
  99.   delay(3000);
  100.  
  101.   Serial.print(F("Lines                    "));
  102.   Serial.println(testLines(ILI9341_CYAN));
  103.   delay(500);
  104.  
  105.   Serial.print(F("Horiz/Vert Lines         "));
  106.   Serial.println(testFastLines(ILI9341_RED, ILI9341_BLUE));
  107.   delay(500);
  108.  
  109.   Serial.print(F("Rectangles (outline)     "));
  110.   Serial.println(testRects(ILI9341_GREEN));
  111.   delay(500);
  112.  
  113.   Serial.print(F("Rectangles (filled)      "));
  114.   Serial.println(testFilledRects(ILI9341_YELLOW, ILI9341_MAGENTA));
  115.   delay(500);
  116.  
  117.   Serial.print(F("Circles (filled)         "));
  118.   Serial.println(testFilledCircles(10, ILI9341_MAGENTA));
  119.  
  120.   Serial.print(F("Circles (outline)        "));
  121.   Serial.println(testCircles(10, ILI9341_WHITE));
  122.   delay(500);
  123.  
  124.   Serial.print(F("Triangles (outline)      "));
  125.   Serial.println(testTriangles());
  126.   delay(500);
  127.  
  128.   Serial.print(F("Triangles (filled)       "));
  129.   Serial.println(testFilledTriangles());
  130.   delay(500);
  131.  
  132.   Serial.print(F("Rounded rects (outline)  "));
  133.   Serial.println(testRoundRects());
  134.   delay(500);
  135.  
  136.   Serial.print(F("Rounded rects (filled)   "));
  137.   Serial.println(testFilledRoundRects());
  138.   delay(500);
  139.  
  140.   Serial.println(F("Done!"));
  141.  
  142. }
  143.  
  144.  
  145. void loop02(void) {
  146.   for(uint8_t rotation=0; rotation<4; rotation++) {
  147.     tft.setRotation(rotation);
  148.     testText();
  149.     delay(1000);
  150.   }
  151. }
  152.  
  153. unsigned long testFillScreen() {
  154.   unsigned long start = micros();
  155.   tft.fillScreen(ILI9341_BLACK);
  156.   yield();
  157.   tft.fillScreen(ILI9341_RED);
  158.   yield();
  159.   tft.fillScreen(ILI9341_GREEN);
  160.   yield();
  161.   tft.fillScreen(ILI9341_BLUE);
  162.   yield();
  163.   tft.fillScreen(ILI9341_BLACK);
  164.   yield();
  165.   return micros() - start;
  166. }
  167.  
  168. unsigned long testText() {
  169.   tft.fillScreen(ILI9341_BLACK);
  170.   unsigned long start = micros();
  171.   tft.setCursor(0, 0);
  172.   tft.setTextColor(ILI9341_WHITE);  tft.setTextSize(1);
  173.   tft.println("Hello World!");
  174.   tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(2);
  175.   tft.println(1234.56);
  176.   tft.setTextColor(ILI9341_RED);    tft.setTextSize(3);
  177.   tft.println(0xDEADBEEF, HEX);
  178.   tft.println();
  179.   tft.setTextColor(ILI9341_GREEN);
  180.   tft.setTextSize(5);
  181.   tft.println("Groop");
  182.   tft.setTextSize(2);
  183.   tft.println("I implore thee,");
  184.   tft.setTextSize(1);
  185.   tft.println("my foonting turlingdromes.");
  186.   tft.println("And hooptiously drangle me");
  187.   tft.println("with crinkly bindlewurdles,");
  188.   tft.println("Or I will rend thee");
  189.   tft.println("in the gobberwarts");
  190.   tft.println("with my blurglecruncheon,");
  191.   tft.println("see if I don't!");
  192.   return micros() - start;
  193. }
  194.  
  195. unsigned long testLines(uint16_t color) {
  196.   unsigned long start, t;
  197.   int           x1, y1, x2, y2,
  198.                 w = tft.width(),
  199.                 h = tft.height();
  200.  
  201.   tft.fillScreen(ILI9341_BLACK);
  202.   yield();
  203.  
  204.   x1 = y1 = 0;
  205.   y2    = h - 1;
  206.   start = micros();
  207.   for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  208.   x2    = w - 1;
  209.   for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  210.   t     = micros() - start; // fillScreen doesn't count against timing
  211.  
  212.   yield();
  213.   tft.fillScreen(ILI9341_BLACK);
  214.   yield();
  215.  
  216.   x1    = w - 1;
  217.   y1    = 0;
  218.   y2    = h - 1;
  219.   start = micros();
  220.   for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  221.   x2    = 0;
  222.   for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  223.   t    += micros() - start;
  224.  
  225.   yield();
  226.   tft.fillScreen(ILI9341_BLACK);
  227.   yield();
  228.  
  229.   x1    = 0;
  230.   y1    = h - 1;
  231.   y2    = 0;
  232.   start = micros();
  233.   for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  234.   x2    = w - 1;
  235.   for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  236.   t    += micros() - start;
  237.  
  238.   yield();
  239.   tft.fillScreen(ILI9341_BLACK);
  240.   yield();
  241.  
  242.   x1    = w - 1;
  243.   y1    = h - 1;
  244.   y2    = 0;
  245.   start = micros();
  246.   for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
  247.   x2    = 0;
  248.   for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
  249.  
  250.   yield();
  251.   return micros() - start;
  252. }
  253.  
  254. unsigned long testFastLines(uint16_t color1, uint16_t color2) {
  255.   unsigned long start;
  256.   int           x, y, w = tft.width(), h = tft.height();
  257.  
  258.   tft.fillScreen(ILI9341_BLACK);
  259.   start = micros();
  260.   for(y=0; y<h; y+=5) tft.drawFastHLine(0, y, w, color1);
  261.   for(x=0; x<w; x+=5) tft.drawFastVLine(x, 0, h, color2);
  262.  
  263.   return micros() - start;
  264. }
  265.  
  266. unsigned long testRects(uint16_t color) {
  267.   unsigned long start;
  268.   int           n, i, i2,
  269.                 cx = tft.width()  / 2,
  270.                 cy = tft.height() / 2;
  271.  
  272.   tft.fillScreen(ILI9341_BLACK);
  273.   n     = min(tft.width(), tft.height());
  274.   start = micros();
  275.   for(i=2; i<n; i+=6) {
  276.     i2 = i / 2;
  277.     tft.drawRect(cx-i2, cy-i2, i, i, color);
  278.   }
  279.  
  280.   return micros() - start;
  281. }
  282.  
  283. unsigned long testFilledRects(uint16_t color1, uint16_t color2) {
  284.   unsigned long start, t = 0;
  285.   int           n, i, i2,
  286.                 cx = tft.width()  / 2 - 1,
  287.                 cy = tft.height() / 2 - 1;
  288.  
  289.   tft.fillScreen(ILI9341_BLACK);
  290.   n = min(tft.width(), tft.height());
  291.   for(i=n; i>0; i-=6) {
  292.     i2    = i / 2;
  293.     start = micros();
  294.     tft.fillRect(cx-i2, cy-i2, i, i, color1);
  295.     t    += micros() - start;
  296.     // Outlines are not included in timing results
  297.     tft.drawRect(cx-i2, cy-i2, i, i, color2);
  298.     yield();
  299.   }
  300.  
  301.   return t;
  302. }
  303.  
  304. unsigned long testFilledCircles(uint8_t radius, uint16_t color) {
  305.   unsigned long start;
  306.   int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;
  307.  
  308.   tft.fillScreen(ILI9341_BLACK);
  309.   start = micros();
  310.   for(x=radius; x<w; x+=r2) {
  311.     for(y=radius; y<h; y+=r2) {
  312.       tft.fillCircle(x, y, radius, color);
  313.     }
  314.   }
  315.  
  316.   return micros() - start;
  317. }
  318.  
  319. unsigned long testCircles(uint8_t radius, uint16_t color) {
  320.   unsigned long start;
  321.   int           x, y, r2 = radius * 2,
  322.                 w = tft.width()  + radius,
  323.                 h = tft.height() + radius;
  324.  
  325.   // Screen is not cleared for this one -- this is
  326.   // intentional and does not affect the reported time.
  327.   start = micros();
  328.   for(x=0; x<w; x+=r2) {
  329.     for(y=0; y<h; y+=r2) {
  330.       tft.drawCircle(x, y, radius, color);
  331.     }
  332.   }
  333.  
  334.   return micros() - start;
  335. }
  336.  
  337. unsigned long testTriangles() {
  338.   unsigned long start;
  339.   int           n, i, cx = tft.width()  / 2 - 1,
  340.                       cy = tft.height() / 2 - 1;
  341.  
  342.   tft.fillScreen(ILI9341_BLACK);
  343.   n     = min(cx, cy);
  344.   start = micros();
  345.   for(i=0; i<n; i+=5) {
  346.     tft.drawTriangle(
  347.       cx    , cy - i, // peak
  348.       cx - i, cy + i, // bottom left
  349.       cx + i, cy + i, // bottom right
  350.       tft.color565(i, i, i));
  351.   }
  352.  
  353.   return micros() - start;
  354. }
  355.  
  356. unsigned long testFilledTriangles() {
  357.   unsigned long start, t = 0;
  358.   int           i, cx = tft.width()  / 2 - 1,
  359.                    cy = tft.height() / 2 - 1;
  360.  
  361.   tft.fillScreen(ILI9341_BLACK);
  362.   start = micros();
  363.   for(i=min(cx,cy); i>10; i-=5) {
  364.     start = micros();
  365.     tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
  366.       tft.color565(0, i*10, i*10));
  367.     t += micros() - start;
  368.     tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
  369.       tft.color565(i*10, i*10, 0));
  370.     yield();
  371.   }
  372.  
  373.   return t;
  374. }
  375.  
  376. unsigned long testRoundRects() {
  377.   unsigned long start;
  378.   int           w, i, i2,
  379.                 cx = tft.width()  / 2 - 1,
  380.                 cy = tft.height() / 2 - 1;
  381.  
  382.   tft.fillScreen(ILI9341_BLACK);
  383.   w     = min(tft.width(), tft.height());
  384.   start = micros();
  385.   for(i=0; i<w; i+=6) {
  386.     i2 = i / 2;
  387.     tft.drawRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(i, 0, 0));
  388.   }
  389.  
  390.   return micros() - start;
  391. }
  392.  
  393. unsigned long testFilledRoundRects() {
  394.   unsigned long start;
  395.   int           i, i2,
  396.                 cx = tft.width()  / 2 - 1,
  397.                 cy = tft.height() / 2 - 1;
  398.  
  399.   tft.fillScreen(ILI9341_BLACK);
  400.   start = micros();
  401.   for(i=min(tft.width(), tft.height()); i>20; i-=6) {
  402.     i2 = i / 2;
  403.     tft.fillRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(0, i, 0));
  404.     yield();
  405.   }
  406.  
  407.   return micros() - start;
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement