| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 
 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                          //
//                                           T-Display-S3 Clock                                             //
//                                                                                                          //
//                                          Copyright © 2023 by                                             //
//                                        Zumwalt Properties, LLC.                                          //
//                                          All Rights Reserved.                                            //
//                                                                                                          //
//          This software is for NON COMMERCIAL USE ONLY in accordance with the licensing agreement         //
//                                     as published on this website.                                        //
//                                                                                                          //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
//          
// T-Display-S3 Clock Parts List.
//
//  1) One lilygo T-Display-S3.
//  2) One 3.7V 1000ma lithium rechargable battery with 2 pin JST1.25 connector.
//
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Included files.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
#include "TFT_eSPI.h"
#include <WiFi.h>
#include "time.h"
#include "Greg on pole small.h"
#include "settings.h"
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Global constants..
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
// Display.
 
#define     DISPLAY_HEIGHT          170                         // T-Display-S3 display height in pixels.
#define     DISPLAY_WIDTH           320                         // T-Display-S3 display width in pixels.
#define     DISPLAY_BRIGHTNESS_MAX  252                         // T-Display-S3 display brightness maximum.
#define     DISPLAY_BRIGHTNESS_MIN  0                           // T-Display-S3 display brightness minimum.
 
// Sprites.
 
#define     SPRITE_BATTERY_FONT     2                           // Battery sprite font size.
#define     SPRITE_BATTERY_HEIGHT   30                          // Battery sprite height in pixels.
#define     SPRITE_BATTERY_WIDTH    100                         // Battery sprite width in pixels.
#define     SPRITE_DATE_FONT        4                           // Date sprite font size.
#define     SPRITE_DATE_HEIGHT      40                          // Date sprite height in pixels.
#define     SPRITE_DATE_WIDTH       200                         // Date sprite width in pixels.
#define     SPRITE_TIME_FONT        6                           // Time sprite font size.
#define     SPRITE_TIME_HEIGHT      40                          // Time sprite height in pixels.
#define     SPRITE_TIME_WIDTH       200                         // Time sprite width in pixels.
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Global variables.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
// Battery.
 
uint32_t    uVolt;                                              // Battery voltage.
 
// Display (T-Display-S3 lcd display).
 
TFT_eSPI    lcd = TFT_eSPI();                                   // T-Display-S3 lcd.
int         lcdBacklighBrightness = DISPLAY_BRIGHTNESS_MAX;     // T-Display-S3 brightness.
 
// Sprites.
 
TFT_eSprite spriteBackground = TFT_eSprite(& lcd);              // Background sprite.
TFT_eSprite spriteBattery = TFT_eSprite(& lcd);                 // Battery sprite.
TFT_eSprite spriteDate = TFT_eSprite(& lcd);                    // Date sprite.
TFT_eSprite spriteTime = TFT_eSprite(& lcd);                    // Time sprite.
 
// Time.
 
char        chDayOfMonth[3];                                    // Day of month (0 through 31).
const int   nDaylightOffsetSeconds = 3600;                      // Daylight savings time offset.
char        chDayofWeek[4];                                     // Day of week (Sunday through Saturday).
const long  lGmtOffsetSeconds = +1 * 3600;                      // Time zone offset.
char        chHour[3];                                          // Hour.
char        chMinute[3];                                        // Minute.
char        chMonth[4];                                         // Month.
const char* chNtpServer = "pool.ntp.org";                       // NTP time server.
char        chSecond[3];                                        // Second.
char        chYear[5];                                          // Year.
 
// Wifi.
 
String      stringIP;                                           // IP address.
const char* chPassword = WIFI_PASS;                  // Your router password.
const char* chSsid     = WIFI_SSID;                      // Your router SSID.
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// setup().
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
void setup()
{
  // Serial.
 
  Serial.begin(115200);
 
  // Analog.
 
  analogReadResolution(12);
 
  // LCD.
 
  lcd.init();
  lcd.setRotation(1);
  ledcSetup(0, 10000, 8);
  ledcAttachPin(38, 0);
  ledcWrite(0, lcdBacklighBrightness);
 
  // Battery.
 
    // Sprite.
 
    spriteBattery.createSprite(SPRITE_BATTERY_WIDTH, SPRITE_BATTERY_HEIGHT);
    spriteBattery.setSwapBytes(true);
    spriteBattery.setTextColor(TFT_BLACK, TFT_WHITE);
 
    // Enable the modeul to operate from an external LiPo battery.
 
    pinMode(15, OUTPUT);
    digitalWrite(15, 1);
 
  // Background sprite.
 
  spriteBackground.createSprite(DISPLAY_WIDTH, DISPLAY_HEIGHT);
  spriteBackground.setSwapBytes(true);
  spriteBackground.setTextColor(TFT_WHITE, TFT_BLACK);
 
  // Date sprite.
 
  spriteDate.createSprite(SPRITE_DATE_WIDTH, SPRITE_DATE_HEIGHT);
  spriteDate.setSwapBytes(true);
  spriteDate.setTextColor(TFT_WHITE, TFT_BLACK);
 
  // Time sprite.
 
  spriteTime.createSprite(SPRITE_TIME_WIDTH, SPRITE_TIME_HEIGHT);
  spriteTime.setSwapBytes(true);
  spriteTime.setTextColor(TFT_WHITE, TFT_BLACK);
 
  // Wifi.
 
    // Start wifi.
 
    lcd.fillScreen(TFT_BLACK);
    lcd.drawString("Awaiting wifi connection...", 0, 0, 2);
    WiFi.begin(chSsid, chPassword);
 
    // Wait for a connection.
 
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      Serial.print(".");
    }
 
    // Connected.
 
    Serial.println("\nWiFi connected.");
    stringIP = WiFi.localIP().toString();
    lcd.fillScreen(TFT_BLACK);
    lcd.drawString("Wifi connected to " + stringIP + ".", 0, 0, 2);
    delay(2000);
 
  // Time.
 
  configTime(lGmtOffsetSeconds, nDaylightOffsetSeconds, chNtpServer);
}
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// loop().
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
void loop(void)
{
  // Local variables.
 
  static  char                chBuffer[81];
  static  struct              tm timeinfo;
 
  // Check for time and date availability.
 
  if(getLocalTime(& timeinfo))
  {
    // Time and date available, obtain hours, minutes and seconds.
 
    strftime(chHour, sizeof(chHour), "%H", & timeinfo);
    strftime(chMinute, sizeof(chMinute), "%M", & timeinfo);
    strftime(chSecond, sizeof(chSecond), "%S", & timeinfo);
 
    // Then obtain day of week, day of month, month and year.
 
    strftime(chDayofWeek, sizeof(chDayofWeek), "%A", & timeinfo);
    strftime(chDayOfMonth, sizeof(chDayOfMonth), "%d", & timeinfo);
    strftime(chMonth, sizeof(chMonth), "%B", & timeinfo);
    strftime(chYear, sizeof(chYear), "%Y", & timeinfo);
  }
 
  // Allow the user to adjust backlight brightness.
 
  if((digitalRead(14) == 0) && (lcdBacklighBrightness < DISPLAY_BRIGHTNESS_MAX))
  {
    // Increase.
 
    lcdBacklighBrightness += 4;
    ledcSetup(0, 10000, 8);
    ledcAttachPin(38, 0);
    ledcWrite(0, lcdBacklighBrightness);
  }
  else if((digitalRead(0)) == 0 && (lcdBacklighBrightness > DISPLAY_BRIGHTNESS_MIN))
  {
    // Decrease.
 
    lcdBacklighBrightness -= 4;
    ledcSetup(0, 10000, 8);
    ledcAttachPin(38, 0);
    ledcWrite(0, lcdBacklighBrightness);
  }
 
  // Transfer "Greg_on_a pole_small" to the background sprite in order to "erase" the background sprite.
 
  spriteBackground.pushImage(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, Greg_on_pole_small);
 
  // Create and draw the time sprite onto the background sprite.
 
    // Convert time from 24 hour to 12 hour (optional).
 
    int nTemp = atoi(chHour);
    if(nTemp > 12)
      nTemp -= 12;
    sprintf(chHour, "%2d", nTemp);
 
    // Update the time sprite.
 
    spriteTime.fillRect(0, 0, SPRITE_TIME_WIDTH, SPRITE_TIME_HEIGHT, TFT_BLACK);
    sprintf(chBuffer, "%s:%s:%s", String(chHour), String(chMinute), String(chSecond));
    spriteTime.drawString(String(chBuffer), (SPRITE_TIME_WIDTH / 2) - (lcd.textWidth(chBuffer, SPRITE_TIME_FONT) / 2), 0, SPRITE_TIME_FONT);
 
    // Transfer the time sprite onto the background sprite with black transparency.
 
    spriteTime.pushToSprite(& spriteBackground, (DISPLAY_WIDTH / 2) - (SPRITE_TIME_WIDTH / 2), 30, TFT_BLACK);
 
  // Create and draw the date sprite onto the background sprite.
 
    // Leading zero supress the day of month.
 
    nTemp = atoi(chDayOfMonth);
    if(nTemp < 10)
      sprintf(chDayOfMonth, "%2d", nTemp);
 
    // Update the date sprite
 
    spriteDate.fillRect(0, 0, SPRITE_DATE_WIDTH, SPRITE_DATE_HEIGHT, TFT_BLACK);
    sprintf(chBuffer, "%s, %s %s, %s", String(chDayofWeek), String(chMonth), String(chDayOfMonth), String(chYear));
    spriteDate.drawString(String(chBuffer), (SPRITE_DATE_WIDTH / 2) - (lcd.textWidth(chBuffer, SPRITE_DATE_FONT) / 2), 0, SPRITE_DATE_FONT);
 
    // Transfer the date sprite onto the background sprite with black transparency.
 
    spriteDate.pushToSprite(& spriteBackground, (DISPLAY_WIDTH / 2) - (SPRITE_DATE_WIDTH / 2), 140, TFT_BLACK);
 
  // Update the battery sprite.
 
    // Read the battery voltage.
 
    uVolt = (analogRead(4) * 2 * 3.3 * 1000) / 4096;
 
    // Upeate the battery sprite.
 
    spriteBattery.fillRect(0, 0, SPRITE_BATTERY_WIDTH, SPRITE_BATTERY_HEIGHT, TFT_WHITE);
    spriteBattery.drawString(String(uVolt / 1000) + "." + String(uVolt % 1000) + " vDC", 0, 0, SPRITE_BATTERY_FONT);
 
    // Transfer the battery sprite onto the background sprite with white transparency.
 
    spriteBattery.pushToSprite(& spriteBackground, 0, 0, TFT_WHITE);
 
  // Update the backlight brightness bar if either button is pressed.
 
  if((digitalRead(14) == 0) || (digitalRead(0) == 0))
  {
    // Either button is pressed, display the backlight brightness bar.
 
    spriteBackground.fillRect(DISPLAY_WIDTH - 16, 
                              DISPLAY_HEIGHT - (lcdBacklighBrightness * DISPLAY_HEIGHT / DISPLAY_BRIGHTNESS_MAX),
                              16,
                              lcdBacklighBrightness * DISPLAY_HEIGHT / DISPLAY_BRIGHTNESS_MAX,
                              TFT_WHITE);
  }
 
  // With the background sprite now completed, transfer the background sprite to the LCD.
 
  spriteBackground.pushSprite(0, 0);
 
  // Delay 100 milliseconds.
 
  delay(100);
} | 
Partager