#include <SPI.h>
#include "Ucglib.h"
#include <RTClib.h>
#include <Wire.h>
RTC_DS3231 rtc;
// Define the pins for the ILI9486 TFT display
Ucglib_ILI9486_18x320x480_SWSPI ucg(/*sclk=*/ 13, /*data=*/ 11, /*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
void setup() {
// Initialize communication with the TFT display
ucg.begin(UCG_FONT_MODE_SOLID);
// Set rotation of the display (optional, adjust as needed)
// ucg.setRotate(90);
// Clear the screen
ucg.clearScreen();
// Initialize the RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC lost power and if so, set the time
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void displayAnalogClock() {
DateTime now = rtc.now();
// Set clock center and radius
int centerX = ucg.getWidth() / 2;
int centerY = ucg.getHeight() / 2;
int radius = 100;
// Draw clock face
ucg.setColor(255, 255, 255);
ucg.drawCircle(centerX, centerY, radius, UCG_DRAW_ALL);
// Draw clock hands
drawClockHand(centerX, centerY, radius * 0.6, now.hour() % 12, 12, 6);
drawClockHand(centerX, centerY, radius * 0.8, now.minute(), 60, 3);
drawClockHand(centerX, centerY, radius * 0.9, now.second(), 60, 1);
}
void drawClockHand(int centerX, int centerY, int length, int value, int range, int thickness) {
float angle = map(value, 0, range, 0, 360) - 90; // Map the value to an angle (subtract 90 to start from 12 o'clock)
angle = radians(angle); // Convert angle to radians
int xEnd = centerX + length * cos(angle);
int yEnd = centerY + length * sin(angle);
ucg.setColor(255, 255, 255);
ucg.drawLine(centerX, centerY, xEnd, yEnd);
}
void ucglib_graphics_test(void) {
// Set background gradient colors
//ucg.setColor(0, 0, 40, 80);
//ucg.setColor(1, 80, 0, 40);
// ucg.setColor(2, 255, 0, 255);
ucg.setColor(3, 0, 0, 0);
// Draw a gradient box
ucg.drawGradientBox(0, 0, ucg.getWidth(), ucg.getHeight());
// Set text color
ucg.setColor(255, 0, 0);
// Print text on the screen
ucg.setColor(255, 160, 0);
ucg.setPrintDir(0);
ucg.setPrintPos(2, 50);
ucg.setColor(55, 160, 0);
// ucg.print("Ucglib");
ucg.setColor(255, 255, 255);
ucg.setPrintPos(2, 150 + 20);
//ucg.print("GraphicsTest");
// Set text color to yellow
ucg.setColor(255, 2500, 250);
ucg.setPrintPos(20, 50);
ucg.setPrintPos(2, 250 + 20);
ucg.print("code by ARVIND");
}
void loop() {
// Run the graphics test function
ucglib_graphics_test();
// Display the analog clock
displayAnalogClock();
// Delay for 1000 milliseconds (1 second)
delay(1000);
// Clear the screen for the next iteration
ucg.clearScreen();
}