acidportal

- 😈 Worlds smallest Evil Portal on a LilyGo T-QT
git clone git://git.acid.vegas/acidportal.git
Log | Files | Refs | Archive | README | LICENSE

TFT_Clock.ino (4065B)

      1 /*
      2  An example analogue clock using a TFT LCD screen to show the time
      3  use of some of the drawing commands with the ST7735 library.
      4 
      5  For a more accurate clock, it would be better to use the RTClib library.
      6  But this is just a demo. 
      7 
      8  Uses compile time to set the time so a reset will start with the compile time again
      9  
     10  Gilchrist 6/2/2014 1.0
     11  Updated by Bodmer
     12  */
     13 
     14 #include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
     15 #include <SPI.h>
     16 
     17 TFT_eSPI tft = TFT_eSPI();  // Invoke library, pins defined in User_Setup.h
     18 
     19 #define TFT_GREY 0xBDF7
     20 
     21 float sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0;    // Saved H, M, S x & y multipliers
     22 float sdeg=0, mdeg=0, hdeg=0;
     23 uint16_t osx=64, osy=64, omx=64, omy=64, ohx=64, ohy=64;  // Saved H, M, S x & y coords
     24 uint16_t x0=0, x1=0, yy0=0, yy1=0;
     25 uint32_t targetTime = 0;                    // for next 1 second timeout
     26 
     27 static uint8_t conv2d(const char* p) {
     28   uint8_t v = 0;
     29   if ('0' <= *p && *p <= '9')
     30     v = *p - '0';
     31   return 10 * v + *++p - '0';
     32 }
     33 
     34 uint8_t hh=conv2d(__TIME__), mm=conv2d(__TIME__+3), ss=conv2d(__TIME__+6);  // Get H, M, S from compile time
     35 
     36 bool initial = 1;
     37 
     38 void setup(void) {
     39   tft.init();
     40   tft.setRotation(0);
     41   tft.fillScreen(TFT_GREY);
     42   tft.setTextColor(TFT_GREEN, TFT_GREY);  // Adding a black background colour erases previous text automatically
     43   
     44   // Draw clock face
     45   tft.fillCircle(64, 64, 61, TFT_BLUE);
     46   tft.fillCircle(64, 64, 57, TFT_BLACK);
     47 
     48   // Draw 12 lines
     49   for(int i = 0; i<360; i+= 30) {
     50     sx = cos((i-90)*0.0174532925);
     51     sy = sin((i-90)*0.0174532925);
     52     x0 = sx*57+64;
     53     yy0 = sy*57+64;
     54     x1 = sx*50+64;
     55     yy1 = sy*50+64;
     56 
     57     tft.drawLine(x0, yy0, x1, yy1, TFT_BLUE);
     58   }
     59 
     60   // Draw 60 dots
     61   for(int i = 0; i<360; i+= 6) {
     62     sx = cos((i-90)*0.0174532925);
     63     sy = sin((i-90)*0.0174532925);
     64     x0 = sx*53+64;
     65     yy0 = sy*53+64;
     66     
     67     tft.drawPixel(x0, yy0, TFT_BLUE);
     68     if(i==0 || i==180) tft.fillCircle(x0, yy0, 1, TFT_CYAN);
     69     if(i==0 || i==180) tft.fillCircle(x0+1, yy0, 1, TFT_CYAN);
     70     if(i==90 || i==270) tft.fillCircle(x0, yy0, 1, TFT_CYAN);
     71     if(i==90 || i==270) tft.fillCircle(x0+1, yy0, 1, TFT_CYAN);
     72   }
     73 
     74   tft.fillCircle(65, 65, 3, TFT_RED);
     75 
     76   // Draw text at position 64,125 using fonts 4
     77   // Only font numbers 2,4,6,7 are valid. Font 6 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : . a p m
     78   // Font 7 is a 7 segment font and only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : .
     79   tft.drawCentreString("Time flies",64,130,4);
     80 
     81   targetTime = millis() + 1000; 
     82 }
     83 
     84 void loop() {
     85   if (targetTime < millis()) {
     86     targetTime = millis()+1000;
     87     ss++;              // Advance second
     88     if (ss==60) {
     89       ss=0;
     90       mm++;            // Advance minute
     91       if(mm>59) {
     92         mm=0;
     93         hh++;          // Advance hour
     94         if (hh>23) {
     95           hh=0;
     96         }
     97       }
     98     }
     99 
    100     // Pre-compute hand degrees, x & y coords for a fast screen update
    101     sdeg = ss*6;                  // 0-59 -> 0-354
    102     mdeg = mm*6+sdeg*0.01666667;  // 0-59 -> 0-360 - includes seconds
    103     hdeg = hh*30+mdeg*0.0833333;  // 0-11 -> 0-360 - includes minutes and seconds
    104     hx = cos((hdeg-90)*0.0174532925);    
    105     hy = sin((hdeg-90)*0.0174532925);
    106     mx = cos((mdeg-90)*0.0174532925);    
    107     my = sin((mdeg-90)*0.0174532925);
    108     sx = cos((sdeg-90)*0.0174532925);    
    109     sy = sin((sdeg-90)*0.0174532925);
    110 
    111     if (ss==0 || initial) {
    112       initial = 0;
    113       // Erase hour and minute hand positions every minute
    114       tft.drawLine(ohx, ohy, 65, 65, TFT_BLACK);
    115       ohx = hx*33+65;    
    116       ohy = hy*33+65;
    117       tft.drawLine(omx, omy, 65, 65, TFT_BLACK);
    118       omx = mx*44+65;    
    119       omy = my*44+65;
    120     }
    121 
    122       // Redraw new hand positions, hour and minute hands not erased here to avoid flicker
    123       tft.drawLine(osx, osy, 65, 65, TFT_BLACK);
    124       tft.drawLine(ohx, ohy, 65, 65, TFT_WHITE);
    125       tft.drawLine(omx, omy, 65, 65, TFT_WHITE);
    126       osx = sx*47+65;    
    127       osy = sy*47+65;
    128       tft.drawLine(osx, osy, 65, 65, TFT_RED);
    129 
    130     tft.fillCircle(65, 65, 3, TFT_RED);
    131   }
    132 }
    133 
    134