Files
arduino-clock/arduino_clock.ino

176 lines
5.5 KiB
C++

#include <RTClib.h>
#include <SPI.h>
#include "LCD_Driver.h"
#include "GUI_Paint.h"
#include "image.h"
#include <Wire.h>
RTC_DS1307 rtc;
int DIGIT_SIZE = 40;
// Month + weekday names
const char* MONTH_NAMES[] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
const char* WEEKDAY_NAMES[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
// ---------------------------
// DRAW HELPERS
// ---------------------------
void drawSegment(int x, int y, int w, int h, uint16_t color) {
Paint_DrawRectangle(x, y, x + w, y + h, color,
DOT_PIXEL_1X1, DRAW_FILL_FULL);
}
void drawDigit(int x, int y, int size, int digit, uint16_t color) {
int thick = size / 5;
int longSeg = size;
int wideSeg = size;
bool A=false, B=false, C=false, D=false, E=false, F=false, G=false;
switch (digit) {
case 0: A=B=C=D=E=F=true; break;
case 1: B=C=true; break;
case 2: A=B=G=E=D=true; break;
case 3: A=B=G=C=D=true; break;
case 4: F=G=B=C=true; break;
case 5: A=F=G=C=D=true; break;
case 6: A=F=G=E=C=D=true; break;
case 7: A=B=C=true; break;
case 8: A=B=C=D=E=F=G=true; break;
case 9: A=B=C=D=F=G=true; break;
}
if (A) drawSegment(x + thick, y, wideSeg, thick, color);
if (B) drawSegment(x + wideSeg + thick, y + thick, thick, longSeg, color);
if (C) drawSegment(x + wideSeg + thick, y + thick + longSeg + thick, thick, longSeg, color);
if (D) drawSegment(x + thick, y + 2*longSeg + 2*thick, wideSeg, thick, color);
if (E) drawSegment(x, y + thick + longSeg + thick, thick, longSeg, color);
if (F) drawSegment(x, y + thick, thick, longSeg, color);
if (G) drawSegment(x + thick, y + longSeg + thick, wideSeg, thick, color);
}
void drawColon(int x, int y, int size, uint16_t color) {
int dot = size / 5;
drawSegment(x, y + size/2 - 10, dot, dot, color);
drawSegment(x, y + size + 10, dot, dot, color);
}
// ---------------------------
// SETUP
// ---------------------------
void setup() {
Wire.begin();
rtc.begin();
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // run once if needed
Config_Init();
LCD_Init();
LCD_Clear(BLACK);
Paint_NewImage(LCD_WIDTH, LCD_HEIGHT, 90, BLACK);
Paint_Clear(BLACK);
Paint_DrawString_EN(30, 110, "Starting Up...", &Font24, BLACK, GREEN);
delay(1000);
Paint_Clear(BLACK);
}
// ---------------------------
// LOOP
// ---------------------------
void loop() {
static unsigned long lastUpdate = 0;
static char lastTime[16] = "";
if (millis() - lastUpdate >= 1000) {
lastUpdate = millis();
DateTime now = rtc.now();
// Convert to 12-hour format
int hour12 = now.hour();
if (hour12 == 0) hour12 = 12;
else if (hour12 > 12) hour12 -= 12;
char timebuf[16];
sprintf(timebuf, "%02d:%02d", hour12, now.minute());
// Build weekday + date strings
char weekdaybuf[16];
char datebuf[32];
sprintf(weekdaybuf, "%s", WEEKDAY_NAMES[now.dayOfTheWeek()]);
sprintf(datebuf, "%s %d, %d",
MONTH_NAMES[now.month() - 1],
now.day(),
now.year());
// Draw weekday + date (always update)
Paint_DrawString_EN(15, 160, weekdaybuf, &Font24, BLACK, WHITE);
Paint_DrawString_EN(15, 200, datebuf, &Font24, BLACK, WHITE);
// Only update digits that changed
if (strcmp(timebuf, lastTime) != 0) {
int size = DIGIT_SIZE;
// Extract digits
int h1 = hour12 / 10;
int h2 = hour12 % 10;
int m1 = now.minute() / 10;
int m2 = now.minute() % 10;
// HOUR tens
if (lastTime[0] != timebuf[0]) {
if (h1 == 0)
{
drawDigit(10, 20, size, h1, BLACK);
}
else
{
drawDigit(10, 20, size, lastTime[0] - '0', BLACK);
drawDigit(10, 20, size, h1, WHITE);
}
}
// HOUR ones
if (lastTime[1] != timebuf[1]) {
drawDigit(10 + size + 30, 20, size, lastTime[1] - '0', BLACK);
drawDigit(10 + size + 30, 20, size, h2, WHITE);
}
// COLON (always drawn)
Paint_DrawRectangle(10 + 2*(size + 35), 50,
10 + 2*(size + 35) + 10, 60,
WHITE, DOT_PIXEL_1X1, DRAW_FILL_FULL);
Paint_DrawRectangle(10 + 2*(size + 35), 100,
10 + 2*(size + 35) + 10, 110,
WHITE, DOT_PIXEL_1X1, DRAW_FILL_FULL);
// MIN tens
if (lastTime[3] != timebuf[3]) {
drawDigit(10 + 2*(size + 30) + 30, 20, size, lastTime[3] - '0', BLACK);
drawDigit(10 + 2*(size + 30) + 30, 20, size, m1, WHITE);
}
// MIN ones
if (lastTime[4] != timebuf[4]) {
drawDigit(10 + 3*(size + 30) + 30, 20, size, lastTime[4] - '0', BLACK);
drawDigit(10 + 3*(size + 30) + 30, 20, size, m2, WHITE);
}
strcpy(lastTime, timebuf);
}
}
}