void drawUpButton(int x, int y)
{
  myGLCD.setColor(64, 64, 128);
  myGLCD.fillRoundRect(x, y, 32*2+x, 25*2+y);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(x, y, 32*2+x, 25*2+y);
  myGLCD.setColor(128, 128, 255);
  for (int i=0; i<2*15; i++)
    myGLCD.drawLine(6*2+x+(i/1.5), 20*2+y-i, 27*2+x-(i/1.5), 20*2+y-i);
}

void drawDownButton(int x, int y)
{
  myGLCD.setColor(64, 64, 128);
  myGLCD.fillRoundRect(x, y,32*2+x, 25*2+y);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(x, y,32*2+x, 25*2+y);
  myGLCD.setColor(128, 128, 255);
  for (int i=0; i<15*2; i++)
    myGLCD.drawLine(6*2+x+(i/1.5), y+10+i, 27*2+x-(i/1.5), y+10+i);
}

void showDOW(byte dow)
{
  myGLCD.setFont( Ubuntubold);
  char* str[] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
  
  myGLCD.setColor(128, 128, 255);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.print(str[dow-1], 2*26, 2*167);
}



byte validateDate(byte d, byte m, word y)
{
  byte mArr[12] = {31,0,31,30,31,30,31,31,30,31,30,31};
  byte od;
  
  if (m==2)
  {
    if ((y % 4)==0)
    {
      if (d==30)
        od=1;
      else if (d==0)
        od=29;
      else
        od=d;
    }
    else
    {
      if (d==29)
        od=1;
      else if (d==0)
        od=28;
      else
        od=d;
    }
  }
  else
  {
    if (d==0)
      od=mArr[m-1];
    else if (d==(mArr[m-1]+1))
      od=1;
    else
      od=d;
  }
  
  return od;
}

byte validateDateForMonth(byte d, byte m, word y)
{
  myGLCD.setFont( Ubuntubold);
  byte mArr[12] = {31,0,31,30,31,30,31,31,30,31,30,31};
  byte od;
  boolean dc=false;
  
  if (m==2)
  {
    if ((y % 4)==0)
    {
      if (d>29)
      {
        d=29;
        dc=true;
      }
    }
    else
    {  
      if (d>28)
      {
        d=28;
        dc=true;
      }
    }
  }
  else
  {
    if (d>mArr[m-1])
    {
      d=mArr[m-1];
      dc=true;
    }
  }
  
  if (dc)
  {
    if (d<10)
    {
      myGLCD.printNumI(0, 122*2, 140*2);
      myGLCD.printNumI(d, 138*2-8, 140*2);
    }
    else
    {
      myGLCD.printNumI(d, 122*2, 140*2);
    }
  }
  
  return d;
}

void setClock()
{
//  Time t_temp;
  int x, y;
  int res = 0;
  boolean ct=false;
  boolean cd=false;
  
  waitForTouchRelease();    
  
  myGLCD.clrScr();
 // myGLCD.setFont(BigFont);
  
  // Draw Save button
  myGLCD.setFont( Ubuntubold);
  myGLCD.setColor(64, 64, 128);
  myGLCD.fillRoundRect(2*165, 2*200, 2*319, 2*239);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(2*165, 2*200, 2*319, 2*239);
  myGLCD.setBackColor(64, 64, 128);
  myGLCD.print("SAVE", 2*210, 2*212);
  myGLCD.setBackColor(0, 0, 0);
  // Draw Cancel button
  myGLCD.setColor(64, 64, 128);
  myGLCD.fillRoundRect(0, 2*200, 2*154, 2*239);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(0, 2*200, 2*154, 2*239);
  myGLCD.setBackColor(64, 64, 128);
  myGLCD.print("CANCEL", 2*29, 2*212);
  myGLCD.setBackColor(0, 0, 0);
  
  // Draw frames
  myGLCD.setColor(0, 0, 255);
  myGLCD.drawRoundRect(0, 0, 319*2, 96*2);
  myGLCD.drawRoundRect(0, 100*2, 319*2, 196*2);
  myGLCD.print("TIME:", 10*2, 40*2);
  myGLCD.print(":", 154*2, 40*2);
  myGLCD.print(":", 202*2, 40*2);
  myGLCD.print("DATE:", 10*2, 140*2);
  myGLCD.print(".", 154*2, 140*2);
  myGLCD.print(".", 202*2, 140*2);
  
  // Draw Buttons
  drawUpButton(122*2, 10*2);
  drawUpButton(170*2, 10*2);
  drawUpButton(218*2, 10*2);
  drawDownButton(122*2, 61*2);
  drawDownButton(170*2, 61*2);
  drawDownButton(218*2, 61*2);
  drawUpButton(122*2, 110*2);
  drawUpButton(170*2, 110*2);
  drawUpButton(234*2, 110*2);
  drawDownButton(122*2, 161*2);
  drawDownButton(170*2, 161*2);
  drawDownButton(234*2, 161*2);
  
  // Print current time and date
  myGLCD.setColor(255, 255, 255);
 // t_temp = rtc.getTime();
  if (t.date==0)
  {
    rtc.setTime(15, 11, 0);
 rtc.setDate(28, 12, 2015);
  }
  
  if (t.hour<10)
  {
    myGLCD.printNumI(0, 122*2, 40*2);
    myGLCD.printNumI(t.hour, 138*2-8, 40*2);
  }
  else
  {
    myGLCD.printNumI(t.hour, 122*2, 40*2);
  }
  if (t.min<10)
  {
    myGLCD.printNumI(0, 170*2, 40*2);
    myGLCD.printNumI(t.min, 186*2-8, 40*2);
  }
  else
  {
    myGLCD.printNumI(t.min, 170*2, 40*2);
  }
  if (t.sec<10)
  {
    myGLCD.printNumI(0, 218*2, 40*2);
    myGLCD.printNumI(t.sec, 234*2-8, 40*2);
  }
  else
  {
    myGLCD.printNumI(t.sec, 218*2, 40*2);
  }
  if (t.date<10)
  {
    myGLCD.printNumI(0, 122*2, 140*2);
    myGLCD.printNumI(t.date, 138*2-8, 140*2);
  }
  else
  {
    myGLCD.printNumI(t.date, 122*2, 140*2);
  }
  if (t.mon<10)
  {
    myGLCD.printNumI(0, 170*2, 140*2);
    myGLCD.printNumI(t.mon, 186*2-8, 140*2);
  }
  else
  {
    myGLCD.printNumI(t.mon, 170*2, 140*2);
  }
  myGLCD.printNumI(t.year, 218*2, 140*2);
 // showDOW(t_temp.dow);
 hour1=t.hour;
 min1=t.min;
sec= t.sec;
date=t.date;
mon= t.mon;
year1 =t.year;
dow= t.dow;  
  while (res==0)
  {

    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
      if ((y>=10*2) && (y<=35*2)) // Buttons: Time UP
      {
        if ((x>=122*2) && (x<=154*2))
        {
          buttonWait(122*2, 10*2);
          hour1+=1;
          if (hour1==24)
            hour1=0;
          if (hour1<10)
          {
            myGLCD.printNumI(0, 122*2, 40*2);
            myGLCD.printNumI(hour1, 138*2-8, 40*2);
          }
          else
          {
            myGLCD.printNumI(hour1, 122*2, 40*2);
          }
          if (ct==false)
          {
            ct=true;
            myGLCD.setColor(64, 64, 128);
           // myGLCD.setFont(SmallFont);
            myGLCD.print("Changed!", 10*2, 10*2);
            myGLCD.setColor(255, 255, 255);
            //myGLCD.setFont(BigFont);
            myGLCD.setFont( Ubuntubold);
          }
        }
        else if ((x>=170*2) && (x<=202*2))
        {
          buttonWait(170*2, 10*2);
          min1+=1;
          if (min1==60)
            min1=0;
          if (min1<10)
          {
            myGLCD.printNumI(0, 170*2, 40*2);
            myGLCD.printNumI(min1, 186*2-8, 40*2);
          }
          else
          {
            myGLCD.printNumI(min1, 170*2, 40*2);
          }
          if (ct==false)
          {
            ct=true;
            myGLCD.setColor(64, 64, 128);
            //myGLCD.setFont(SmallFont);
            myGLCD.print("Changed!", 10*2, 10*2);
            myGLCD.setColor(255, 255, 255);
            //myGLCD.setFont(BigFont);
            myGLCD.setFont( Ubuntubold);
          }
        }
        else if ((x>=218*2) && (x<=250*2))
        {
          buttonWait(218*2, 10*2);
          sec+=1;
          if (sec==60)
            sec=0;
          if (sec<10)
          {
            myGLCD.printNumI(0, 218*2, 40*2);
            myGLCD.printNumI(sec, 234*2-8, 40*2);
          }
          else
          {
            myGLCD.printNumI(sec, 218*2, 40*2);
          }
          if (ct==false)
          {
            ct=true;
            myGLCD.setColor(64, 64, 128);
           // myGLCD.setFont(SmallFont);
            myGLCD.print("Changed!", 10*2, 10*2);
            myGLCD.setColor(255, 255, 255);
            myGLCD.setFont( Ubuntubold);
           // myGLCD.setFont(BigFont);
          }
        }
      }
      else if ((y>=61*2) && (y<=86*2)) // Buttons: Time DOWN
      {
        if ((x>=122*2) && (x<=154*2))
        {
          buttonWait(122*2, 61*2);
          hour1-=1;
          if (hour1==255)
            hour1=23;
          if (hour1<10)
          {
            myGLCD.printNumI(0, 122*2, 40*2);
            myGLCD.printNumI(hour1, 138*2-8, 40*2);
          }
          else
          {
            myGLCD.printNumI(hour1, 122*2, 40*2);
          }
          if (ct==false)
          {
            ct=true;
            myGLCD.setColor(64, 64, 128);
            //myGLCD.setFont(SmallFont);
            myGLCD.print("Changed!", 10*2, 10*2);
            myGLCD.setColor(255, 255, 255);
            myGLCD.setFont( Ubuntubold);
           // myGLCD.setFont(BigFont);
          }
        }
        else if ((x>=170*2) && (x<=202*2))
        {
          buttonWait(170*2, 61*2);
          min1-=1;
          if (min1==255)
            min1=59;
          if (min1<10)
          {
            myGLCD.printNumI(0, 170*2, 40*2);
            myGLCD.printNumI(min1, 186*2-8, 40*2);
          }
          else
          {
            myGLCD.printNumI(min1, 170*2, 40*2);
          }
          if (ct==false)
          {
            ct=true;
            myGLCD.setColor(64, 64, 128);
            //myGLCD.setFont(SmallFont);
            myGLCD.print("Changed!", 10*2, 10*2);
            myGLCD.setColor(255, 255, 255);
            myGLCD.setFont( Ubuntubold);
           // myGLCD.setFont(BigFont);
          }
        }
        else if ((x>=218*2) && (x<=250*2))
        {
          buttonWait(218*2, 61*2);
          sec-=1;
          if (sec==255)
            sec=59;
          if (sec<10)
          {
            myGLCD.printNumI(0, 218*2, 40*2);
            myGLCD.printNumI(sec, 234*2-8, 40*2);
          }
          else
          {
            myGLCD.printNumI(sec, 218*2, 40*2);
          }
          if (ct==false)
          {
            ct=true;
            myGLCD.setColor(64, 64, 128);
            //myGLCD.setFont(SmallFont);
            myGLCD.print("Changed!", 10*2, 10*2);
            myGLCD.setColor(255, 255, 255);
            myGLCD.setFont( Ubuntubold);
            //myGLCD.setFont(BigFont);
          }
        }
      }
      if ((y>=110*2) && (y<=135*2)) // Buttons: Date UP
      {
        if ((x>=122*2) && (x<=154*2))
        {
          buttonWait(122*2, 110*2);
          date+=1;
          date=validateDate(date, mon, year1);
          if (date<10)
          {
            myGLCD.printNumI(0, 122*2, 140*2);
            myGLCD.printNumI(date, 138*2-8, 140*2);
          }
          else
          {
            myGLCD.printNumI(date, 122*2, 140*2);
          }
          if (cd==false)
          {
            cd=true;
            myGLCD.setColor(64, 64, 128);
           // myGLCD.setFont(SmallFont);
            myGLCD.print("Changed!", 10*2, 110*2);
            myGLCD.setColor(255, 255, 255);
            myGLCD.setFont( Ubuntubold);
           // myGLCD.setFont(BigFont);
          }
        }
        else if ((x>=170*2) && (x<=202*2))
        {
          buttonWait(170*2, 110*2);
          mon+=1;
          if (mon==13)
            mon=1;
          if (mon<10)
          {
            myGLCD.printNumI(0, 170*2, 140*2);
            myGLCD.printNumI(mon, 186*2-8, 140*2);
          }
          else
          {
            myGLCD.printNumI(mon, 170*2, 140*2);
          }
         date=validateDateForMonth(date, mon, year1);
         //  rtc_clock.set_date(date, mon, year);
          if (cd==false)
          {
            cd=true;
            myGLCD.setColor(64, 64, 128);
          //  myGLCD.setFont(SmallFont);
            myGLCD.print("Changed!", 10*2, 110*2);
            myGLCD.setColor(255, 255, 255);
            //myGLCD.setFont(BigFont);
            myGLCD.setFont( Ubuntubold);
          }
        }
        else if ((x>=218*2) && (x<=250*2))
        {
          buttonWait(234*2, 110*2);
          year1+=1;
          if (year1==2100)
            year1=2000;
          myGLCD.printNumI(year1, 218*2, 140*2);
         date=validateDateForMonth(date, mon, year1);
        //  rtc_clock.set_date(date, mon, year);
          if (cd==false)
          {
            cd=true;
            myGLCD.setColor(64, 64, 128);
            //myGLCD.setFont(SmallFont);
            myGLCD.print("Changed!", 10*2, 110*2);
            myGLCD.setColor(255, 255, 255);
            myGLCD.setFont( Ubuntubold);
            //myGLCD.setFont(BigFont);
          }
        }
        dow=calcDOW(date, mon, year1);
        showDOW(dow);
      }
      else if ((y>=161*2) && (y<=186*2)) // Buttons: Date DOWN
      {
        if ((x>=122*2) && (x<=154*2))
        {
          buttonWait(122*2, 161*2);
          date-=1;
          date=validateDate(date, mon, year1);
          if (date<10)
          {
            myGLCD.printNumI(0, 122*2, 140*2);
            myGLCD.printNumI(date, 138*2-8, 140*2);
          }
          else
          {
            myGLCD.printNumI(date, 122*2, 140*2);
          }
          if (cd==false)
          {
            cd=true;
            myGLCD.setColor(64, 64, 128);
            //myGLCD.setFont(SmallFont);
            myGLCD.print("Changed!", 10*2, 110*2);
            myGLCD.setColor(255, 255, 255);
            //myGLCD.setFont(BigFont);
            myGLCD.setFont( Ubuntubold);
          }
        }
        else if ((x>=170*2) && (x<=202*2))
        {
          buttonWait(170*2, 161*2);
          mon-=1;
          if (mon==0)
            mon=12;
          if (mon<10)
          {
            myGLCD.printNumI(0, 170*2, 140*2);
            myGLCD.printNumI(mon, 186*2-8, 140*2);
          }
          else
          {
            myGLCD.printNumI(mon, 170*2, 140*2);
          }
          date=validateDateForMonth(date, mon, year1);
          if (cd==false)
          {
            cd=true;
            myGLCD.setColor(64, 64, 128);
            //myGLCD.setFont(SmallFont);
            myGLCD.print("Changed!", 10*2, 110*2);
            myGLCD.setColor(255, 255, 255);
            myGLCD.setFont(BigFont);
          }
        }
        else if ((x>=218*2) && (x<=250*2))
        {
          buttonWait(234*2, 161*2);
          year1-=1;
          if (year1==1999)
            year1=2099;
          myGLCD.printNumI(year1, 218*2, 140*2);
          date=validateDateForMonth(date, mon, year1);
          if (cd==false)
          {
            cd=true;
            myGLCD.setColor(64, 64, 128);
           //myGLCD.setFont(SmallFont);
            myGLCD.print("Changed!", 10*2, 110*2);
            myGLCD.setColor(255, 255, 255);
            myGLCD.setFont(BigFont);
          }
        }
        dow=calcDOW(date, mon, year1);
        showDOW(dow);
      }
      else if ((y>=200*2) && (y<=239*2)) // Buttons: CANCEL / SAVE
      {
        if ((x>=165*2) && (x<=319*2))
        {
          res=1;
          myGLCD.setColor (255, 0, 0);
          myGLCD.drawRoundRect(165*2, 200*2, 319*2, 239*2);
        }
        else if ((x>=0) && (x<=154*2))
        {
          res=2;
          myGLCD.setColor (255, 0, 0);
          myGLCD.drawRoundRect(0, 200*2, 154*2, 239*2);
        }
      }
    }
  }
//************************************************************************************


  waitForTouchRelease();

  if (res==1)
  {
    if (ct)
      //rtc.setTime(t_temp.hour, t_temp.min, t_temp.sec);
      rtc.setTime(hour1, min1 , sec);
  //  if (cd)
  //  {
      //rtc.setDate(t_temp.date, t_temp.mon, t_temp.year1);
      rtc.setDate(date, mon, year1);
      dow=calcDOW(date, mon, year1);
      rtc.setDOW(dow);
      //rtc.setDOW(t_temp.dow);
   // }
  }
  if( t.hour<7 )
{night=1;
}
else
{night=0;
}
  q=1;
  clok();
  myGLCD.setFont(BigFont);
 // drawDisplay();
 // printDate();
}
