#include <SPI.h>
#include "Ucglib.h"
#include <RTClib.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 displayCurrentTime() {
DateTime now = rtc.now();
// Set text color to yellow
ucg.setColor(255, 255, 255);
// Set font size for double-sized digits
ucg.setFontMode(UCG_FONT_MODE_TRANSPARENT);
ucg.setFont(ucg_font_helvR24_tr);
// Print the current time with double-sized digits
int xPos = 20; // Adjust the X position as needed
int yPos = 100; // Adjust the Y position as needed
int fontSize = 2; // Double-sized digits
ucg.setFont(ucg_font_helvR24_tr); // Set the font again (just to be sure)
// Hour
ucg.setPrintPos(xPos, yPos);
ucg.setFont(ucg_font_helvR24_tr); // Set the font again (just to be sure)
ucg.setPrintPos(xPos, yPos);
ucg.setFontMode(UCG_FONT_MODE_TRANSPARENT);
ucg.setFont(ucg_font_helvR24_tr);
ucg.setFontPosBottom();
//ucg.setFontPosBottom
ucg.setScale2x2();
ucg.print(now.hour(), DEC);
// Minute
xPos += ucg.getStrWidth("00:"); // Adjust the X position based on the width of the hour
ucg.setPrintPos(xPos, yPos);
ucg.print(":");
ucg.setPrintPos(xPos + ucg.getStrWidth(":"), yPos);
ucg.print(now.minute(), DEC);
// Second
xPos += ucg.getStrWidth("00:"); // Adjust the X position based on the width of the minute
ucg.setPrintPos(xPos, yPos);
ucg.print(":");
ucg.setPrintPos(xPos + ucg.getStrWidth(":"), yPos);
ucg.print(now.second(), DEC);
// Reset font size and position
ucg.setFontMode(UCG_FONT_MODE_SOLID);
ucg.setScale2x2();
}
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, 255, 255);
// 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("GraphTest");
// Set text color to white
ucg.setColor(255, 250, 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 current time
displayCurrentTime();
// Delay for 5 seconds before restarting
delay(5000);
// Clear the screen for the next iteration
ucg.clearScreen();
}