#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include "time.h"
#include "Image.h"
#include "pgmspace.h"
Adafruit_ILI9341 tft(16,4);
void draw_clock_hand(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int thickness, uint16_t color) {
if(abs(x1-x0)>abs(y1-y0)) {
if((x1-x0)>0) {
for(int16_t x=x0; x<x1; x++) {
int16_t y = (((y1-y0)*(x-x0))/(x1-x0))+y0;
tft.fillCircle(x,y, thickness, color);
}
}
else if((x1-x0)<0) {
for(int16_t x=x1; x<x0; x++) {
int16_t y = (((y1-y0)*(x-x0))/(x1-x0))+y0;
tft.fillCircle(x,y, thickness, color);
}
}
}
else {
if((y1-y0)>0) {
for(int16_t y=y0; y<y1; y++) {
int16_t x = (((x1-x0)*(y-y0))/(y1-y0))+x0;
tft.fillCircle(x,y, thickness, color);
}
}
else if((y1-y0)<0) {
for(int16_t y=y1; y<y0; y++) {
int16_t x = (((x1-x0)*(y-y0))/(y1-y0))+x0;
tft.fillCircle(x,y, thickness, color);
}
}
}
}
void drawBitmap(int x, int y, const uint16_t *bitmap, int w, int h) {
for(int i=0; i<h; i++) {
for(int j=0; j<w; j++) {
noInterrupts();
tft.drawPixel(x+j, y+i, pgm_read_word(&bitmap[i*w+j]));
}
}
interrupts();
}
void drawCroppedBitmap(int x0, int y0, int x1, int y1, const uint16_t *bitmap) {
for(int i=y0-40; i<y1-40; i++) {
for(int j=x0; j<x1; j++) {
noInterrupts();
tft.drawPixel(j, i+40, pgm_read_word(&bitmap[i*240+j]));
}
}
interrupts();
}
void setup() {
tft.begin();
tft.fillScreen(0x0000);
}
void show_time(int hr, int minute, int sec) {
int angle = 6*sec;
while(angle<360) {
float rad = (angle*3.14)/180;
int x_sec = 120+90*cos((rad)-(3.14/2));
int y_sec = 160+90*sin((rad)-(3.14/2));
int x_min = 120+75*cos((((minute*3.14)/30)+(rad/60))-(3.14/2));
int y_min = 160+75*sin((((minute*3.14)/30)+(rad/60))-(3.14/2));
int x_hr = 120+45*cos((((hr*3.14)/6)+((minute*3.14)/360)+(rad/540))-(3.14/2));
int y_hr = 160+45*sin((((hr*3.14)/6)+((minute*3.14)/360)+(rad/540))-(3.14/2));
draw_clock_hand((x_hr-120)/3+120, (y_hr-160)/3+160, x_hr, y_hr, 3, 0x7BEF);
draw_clock_hand(120, 160, (x_hr-120)/3+120, (y_hr-160)/3+160, 1, 0x7BEF);
draw_clock_hand((x_hr-120)/1.9+120, (y_hr-160)/1.9+160, (x_hr-120)/1.1+120, (y_hr-160)/1.1+160, 1, 0x0000);
draw_clock_hand((x_min-120)/5+120, (y_min-160)/5+160, x_min, y_min, 3, 0xFFFF);
draw_clock_hand(120, 160, (x_min-120)/5+120, (y_min-160)/5+160, 1, 0xFFFF);
draw_clock_hand((x_min-120)/2+120, (y_min-160)/2+160, (x_min-120)/1.05+120, (y_min-160)/1.05+160, 1, 0xFBE0);
draw_clock_hand(120, 160, x_sec, y_sec, 1, 0x000F);
draw_clock_hand(120, 160, 120-((x_sec-120)/4), 160-((y_sec-160)/4), 1, 0x000F);
tft.fillCircle(120, 160, 7, 0x000F);
delay(1000);
noInterrupts();
drawCroppedBitmap(min(120, x_sec)-2, min(160, y_sec)-2, max(120, x_sec)+2, max(160, y_sec)+2, image);
drawCroppedBitmap(min(120, 120-((x_sec-120)/4))-2, min(160, 160-((y_sec-160)/4))-2, max(120, 120-((x_sec-120)/4))+2, max(160, 160-((y_sec-160)/4))+2, image);
tft.fillCircle(120, 160, 7, 0x000F);
drawCroppedBitmap(min(120, x_min)-4, min(160, y_min)-4, max(120, x_min)+4, max(160, y_min)+4, image);
drawCroppedBitmap(min(120, x_hr)-4, min(160, y_hr)-4, max(120, x_hr)+4, max(160, y_hr)+4, image);
interrupts();
while(minute == 59) {
hr = (hr+1)%12;
}
if(angle == 360-6) {
minute = (minute+1)%60;
}
angle = (angle+6)%360;
}
}
void loop() {
drawBitmap(0, 40, image, 240, 240);
show_time(5,50,10);
}