#include "timelib.h"
#include "twi_lib.h"

unsigned char convToWrite(unsigned char value)
{
  return ((((((value%10) / 10) << 4) | ((value%10) % 10)) )+((value/10)<<4));
}

void Transform_Time(sTime *fTime)
{
  (*fTime).seconds = ((((*fTime).seconds & 0x7F) & 0xF0) >> 4)*10 + ((*fTime).seconds & 0x0F);
  (*fTime).minutes  =  ((((*fTime).minutes & 0x7F) & 0xF0) >> 4)*10 + ((*fTime).minutes & 0x0F);
  (*fTime).hours    =  ((((*fTime).hours & 0x3F) & 0xF0)  >> 4)*10  + ((*fTime).hours & 0x0F);
  (*fTime).year     =   ((((*fTime).year & 0xF0)>>4)*10 + ((*fTime).year & 0x0F));
  (*fTime).day      =  (((((*fTime).day & 0x3F) & 0xF0)>>4)*10 + ((*fTime).day & 0x0F));
  (*fTime).month    =  (((((*fTime).month & 0x9F) & 0xF0)>>4)*10 + ((*fTime).month & 0x0F));
}

void GetTime(sTime *fTime)
{

    i2c_start_wait(Casovatko + I2C_WRITE);
    i2c_write(0x02);
    i2c_rep_start(Casovatko + I2C_READ);
    (*fTime).seconds = i2c_readAck();
    (*fTime).minutes = i2c_readAck();
    (*fTime).hours =i2c_readAck();
    (*fTime).day = i2c_readAck();
    i2c_readAck();
    (*fTime).month = i2c_readAck();
    (*fTime).year = i2c_readNak();
    i2c_stop();
    Transform_Time(fTime);
}
void SetTime(const unsigned char fRadek, const unsigned char fSmer)
{
    unsigned char fZmenena=0;
    unsigned char fPomocna=0;
    unsigned char fHranice=0;
    sTime fTime;
    GetTime(&fTime);
    switch (fRadek)
    {
        case 1:
            fZmenena = fTime.hours;
            fHranice = 24;
            if (fZmenena<fHranice && fSmer)
                fZmenena++;
            else if (fZmenena>0 && !fSmer)
                fZmenena--;
            else if (fZmenena==fHranice && fSmer)
                fZmenena = 0;
            else if (!fZmenena && !fSmer)
                fZmenena = fHranice;
            fPomocna = 0x04;
        break;
        case 2:
            fZmenena = fTime.minutes;
            fHranice = 60;
            if (fZmenena<fHranice && fSmer)
                fZmenena++;
            else if (fZmenena>0 && !fSmer)
                fZmenena--;
            else if (fZmenena==fHranice && fSmer)
                fZmenena = 0;
            else if (!fZmenena && !fSmer)
                fZmenena = fHranice;
            fPomocna = 0x03;
        break;
        case 3:
            fZmenena = fTime.seconds;
            fHranice = 60;
            if (fZmenena<fHranice && fSmer)
                fZmenena++;
            else if (fZmenena>0 && !fSmer)
                fZmenena--;
            else if (fZmenena==fHranice && fSmer)
                fZmenena = 0;
            else if (!fZmenena && !fSmer)
                fZmenena = fHranice;
            fPomocna = 0x02;
        break;
        case 4:
            if (fTime.month==1 || fTime.month==3 || fTime.month==5 || fTime.month==7 || fTime.month==8 || fTime.month==10 || fTime.month==12)
                fHranice = 31;
            else
                fHranice = 30;
            if (fTime.month==2)
                fHranice = 28;
            if (!(fTime.year%4))
                fHranice = 29;
            fZmenena = fTime.day;
            if (fZmenena<fHranice && fSmer)
                fZmenena++;
            else if (fZmenena>0 && !fSmer)
                fZmenena--;
            else if (fZmenena==fHranice && fSmer)
                fZmenena = 0;
            else if (!fZmenena && !fSmer)
                fZmenena = fHranice;
            fPomocna = 0x05;
        break;
        case 5:
            fZmenena = fTime.month;
            fHranice = 60;
            if (fZmenena<fHranice && fSmer)
                fZmenena++;
            else if (fZmenena>0 && !fSmer)
                fZmenena--;
            else if (fZmenena==fHranice && fSmer)
                fZmenena = 0;
            else if (!fZmenena && !fSmer)
                fZmenena = fHranice;
            fPomocna = 0x07;
        break;
        case 6:
            fZmenena = fTime.year;
            if (fSmer)
                fZmenena++;
            else
                fZmenena--;
            fPomocna = 0x08;
        break;
    }

    i2c_start_wait(Casovatko + I2C_WRITE);
    i2c_write(fPomocna);
    i2c_write(convToWrite(fZmenena));
    i2c_stop();
}
void SetTimer(unsigned char time)
{
    i2c_start_wait(Casovatko + I2C_WRITE);
    i2c_write(0x01);
    i2c_write(0x01);
    i2c_stop();
    i2c_start_wait(Casovatko + I2C_WRITE);
    i2c_write(0x0E);
    i2c_write(0x82);
    i2c_stop();
    i2c_start_wait(Casovatko + I2C_WRITE);
    i2c_write(0x0F);
    i2c_write(time);
    i2c_stop();
}
void ResetTimer()
{
    i2c_start_wait(Casovatko + I2C_WRITE);
    i2c_write(0x01);
    i2c_write(0x01);
    i2c_stop();
}
