#include "RTClib.h"
#include "U8glib.h"
#include "Wire.h"
U8GLIB_SSD1306_128X64 oled(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI
int horas;
int minutos;
int segundos;
char *numero[12]={"6","5","4","3","2","1","12","11","10","9","8","7"};
const int oled_ancho = 128;
const int oled_alto = 64;
float radio = min(oled_alto, oled_ancho)/2-1;
const int centro_x = oled_ancho / 2;
const int centro_y = oled_alto / 2;
/*int x1, y1, x2, y2;
double angulo;*/
//uint8_t horas, minutos, segundos;
RTC_DS1307 rtc;
void setup(void) {
Serial.begin(9600);
rtc.begin();
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//rtc.adjust(DateTime(2023. 5. 29, 12, 48, 0));
//rtc.isrunning();
}
void DatoTiempo(){
DateTime now = rtc.now();
horas = now.hour();
minutos = now.minute();
segundos = now.second();
}
void loop(void) {
DatoTiempo();
oled.firstPage();
do {
draw();
} while( oled.nextPage() );
}
void draw(void ) {
int x1, y1, x2, y2;
double angulo;
//oled.drawCircle(centro_x, centro_y, radio);
oled.drawCircle(centro_x, centro_y, 1);
//draw minute's ticks (60 lines)
for(int j=1; j<=60; j++){
angulo = j*6;
angulo = angulo * 0.0174533;
x1 = centro_x + (sin(angulo) * radio);
y1 = centro_y + (cos(angulo) * radio);
x2 = centro_x + (sin(angulo) * (radio));
y2 = centro_y + (cos(angulo) * (radio));
oled.drawLine(x1,y1,x2,y2);
}
//draw hour's ticks (12 lines)
for(int j=0; j<12; j++){
angulo = j*30;
angulo = angulo * 0.0174533;
x1 = centro_x + (sin(angulo) * radio);
y1 = centro_y + (cos(angulo) * radio);
x2 = centro_x + (sin(angulo) * (radio - 4));
y2 = centro_y + (cos(angulo) * (radio - 4));
oled.drawLine(x1,y1,x2,y2);
//draw hour digits(12 lines)
x2 = centro_x + (sin(angulo) * (radio - 8));
y2 = centro_y + (cos(angulo) * (radio - 8));
oled.setFont(u8g_font_chikita);
oled.drawStr(x2-2, y2+3, String(numero[j]).c_str());
/* oled.setFont(u8g_font_6x12);
oled.setPrintPos(58, 14);
oled.print("12");
oled.setPrintPos(39, 35);
oled.print("9");
oled.setPrintPos(62, 58);
oled.print("6");
oled.setPrintPos(85, 35);
oled.print("3"); */
angulo = segundos*6;
angulo = angulo * 0.0174533;
x2 = centro_x + (sin(angulo) * (radio - 1));
y2 = centro_y - (cos(angulo) * (radio - 1));
oled.drawLine(centro_x,centro_y,x2,y2);
angulo = minutos*6;
angulo = angulo * 0.0174533;
x2 = centro_x + (sin(angulo) * (radio - 10));
y2 = centro_y - (cos(angulo) * (radio - 10));
oled.drawLine(centro_x,centro_y,x2,y2);
angulo = horas*30 + ((minutos / 12) * 6);
angulo = angulo * 0.0174533;
x2 = centro_x + (sin(angulo) * (radio / 2));
y2 = centro_y - (cos(angulo) * (radio / 2));
oled.drawLine(centro_x,centro_y,x2,y2);
}
}