/****************************************************************************
	Aplikace ridici hodiny. Mozna by mohla byt mezi ovladaci, ale tady se mi libi.

*****************************************************************************/
#include "stdafx.h"
#include "Applications.h"
#include "Drivers.h"


TIME currentTime = {0, 45, 23, 31, 12, 2009};			//Aktualni cas

/****************************************************************************
	Funkce vraci ukazatel na strukturu s aktualnim casem

	Vstup: 
		neni

	Výstup:
		TIME*:		ukazatel na strukturu s casem

*****************************************************************************/
TIME* getTime(void)
{
	return &currentTime;
}

/****************************************************************************
	Nacteni casu z registru procesoru

	TODO: nebyl cas, nutno dokoncit

	Vstup: 
		neni

	Výstup:
		neni

*****************************************************************************/
void loadTime(void)
{
//TODO: get time somewhere and load it to timer
}

/****************************************************************************
	Funkce pro spravnou inkrementaci dnu dle kalendare.

	Vstup: 
		neni

	Výstup:
		neni

*****************************************************************************/
void Calender(void)
{
	currentTime.day++;
	if ((currentTime.day == 29)&&(currentTime.month == 2)&&((currentTime.year%4)!=0))															//Pokud neni prestupny rok a je unor
	{
		currentTime.day = 1;
		currentTime.month++;	
	} else 
	if ((currentTime.day == 30)&&(currentTime.month == 2)&&((currentTime.year%4)==0))															//Pokud je prestupny rok a unor
	{
		currentTime.day = 1;
		currentTime.month++;	
	} 
	else
	if ((currentTime.day == 31)&&((currentTime.month == 4)||(currentTime.month == 6)||(currentTime.month == 9)||(currentTime.month == 11)))		//Ostatni mesice s 30 dny		
	{
		currentTime.day = 1;
		currentTime.month++;	
	} 
	else																																		//Vsechny ostatni mesice
	if (currentTime.day == 32)
	{
		if (currentTime.month == 12)
		{
			currentTime.day = 1;
			currentTime.month = 1;
			currentTime.year++;
		}
		else 
		{
			currentTime.day = 1;
			currentTime.month++;
		}
	}
}

ANSWER ledOn = NO;							//flag pro prepinani led v rytmu 1 s

void Tick(void)
{
	currentTime.seconds++;					//Inkrementace sekund
#ifndef PCSIM
        if (ledOn == NO)					//blikani ledky
        {
          LedOn(LED_3);
          ledOn = YES;
        }
        else
        {
          LedOff(LED_3);
          ledOn = NO;
        }
#endif
	if (currentTime.seconds == 60)			//Pokud doslo k preteceni pocitadla hodin
	{
		currentTime.seconds = 0;			//pridej minutu
		currentTime.minutes++;
		if (currentTime.minutes == 60)		//To same s minutami
		{
			currentTime.minutes = 0;		//Pridej hodinu
			currentTime.hours++;			
			if (currentTime.hours == 24)	//To same s hodinami
			{
				currentTime.hours = 0;		//Pridej den
				Calender();
			}
		}
	}
}




void AppClock(void)
{
	static char state = 0;
	switch(state)
	{
	case CLOCK_FRESH_START:
		loadTime();
		state = CLOCK_RUNNING;
		break;
	case CLOCK_RUNNING:
		Tick();
		break;
	default:
		state = CLOCK_FRESH_START;
		break;
	}

}