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

      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  For a more accurate clock, it would be better to use the RTClib library.
      6  But this is just a demo. 
      7  
      8  This examples uses the hardware SPI only. Non-hardware SPI
      9  is just too slow (~8 times slower!)
     10  
     11  Based on clock sketch by Gilchrist 6/2/2014 1.0
     12  Updated by Bodmer
     13 A few colour codes:
     14  
     15 code	color
     16 0x0000	Black
     17 0xFFFF	White
     18 0xBDF7	Light Gray
     19 0x7BEF	Dark Gray
     20 0xF800	Red
     21 0xFFE0	Yellow
     22 0xFBE0	Orange
     23 0x79E0	Brown
     24 0x7E0	Green
     25 0x7FF	Cyan
     26 0x1F	Blue
     27 0xF81F	Pink
     28 
     29  */
     30 
     31 #include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
     32 #include <SPI.h>
     33 
     34 TFT_eSPI tft = TFT_eSPI();  // Invoke library, pins defined in User_Setup.h
     35 
     36 uint32_t targetTime = 0;       // for next 1 second timeout
     37 
     38 byte omm = 99;
     39 bool initial = 1;
     40 byte xcolon = 0;
     41 unsigned int colour = 0;
     42 
     43 static uint8_t conv2d(const char* p) {
     44   uint8_t v = 0;
     45   if ('0' <= *p && *p <= '9')
     46     v = *p - '0';
     47   return 10 * v + *++p - '0';
     48 }
     49 
     50 uint8_t hh=conv2d(__TIME__), mm=conv2d(__TIME__+3), ss=conv2d(__TIME__+6);  // Get H, M, S from compile time
     51 
     52 void setup(void) {
     53   tft.init();
     54   tft.setRotation(1);
     55   tft.fillScreen(TFT_BLACK);
     56 
     57   tft.setTextColor(TFT_YELLOW, TFT_BLACK); // Note: the new fonts do not draw the background colour
     58 
     59   targetTime = millis() + 1000; 
     60 }
     61 
     62 void loop() {
     63   if (targetTime < millis()) {
     64     targetTime = millis()+1000;
     65     ss++;              // Advance second
     66     if (ss==60) {
     67       ss=0;
     68       omm = mm;
     69       mm++;            // Advance minute
     70       if(mm>59) {
     71         mm=0;
     72         hh++;          // Advance hour
     73         if (hh>23) {
     74           hh=0;
     75         }
     76       }
     77     }
     78 
     79     if (ss==0 || initial) {
     80       initial = 0;
     81       tft.setTextColor(TFT_GREEN, TFT_BLACK);
     82       tft.setCursor (8, 52);
     83       tft.print(__DATE__); // This uses the standard ADAFruit small font
     84 
     85       tft.setTextColor(TFT_BLUE, TFT_BLACK);
     86       tft.drawCentreString("It is windy",120,48,2); // Next size up font 2
     87 
     88       //tft.setTextColor(0xF81F, TFT_BLACK); // Pink
     89       //tft.drawCentreString("12.34",80,100,6); // Large font 6 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 . : a p m
     90     }
     91 
     92     // Update digital time
     93     byte xpos = 6;
     94     byte ypos = 0;
     95     if (omm != mm) { // Only redraw every minute to minimise flicker
     96       // Uncomment ONE of the next 2 lines, using the ghost image demonstrates text overlay as time is drawn over it
     97       tft.setTextColor(0x39C4, TFT_BLACK);  // Leave a 7 segment ghost image, comment out next line!
     98       //tft.setTextColor(TFT_BLACK, TFT_BLACK); // Set font colour to black to wipe image
     99       // Font 7 is to show a pseudo 7 segment display.
    100       // Font 7 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 0 : .
    101       tft.drawString("88:88",xpos,ypos,7); // Overwrite the text to clear it
    102       tft.setTextColor(0xFBE0); // Orange
    103       omm = mm;
    104 
    105       if (hh<10) xpos+= tft.drawChar('0',xpos,ypos,7);
    106       xpos+= tft.drawNumber(hh,xpos,ypos,7);
    107       xcolon=xpos;
    108       xpos+= tft.drawChar(':',xpos,ypos,7);
    109       if (mm<10) xpos+= tft.drawChar('0',xpos,ypos,7);
    110       tft.drawNumber(mm,xpos,ypos,7);
    111     }
    112 
    113     if (ss%2) { // Flash the colon
    114       tft.setTextColor(0x39C4, TFT_BLACK);
    115       xpos+= tft.drawChar(':',xcolon,ypos,7);
    116       tft.setTextColor(0xFBE0, TFT_BLACK);
    117     }
    118     else {
    119       tft.drawChar(':',xcolon,ypos,7);
    120       colour = random(0xFFFF);
    121       // Erase the old text with a rectangle, the disadvantage of this method is increased display flicker
    122       tft.fillRect (0, 64, 160, 20, TFT_BLACK);
    123       tft.setTextColor(colour);
    124       tft.drawRightString("Colour",75,64,4); // Right justified string drawing to x position 75
    125       String scolour = String(colour,HEX);
    126       scolour.toUpperCase();
    127       char buffer[20];
    128       scolour.toCharArray(buffer,20);
    129       tft.drawString(buffer,82,64,4);
    130     }
    131   }
    132 }
    133 
    134 
    135