

void drawDisplay()
{
  //t = rtc.getTime();
//  t.hour=rtc_clock.get_hours();
//  t.min=rtc_clock.get_minutes()
// sec=t.sec;
  t = rtc.getTime();
min1= t.min;
hour1=t.hour;
  drawSec(t.sec);
  oldsec=t.sec;
if (t.sec==0)
{
  if(( t.hour>5)&&( t.hour<7)&&(t.min>29))
{night=0;
savefld();//запись новой даты в файлы полива цветов
}
else
{night=1;
}

 drawMin(t.min);
  drawHour(t.hour, t.min);
    myGLCD.setColor(255, 255, 255);
  myGLCD.setBackColor(0, 0, 0);
   myGLCD.setFont( SevenSegNumFont);
//   myGLCD.print(rtc_clock.get_hours(), clockCenterX+92+111+32, clockCenterY-8-20);
  if (night==1) 
{  myGLCD.setColor(192, 192, 255);

}
else
{  myGLCD.setColor(255,255, 255);

}  
    if (t.hour<10)
  {
    myGLCD.printNumI(0,clockCenterX+340, clockCenterY-8-20);
    myGLCD.printNumI(t.hour, clockCenterX+340+32, clockCenterY-8-20);
  }
  else
  {
    myGLCD.printNumI(t.hour,clockCenterX+340, clockCenterY-8-20);
  }
  if (t.min<10)
  {
    myGLCD.printNumI(0,clockCenterX+340+96, clockCenterY-8-20);
    myGLCD.printNumI(t.min, clockCenterX+340+128, clockCenterY-8-20);
  }
  else
  {
    myGLCD.printNumI(t.min, clockCenterX+340+96, clockCenterY-8-20);
  }
  
}
if (q==1)
{ drawMin(t.min);
  drawHour(t.hour, t.min);
  printDate();
}
if( (hour1==0) && (min1==0))
{ 
  printDate();
}
}
//****************************************************************************
void drawMark(int h)
{
  float x1, y1, x2, y2;
  
  h=h*30;
  h=h+270;
  
  x1=2*110*cos(h*0.0175);
  y1=2*110*sin(h*0.0175);
  x2=2*100*cos(h*0.0175);
  y2=2*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=2*95*cos(ps*0.0175);
  y1=2*95*sin(ps*0.0175);
  x2=2*80*cos(ps*0.0175);
  y2=2*80*sin(ps*0.0175);
  
  myGLCD.drawLine(x1+clockCenterX, y1+clockCenterY, x2+clockCenterX, y2+clockCenterY);

  myGLCD.setColor(255,255,255);
  s=s*6;
  s=s+270;
  
  x1=2*95*cos(s*0.0175);
  y1=2*95*sin(s*0.0175);
  x2=2*80*cos(s*0.0175);
  y2=2*80*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=2*80*cos(pm*0.0175);
  y1=2*80*sin(pm*0.0175);
  x2=2*5*cos(pm*0.0175);
  y2=2*5*sin(pm*0.0175);
  x3=2*30*cos((pm+4)*0.0175);
  y3=2*30*sin((pm+4)*0.0175);
  x4=2*30*cos((pm-4)*0.0175);
  y4=2*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=2*80*cos(m*0.0175);
  y1=2*80*sin(m*0.0175);
  x2=2*5*cos(m*0.0175);
  y2=2*5*sin(m*0.0175);
  x3=2*30*cos((m+4)*0.0175);
  y3=2*30*sin((m+4)*0.0175);
  x4=2*30*cos((m-4)*0.0175);
  y4=2*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=2*60*cos(ph*0.0175);
  y1=2*60*sin(ph*0.0175);
  x2=2*5*cos(ph*0.0175);
  y2=2*5*sin(ph*0.0175);
  x3=2*20*cos((ph+5)*0.0175);
  y3=2*20*sin((ph+5)*0.0175);
  x4=2*20*cos((ph-5)*0.0175);
  y4=2*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=2*60*cos(h*0.0175);
  y1=2*60*sin(h*0.0175);
  x2=2*5*cos(h*0.0175);
  y2=2*5*sin(h*0.0175);
  x3=2*20*cos((h+5)*0.0175);
  y3=2*20*sin((h+5)*0.0175);
  x4=2*20*cos((h-5)*0.0175);
  y4=2*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 printDate()
{
  myGLCD.setFont( Ubuntubold);
 // Time t_temp;
  
  //t_temp = rtc.getTime();
 
if (night==1) 
{   myGLCD.setColor(142, 142, 200);//ночной календарь
  myGLCD.setBackColor(0,38,92);
}
else
{  myGLCD.setColor(0, 0, 255);
  myGLCD.setBackColor(255, 255, 255);
}  
  dow= t.dow;

   myGLCD.print(str[dow-1], 272+450-80, 24);
//  myGLCD.print(rtc.getDOWStr(FORMAT_SHORT), 256+450, 8);
  if (t.date<10)
    myGLCD.printNumI(t.date, 272+450-50, 58);
  else
    myGLCD.printNumI(t.date, 264+450-50, 58);
  myGLCD.printNumI(t.mon, 256+450-50, 92);
  myGLCD.printNumI(t.year, 248+450-50, 126);
   myGLCD.setFont(BigFont);
}

void clearDate()
{
  if (night==1) 
{ myGLCD.setColor (0,38,92);
 }
else
{  myGLCD.setColor(255, 255, 255);
  }
  
  myGLCD.fillRect(240+450-80, 15, 319+450, 166);
  // myGLCD.fillRoundRect(240+450-80, 0, 319+450, 85+80);
}
//*************************************************************************
//******************************************************************
void clok()

{   //t = rtc.getTime();
 int a=0;
 q=1;
  int x, y;
do
  {
 t = rtc.getTime();
 
if( night==1 )
{constbr=20;
        br=constbr+brightness;
       analogWrite(12, br);    
}
else
{constbr=200;
        br=constbr+brightness;
       analogWrite(12, br);
  if (rawp==1)
  {rawp=0;
  myGLCD.fillScr(VGA_AQUA);//выход утром в RAW
  byte bait;
byte bait1;
 int w; 
 sd.chdir("/");
  delay (100);

   myFile.open("33.raw", O_READ);
  
int n=0;
int m=0;
  for (int i =0 ; i < 96000; i = i++)
  {
    n=n+1;
  bait = myFile.read();
 
  bait1 =myFile.read();
  
// w1= (bait - 48) << 4 | (bait1 - 48);
// w2= (bait2 - 48) << 4 | (bait3 - 48);
 w= bait<<8|bait1;
 
 pic1[n]=w;
 //3199
 if (n>399){
 n=0;  
// myGLCD.drawBitmap (0, 20*m, 320, 10, pic1, 2);
//   myGLCD.drawBitmap (0, 2*m, 320, 1, pic1, 2);
  myGLCD.drawBitmap (0, 2*m, 400, 1, pic1, 2);
 m=m+1;
 }
  } 
  myGLCD.drawBitmap (639, 370, 160, 80,  cl);
     myGLCD.drawRoundRect (639, 370, 799, 450); //Clok  
 kalend(); 
  
  
  
  
  
  
  
  raw();
  }
       
}

if (q==1)
{
  
  // Clear screen
  myGLCD.clrScr();
  
  // Draw Clockface
  myGLCD.setColor(0, 0, 255);
  myGLCD.setBackColor(0, 0, 0);
  for (int i=0; i<5; i++)
  {
    myGLCD.drawCircle(clockCenterX, clockCenterY, 230-i);//119-i
  }
  for (int i=0; i<5; i++)
  {
    myGLCD.drawCircle(clockCenterX, clockCenterY, 2*i);
  }
  
  myGLCD.setColor(192, 192, 255);
   myGLCD.setFont( SevenSegNumFont);
  myGLCD.print("3", clockCenterX+92+111-16, clockCenterY-8-20);
  myGLCD.print("6", clockCenterX-32+12, clockCenterY+95+111-30);
  myGLCD.print("9", clockCenterX-109-111, clockCenterY-8-20);
  myGLCD.print("12", clockCenterX-32, clockCenterY-109-111);
  for (int i=0; i<12; i++)
  {
    if ((i % 3)!=0)
      drawMark(i);
  }
  // myGLCD.setFont(BigFont);
   
   // Draw calendar
if (night==1) 
{ myGLCD.setColor(0,38,92);

}
else
{ myGLCD.setColor(255, 255, 255);

}

 // myGLCD.setColor(255, 255, 255);
  myGLCD.fillRoundRect(240+450-80, 0, 319+450, 85+80);
  
if (night==1) 
{ myGLCD.setColor(255, 255, 255);

}
else
{  myGLCD.setColor(0, 0, 0);

}  
 // myGLCD.setColor(0, 0, 0); 
 
  for (int i=0; i<14; i++)
  {
    myGLCD.drawLine(249+450-80+(i*10), 0, 248+450-80+(i*10), 7);
    myGLCD.drawLine(250+450-80+(i*10), 0, 249+450-80+(i*10), 7);
    myGLCD.drawLine(251+450-80+(i*10), 0, 250+450-80+(i*10), 7);
  }
myGLCD.setFont( BigFont);
  // Draw SET button
  myGLCD.setColor(64, 64, 128);
  myGLCD.fillRoundRect(260+450, 200+200, 319+450, 239+200);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(260+450, 200+200, 319+450, 239+200);
  myGLCD.setBackColor(64, 64, 128);
  myGLCD.print("SET", 266+450, 212+200);
  myGLCD.setBackColor(0, 0, 0);
  // Draw exit button
    myGLCD.setColor(64, 64, 128);
  myGLCD.fillRoundRect(260+450, 200+150, 319+450, 239+150);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(260+450, 200+150, 319+450, 239+150);
  myGLCD.setBackColor(64, 64, 128);
  myGLCD.print("EXT", 266+450, 212+150);
  myGLCD.setBackColor(0, 0, 0);
  
  
  if (night==1) 
{  myGLCD.setColor(192, 192, 255);

}
else
{  myGLCD.setColor(255,255, 255);

}  
  
 
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont( SevenSegNumFont);

 t = rtc.getTime();
    if (t.hour<10)
  {
    myGLCD.printNumI(0,clockCenterX+340, clockCenterY-8-20);
    myGLCD.printNumI(t.hour, clockCenterX+340+32, clockCenterY-8-20);
  }
  else
  {
    myGLCD.printNumI(t.hour,clockCenterX+340, clockCenterY-8-20);
  }
  myGLCD.print(":",clockCenterX+340+64, clockCenterY-8-20);
  if (t.min<10)
  {
    myGLCD.printNumI(0,clockCenterX+340+96, clockCenterY-8-20);
    myGLCD.printNumI(t.min, clockCenterX+340+128, clockCenterY-8-20);
  }
  else
  {
    myGLCD.printNumI(t.min, clockCenterX+340+96, clockCenterY-8-20);
  }
 
}  

  if (t.date==0)
  {
    setClock();
  }

 drawDisplay();
// printDate();
q=0;

    

    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
      if (((y>=200+200) && (y<=239+200)) && ((x>=260+450) && (x<=319+450)))
      {
        myGLCD.setColor (255, 0, 0);
        myGLCD.drawRoundRect(260+450, 200+200, 319+450, 239+200);
        setClock();
      }
       if (((y>=200+150) && (y<=239+150)) && ((x>=260+450) && (x<=319+450)))
      {
        if (c==1)
        {
          menu();
        }
        myGLCD.setColor (255, 0, 0);
        myGLCD.drawRoundRect(260+450, 200+150, 319+450, 239+150);
        menu();
      }
      
    }
//**********TOUCH    
    delay(10);
     t = rtc.getTime();
 // rawp=1 ; 
 if (rawp==1)   
{   if (t.sec==17)
  {  prognoz();
    q=1;
    delay(5000);
  } 
   if (t.sec==37)
  {  nightgraf();
  q=1;
  delay(5000);
    } 
   if ((sec==10) || (sec==29) )
{
 if(Serial.available()) {  
 priem();
}
 
 }
}
 //**************************massiv******************
//  t = rtc.getTime();
 if (t.min==0)
 {
if ((t.sec==0)||(t.sec==1))
  {
  //  printmassiv();
  if (fsec==1)
  {uart();
  bmp085();
  uart();
  savemastd();
  
  
  fsec=0;  //delay (300); 
  if(t.hour==23)
  {sdwrite();
   savemasclok();
   saveminmax();//запись макс  мин и средних значений датчиков за день
  } 
  if((t.hour>=0)||(t.hour<23))
  {
   savemasclok();//запись времени на sd 12:56 15/05/14 и массива rab
  } 
  
  }    
  }
}
//*****************************************************
 if (t.min>3)
 {fsec=1;
 }
 //***************************************************************** 



} while (a<1);
}
