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_Digital.ino (4147B)

      1 /*
      2  An example digital clock using a TFT LCD screen to show the time.
      3  Demonstrates use of the font printing routines. (Time updates but date does not.)
      4 
      5  It uses the time of compile/upload to set the time
      6  For a more accurate clock, it would be better to use the RTClib library.
      7  But this is just a demo...
      8 
      9  Make sure all the display driver and pin connections are correct by
     10  editing the User_Setup.h file in the TFT_eSPI library folder.
     11 
     12  #########################################################################
     13  ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
     14  #########################################################################
     15 
     16  Based on clock sketch by Gilchrist 6/2/2014 1.0
     17 
     18 A few colour codes:
     19 
     20 code	color
     21 0x0000	Black
     22 0xFFFF	White
     23 0xBDF7	Light Gray
     24 0x7BEF	Dark Gray
     25 0xF800	Red
     26 0xFFE0	Yellow
     27 0xFBE0	Orange
     28 0x79E0	Brown
     29 0x7E0	Green
     30 0x7FF	Cyan
     31 0x1F	Blue
     32 0xF81F	Pink
     33 
     34  */
     35 
     36 #include <TFT_eSPI.h> // Hardware-specific library
     37 #include <SPI.h>
     38 
     39 #define TFT_GREY 0x5AEB
     40 
     41 TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
     42 
     43 uint32_t targetTime = 0;                    // for next 1 second timeout
     44 
     45 static uint8_t conv2d(const char* p); // Forward declaration needed for IDE 1.6.x
     46 
     47 uint8_t hh = conv2d(__TIME__), mm = conv2d(__TIME__ + 3), ss = conv2d(__TIME__ + 6); // Get H, M, S from compile time
     48 
     49 byte omm = 99, oss = 99;
     50 byte xcolon = 0, xsecs = 0;
     51 unsigned int colour = 0;
     52 
     53 void setup(void) {
     54   //Serial.begin(115200);
     55   tft.init();
     56   tft.setRotation(1);
     57   tft.fillScreen(TFT_BLACK);
     58 
     59   tft.setTextSize(1);
     60   tft.setTextColor(TFT_YELLOW, TFT_BLACK);
     61 
     62   targetTime = millis() + 1000;
     63 }
     64 
     65 void loop() {
     66   if (targetTime < millis()) {
     67     // Set next update for 1 second later
     68     targetTime = millis() + 1000;
     69 
     70     // Adjust the time values by adding 1 second
     71     ss++;              // Advance second
     72     if (ss == 60) {    // Check for roll-over
     73       ss = 0;          // Reset seconds to zero
     74       omm = mm;        // Save last minute time for display update
     75       mm++;            // Advance minute
     76       if (mm > 59) {   // Check for roll-over
     77         mm = 0;
     78         hh++;          // Advance hour
     79         if (hh > 23) { // Check for 24hr roll-over (could roll-over on 13)
     80           hh = 0;      // 0 for 24 hour clock, set to 1 for 12 hour clock
     81         }
     82       }
     83     }
     84 
     85 
     86     // Update digital time
     87     int xpos = 0;
     88     int ypos = 85; // Top left corner ot clock text, about half way down
     89     int ysecs = ypos + 24;
     90 
     91     if (omm != mm) { // Redraw hours and minutes time every minute
     92       omm = mm;
     93       // Draw hours and minutes
     94       if (hh < 10) xpos += tft.drawChar('0', xpos, ypos, 8); // Add hours leading zero for 24 hr clock
     95       xpos += tft.drawNumber(hh, xpos, ypos, 8);             // Draw hours
     96       xcolon = xpos; // Save colon coord for later to flash on/off later
     97       xpos += tft.drawChar(':', xpos, ypos - 8, 8);
     98       if (mm < 10) xpos += tft.drawChar('0', xpos, ypos, 8); // Add minutes leading zero
     99       xpos += tft.drawNumber(mm, xpos, ypos, 8);             // Draw minutes
    100       xsecs = xpos; // Sae seconds 'x' position for later display updates
    101     }
    102     if (oss != ss) { // Redraw seconds time every second
    103       oss = ss;
    104       xpos = xsecs;
    105 
    106       if (ss % 2) { // Flash the colons on/off
    107         tft.setTextColor(0x39C4, TFT_BLACK);        // Set colour to grey to dim colon
    108         tft.drawChar(':', xcolon, ypos - 8, 8);     // Hour:minute colon
    109         xpos += tft.drawChar(':', xsecs, ysecs, 6); // Seconds colon
    110         tft.setTextColor(TFT_YELLOW, TFT_BLACK);    // Set colour back to yellow
    111       }
    112       else {
    113         tft.drawChar(':', xcolon, ypos - 8, 8);     // Hour:minute colon
    114         xpos += tft.drawChar(':', xsecs, ysecs, 6); // Seconds colon
    115       }
    116 
    117       //Draw seconds
    118       if (ss < 10) xpos += tft.drawChar('0', xpos, ysecs, 6); // Add leading zero
    119       tft.drawNumber(ss, xpos, ysecs, 6);                     // Draw seconds
    120     }
    121   }
    122 }
    123 
    124 
    125 // Function to extract numbers from compile time string
    126 static uint8_t conv2d(const char* p) {
    127   uint8_t v = 0;
    128   if ('0' <= *p && *p <= '9')
    129     v = *p - '0';
    130   return 10 * v + *++p - '0';
    131 }
    132 
    133 
    134