Guest User

1st lib

a guest
Aug 28th, 2021
58
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.54" TFT breakout
  11.     ----> https://www.adafruit.com/product/3787
  12.   The 2.0" TFT breakout
  13.     ----> https://www.adafruit.com/product/4311
  14.   as well as Adafruit raw 1.8" TFT display
  15.     ----> http://www.adafruit.com/products/618
  16.  
  17.   Check out the links above for our tutorials and wiring diagrams
  18.   These displays use SPI to communicate, 4 or 5 pins are required to
  19.   interface (RST is optional)
  20.   Adafruit invests time and resources providing this open source code,
  21.   please support Adafruit and open-source hardware by purchasing
  22.   products from Adafruit!
  23.  
  24.   Written by Limor Fried/Ladyada for Adafruit Industries.
  25.   MIT license, all text above must be included in any redistribution
  26.  ****************************************************/
  27.  
  28. // This Teensy3 and 4 native optimized and extended version
  29. // requires specific pins.
  30. // If you use the short version of the constructor and the DC
  31. // pin is hardware CS pin, then it will be slower.
  32.  
  33. #define TFT_MISO  12
  34. #define TFT_MOSI  11  //a12
  35. #define TFT_SCLK   13  //a13
  36. #define TFT_DC   9
  37. #define TFT_CS   10  
  38. #define TFT_RST  8
  39.  
  40. // Note the above pins are for the SPI object.  For those Teensy boards which have
  41. // more than one SPI object, such as T3.5, T3.6, T4 which have at SPI1 and SPI2
  42. // LC with SPI1, look at the cards that come with the teensy or the web page
  43. // https://www.pjrc.com/teensy/pinout.html to select the appropriate IO pins.
  44.  
  45. //#include <Adafruit_GFX.h>    // Core graphics library
  46. #include <ST7735_t3.h> // Hardware-specific library
  47. #include <ST7789_t3.h> // Hardware-specific library
  48. #include <SPI.h>
  49.  
  50. // Option 1: use any pins but a little slower
  51. // Note: code will detect if specified pins are the hardware SPI pins
  52. //       and will use hardware SPI if appropriate
  53. // For 1.44" and 1.8" TFT with ST7735 use
  54. //ST7789_t3 tft = ST7789_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCK, TFT_RST);
  55.  
  56. // For 1.54" or other TFT with ST7789, This has worked with some ST7789
  57. // displays without CS pins, for those you can pass in -1 or 0xff for CS
  58. // More notes by the tft.init call
  59. //ST7789_t3 tft = ST7789_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
  60.  
  61. // Option 2: must use the hardware SPI pins
  62. // (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
  63. // an output. This is much faster - also required if you want
  64. // to use the microSD card (see the image drawing example)
  65. // For 1.44" and 1.8" TFT with ST7735 use
  66. ST7735_t3 tft = ST7735_t3(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
  67.  
  68. // For 1.54" TFT with ST7789
  69. //ST7789_t3 tft = ST7789_t3(TFT_CS,  TFT_DC, TFT_RST);
  70.  
  71. float p = 3.1415926;
  72.  
  73.  
  74. void setup(void) {
  75.   //pinMode(SD_CS, INPUT_PULLUP);  // don't touch the SD card
  76.   Serial.begin(9600);
  77.   Serial.print("hello!");
  78.  
  79.   // Use this initializer if you're using a 1.8" TFT 128x160 displays
  80.   //tft.initR(INITR_BLACKTAB);
  81.  
  82.   // Or use this initializer (uncomment) if you're using a 1.44" TFT (128x128)
  83.   //tft.initR(INITR_144GREENTAB);
  84.  
  85.   // Or use this initializer (uncomment) if you're using a .96" TFT(160x80)
  86.   tft.initR(INITR_MINI160x80);
  87.  
  88.   // Or use this initializer (uncomment) for Some 1.44" displays use different memory offsets
  89.   // Try it if yours is not working properly
  90.   // May need to tweek the offsets
  91.   //tft.setRowColStart(32,0);
  92.  
  93.   // Or use this initializer (uncomment) if you're using a 1.54" 240x240 TFT
  94.   //tft.init(240, 240);   // initialize a ST7789 chip, 240x240 pixels
  95.  
  96.   // OR use this initializer (uncomment) if using a 2.0" 320x240 TFT:
  97.   //tft.init(240, 320);           // Init ST7789 320x240
  98.  
  99.   // OR use this initializer (uncomment) if using a 240x240 clone
  100.   // that does not have a CS pin2.0" 320x240 TFT:
  101.   //tft.init(240, 240, SPI_MODE2);           // Init ST7789 240x240 no CS
  102.  
  103.   Serial.println("lol");
  104.  
  105.   uint16_t time = millis();
  106.   tft.fillScreen(ST7735_BLACK);
  107.   time = millis() - time;
  108.  
  109.   Serial.println(time, DEC);
  110.   delay(500);
  111.  
  112.   // large block of text
  113.   tft.fillScreen(ST7735_BLACK);
  114.   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. ", ST7735_WHITE);
  115.   delay(1000);
  116.  
  117.   // tft print function!
  118.   tftPrintTest();
  119.   delay(4000);
  120.  
  121.   // a single pixel
  122.   tft.drawPixel(tft.width()/2, tft.height()/2, ST7735_GREEN);
  123.   delay(500);
  124.  
  125.   // line draw test
  126.   testlines(ST7735_YELLOW);
  127.   delay(1000);
  128.  
  129.   // optimized lines
  130.   testfastlines(ST7735_RED, ST7735_BLUE);
  131.   delay(1000);
  132.  
  133.   testdrawrects(ST7735_GREEN);
  134.   delay(1000);
  135.  
  136.   testfillrects(ST7735_YELLOW, ST7735_MAGENTA);
  137.   delay(1000);
  138.  
  139.   tft.fillScreen(ST7735_BLACK);
  140.   testfillcircles(10, ST7735_BLUE);
  141.   testdrawcircles(10, ST7735_WHITE);
  142.   delay(500);
  143.  
  144.   testroundrects();
  145.   delay(1000);
  146.  
  147.   testtriangles();
  148.   delay(1000);
  149.  
  150.   mediabuttons();
  151.   delay(500);
  152.  
  153.   Serial.println("done");
  154.   delay(1000);
  155. }
  156.  
  157. void loop() {
  158.   tft.invertDisplay(true);
  159.   delay(500);
  160.   tft.invertDisplay(false);
  161.   delay(500);
  162. }
  163.  
  164. void testlines(uint16_t color) {
  165.   tft.fillScreen(ST7735_BLACK);
  166.   for (int16_t x=0; x < tft.width(); x+=6) {
  167.     tft.drawLine(0, 0, x, tft.height()-1, color);
  168.     delay(200);
  169.   }
  170.   for (int16_t y=0; y < tft.height(); y+=6) {
  171.     tft.drawLine(0, 0, tft.width()-1, y, color);
  172.   }
  173.  
  174.   tft.fillScreen(ST7735_BLACK);
  175.   for (int16_t x=0; x < tft.width(); x+=6) {
  176.     tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color);
  177.   }
  178.   for (int16_t y=0; y < tft.height(); y+=6) {
  179.     tft.drawLine(tft.width()-1, 0, 0, y, color);
  180.   }
  181.  
  182.   tft.fillScreen(ST7735_BLACK);
  183.   for (int16_t x=0; x < tft.width(); x+=6) {
  184.     tft.drawLine(0, tft.height()-1, x, 0, color);
  185.   }
  186.   for (int16_t y=0; y < tft.height(); y+=6) {
  187.     tft.drawLine(0, tft.height()-1, tft.width()-1, y, color);
  188.   }
  189.  
  190.   tft.fillScreen(ST7735_BLACK);
  191.   for (int16_t x=0; x < tft.width(); x+=6) {
  192.     tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color);
  193.   }
  194.   for (int16_t y=0; y < tft.height(); y+=6) {
  195.     tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color);
  196.   }
  197. }
  198.  
  199. void testdrawtext(const char *text, uint16_t color) {
  200.   tft.setCursor(0, 0);
  201.   tft.setTextColor(color);
  202.   tft.setTextWrap(true);
  203.   tft.print(text);
  204. }
  205.  
  206. void testfastlines(uint16_t color1, uint16_t color2) {
  207.   tft.fillScreen(ST7735_BLACK);
  208.   for (int16_t y=0; y < tft.height(); y+=5) {
  209.     tft.drawFastHLine(0, y, tft.width(), color1);
  210.   }
  211.   for (int16_t x=0; x < tft.width(); x+=5) {
  212.     tft.drawFastVLine(x, 0, tft.height(), color2);
  213.   }
  214. }
  215.  
  216. void testdrawrects(uint16_t color) {
  217.   tft.fillScreen(ST7735_BLACK);
  218.   for (int16_t x=0; x < tft.width(); x+=6) {
  219.     tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
  220.   }
  221. }
  222.  
  223. void testfillrects(uint16_t color1, uint16_t color2) {
  224.   tft.fillScreen(ST7735_BLACK);
  225.   for (int16_t x=tft.width()-1; x > 6; x-=6) {
  226.     tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
  227.     tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
  228.   }
  229. }
  230.  
  231. void testfillcircles(uint8_t radius, uint16_t color) {
  232.   for (int16_t x=radius; x < tft.width(); x+=radius*2) {
  233.     for (int16_t y=radius; y < tft.height(); y+=radius*2) {
  234.       tft.fillCircle(x, y, radius, color);
  235.     }
  236.   }
  237. }
  238.  
  239. void testdrawcircles(uint8_t radius, uint16_t color) {
  240.   for (int16_t x=0; x < tft.width()+radius; x+=radius*2) {
  241.     for (int16_t y=0; y < tft.height()+radius; y+=radius*2) {
  242.       tft.drawCircle(x, y, radius, color);
  243.     }
  244.   }
  245. }
  246.  
  247. void testtriangles() {
  248.   tft.fillScreen(ST7735_BLACK);
  249.   int color = 0xF800;
  250.   int t;
  251.   int w = tft.width()/2;
  252.   int x = tft.height()-1;
  253.   int y = 0;
  254.   int z = tft.width();
  255.   for(t = 0 ; t <= 15; t+=1) {
  256.     tft.drawTriangle(w, y, y, x, z, x, color);
  257.     x-=4;
  258.     y+=4;
  259.     z-=4;
  260.     color+=100;
  261.   }
  262. }
  263.  
  264. void testroundrects() {
  265.   tft.fillScreen(ST7735_BLACK);
  266.   int color = 100;
  267.   int i;
  268.   int t;
  269.   for(t = 0 ; t <= 4; t+=1) {
  270.     int x = 0;
  271.     int y = 0;
  272.     int w = tft.width()-2;
  273.     int h = tft.height()-2;
  274.     for(i = 0 ; i <= 16; i+=1) {
  275.       tft.drawRoundRect(x, y, w, h, 5, color);
  276.       x+=2;
  277.       y+=3;
  278.       w-=4;
  279.       h-=6;
  280.       color+=1100;
  281.     }
  282.     color+=100;
  283.   }
  284. }
  285.  
  286. void tftPrintTest() {
  287.   tft.setTextWrap(false);
  288.   tft.fillScreen(ST7735_BLACK);
  289.   tft.setCursor(0, 30);
  290.   tft.setTextColor(ST7735_RED);
  291.   tft.setTextSize(1);
  292.   tft.println("Hello World!");
  293.   tft.setTextColor(ST7735_YELLOW);
  294.   tft.setTextSize(2);
  295.   tft.println("Hello World!");
  296.   tft.setTextColor(ST7735_GREEN);
  297.   tft.setTextSize(3);
  298.   tft.println("Hello World!");
  299.   tft.setTextColor(ST7735_BLUE);
  300.   tft.setTextSize(4);
  301.   tft.print(1234.567);
  302.   delay(1500);
  303.   tft.setCursor(0, 0);
  304.   tft.fillScreen(ST7735_BLACK);
  305.   tft.setTextColor(ST7735_WHITE);
  306.   tft.setTextSize(0);
  307.   tft.println("Hello World!");
  308.   tft.setTextSize(1);
  309.   tft.setTextColor(ST7735_GREEN);
  310.   tft.print(p, 6);
  311.   tft.println(" Want pi?");
  312.   tft.println(" ");
  313.   tft.print(8675309, HEX); // print 8,675,309 out in HEX!
  314.   tft.println(" Print HEX!");
  315.   tft.println(" ");
  316.   tft.setTextColor(ST7735_WHITE);
  317.   tft.println("Sketch has been");
  318.   tft.println("running for: ");
  319.   tft.setTextColor(ST7735_MAGENTA);
  320.   tft.print(millis() / 1000);
  321.   tft.setTextColor(ST7735_WHITE);
  322.   tft.print(" seconds.");
  323. }
  324.  
  325. void mediabuttons() {
  326.   // play
  327.   tft.fillScreen(ST7735_BLACK);
  328.   tft.fillRoundRect(25, 10, 78, 60, 8, ST7735_WHITE);
  329.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_RED);
  330.   delay(500);
  331.   // pause
  332.   tft.fillRoundRect(25, 90, 78, 60, 8, ST7735_WHITE);
  333.   tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_GREEN);
  334.   tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_GREEN);
  335.   delay(500);
  336.   // play color
  337.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_BLUE);
  338.   delay(50);
  339.   // pause color
  340.   tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_RED);
  341.   tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_RED);
  342.   // play color
  343.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_GREEN);
  344. }
RAW Paste Data