/*
ESP32 + ILI9341 LCD Basic Example
https://wokwi.com/projects/325324981270479442
*/
#include "SPI.h"
#include "Wire.h"
#include "TFT_eSPI.h"
// Use hardware SPI
TFT_eSPI tft = TFT_eSPI();
#define WHITE tft.color565(255,255,255)
#define BLACK tft.color565(0,0,0)
#define RED tft.color565(255,58,47)
#define GREEN tft.color565(58,255,47)
#define BLUE tft.color565(13,143,209)
#define YELLOW tft.color565(209,193,13)
#define DARK_GRAY tft.color565(40,40,40)
//#define DARK_GRAY tft.color565(150,150,150)
#define GRAY tft.color565(60,60,60)
#define LIGHT_GRAY tft.color565(150,150,150)
#define BACKLIGHT_PIN 3
#define BIG_FONT 3
extern uint8_t BigFont[];
byte dimCycle[4] = {30, 60, 90, 120};
byte dimValue = 1;
int digitPos[6] = {10, 82, 172, 244, 92, 164}; //for small font--> 2 line
byte lastHour, lastMinute, lastSecond = 0xFF;
boolean showDots = true;
int top = 15;
int hours = 21;
int minutes = 45;
int seconds = 15;
unsigned long last_time = 0;
void setup() {
Serial.begin(115200);
Wire.begin();
pinMode(BACKLIGHT_PIN, OUTPUT);
analogWrite(BACKLIGHT_PIN, dimCycle[dimValue]);
tft.begin();
tft.fillScreen(DARK_GRAY);
//tft.fillScreen(WHITE);
tft.setRotation(1);
display_clock();
delay(1000);
}
void loop(void) {
if (millis() - last_time > 1000)
{
seconds++;
if (seconds > 59)
{
seconds = 0;
minutes += 1;
}
last_time = millis();
print();
}
if (minutes > 59)
{
minutes = 0;
hours += 1;
}
if (hours > 23)
{
hours = 0;
}
// Display hours
if (lastHour != hours) {
showDigit(digitPos[1], top, hours % 10);
lastHour = hours;
if (hours % 10 == 0)
{
showDigit(digitPos[0], top, hours / 10);
}
}
// Display minutes
if (lastMinute != minutes) {
showDigit(digitPos[3], top, minutes % 10);
lastMinute = minutes;
if (minutes % 10 == 0)
{
showDigit(digitPos[2], top, minutes / 10);
}
}
// Display second
if (lastSecond != seconds)
{
showDigit(digitPos[5], top + 110, seconds % 10);
lastSecond = seconds;
if (seconds % 10 == 0)
{
showDigit(digitPos[4], top + 110, seconds / 10);
}
drawDots(top);
}
}
// ---------------------------------- Fonctions ---------------------------------- //
void drawDots(int y) {
tft.fillRect(155, y + 23, 10, 10, (showDots) ? DARK_GRAY : RED);
tft.fillRect(155, y + 73, 10, 10, (showDots) ? DARK_GRAY : RED);
showDots = !showDots;
}
void showDigit(int x, int y, int number) {
drawFlap(x, y, 66, 102, 20, 2);
showCar(x + 9, y + 12, number, BIG_FONT, BLUE, GRAY);
}
// Affiche un chiffre en x, y
void showCar(int x, int y, int number, int font_select, int color, int bgColor) {
// font_size : 3 = BigFont
const uint8_t *font;
int fw, fh;
if (font_select == 3) {
font = BigFont;
fw = 48;
fh = 76;
}
int hh = fh / 2; // half height
int cs = (fw / 8) * fh; // caracter size in octets
for (int n = 0; n < 2; n++) {
delay(30);
tft.setAddrWindow(x, y + (n * +(hh + 2)), x + fw - 1, y + hh - 1 + (n * +(hh + 2)));
for (int i = 0; i < (cs / 2); i++) {
setRow(pgm_read_byte_near(font + i + number * cs + (n * +(cs / 2))), color, bgColor);
}
}
}
void setRow(byte row, int color, int bgColor) {
for (int i = 7; i > -1; i--) {
tft.pushColor((bitRead(row, i) == 1) ? color : bgColor);
}
}
void drawFlap(int x, int y, int w, int h, int r, int p) {
int hr = r / 2;
tft.fillRect(x, y, w, h, GREEN);
tft.fillRect(x + 2, y + 2, w - 4, (h / 2) - 3, GRAY);
tft.fillRect(x + 2, y + ((h + 2) / 2), w - 4, (h / 2) - 3, GRAY);
tft.fillRect(x + 2, y + (h / 2) - hr, p * 2, r, GREEN);
tft.fillRect(x + 2, y + (h / 2) - hr + 2, p, r - 4, GRAY);
tft.fillRect(x + w - p * 2 - 2, y + (h / 2) - hr, p * 2, r, GREEN);
tft.fillRect(x + w - p - 2, y + (h / 2) - hr + 2, p, r - 4, GRAY);
}
void display_clock() {
showDigit(digitPos[0], top, hours / 10);
showDigit(digitPos[1], top, hours % 10);
showDigit(digitPos[2], top, minutes / 10);
showDigit(digitPos[3], top, minutes % 10);
showDigit(digitPos[4], top + 110, seconds / 10);
showDigit(digitPos[5], top + 110, seconds % 10);
}
void print() {
/*
Serial.print(hours);
Serial.print(":");
Serial.print(minutes);
Serial.print(":");
Serial.println(seconds);
*/
char timeString[9];
snprintf(timeString, sizeof(timeString), "%02d:%02d:%02d", hours, minutes, seconds);
Serial.println(timeString);
}