Guest User

2nd lib

a guest
Aug 28th, 2021
35
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**************************************************************************
  2.   This is a library for several Adafruit displays based on ST77* drivers.
  3.  
  4.   Works with the Adafruit 1.8" TFT Breakout w/SD card
  5.     ----> http://www.adafruit.com/products/358
  6.   The 1.8" TFT shield
  7.     ----> https://www.adafruit.com/product/802
  8.   The 1.44" TFT breakout
  9.     ----> https://www.adafruit.com/product/2088
  10.   The 1.14" TFT breakout
  11.   ----> https://www.adafruit.com/product/4383
  12.   The 1.3" TFT breakout
  13.   ----> https://www.adafruit.com/product/4313
  14.   The 1.54" TFT breakout
  15.     ----> https://www.adafruit.com/product/3787
  16.   The 2.0" TFT breakout
  17.     ----> https://www.adafruit.com/product/4311
  18.   as well as Adafruit raw 1.8" TFT display
  19.     ----> http://www.adafruit.com/products/618
  20.  
  21.   Check out the links above for our tutorials and wiring diagrams.
  22.   These displays use SPI to communicate, 4 or 5 pins are required to
  23.   interface (RST is optional).
  24.  
  25.   Adafruit invests time and resources providing this open source code,
  26.   please support Adafruit and open-source hardware by purchasing
  27.   products from Adafruit!
  28.  
  29.   Written by Limor Fried/Ladyada for Adafruit Industries.
  30.   MIT license, all text above must be included in any redistribution
  31.  **************************************************************************/
  32.  
  33. #include <Adafruit_GFX.h>    // Core graphics library
  34. #include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
  35. #include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
  36. #include <SPI.h>
  37.  
  38. #if defined(ARDUINO_FEATHER_ESP32) // Feather Huzzah32
  39.   #define TFT_CS         14
  40.   #define TFT_RST        15
  41.   #define TFT_DC         32
  42.  
  43. #elif defined(ESP8266)
  44.   #define TFT_CS         4
  45.   #define TFT_RST        16                                            
  46.   #define TFT_DC         5
  47.  
  48. #else
  49.   // For the breakout board, you can use any 2 or 3 pins.
  50.   // These pins will also work for the 1.8" TFT shield.
  51.   #define TFT_CS        10
  52.   #define TFT_RST        8 // Or set to -1 and connect to Arduino RESET pin
  53.   #define TFT_DC         9
  54. #endif
  55.  
  56. // OPTION 1 (recommended) is to use the HARDWARE SPI pins, which are unique
  57. // to each board and not reassignable. For Arduino Uno: MOSI = pin 11 and
  58. // SCLK = pin 13. This is the fastest mode of operation and is required if
  59. // using the breakout board's microSD card.
  60.  
  61. // For 1.44" and 1.8" TFT with ST7735 use:
  62. //Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  63.  
  64. // For 1.14", 1.3", 1.54", and 2.0" TFT with ST7789:
  65. //Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
  66.  
  67.  
  68. // OPTION 2 lets you interface the display using ANY TWO or THREE PINS,
  69. // tradeoff being that performance is not as fast as hardware SPI above.
  70. //#define TFT_MOSI 11  // Data out
  71. //#define TFT_SCLK 13  // Clock out
  72.  
  73. // For ST7735-based displays, we will use this call
  74. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, 11, 13, TFT_RST);
  75.  
  76. // OR for the ST7789-based displays, we will use this call
  77. //Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
  78.  
  79.  
  80. float p = 3.1415926;
  81.  
  82. void setup(void) {
  83.   Serial.begin(9600);
  84.   Serial.print(F("Hello! LOL!!! TFT Test"));
  85.  
  86.   // Use this initializer if using a 1.8" TFT screen:
  87.   //tft.initR(INITR_BLACKTAB);      // Init ST7735S chip, black tab
  88.  
  89.   // OR use this initializer if using a 1.8" TFT screen with offset such as WaveShare:
  90.   // tft.initR(INITR_GREENTAB);      // Init ST7735S chip, green tab
  91.  
  92.   // OR use this initializer (uncomment) if using a 1.44" TFT:
  93.   //tft.initR(INITR_144GREENTAB); // Init ST7735R chip, green tab
  94.  
  95.   // OR use this initializer (uncomment) if using a 0.96" 160x80 TFT:
  96.   tft.initR(INITR_MINI160x80);  // Init ST7735S mini display
  97.  
  98.   // OR use this initializer (uncomment) if using a 1.3" or 1.54" 240x240 TFT:
  99.   //tft.init(240, 240);           // Init ST7789 240x240
  100.  
  101.   // OR use this initializer (uncomment) if using a 2.0" 320x240 TFT:
  102.   //tft.init(240, 320);           // Init ST7789 320x240
  103.  
  104.   // OR use this initializer (uncomment) if using a 1.14" 240x135 TFT:
  105.   //tft.init(135, 240);           // Init ST7789 240x135
  106.  
  107.   // SPI speed defaults to SPI_DEFAULT_FREQ defined in the library, you can override it here
  108.   // Note that speed allowable depends on chip and quality of wiring, if you go too fast, you
  109.   // may end up with a black screen some times, or all the time.
  110.   //tft.setSPISpeed(40000000);
  111.  
  112.   Serial.println(F("Initialized"));
  113.  
  114.   uint16_t time = millis();
  115.   tft.fillScreen(ST77XX_BLACK);
  116.   time = millis() - time;
  117.  
  118.   Serial.println(time, DEC);
  119.   delay(500);
  120.  
  121.   // large block of text
  122.   tft.fillScreen(ST77XX_BLACK);
  123.   testdrawtext("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ", ST77XX_WHITE);
  124.   delay(1000);
  125.  
  126.   // tft print function!
  127.   tftPrintTest();
  128.   delay(4000);
  129.  
  130.   // a single pixel
  131.   tft.drawPixel(tft.width()/2, tft.height()/2, ST77XX_GREEN);
  132.   delay(500);
  133.  
  134.   // line draw test
  135.   testlines(ST77XX_YELLOW);
  136.   delay(500);
  137.  
  138.   // optimized lines
  139.   testfastlines(ST77XX_RED, ST77XX_BLUE);
  140.   delay(500);
  141.  
  142.   testdrawrects(ST77XX_GREEN);
  143.   delay(500);
  144.  
  145.   testfillrects(ST77XX_YELLOW, ST77XX_MAGENTA);
  146.   delay(500);
  147.  
  148.   tft.fillScreen(ST77XX_BLACK);
  149.   testfillcircles(10, ST77XX_BLUE);
  150.   testdrawcircles(10, ST77XX_WHITE);
  151.   delay(500);
  152.  
  153.   testroundrects();
  154.   delay(500);
  155.  
  156.   testtriangles();
  157.   delay(500);
  158.  
  159.   mediabuttons();
  160.   delay(500);
  161.  
  162.   Serial.println("done");
  163.   delay(1000);
  164. }
  165.  
  166. void loop() {
  167.   tft.invertDisplay(true);
  168.   delay(500);
  169.   tft.invertDisplay(false);
  170.   delay(500);
  171. }
  172.  
  173. void testlines(uint16_t color) {
  174.   tft.fillScreen(ST77XX_BLACK);
  175.   for (int16_t x=0; x < tft.width(); x+=6) {
  176.     tft.drawLine(0, 0, x, tft.height()-1, color);
  177.     delay(0);
  178.   }
  179.   for (int16_t y=0; y < tft.height(); y+=6) {
  180.     tft.drawLine(0, 0, tft.width()-1, y, color);
  181.     delay(0);
  182.   }
  183.  
  184.   tft.fillScreen(ST77XX_BLACK);
  185.   for (int16_t x=0; x < tft.width(); x+=6) {
  186.     tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color);
  187.     delay(0);
  188.   }
  189.   for (int16_t y=0; y < tft.height(); y+=6) {
  190.     tft.drawLine(tft.width()-1, 0, 0, y, color);
  191.     delay(0);
  192.   }
  193.  
  194.   tft.fillScreen(ST77XX_BLACK);
  195.   for (int16_t x=0; x < tft.width(); x+=6) {
  196.     tft.drawLine(0, tft.height()-1, x, 0, color);
  197.     delay(0);
  198.   }
  199.   for (int16_t y=0; y < tft.height(); y+=6) {
  200.     tft.drawLine(0, tft.height()-1, tft.width()-1, y, color);
  201.     delay(0);
  202.   }
  203.  
  204.   tft.fillScreen(ST77XX_BLACK);
  205.   for (int16_t x=0; x < tft.width(); x+=6) {
  206.     tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color);
  207.     delay(0);
  208.   }
  209.   for (int16_t y=0; y < tft.height(); y+=6) {
  210.     tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color);
  211.     delay(0);
  212.   }
  213. }
  214.  
  215. void testdrawtext(char *text, uint16_t color) {
  216.   tft.setCursor(0, 0);
  217.   tft.setTextColor(color);
  218.   tft.setTextWrap(true);
  219.   tft.print(text);
  220. }
  221.  
  222. void testfastlines(uint16_t color1, uint16_t color2) {
  223.   tft.fillScreen(ST77XX_BLACK);
  224.   for (int16_t y=0; y < tft.height(); y+=5) {
  225.     tft.drawFastHLine(0, y, tft.width(), color1);
  226.   }
  227.   for (int16_t x=0; x < tft.width(); x+=5) {
  228.     tft.drawFastVLine(x, 0, tft.height(), color2);
  229.   }
  230. }
  231.  
  232. void testdrawrects(uint16_t color) {
  233.   tft.fillScreen(ST77XX_BLACK);
  234.   for (int16_t x=0; x < tft.width(); x+=6) {
  235.     tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
  236.   }
  237. }
  238.  
  239. void testfillrects(uint16_t color1, uint16_t color2) {
  240.   tft.fillScreen(ST77XX_BLACK);
  241.   for (int16_t x=tft.width()-1; x > 6; x-=6) {
  242.     tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
  243.     tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
  244.   }
  245. }
  246.  
  247. void testfillcircles(uint8_t radius, uint16_t color) {
  248.   for (int16_t x=radius; x < tft.width(); x+=radius*2) {
  249.     for (int16_t y=radius; y < tft.height(); y+=radius*2) {
  250.       tft.fillCircle(x, y, radius, color);
  251.     }
  252.   }
  253. }
  254.  
  255. void testdrawcircles(uint8_t radius, uint16_t color) {
  256.   for (int16_t x=0; x < tft.width()+radius; x+=radius*2) {
  257.     for (int16_t y=0; y < tft.height()+radius; y+=radius*2) {
  258.       tft.drawCircle(x, y, radius, color);
  259.     }
  260.   }
  261. }
  262.  
  263. void testtriangles() {
  264.   tft.fillScreen(ST77XX_BLACK);
  265.   uint16_t color = 0xF800;
  266.   int t;
  267.   int w = tft.width()/2;
  268.   int x = tft.height()-1;
  269.   int y = 0;
  270.   int z = tft.width();
  271.   for(t = 0 ; t <= 15; t++) {
  272.     tft.drawTriangle(w, y, y, x, z, x, color);
  273.     x-=4;
  274.     y+=4;
  275.     z-=4;
  276.     color+=100;
  277.   }
  278. }
  279.  
  280. void testroundrects() {
  281.   tft.fillScreen(ST77XX_BLACK);
  282.   uint16_t color = 100;
  283.   int i;
  284.   int t;
  285.   for(t = 0 ; t <= 4; t+=1) {
  286.     int x = 0;
  287.     int y = 0;
  288.     int w = tft.width()-2;
  289.     int h = tft.height()-2;
  290.     for(i = 0 ; i <= 16; i+=1) {
  291.       tft.drawRoundRect(x, y, w, h, 5, color);
  292.       x+=2;
  293.       y+=3;
  294.       w-=4;
  295.       h-=6;
  296.       color+=1100;
  297.     }
  298.     color+=100;
  299.   }
  300. }
  301.  
  302. void tftPrintTest() {
  303.   tft.setTextWrap(false);
  304.   tft.fillScreen(ST77XX_BLACK);
  305.   tft.setCursor(0, 30);
  306.   tft.setTextColor(ST77XX_RED);
  307.   tft.setTextSize(1);
  308.   tft.println("Hello World!");
  309.   tft.setTextColor(ST77XX_YELLOW);
  310.   tft.setTextSize(2);
  311.   tft.println("Hello World!");
  312.   tft.setTextColor(ST77XX_GREEN);
  313.   tft.setTextSize(3);
  314.   tft.println("Hello World!");
  315.   tft.setTextColor(ST77XX_BLUE);
  316.   tft.setTextSize(4);
  317.   tft.print(1234.567);
  318.   delay(1500);
  319.   tft.setCursor(0, 0);
  320.   tft.fillScreen(ST77XX_BLACK);
  321.   tft.setTextColor(ST77XX_WHITE);
  322.   tft.setTextSize(0);
  323.   tft.println("Hello World!");
  324.   tft.setTextSize(1);
  325.   tft.setTextColor(ST77XX_GREEN);
  326.   tft.print(p, 6);
  327.   tft.println(" Want pi?");
  328.   tft.println(" ");
  329.   tft.print(8675309, HEX); // print 8,675,309 out in HEX!
  330.   tft.println(" Print HEX!");
  331.   tft.println(" ");
  332.   tft.setTextColor(ST77XX_WHITE);
  333.   tft.println("Sketch has been");
  334.   tft.println("running for: ");
  335.   tft.setTextColor(ST77XX_MAGENTA);
  336.   tft.print(millis() / 1000);
  337.   tft.setTextColor(ST77XX_WHITE);
  338.   tft.print(" seconds.");
  339. }
  340.  
  341. void mediabuttons() {
  342.   // play
  343.   tft.fillScreen(ST77XX_BLACK);
  344.   tft.fillRoundRect(25, 10, 78, 60, 8, ST77XX_WHITE);
  345.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_RED);
  346.   delay(500);
  347.   // pause
  348.   tft.fillRoundRect(25, 90, 78, 60, 8, ST77XX_WHITE);
  349.   tft.fillRoundRect(39, 98, 20, 45, 5, ST77XX_GREEN);
  350.   tft.fillRoundRect(69, 98, 20, 45, 5, ST77XX_GREEN);
  351.   delay(500);
  352.   // play color
  353.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_BLUE);
  354.   delay(50);
  355.   // pause color
  356.   tft.fillRoundRect(39, 98, 20, 45, 5, ST77XX_RED);
  357.   tft.fillRoundRect(69, 98, 20, 45, 5, ST77XX_RED);
  358.   // play color
  359.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_GREEN);
  360. }
RAW Paste Data