
void kalendar(int days,  int mon, word years)
//void kalen()
{
  char* strm[] = {"JAN","FEBR","MARCH","APRIL","MAY","JUNE","JULY","AUG","SEPT","OCT","NOV","DEC"};
   char* strw[] = {"MO","TU","WE","TH","FR","SA","SU"};
 int mon1;
//  int days;
  int dmax;
word years1;
  byte dow;

  
//***************************************************************************************** 
  myGLCD.setColor(255, 255, 255);
  myGLCD.fillRoundRect(80, 35, 632, 420);
  myGLCD.setColor(255,114,0);
 myGLCD.fillRoundRect(80, 35, 632, 87);
 
 myGLCD.setBackColor( VGA_TRANSPARENT);
    myGLCD.setColor(255, 255, 255);
 myGLCD.setFont( Ubuntubold);
 myGLCD.print(strm[mon-1], 250, 50);
 myGLCD.printNumI(years, 420, 50);
 myGLCD.setColor(0, 0, 100);
 dmax=validmax(days, mon, years);
 for(int i=0;i<7;i++)//вывод дней недели
 {
  if (i>4)
 {myGLCD.setColor(255, 0, 0); //вывод красным выходных дней
 }
   myGLCD.print(strw[i], 104+i*72, 89);
 }
  myGLCD.setBackColor( VGA_TRANSPARENT);
    myGLCD.setColor(0, 0, 255);
 days=1;
 int yk=0;
 dow=calcDOW1(days, mon, years);
 
 for (days=1;days<dmax+1;days++)//вывод чисел календаря
 {
 
  if (mon==t.mon)// текущая дата
  {
   if ( years==t.year)
   {
     if ( days==t.date)
   {
    //   myGLCD.setColor(200, 200, 200);
 // myGLCD.fillRoundRect((dow-1)*72+95, yk*50+125, (dow-1)*72+160, yk*50+170);
  
    if (dow>5)
 {
    myGLCD.setColor(250, 198, 239);
  myGLCD.fillRoundRect((dow-1)*72+95, yk*50+125, (dow-1)*72+160, yk*50+170);
   myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect((dow-1)*72+95, yk*50+125, (dow-1)*72+160, yk*50+170);
 
 }
 else
 {
     myGLCD.setColor(182, 217, 252);
  myGLCD.fillRoundRect((dow-1)*72+95, yk*50+125, (dow-1)*72+160, yk*50+170);
   myGLCD.setColor(0, 0, 255); 
     myGLCD.drawRoundRect((dow-1)*72+95, yk*50+125, (dow-1)*72+160, yk*50+170);
 }

  
    //  myGLCD.printNumI(days, (dow-1)*72+104, yk*50+135);
   }
   }
  }  
   
     
  if (dow>5)
 {myGLCD.setColor(255, 0, 0); 
 }
 else
 {
   myGLCD.setColor(0, 0, 255); 
 }
   myGLCD.printNumI(days, (dow-1)*72+104, yk*50+135);//вывод чисел календаря
   
 



  
   if (dow>6)
   {dow=0;
   yk=yk+1;
   }

   dow=dow+1;
 }  
 
 
  days=1;
  if (mon==12)
  {mon1=1;
    years1=years+1;
  }
  else
  {
  mon1=mon+1;
   years1=years;
  }
 dow=calcDOW1(days, mon1, years1); 
 myGLCD.setColor(100, 100, 100); //*********************************
 for (days=1;dow<8;days++)
 {
 myGLCD.printNumI(days, (dow-1)*72+104, yk*50+135); 
  dow=dow+1;
 }

if (mon==1)
  {mon1=12;
    years1=years-1;
  }
  else
  {
  mon1=mon-1;
  years1=years;
  }
dmax=validmax(days, mon1, years1);
days=dmax;
 dow=calcDOW1(days, mon1, years1); 
 if(dow<7)
 {
 myGLCD.setColor(100, 100, 100); //*************************
 for (days=dmax;dow>0;days--)
 {
 myGLCD.printNumI(days, (dow-1)*72+104, 135); 
  dow=dow-1;
 }
 }
}

//******************************kalendar*******************************************************************
void  kalendarset()  
{
  


 int  days=t.date;
int  mon=t.mon;
 word years=t.year;  
  
 kalendar(days, mon, years); 

 
// myButtons.setButtonColors(VGA_WHITE,VGA_AQUA,VGA_WHITE,VGA_YELLOW,VGA_RED);
//   myGLCD.setFont(BigFont);
//    myButtons.setSymbolFont(Dingbats1_XL);
 drawButkal();


 while(1)
 {
 if (myTouch.dataAvailable())
 
    {
      myTouch.read();
     int x=myTouch.getX();
      int y=myTouch.getY();
      
      if ((y>=390) && (y<=390+30))  // Upper row
      {
        
        if ((x>= 588) && (x<= 588+30))  //X
             {waitForIt( 588, 390, 588+30, 390+30);
             return;
           }
      } 
        
       if ((y>=40) && (y<=40+30))  // Upper row
      {
        
        if ((x>= 588) && (x<= 588+30))  //+
             {waitForIt( 588, 40, 588+30, 40+30);    
              mon=mon+1;
              if (mon>12)
                 {mon=1;
                 }
                   kalendar(days, mon, years);
                    drawButkal();
             }
              if ((x>= 90) && (x<= 90+30))  //-
             {waitForIt( 90, 40, 90+30, 40+30);  
              mon=mon-1;
               if (mon<1)
               {mon=12;
                }
               kalendar(days, mon, years); 
                drawButkal();
             }
       if ((x>= 545) && (x<= 545+30))  //>
             {waitForIt( 545, 40, 545+30, 40+30);         
               years=years+1;
            if (years>2099)
                {years=2000;
                }
               kalendar(days, mon, years);
               drawButkal();
             }
        if ((x>= 135) && (x<= 135+30))  //<
             {waitForIt( 135, 40, 135+30, 40+30);  
              years=years-1;
            if (years<2000)
              {years=2099;
              }
                 kalendar(days, mon, years);
                      drawButkal();
             }

     
      
      }
      } 
   

 sec=t.sec;
if (sec==0)
{
delay(800);
clc();
} 
 
 
 }

}

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




byte calcDOW1(byte d, byte m, int y)
{
  int dow;
  byte mArr[12] = {6,2,2,5,0,3,5,1,4,6,2,4};
  
  dow = (y % 100);
  dow = dow*1.25;
  dow += d;
  dow += mArr[m-1];
  if (((y % 4)==0) && (m<3))
    dow -= 1;
  while (dow>7)
    dow -= 7;
    
  return dow;
}



void drawButkal()// календарь на месяц 
{
myGLCD.setColor(255, 255, 255);
   myGLCD.drawBitmap (588, 390, 30, 30, xf);
    myGLCD.drawRoundRect (588, 390, 588+30, 390+30);//X
    
 myGLCD.drawBitmap (588, 40, 30, 30, ad);
     myGLCD.drawRoundRect ( 588, 40,  618, 70); //+   

 myGLCD.drawBitmap (90, 40, 30, 30, mi); 
     myGLCD.drawRoundRect (  90, 40,  120, 70); //-

 myGLCD.drawBitmap (545, 40, 30, 30, pr);
     myGLCD.drawRoundRect ( 545, 40, 545+30, 40+30);//>

 myGLCD.drawBitmap (135, 40, 30, 30, le);
     myGLCD.drawRoundRect (  135, 40,  135+30, 40+30);//<
}    
