Skip to content

Instantly share code, notes, and snippets.

@marcelrv
Created August 12, 2016 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelrv/ec04cdcdf0a2df79c8ddfa286e1112f2 to your computer and use it in GitHub Desktop.
Save marcelrv/ec04cdcdf0a2df79c8ddfa286e1112f2 to your computer and use it in GitHub Desktop.
//Подключение библиотек
#include <UTFT.h>
#include <DS1307.h>
//Подключения шрифта
extern uint8_t BigFont[];
//Подключение дисплея TFT01-22SP
UTFT myGLCD(TFT01_22SP,9,8,50,51,53);
//Подключение модуля DS1307
DS1307 rtc(A4, A5);
Time t;
//Центральные координаты для рисования стрелочных часов
int clockCenterX=119;
int clockCenterY=159;
int oldsec=0;
void setup(){
//
myGLCD.InitLCD(0); // вертикальное отображение
myGLCD.setFont(BigFont);
rtc.halt(false);
// второй pin для регулировки яркости дисплея
analogWrite(2, 100);
/*Для установки времени, введите ваши параметры и
раскомментируйте 3 строки ниже и загрузите скетч
на ардуино,а потом назад закомментируйте обратно*/
//rtc.setTime(17, 07, 0);
//rtc.setDate(14, 9, 2014);
//rtc.setDOW(SUNDAY);
}
void drawDisplay()
{
// Очистка дисплея
myGLCD.clrScr();
//Отображение текущей даты
myGLCD.setColor(0, 255, 0);
myGLCD.print(rtc.getDateStr(), CENTER, 5);
//Отображение дня недели
myGLCD.setColor(0, 255, 0);
myGLCD.print(rtc.getDOWStr(), CENTER, 290);
// Рисуем циферблат
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(0, 0, 0);
for (int i=0; i<5; i++)
{
myGLCD.drawCircle(clockCenterX, clockCenterY, 119-i);
}
for (int i=0; i<5; i++)
{
myGLCD.drawCircle(clockCenterX, clockCenterY, i);
}
//Отображаем числа на циферблате
myGLCD.setColor(192, 192, 255);
myGLCD.print("3", clockCenterX+92, clockCenterY-8);
myGLCD.print("6", clockCenterX-8, clockCenterY+89);
myGLCD.print("9", clockCenterX-109, clockCenterY-8);
myGLCD.print("12", clockCenterX-16, clockCenterY-109);
//Рисуем метки часов в виде рысочек
for (int i=0; i<12; i++)
{
if ((i % 3)!=0)
drawMark(i);
}
t = rtc.getTime();
drawMin(t.min);
drawHour(t.hour, t.min);
drawSec(t.sec);
oldsec=t.sec;
}
void drawMark(int h)//Отрисовка меток
{
float x1, y1, x2, y2;
h=h*30;
h=h+270;
x1=110*cos(h*0.0175);
y1=110*sin(h*0.0175);
x2=100*cos(h*0.0175);
y2=100*sin(h*0.0175);
myGLCD.drawLine(x1+clockCenterX, y1+clockCenterY, x2+clockCenterX, y2+clockCenterY);
}
void drawSec(int s)//отрисовка секундной стрелки
{
float x1, y1, x2, y2;
int ps = s-1;
myGLCD.setColor(0, 0, 0);
if (ps==-1)
ps=59;
ps=ps*6;
ps=ps+270;
x1=95*cos(ps*0.0175);
y1=95*sin(ps*0.0175);
x2=5*cos(ps*0.0175);
y2=5*sin(ps*0.0175);
myGLCD.drawLine(x1+clockCenterX, y1+clockCenterY, x2+clockCenterX, y2+clockCenterY);
myGLCD.setColor(255, 0, 0);
s=s*6;
s=s+270;
x1=95*cos(s*0.0175);
y1=95*sin(s*0.0175);
x2=5*cos(s*0.0175);
y2=5*sin(s*0.0175);
myGLCD.drawLine(x1+clockCenterX, y1+clockCenterY, x2+clockCenterX, y2+clockCenterY);
}
void drawMin(int m)//отрисовка минутной стрелки
{
float x1, y1, x2, y2, x3, y3, x4, y4;
int pm = m-1;
myGLCD.setColor(0, 0, 0);
if (pm==-1)
pm=59;
pm=pm*6;
pm=pm+270;
x1=80*cos(pm*0.0175);
y1=80*sin(pm*0.0175);
x2=5*cos(pm*0.0175);
y2=5*sin(pm*0.0175);
x3=30*cos((pm+4)*0.0175);
y3=30*sin((pm+4)*0.0175);
x4=30*cos((pm-4)*0.0175);
y4=30*sin((pm-4)*0.0175);
myGLCD.drawLine(x1+clockCenterX, y1+clockCenterY, x3+clockCenterX, y3+clockCenterY);
myGLCD.drawLine(x3+clockCenterX, y3+clockCenterY, x2+clockCenterX, y2+clockCenterY);
myGLCD.drawLine(x2+clockCenterX, y2+clockCenterY, x4+clockCenterX, y4+clockCenterY);
myGLCD.drawLine(x4+clockCenterX, y4+clockCenterY, x1+clockCenterX, y1+clockCenterY);
myGLCD.setColor(0, 255, 0);
m=m*6;
m=m+270;
x1=80*cos(m*0.0175);
y1=80*sin(m*0.0175);
x2=5*cos(m*0.0175);
y2=5*sin(m*0.0175);
x3=30*cos((m+4)*0.0175);
y3=30*sin((m+4)*0.0175);
x4=30*cos((m-4)*0.0175);
y4=30*sin((m-4)*0.0175);
myGLCD.drawLine(x1+clockCenterX, y1+clockCenterY, x3+clockCenterX, y3+clockCenterY);
myGLCD.drawLine(x3+clockCenterX, y3+clockCenterY, x2+clockCenterX, y2+clockCenterY);
myGLCD.drawLine(x2+clockCenterX, y2+clockCenterY, x4+clockCenterX, y4+clockCenterY);
myGLCD.drawLine(x4+clockCenterX, y4+clockCenterY, x1+clockCenterX, y1+clockCenterY);
}
void drawHour(int h, int m)//отрисовка часовой стрелки
{
float x1, y1, x2, y2, x3, y3, x4, y4;
int ph = h;
myGLCD.setColor(0, 0, 0);
if (m==0)
{
ph=((ph-1)*30)+((m+59)/2);
}
else
{
ph=(ph*30)+((m-1)/2);
}
ph=ph+270;
x1=60*cos(ph*0.0175);
y1=60*sin(ph*0.0175);
x2=5*cos(ph*0.0175);
y2=5*sin(ph*0.0175);
x3=20*cos((ph+5)*0.0175);
y3=20*sin((ph+5)*0.0175);
x4=20*cos((ph-5)*0.0175);
y4=20*sin((ph-5)*0.0175);
myGLCD.drawLine(x1+clockCenterX, y1+clockCenterY, x3+clockCenterX, y3+clockCenterY);
myGLCD.drawLine(x3+clockCenterX, y3+clockCenterY, x2+clockCenterX, y2+clockCenterY);
myGLCD.drawLine(x2+clockCenterX, y2+clockCenterY, x4+clockCenterX, y4+clockCenterY);
myGLCD.drawLine(x4+clockCenterX, y4+clockCenterY, x1+clockCenterX, y1+clockCenterY);
myGLCD.setColor(255, 255, 0);
h=(h*30)+(m/2);
h=h+270;
x1=60*cos(h*0.0175);
y1=60*sin(h*0.0175);
x2=5*cos(h*0.0175);
y2=5*sin(h*0.0175);
x3=20*cos((h+5)*0.0175);
y3=20*sin((h+5)*0.0175);
x4=20*cos((h-5)*0.0175);
y4=20*sin((h-5)*0.0175);
myGLCD.drawLine(x1+clockCenterX, y1+clockCenterY, x3+clockCenterX, y3+clockCenterY);
myGLCD.drawLine(x3+clockCenterX, y3+clockCenterY, x2+clockCenterX, y2+clockCenterY);
myGLCD.drawLine(x2+clockCenterX, y2+clockCenterY, x4+clockCenterX, y4+clockCenterY);
myGLCD.drawLine(x4+clockCenterX, y4+clockCenterY, x1+clockCenterX, y1+clockCenterY);
}
void loop()//основной цикл работы стрелочных часов
{
int x, y;
t = rtc.getTime();
drawDisplay();
while (true)
{
if (oldsec!=t.sec)
{
int t1=0;
int t2=0;
int t3=0;
if(t.hour>=12){
t1=(t.hour-12)*5;
}
else
{
t1=t.hour*5;
}
t2=t.min/12;
t3=t1+t2;
int a=t.min+1;
int b=t.min+2;
int c=t.min+3;
int d=t3+1;
int e=t3+2;
int f=t3+3;
int g=t3+4;
if ((t.sec==d)||(t.sec==e)||(t.sec==f)||(t.sec==g))
{
drawHour(t.hour, t.min);
}
if ((t.sec==a)||(t.sec==b)||(t.sec==c))
{
drawMin(t.min);
}
if (t.sec==0)
{
drawMin(t.min);
drawHour(t.hour, t.min);
}
drawSec(t.sec);
oldsec=t.sec;
}
delay(10);
t = rtc.getTime();
}
}
@marcelrv
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment