//#include <U8g2lib.h>
#include <U8g2.h>
//U8G2_SSD1327_128X128_1_4W_HW_SPI u8g2(U8G2_R0, /* CS=*/ 10, /* DC=*/ 9, /* RESET=*/ 8);
U8G2_SSD1327 display(128, 128, &Wire, OLED_RESET, 1000000);

int hours = 5;
int minutes = 30;
int seconds = 0;
char *number[12] = {"6", "5", "4", "3", "2", "1", "12", "11", "10", "9", "8", "7"};

const int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 128;
float radius = min(SCREEN_HEIGHT, SCREEN_WIDTH) / 2 - 1;

const int X_CENTER = SCREEN_WIDTH / 2;
const int Y_CENTER = SCREEN_HEIGHT / 2;

int x1, y1, x2, y2;
double angle;

void setup(void) {
  u8g2.begin();
}

void draw(void) {
  u8g2.clearBuffer();
  u8g2.drawCircle(X_CENTER, Y_CENTER, 1);

  // Draw minute's ticks (60 lines)
  for (int j = 1; j <= 60; j++) {
    angle = j * 6;
    angle = angle * 0.0174533;

    x1 = X_CENTER + (sin(angle) * radius);
    y1 = Y_CENTER + (cos(angle) * radius);
    x2 = X_CENTER + (sin(angle) * (radius));
    y2 = Y_CENTER + (cos(angle) * (radius));
    u8g2.drawLine(x1, y1, x2, y2);
  }

  // Draw hour's ticks (12 lines)
  for (int j = 0; j < 12; j++) {
    angle = j * 30;
    angle = angle * 0.0174533;

    x1 = X_CENTER + (sin(angle) * radius);
    y1 = Y_CENTER + (cos(angle) * radius);
    x2 = X_CENTER + (sin(angle) * (radius - 4));
    y2 = Y_CENTER + (cos(angle) * (radius - 4));
    u8g2.drawLine(x1, y1, x2, y2);

    // Draw hour digits (12 lines)
    x2 = X_CENTER + (sin(angle) * (radius - 8));
    y2 = Y_CENTER + (cos(angle) * (radius - 8));
    u8g2.setFont(u8g2_font_chikita_tr);
    u8g2.drawStr(x2 - 2, y2 + 3, String(number[j]).c_str());
  }

  angle = seconds * 6;
  angle = angle * 0.0174533;
  x2 = X_CENTER + (sin(angle) * (radius - 1));
  y2 = Y_CENTER - (cos(angle) * (radius - 1));
  u8g2.drawLine(X_CENTER, Y_CENTER, x2, y2);

  angle = minutes * 6;
  angle = angle * 0.0174533;
  x2 = X_CENTER + (sin(angle) * (radius - 10));
  y2 = Y_CENTER - (cos(angle) * (radius - 10));
  u8g2.drawLine(X_CENTER, Y_CENTER, x2, y2);

  angle = hours * 30 + ((minutes / 12) * 6);
  angle = angle * 0.0174533;
  x2 = X_CENTER + (sin(angle) * (radius / 2));
  y2 = Y_CENTER - (cos(angle) * (radius / 2));
  u8g2.drawLine(X_CENTER, Y_CENTER, x2, y2);

  u8g2.sendBuffer();
}

void loop(void) {
  seconds += 1;
  if (seconds == 60) {
    seconds = seconds - 60;
    minutes += 1;
  }
  if (minutes == 60) {
    minutes = 0;
    hours += 1;
  }
  if (hours == 24) {
    hours = 1;
  }

  draw();
  delay(1000);  // Update the clock every second
}
SH1107Breakout