#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <RTClib.h> // Include RTC Library
U8G2_SH1107_128X128_1_HW_I2C u8g2(U8G2_R0);
RTC_DS3231 rtc; // Initialize RTC object
// Constants for center and logo
int center_x = 64;
int center_y = 64;
const unsigned char upir_logo [] PROGMEM = {0xEA, 0x3A, 0xAA, 0x28, 0x6A, 0x1A, 0x26, 0x2A};
// LED pin
const int ledPin = 13;
void setup(void) {
u8g2.begin();
u8g2.setContrast(255);
rtc.begin(); // Initialize RTC
pinMode(ledPin, OUTPUT); // Initialize LED pin as output
}
void draw_background() {
float xpos, ypos, xpos2, ypos2;
u8g2.setDrawColor(1); // set the drawing color to white
u8g2.drawCircle(center_x, center_y, 63, U8G2_DRAW_ALL); // draw fullscreen circle
for (int i = 0; i < 60; i++) {
xpos = round(center_x + sin(radians(i * 6)) * 60);
ypos = round(center_y - cos(radians(i * 6)) * 60);
u8g2.drawPixel(xpos, ypos);
}
for (int i = 0; i < 12; i++) {
if ((i % 3) != 0) {
xpos = round(center_x + sin(radians(i * 30)) * 54);
ypos = round(center_y - cos(radians(i * 30)) * 54);
xpos2 = round(center_x + sin(radians(i * 30)) * 46);
ypos2 = round(center_y - cos(radians(i * 30)) * 46);
u8g2.drawLine(xpos, ypos, xpos2, ypos2);
}
}
u8g2.setFont(u8g2_font_8x13B_mn);
u8g2.drawStr(57, 20, "12");
u8g2.drawStr(112, 69, "3");
u8g2.drawStr(61, 120, "6");
u8g2.drawStr(9, 69, "9");
}
void draw_hand_thin(int hand_angle, int hand_length_long, int hand_length_short) {
float xpos, ypos, xpos2, ypos2;
xpos = round(center_x + sin(radians(hand_angle)) * hand_length_long);
ypos = round(center_y - cos(radians(hand_angle)) * hand_length_long);
xpos2 = round(center_x + sin(radians(hand_angle + 180)) * hand_length_short);
ypos2 = round(center_y - cos(radians(hand_angle + 180)) * hand_length_short);
u8g2.drawLine(xpos, ypos, xpos2, ypos2);
u8g2.setDrawColor(0);
u8g2.drawDisc(xpos2, ypos2, 3);
u8g2.setDrawColor(1);
u8g2.drawCircle(xpos2, ypos2, 3, U8G2_DRAW_ALL);
}
void draw_hand_bold(int hand_angle, int hand_length_long, int hand_length_short, int hand_dot_size) {
float xpos, ypos, xpos2, ypos2;
float tri_xoff, tri_yoff;
xpos = round(center_x + sin(radians(hand_angle)) * hand_length_long);
ypos = round(center_y - cos(radians(hand_angle)) * hand_length_long);
xpos2 = round(center_x + sin(radians(hand_angle)) * hand_length_short);
ypos2 = round(center_y - cos(radians(hand_angle)) * hand_length_short);
tri_xoff = round(sin(radians(hand_angle + 90)) * hand_dot_size);
tri_yoff = round(-cos(radians(hand_angle + 90)) * hand_dot_size);
u8g2.drawLine(center_x, center_y, xpos2, ypos2);
u8g2.drawDisc(xpos, ypos, hand_dot_size);
u8g2.drawDisc(xpos2, ypos2, hand_dot_size);
u8g2.drawTriangle(xpos + tri_xoff, ypos + tri_yoff, xpos - tri_xoff, ypos - tri_yoff, xpos2 + tri_xoff, ypos2 + tri_yoff);
u8g2.drawTriangle(xpos2 + tri_xoff, ypos2 + tri_yoff, xpos2 - tri_xoff, ypos2 - tri_yoff, xpos - tri_xoff, ypos - tri_yoff);
}
void loop(void) {
DateTime now = rtc.now();
int time_hours = now.hour();
int time_minutes = now.minute();
int time_seconds = now.second();
// Control LED based on time
if (time_hours == 14 && time_minutes == 14) { // 7:52 PM
digitalWrite(ledPin, HIGH); // Turn on LED
} else if (time_hours == 14 && time_minutes == 15) { // 7:57 PM
digitalWrite(ledPin, LOW); // Turn off LED
}
u8g2.firstPage();
do {
draw_background();
draw_hand_bold(time_minutes * 6, 48, 15, 2);
draw_hand_bold(time_hours * 30 + (time_minutes / 2), 32, 15, 2);
draw_hand_thin(time_seconds * 6, 56, 24);
u8g2.drawXBMP(57, 24, 16, 4, upir_logo);
u8g2.setColorIndex(0);
u8g2.drawDisc(center_x, center_y, 4);
u8g2.setColorIndex(1);
u8g2.drawCircle(center_x, center_y, 4);
} while (u8g2.nextPage());
}