#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Wire.h>
#include <RTClib.h>
#include <Fonts/FreeSerif9pt7b.h>
#define GOLD 0xFEA0
#define SILVER 0xC618
#define TFT_CS 40
#define TFT_DC 41
#define TFT_RST 42
Adafruit_ILI9341 lcd2(TFT_CS, TFT_DC, TFT_RST);
RTC_DS1307 rtc;
int lastSecond = -1;
int lastMinute = -1;
int lastHour = -1;
void setup() {
Serial.begin(9600);
lcd2.begin();
lcd2.setRotation(1);
lcd2.fillScreen(ILI9341_BLACK);
rtc.begin();
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
dessinerCadran();
}
void dessinerCadran() {
int centerX = lcd2.width() / 2;
int centerY = lcd2.height() / 2;
int radius = 110;
// Fond du cadran
lcd2.fillCircle(centerX, centerY, radius, ILI9341_NAVY);
for (int i = 0; i < 60; i++) {
float angle = (i * 6 - 90) * DEG_TO_RAD;
int x = centerX + cos(angle) * (radius - 6);
int y = centerY + sin(angle) * (radius - 6);
if (i % 5 == 0) {
// Heures
if (i % 15 == 0) {
// Quarts (12, 3, 6, 9) : traits épaissis (2 traits parallèles)
int xStart = centerX + cos(angle) * (radius - 3);
int yStart = centerY + sin(angle) * (radius - 3);
int xEnd = centerX + cos(angle) * (radius - 14);
int yEnd = centerY + sin(angle) * (radius - 14);
lcd2.drawLine(xStart, yStart, xEnd, yEnd, SILVER);
lcd2.drawLine(xStart + 1, yStart + 1, xEnd + 1, yEnd + 1, SILVER);
} else {
// Autres heures (points plus gros)
lcd2.fillCircle(x, y, 3, SILVER);
}
} else {
// Minutes (petits points)
lcd2.fillCircle(x, y, 2, SILVER);
}
}
lcd2.setFont(&FreeSerif9pt7b);
lcd2.setTextColor(SILVER);
for (int i = 1; i <= 12; i++) {
float angle = (i * 30 - 90) * DEG_TO_RAD;
int x = centerX + cos(angle) * (radius - 22);
int y = centerY + sin(angle) * (radius - 22);
char buf[3];
sprintf(buf, "%d", i);
int16_t x1, y1;
uint16_t w, h;
lcd2.getTextBounds(buf, x, y, &x1, &y1, &w, &h);
lcd2.setCursor(x - w / 2, y + h / 3);
lcd2.print(buf);
}
lcd2.setFont();
}
void dessinerAiguille(float angle, int longueur, int largeur, uint16_t couleur) {
int centerX = lcd2.width() / 2;
int centerY = lcd2.height() / 2;
float rad = angle - HALF_PI;
int x0 = centerX + cos(rad) * longueur;
int y0 = centerY + sin(rad) * longueur;
float offset = PI / 2;
int x1 = centerX + cos(rad + offset) * (largeur / 2);
int y1 = centerY + sin(rad + offset) * (largeur / 2);
int x2 = centerX + cos(rad - offset) * (largeur / 2);
int y2 = centerY + sin(rad - offset) * (largeur / 2);
lcd2.fillTriangle(x0, y0, x1, y1, x2, y2, couleur);
}
void dessinerAiguilleSeconde(float angle, uint16_t couleur) {
int centerX = lcd2.width() / 2;
int centerY = lcd2.height() / 2;
float rad = angle - HALF_PI;
int xEnd = centerX + cos(rad) * 70;
int yEnd = centerY + sin(rad) * 70;
int xTail = centerX - cos(rad) * 8;
int yTail = centerY - sin(rad) * 8;
lcd2.drawLine(xTail, yTail, xEnd, yEnd, couleur);
}
void loop() {
DateTime now = rtc.now();
int currentSecond = now.second();
int currentMinute = now.minute();
int currentHour = now.hour();
if (lastSecond >= 0 && currentSecond != lastSecond) {
float oldSecAngle = lastSecond * 6 * DEG_TO_RAD;
dessinerAiguilleSeconde(oldSecAngle, ILI9341_NAVY);
lcd2.fillCircle(lcd2.width() / 2, lcd2.height() / 2, 5, ILI9341_NAVY);
}
if (lastMinute >= 0 && currentMinute != lastMinute) {
float oldMinAngle = lastMinute * 6 * DEG_TO_RAD;
dessinerAiguille(oldMinAngle, 65, 4, ILI9341_NAVY);
}
if (lastHour >= 0 && (currentHour != lastHour || currentMinute != lastMinute)) {
float oldHourAngle = (lastHour % 12 + lastMinute / 60.0) * 30 * DEG_TO_RAD;
dessinerAiguille(oldHourAngle, 50, 8, ILI9341_NAVY);
}
float newHourAngle = (currentHour % 12 + currentMinute / 60.0) * 30 * DEG_TO_RAD;
dessinerAiguille(newHourAngle, 50, 8, SILVER);
float newMinAngle = currentMinute * 6 * DEG_TO_RAD;
dessinerAiguille(newMinAngle, 65, 4, SILVER);
float newSecAngle = currentSecond * 6 * DEG_TO_RAD;
dessinerAiguilleSeconde(newSecAngle, ILI9341_RED);
lcd2.fillCircle(lcd2.width() / 2, lcd2.height() / 2, 5, ILI9341_RED);
lastSecond = currentSecond;
lastMinute = currentMinute;
lastHour = currentHour;
delay(1000);
}
Temp.
Intérieur
Temp.
Extérieur