Advertisement
pan7nikt

1_8_Waveshare_LCD_nodeMCU

Apr 18th, 2023
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.24 KB | None | 0 0
  1. /**************************************************************************************
  2.  
  3.  Interfacing ESP8266 NodeMCU with ST7735 TFT display (128x160 pixel).
  4.  This is a free software with NO WARRANTY.
  5.  https://simple-circuit.com/
  6.  
  7. /**************************************************************************
  8.   This is a library for several Adafruit displays based on ST77* drivers.
  9.  
  10.   Works with the Adafruit 1.8" TFT Breakout w/SD card
  11.     ----> http://www.adafruit.com/products/358
  12.   The 1.8" TFT shield
  13.     ----> https://www.adafruit.com/product/802
  14.   The 1.44" TFT breakout
  15.     ----> https://www.adafruit.com/product/2088
  16.   as well as Adafruit raw 1.8" TFT display
  17.     ----> http://www.adafruit.com/products/618
  18.  
  19.   Check out the links above for our tutorials and wiring diagrams.
  20.   These displays use SPI to communicate, 4 or 5 pins are required to
  21.   interface (RST is optional).
  22.  
  23.   Adafruit invests time and resources providing this open source code,
  24.   please support Adafruit and open-source hardware by purchasing
  25.   products from Adafruit!
  26.  
  27.   Written by Limor Fried/Ladyada for Adafruit Industries.
  28.   MIT license, all text above must be included in any redistribution
  29.  **************************************************************************/
  30.  
  31. #include <Adafruit_GFX.h>      // include Adafruit graphics library
  32. #include <Adafruit_ST7735.h>   // include Adafruit ST7735 TFT library
  33.  
  34. // ST7735 TFT module connections
  35. #define TFT_RST   D4     // TFT RST pin is connected to NodeMCU pin D4 (GPIO2)
  36. #define TFT_CS    D3     // TFT CS  pin is connected to NodeMCU pin D4 (GPIO0)
  37. #define TFT_DC    D2     // TFT DC  pin is connected to NodeMCU pin D4 (GPIO4)
  38. // initialize ST7735 TFT library with hardware SPI module
  39. // SCK (CLK) ---> NodeMCU pin D5 (GPIO14)
  40. // MOSI(DIN) ---> NodeMCU pin D7 (GPIO13)
  41. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  42.  
  43. float p = 3.1415926;
  44.  
  45. void setup(void) {
  46.   tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
  47.  
  48.   uint16_t time = millis();
  49.   tft.fillScreen(ST7735_BLACK);
  50.   time = millis() - time;
  51.  
  52.   delay(500);
  53.  
  54.   // large block of text
  55.   tft.fillScreen(ST7735_BLACK);
  56.   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);
  57.   delay(1000);
  58.  
  59.   // tft print function!
  60.   tftPrintTest();
  61.   delay(4000);
  62.  
  63.   // a single pixel
  64.   tft.drawPixel(tft.width()/2, tft.height()/2, ST7735_GREEN);
  65.   delay(500);
  66.  
  67.   // line draw test
  68.   testlines(ST7735_YELLOW);
  69.   delay(500);
  70.  
  71.   // optimized lines
  72.   testfastlines(ST7735_RED, ST7735_BLUE);
  73.   delay(500);
  74.  
  75.   testdrawrects(ST7735_GREEN);
  76.   delay(500);
  77.  
  78.   testfillrects(ST7735_YELLOW, ST7735_MAGENTA);
  79.   delay(500);
  80.  
  81.   tft.fillScreen(ST7735_BLACK);
  82.   testfillcircles(10, ST7735_BLUE);
  83.   testdrawcircles(10, ST7735_WHITE);
  84.   delay(500);
  85.  
  86.   testroundrects();
  87.   delay(500);
  88.  
  89.   testtriangles();
  90.   delay(500);
  91.  
  92.   mediabuttons();
  93.   delay(500);
  94.  
  95.   delay(1000);
  96. }
  97.  
  98. void loop() {
  99.   tft.invertDisplay(true);
  100.   delay(500);
  101.   tft.invertDisplay(false);
  102.   delay(500);
  103. }
  104.  
  105. void testlines(uint16_t color) {
  106.   tft.fillScreen(ST7735_BLACK);
  107.   for (int16_t x=0; x < tft.width(); x+=6) {
  108.     tft.drawLine(0, 0, x, tft.height()-1, color);
  109.   }
  110.   for (int16_t y=0; y < tft.height(); y+=6) {
  111.     tft.drawLine(0, 0, tft.width()-1, y, color);
  112.   }
  113.  
  114.   tft.fillScreen(ST7735_BLACK);
  115.   for (int16_t x=0; x < tft.width(); x+=6) {
  116.     tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color);
  117.   }
  118.   for (int16_t y=0; y < tft.height(); y+=6) {
  119.     tft.drawLine(tft.width()-1, 0, 0, y, color);
  120.   }
  121.  
  122.   tft.fillScreen(ST7735_BLACK);
  123.   for (int16_t x=0; x < tft.width(); x+=6) {
  124.     tft.drawLine(0, tft.height()-1, x, 0, color);
  125.   }
  126.   for (int16_t y=0; y < tft.height(); y+=6) {
  127.     tft.drawLine(0, tft.height()-1, tft.width()-1, y, color);
  128.   }
  129.  
  130.   tft.fillScreen(ST7735_BLACK);
  131.   for (int16_t x=0; x < tft.width(); x+=6) {
  132.     tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color);
  133.   }
  134.   for (int16_t y=0; y < tft.height(); y+=6) {
  135.     tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color);
  136.   }
  137. }
  138.  
  139. void testdrawtext(char *text, uint16_t color) {
  140.   tft.setCursor(0, 0);
  141.   tft.setTextColor(color);
  142.   tft.setTextWrap(true);
  143.   tft.print(text);
  144. }
  145.  
  146. void testfastlines(uint16_t color1, uint16_t color2) {
  147.   tft.fillScreen(ST7735_BLACK);
  148.   for (int16_t y=0; y < tft.height(); y+=5) {
  149.     tft.drawFastHLine(0, y, tft.width(), color1);
  150.   }
  151.   for (int16_t x=0; x < tft.width(); x+=5) {
  152.     tft.drawFastVLine(x, 0, tft.height(), color2);
  153.   }
  154. }
  155.  
  156. void testdrawrects(uint16_t color) {
  157.   tft.fillScreen(ST7735_BLACK);
  158.   for (int16_t x=0; x < tft.width(); x+=6) {
  159.     tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
  160.   }
  161. }
  162.  
  163. void testfillrects(uint16_t color1, uint16_t color2) {
  164.   tft.fillScreen(ST7735_BLACK);
  165.   for (int16_t x=tft.width()-1; x > 6; x-=6) {
  166.     tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
  167.     tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
  168.   }
  169. }
  170.  
  171. void testfillcircles(uint8_t radius, uint16_t color) {
  172.   for (int16_t x=radius; x < tft.width(); x+=radius*2) {
  173.     for (int16_t y=radius; y < tft.height(); y+=radius*2) {
  174.       tft.fillCircle(x, y, radius, color);
  175.     }
  176.   }
  177. }
  178.  
  179. void testdrawcircles(uint8_t radius, uint16_t color) {
  180.   for (int16_t x=0; x < tft.width()+radius; x+=radius*2) {
  181.     for (int16_t y=0; y < tft.height()+radius; y+=radius*2) {
  182.       tft.drawCircle(x, y, radius, color);
  183.     }
  184.   }
  185. }
  186.  
  187. void testtriangles() {
  188.   tft.fillScreen(ST7735_BLACK);
  189.   int color = 0xF800;
  190.   int t;
  191.   int w = tft.width()/2;
  192.   int x = tft.height()-1;
  193.   int y = 0;
  194.   int z = tft.width();
  195.   for(t = 0 ; t <= 15; t++) {
  196.     tft.drawTriangle(w, y, y, x, z, x, color);
  197.     x-=4;
  198.     y+=4;
  199.     z-=4;
  200.     color+=100;
  201.   }
  202. }
  203.  
  204. void testroundrects() {
  205.   tft.fillScreen(ST7735_BLACK);
  206.   int color = 100;
  207.   int i;
  208.   int t;
  209.   for(t = 0 ; t <= 4; t+=1) {
  210.     int x = 0;
  211.     int y = 0;
  212.     int w = tft.width()-2;
  213.     int h = tft.height()-2;
  214.     for(i = 0 ; i <= 16; i+=1) {
  215.       tft.drawRoundRect(x, y, w, h, 5, color);
  216.       x+=2;
  217.       y+=3;
  218.       w-=4;
  219.       h-=6;
  220.       color+=1100;
  221.     }
  222.     color+=100;
  223.   }
  224. }
  225.  
  226. void tftPrintTest() {
  227.   tft.setTextWrap(false);
  228.   tft.fillScreen(ST7735_BLACK);
  229.   tft.setCursor(0, 30);
  230.   tft.setTextColor(ST7735_RED);
  231.   tft.setTextSize(1);
  232.   tft.println("Hello World!");
  233.   tft.setTextColor(ST7735_YELLOW);
  234.   tft.setTextSize(2);
  235.   tft.println("Hello World!");
  236.   tft.setTextColor(ST7735_GREEN);
  237.   tft.setTextSize(3);
  238.   tft.println("Hello World!");
  239.   tft.setTextColor(ST7735_BLUE);
  240.   tft.setTextSize(4);
  241.   tft.print(1234.567);
  242.   delay(1500);
  243.   tft.setCursor(0, 0);
  244.   tft.fillScreen(ST7735_BLACK);
  245.   tft.setTextColor(ST7735_WHITE);
  246.   tft.setTextSize(0);
  247.   tft.println("Hello World!");
  248.   tft.setTextSize(1);
  249.   tft.setTextColor(ST7735_GREEN);
  250.   tft.print(p, 6);
  251.   tft.println(" Want pi?");
  252.   tft.println(" ");
  253.   tft.print(8675309, HEX); // print 8,675,309 out in HEX!
  254.   tft.println(" Print HEX!");
  255.   tft.println(" ");
  256.   tft.setTextColor(ST7735_WHITE);
  257.   tft.println("Sketch has been");
  258.   tft.println("running for: ");
  259.   tft.setTextColor(ST7735_MAGENTA);
  260.   tft.print(millis() / 1000);
  261.   tft.setTextColor(ST7735_WHITE);
  262.   tft.print(" seconds.");
  263. }
  264.  
  265. void mediabuttons() {
  266.   // play
  267.   tft.fillScreen(ST7735_BLACK);
  268.   tft.fillRoundRect(25, 10, 78, 60, 8, ST7735_WHITE);
  269.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_RED);
  270.   delay(500);
  271.   // pause
  272.   tft.fillRoundRect(25, 90, 78, 60, 8, ST7735_WHITE);
  273.   tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_GREEN);
  274.   tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_GREEN);
  275.   delay(500);
  276.   // play color
  277.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_BLUE);
  278.   delay(50);
  279.   // pause color
  280.   tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_RED);
  281.   tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_RED);
  282.   // play color
  283.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_GREEN);
  284. }
  285.  
  286. // end of code.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement