acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

TFT_Clock.ino (4587B)

      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 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  This sketch uses font 4 only.
      9 
     10  Make sure all the display driver and pin connections are correct by
     11  editing the User_Setup.h file in the TFT_eSPI library folder.
     12 
     13  #########################################################################
     14  ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
     15  #########################################################################
     16  
     17  Based on a sketch by Gilchrist 6/2/2014 1.0
     18  */
     19 
     20 #include <SPI.h>
     21 #include <TFT_eSPI.h> // Hardware-specific library
     22 
     23 #define TFT_GREY 0x5AEB
     24 
     25 TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
     26 
     27 float sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0;    // Saved H, M, S x & y multipliers
     28 float sdeg=0, mdeg=0, hdeg=0;
     29 uint16_t osx=120, osy=120, omx=120, omy=120, ohx=120, ohy=120;  // Saved H, M, S x & y coords
     30 uint16_t x0=0, x1=0, yy0=0, yy1=0;
     31 uint32_t targetTime = 0;                    // for next 1 second timeout
     32 
     33 static uint8_t conv2d(const char* p); // Forward declaration needed for IDE 1.6.x
     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   
     42   //tft.fillScreen(TFT_BLACK);
     43   //tft.fillScreen(TFT_RED);
     44   //tft.fillScreen(TFT_GREEN);
     45   //tft.fillScreen(TFT_BLUE);
     46   //tft.fillScreen(TFT_BLACK);
     47   tft.fillScreen(TFT_GREY);
     48   
     49   tft.setTextColor(TFT_WHITE, TFT_GREY);  // Adding a background colour erases previous text automatically
     50   
     51   // Draw clock face
     52   tft.fillCircle(120, 120, 118, TFT_GREEN);
     53   tft.fillCircle(120, 120, 110, TFT_BLACK);
     54 
     55   // Draw 12 lines
     56   for(int i = 0; i<360; i+= 30) {
     57     sx = cos((i-90)*0.0174532925);
     58     sy = sin((i-90)*0.0174532925);
     59     x0 = sx*114+120;
     60     yy0 = sy*114+120;
     61     x1 = sx*100+120;
     62     yy1 = sy*100+120;
     63 
     64     tft.drawLine(x0, yy0, x1, yy1, TFT_GREEN);
     65   }
     66 
     67   // Draw 60 dots
     68   for(int i = 0; i<360; i+= 6) {
     69     sx = cos((i-90)*0.0174532925);
     70     sy = sin((i-90)*0.0174532925);
     71     x0 = sx*102+120;
     72     yy0 = sy*102+120;
     73     // Draw minute markers
     74     tft.drawPixel(x0, yy0, TFT_WHITE);
     75     
     76     // Draw main quadrant dots
     77     if(i==0 || i==180) tft.fillCircle(x0, yy0, 2, TFT_WHITE);
     78     if(i==90 || i==270) tft.fillCircle(x0, yy0, 2, TFT_WHITE);
     79   }
     80 
     81   tft.fillCircle(120, 121, 3, TFT_WHITE);
     82 
     83   // Draw text at position 120,260 using fonts 4
     84   // 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
     85   // Font 7 is a 7 segment font and only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : .
     86   tft.drawCentreString("Time flies",120,260,4);
     87 
     88   targetTime = millis() + 1000; 
     89 }
     90 
     91 void loop() {
     92   if (targetTime < millis()) {
     93     targetTime += 1000;
     94     ss++;              // Advance second
     95     if (ss==60) {
     96       ss=0;
     97       mm++;            // Advance minute
     98       if(mm>59) {
     99         mm=0;
    100         hh++;          // Advance hour
    101         if (hh>23) {
    102           hh=0;
    103         }
    104       }
    105     }
    106 
    107     // Pre-compute hand degrees, x & y coords for a fast screen update
    108     sdeg = ss*6;                  // 0-59 -> 0-354
    109     mdeg = mm*6+sdeg*0.01666667;  // 0-59 -> 0-360 - includes seconds
    110     hdeg = hh*30+mdeg*0.0833333;  // 0-11 -> 0-360 - includes minutes and seconds
    111     hx = cos((hdeg-90)*0.0174532925);    
    112     hy = sin((hdeg-90)*0.0174532925);
    113     mx = cos((mdeg-90)*0.0174532925);    
    114     my = sin((mdeg-90)*0.0174532925);
    115     sx = cos((sdeg-90)*0.0174532925);    
    116     sy = sin((sdeg-90)*0.0174532925);
    117 
    118     if (ss==0 || initial) {
    119       initial = 0;
    120       // Erase hour and minute hand positions every minute
    121       tft.drawLine(ohx, ohy, 120, 121, TFT_BLACK);
    122       ohx = hx*62+121;    
    123       ohy = hy*62+121;
    124       tft.drawLine(omx, omy, 120, 121, TFT_BLACK);
    125       omx = mx*84+120;    
    126       omy = my*84+121;
    127     }
    128 
    129       // Redraw new hand positions, hour and minute hands not erased here to avoid flicker
    130       tft.drawLine(osx, osy, 120, 121, TFT_BLACK);
    131       osx = sx*90+121;    
    132       osy = sy*90+121;
    133       tft.drawLine(osx, osy, 120, 121, TFT_RED);
    134       tft.drawLine(ohx, ohy, 120, 121, TFT_WHITE);
    135       tft.drawLine(omx, omy, 120, 121, TFT_WHITE);
    136       tft.drawLine(osx, osy, 120, 121, TFT_RED);
    137 
    138     tft.fillCircle(120, 121, 3, TFT_RED);
    139   }
    140 }
    141 
    142 static uint8_t conv2d(const char* p) {
    143   uint8_t v = 0;
    144   if ('0' <= *p && *p <= '9')
    145     v = *p - '0';
    146   return 10 * v + *++p - '0';
    147 }
    148