Advertisement
pan7nikt

RFID_LCD

Apr 18th, 2023
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.59 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. #include "uid_check.h"
  34.  
  35. // ST7735 TFT module connections
  36. #define TFT_RST   D4     // TFT RST pin is connected to NodeMCU pin D4 (GPIO2)
  37. #define TFT_CS    D3     // TFT CS  pin is connected to NodeMCU pin D4 (GPIO0)
  38. #define TFT_DC    D2     // TFT DC  pin is connected to NodeMCU pin D4 (GPIO4)
  39. // initialize ST7735 TFT library with hardware SPI module
  40. // SCK (CLK) ---> NodeMCU pin D5 (GPIO14)
  41. // MOSI(DIN) ---> NodeMCU pin D7 (GPIO13)
  42. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  43.  
  44. float p = 3.1415926;
  45.  
  46. void setup(void) {
  47. //RFID Init
  48.   Serial.begin(9600);
  49. SPI.begin();
  50.  
  51. mfrc522.PCD_Init();
  52. //End of RFID init
  53.  
  54.   tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
  55.  
  56.   uint16_t time = millis();
  57.   tft.fillScreen(ST7735_BLACK);
  58.   time = millis() - time;
  59.  
  60.   delay(500);
  61.  
  62.   // large block of text
  63.   tft.fillScreen(ST7735_BLACK);
  64.   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);
  65.   delay(1000);
  66.  
  67.   // tft print function!
  68.   tftPrintTest();
  69.   delay(4000);
  70.  
  71.   // a single pixel
  72.   tft.drawPixel(tft.width()/2, tft.height()/2, ST7735_GREEN);
  73.   delay(500);
  74.  
  75.   // line draw test
  76.   testlines(ST7735_YELLOW);
  77.   delay(500);
  78.  
  79.   // optimized lines
  80.   testfastlines(ST7735_RED, ST7735_BLUE);
  81.   delay(500);
  82.  
  83.   testdrawrects(ST7735_GREEN);
  84.   delay(500);
  85.  
  86.   testfillrects(ST7735_YELLOW, ST7735_MAGENTA);
  87.   delay(500);
  88.  
  89.   tft.fillScreen(ST7735_BLACK);
  90.   testfillcircles(10, ST7735_BLUE);
  91.   testdrawcircles(10, ST7735_WHITE);
  92.   delay(500);
  93.  
  94.   testroundrects();
  95.   delay(500);
  96.  
  97.   testtriangles();
  98.   delay(500);
  99.  
  100.   mediabuttons();
  101.   delay(500);
  102.  
  103.   delay(1000);
  104. }
  105.  
  106. void loop() {
  107.   int cardId = RfidScan();
  108.   if(cardId == 1 || cardId == 2)
  109.   {
  110.     accessGranted(cardId);
  111.   }
  112.   tft.fillScreen(ST7735_BLACK);
  113.   delay(500);
  114. }
  115.  
  116. void accessGranted(int id)
  117. {
  118.     // large block of text
  119.   tft.fillScreen(ST7735_BLACK);
  120.  
  121.   testdrawtext(("Access Granted! Card ID = "), ST7735_WHITE);
  122.   delay(5000);
  123. }
  124.  
  125. void testlines(uint16_t color) {
  126.   tft.fillScreen(ST7735_BLACK);
  127.   for (int16_t x=0; x < tft.width(); x+=6) {
  128.     tft.drawLine(0, 0, x, tft.height()-1, color);
  129.   }
  130.   for (int16_t y=0; y < tft.height(); y+=6) {
  131.     tft.drawLine(0, 0, tft.width()-1, y, color);
  132.   }
  133.  
  134.   tft.fillScreen(ST7735_BLACK);
  135.   for (int16_t x=0; x < tft.width(); x+=6) {
  136.     tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color);
  137.   }
  138.   for (int16_t y=0; y < tft.height(); y+=6) {
  139.     tft.drawLine(tft.width()-1, 0, 0, y, color);
  140.   }
  141.  
  142.   tft.fillScreen(ST7735_BLACK);
  143.   for (int16_t x=0; x < tft.width(); x+=6) {
  144.     tft.drawLine(0, tft.height()-1, x, 0, color);
  145.   }
  146.   for (int16_t y=0; y < tft.height(); y+=6) {
  147.     tft.drawLine(0, tft.height()-1, tft.width()-1, y, color);
  148.   }
  149.  
  150.   tft.fillScreen(ST7735_BLACK);
  151.   for (int16_t x=0; x < tft.width(); x+=6) {
  152.     tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color);
  153.   }
  154.   for (int16_t y=0; y < tft.height(); y+=6) {
  155.     tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color);
  156.   }
  157. }
  158.  
  159. void testdrawtext(char *text, uint16_t color) {
  160.   tft.setCursor(0, 0);
  161.   tft.setTextColor(color);
  162.   tft.setTextWrap(true);
  163.   tft.print(text);
  164. }
  165.  
  166. void testfastlines(uint16_t color1, uint16_t color2) {
  167.   tft.fillScreen(ST7735_BLACK);
  168.   for (int16_t y=0; y < tft.height(); y+=5) {
  169.     tft.drawFastHLine(0, y, tft.width(), color1);
  170.   }
  171.   for (int16_t x=0; x < tft.width(); x+=5) {
  172.     tft.drawFastVLine(x, 0, tft.height(), color2);
  173.   }
  174. }
  175.  
  176. void testdrawrects(uint16_t color) {
  177.   tft.fillScreen(ST7735_BLACK);
  178.   for (int16_t x=0; x < tft.width(); x+=6) {
  179.     tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
  180.   }
  181. }
  182.  
  183. void testfillrects(uint16_t color1, uint16_t color2) {
  184.   tft.fillScreen(ST7735_BLACK);
  185.   for (int16_t x=tft.width()-1; x > 6; x-=6) {
  186.     tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
  187.     tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
  188.   }
  189. }
  190.  
  191. void testfillcircles(uint8_t radius, uint16_t color) {
  192.   for (int16_t x=radius; x < tft.width(); x+=radius*2) {
  193.     for (int16_t y=radius; y < tft.height(); y+=radius*2) {
  194.       tft.fillCircle(x, y, radius, color);
  195.     }
  196.   }
  197. }
  198.  
  199. void testdrawcircles(uint8_t radius, uint16_t color) {
  200.   for (int16_t x=0; x < tft.width()+radius; x+=radius*2) {
  201.     for (int16_t y=0; y < tft.height()+radius; y+=radius*2) {
  202.       tft.drawCircle(x, y, radius, color);
  203.     }
  204.   }
  205. }
  206.  
  207. void testtriangles() {
  208.   tft.fillScreen(ST7735_BLACK);
  209.   int color = 0xF800;
  210.   int t;
  211.   int w = tft.width()/2;
  212.   int x = tft.height()-1;
  213.   int y = 0;
  214.   int z = tft.width();
  215.   for(t = 0 ; t <= 15; t++) {
  216.     tft.drawTriangle(w, y, y, x, z, x, color);
  217.     x-=4;
  218.     y+=4;
  219.     z-=4;
  220.     color+=100;
  221.   }
  222. }
  223.  
  224. void testroundrects() {
  225.   tft.fillScreen(ST7735_BLACK);
  226.   int color = 100;
  227.   int i;
  228.   int t;
  229.   for(t = 0 ; t <= 4; t+=1) {
  230.     int x = 0;
  231.     int y = 0;
  232.     int w = tft.width()-2;
  233.     int h = tft.height()-2;
  234.     for(i = 0 ; i <= 16; i+=1) {
  235.       tft.drawRoundRect(x, y, w, h, 5, color);
  236.       x+=2;
  237.       y+=3;
  238.       w-=4;
  239.       h-=6;
  240.       color+=1100;
  241.     }
  242.     color+=100;
  243.   }
  244. }
  245.  
  246. void tftPrintTest() {
  247.   tft.setTextWrap(false);
  248.   tft.fillScreen(ST7735_BLACK);
  249.   tft.setCursor(0, 30);
  250.   tft.setTextColor(ST7735_RED);
  251.   tft.setTextSize(1);
  252.   tft.println("Hello World!");
  253.   tft.setTextColor(ST7735_YELLOW);
  254.   tft.setTextSize(2);
  255.   tft.println("Hello World!");
  256.   tft.setTextColor(ST7735_GREEN);
  257.   tft.setTextSize(3);
  258.   tft.println("Hello World!");
  259.   tft.setTextColor(ST7735_BLUE);
  260.   tft.setTextSize(4);
  261.   tft.print(1234.567);
  262.   delay(1500);
  263.   tft.setCursor(0, 0);
  264.   tft.fillScreen(ST7735_BLACK);
  265.   tft.setTextColor(ST7735_WHITE);
  266.   tft.setTextSize(0);
  267.   tft.println("Hello World!");
  268.   tft.setTextSize(1);
  269.   tft.setTextColor(ST7735_GREEN);
  270.   tft.print(p, 6);
  271.   tft.println(" Want pi?");
  272.   tft.println(" ");
  273.   tft.print(8675309, HEX); // print 8,675,309 out in HEX!
  274.   tft.println(" Print HEX!");
  275.   tft.println(" ");
  276.   tft.setTextColor(ST7735_WHITE);
  277.   tft.println("Sketch has been");
  278.   tft.println("running for: ");
  279.   tft.setTextColor(ST7735_MAGENTA);
  280.   tft.print(millis() / 1000);
  281.   tft.setTextColor(ST7735_WHITE);
  282.   tft.print(" seconds.");
  283. }
  284.  
  285. void mediabuttons() {
  286.   // play
  287.   tft.fillScreen(ST7735_BLACK);
  288.   tft.fillRoundRect(25, 10, 78, 60, 8, ST7735_WHITE);
  289.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_RED);
  290.   delay(500);
  291.   // pause
  292.   tft.fillRoundRect(25, 90, 78, 60, 8, ST7735_WHITE);
  293.   tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_GREEN);
  294.   tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_GREEN);
  295.   delay(500);
  296.   // play color
  297.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_BLUE);
  298.   delay(50);
  299.   // pause color
  300.   tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_RED);
  301.   tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_RED);
  302.   // play color
  303.   tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_GREEN);
  304. }
  305.  
  306. // end of code.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement