#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <stdio.h>
#include <SPI.h>
#define TFT_CS 17
#define TFT_DC 20
#define TFT_RST 21
#define SPI1_MISO 12
#define SPI1_MOSI 11
#define SPI1_SCLK 10
#define TFT_WIDTH 240
#define TFT_HEIGHT 240
MbedSPI SPI1(SPI1_MISO, SPI1_MOSI, SPI1_SCLK);
Adafruit_ST7789 tft = Adafruit_ST7789(&SPI1,TFT_CS, TFT_DC, TFT_RST);
RTC_DS3231 rtc;
#define BLACK 0x0000
#define WHITE 0xFFFF
#define RED 0xF800
#define GREEN 0x07E0
#define BLUE 0x001F
#define YELLOW 0xFFE0
int clockCenterX = TFT_WIDTH / 2;
int clockCenterY = TFT_HEIGHT / 2;
void drawClockHands(int hours, int minutes, int seconds) {
static int prevSecond = -1;
static int prevMinute = -1;
static int prevHour = -1;
float hourAngle = (hours + minutes / 60.0) * 30 * PI / 180;
float minuteAngle = (minutes + seconds / 60.0) * 6 * PI / 180;
float secondAngle = seconds * 6 * PI / 180;
int hx = clockCenterX + 50 * cos(hourAngle - PI / 2);
int hy = clockCenterY + 50 * sin(hourAngle - PI / 2);
int mx = clockCenterX + 70 * cos(minuteAngle - PI / 2);
int my = clockCenterY + 70 * sin(minuteAngle - PI / 2);
int sx = clockCenterX + 90 * cos(secondAngle - PI / 2);
int sy = clockCenterY + 90 * sin(secondAngle - PI / 2);
if (prevSecond != seconds) {
// Clear the previous hands
if (prevHour != -1 && prevMinute != -1 && prevSecond != -1) {
float prevHourAngle = (prevHour + prevMinute / 60.0) * 30 * PI / 180;
float prevMinuteAngle = (prevMinute + prevSecond / 60.0) * 6 * PI / 180;
float prevSecondAngle = prevSecond * 6 * PI / 180;
int prevHx = clockCenterX + 50 * cos(prevHourAngle - PI / 2);
int prevHy = clockCenterY + 50 * sin(prevHourAngle - PI / 2);
int prevMx = clockCenterX + 70 * cos(prevMinuteAngle - PI / 2);
int prevMy = clockCenterY + 70 * sin(prevMinuteAngle - PI / 2);
int prevSx = clockCenterX + 90 * cos(prevSecondAngle - PI / 2);
int prevSy = clockCenterY + 90 * sin(prevSecondAngle - PI / 2);
tft.drawLine(clockCenterX, clockCenterY, prevHx, prevHy, BLACK);
tft.drawLine(clockCenterX, clockCenterY, prevMx, prevMy, BLACK);
tft.drawLine(clockCenterX, clockCenterY, prevSx, prevSy, BLACK);
}
// Draw the new hands
tft.drawLine(clockCenterX, clockCenterY, hx, hy, RED);
tft.drawLine(clockCenterX, clockCenterY, mx, my, GREEN);
tft.drawLine(clockCenterX, clockCenterY, sx, sy, YELLOW);
// Update previous values
prevHour = hours;
prevMinute = minutes;
prevSecond = seconds;
}
}
void drawClockFace() {
tft.fillScreen(BLACK); // Clear the screen
tft.drawCircle(clockCenterX, clockCenterY, 100, WHITE);
// Draw the clock numbers
tft.setTextSize(2);
tft.setTextColor(WHITE);
for (int i = 1; i <= 12; i++) {
float angle = i * 30 * PI / 180;
printf("Angle for hour: %d is %f\n", i, angle);
int x = clockCenterX + 90 * cos(angle - PI / 2);
int y = clockCenterY + 90 * sin(angle - PI / 2);
tft.setCursor(x - 5, y - 5);
tft.print(i);
}
}
void setup() {
UART Serial2(8, 9, NC, NC);
Serial2.begin(115200);
Serial2.println("Initializing...");
//TwoWire tw(0,1);
//tw.begin();
Serial2.println("Initializing...");
if (!rtc.begin()) {
Serial2.println("Couldn't find RTC");
while (1);
}
Serial2.println("test0");
if (rtc.lostPower()) {
Serial2.println("RTC lost power, setting the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
Serial2.println("test");
tft.init(TFT_WIDTH, TFT_HEIGHT);
Serial2.println("test");
tft.setRotation(2);
tft.fillScreen(BLACK); // Clear the screen
Serial2.println("Display initialized.");
drawClockFace();
Serial2.println("Clock face drawn.");
}
void loop() {
DateTime now = rtc.now();
drawClockHands(now.hour(), now.minute(), now.second());
delay(1000); // Update every second
// byte error, address;
// int nDevices;
// Serial.println("Scanning...");
// nDevices = 0;
// for(address = 1; address < 127; address++ ) {
// wire1.beginTransmission(address);
// error = wire1.endTransmission();
// if (error == 0) {
// Serial.print("I2C device found at address 0x");
// if (address<16) {
// Serial.print("0");
// }
// Serial.println(address,HEX);
// nDevices++;
// }
// else if (error==4) {
// Serial.print("Unknow error at address 0x");
// if (address<16) {
// Serial.print("0");
// }
// Serial.println(address,HEX);
// }
// }
// if (nDevices == 0) {
// Serial.println("No I2C devices found\n");
// }
// else {
// Serial.println("done\n");
// }
// delay(5000);
}