Basic Digital Watch

1
2
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
/* adapted from https://github.com/Bodmer/TFT_eSPI/blob/90cabab91ad3ef239ef1c95d97db9ab666153292/examples/160%20x%20128/TFT_Clock_Digital/TFT_Clock_Digital.ino
 * adapted to:
 * - read from RTC
 * - use watch routines to setup backlight and display
 * - center the time regardless of font chosen (1-8)
 */

#define LILYGO_TWATCH_2020_V1

#include <TTGO.h>
#include <WiFi.h>

int16_t hours_font = 8;
int16_t minutes_font = 8;
int16_t seconds_font = 4;

int16_t old_hour = 24;
int16_t old_minute = 61;
int16_t old_second = 61;

int16_t x_middle;
int16_t y_middle;

int16_t hours_half_height;

int16_t hours_x;
int16_t hours_y;

int16_t minutes_x;
int16_t minutes_y;

int16_t seconds_x;
int16_t seconds_y;

TTGOClass * watch;
TFT_eSPI * screen;

void setup(void) {
  watch = TTGOClass::getWatch();
  watch->begin();
  watch->rtc->check();
  watch->bl->adjust(150);

  screen = watch->eTFT;
  screen->fillScreen(TFT_BLACK);
  screen->setTextColor(TFT_WHITE, TFT_BLACK);

  x_middle = screen->width() / 2;
  y_middle = screen->height() / 2;
  hours_half_height = screen->fontHeight(hours_font) / 2;
  hours_x = x_middle - screen->textWidth("00", hours_font);
  hours_y = y_middle - hours_half_height;

  minutes_x = x_middle;
  minutes_y = hours_y;

  seconds_x = x_middle - screen->textWidth("0", seconds_font);
  seconds_y = y_middle + hours_half_height + 10;
}

void loop() {
  int16_t position_x;
  RTC_Date time = watch->rtc->getDateTime();

  watch->eTFT->startWrite();
  // hour
  if (old_hour != time.hour) {
    old_hour = time.hour;
    position_x = hours_x;
    screen->setTextPadding(screen->textWidth("0", hours_font));
    if (time.hour < 10) position_x += screen->drawChar('0', position_x, hours_y, hours_font);
    position_x += screen->drawNumber(time.hour, position_x, hours_y, hours_font);
  }

  // minute
  if (old_minute != time.minute) {
    old_minute = time.minute;
    position_x = minutes_x;
    screen->setTextPadding(screen->textWidth("0", minutes_font));
    if (time.minute < 10) position_x += screen->drawChar('0', position_x, minutes_y, minutes_font);
    position_x += screen->drawNumber(time.minute, position_x, minutes_y, minutes_font);
  }

  // seconds
  if (old_second != time.second) {
    old_second = time.second;
    position_x = seconds_x;
    screen->setTextPadding(screen->textWidth("0", seconds_font));
    if (time.second < 10) position_x += screen->drawChar('0', position_x, seconds_y, seconds_font);
    position_x += screen->drawNumber(time.second, position_x, seconds_y, seconds_font);
  }
  watch->eTFT->endWrite();

  vTaskDelay(250);
}