/**********************************************************************
 *****  COMMON FILE FOR EVERY GILD-SYSTEM PROJECTS                *****
 *****  (WARNING: do not change main routines except user areas)  *****
 *****                                                            *****
 *****  Name: _core_func.c                                        *****
 *****  Desc: common routines for GILD projects			      	  *****
 *****  Ver.: 1.0                                                 *****
 *****  Date: 02/2011		                                      *****
 *****  Auth: Martin Mechacek		                              *****
 *****  Comp: ESTELAR s.r.o.									  *****
 *****                                                            *****
 *****  Copyright ESTELAR s.r.o. (c) 2011, all rights reserved    *****
 **********************************************************************/ 


/**********************************************************************
 *****  USER AREA - define dependeces here						  *****
 **********************************************************************/ 


/////// end of USER AREA //////////////////////////////////////////////


/**********************************************************************
 *****  MAIN AREA - do not change !	   						      *****
 **********************************************************************/ 
#include <stdio.h>
#include <string.h>
#include "_core_func.h" 


//========================================================================
//>> Global variables

#ifndef __CORE_CPU_ATMEL
 tsRTC			gDateTime;
 unsigned long	gTimeDelta;
#endif


//<< end of Global variables


//+-------------------------------------------------------------------+
//| CoreInitVar()
//| first initialise global core variables
//+-------------------------------------------------------------------+
void CoreInitVar(void)
{

#ifndef __CORE_CPU_ATMEL
   memset(&gDateTime, 0, sizeof(tsRTC));
#endif
   
}  // end of CoreInitVar()


//+-------------------------------------------------------------------+
//| GetTimeDelta()
//| calculate addition _time in parameter in comparison with real time in gDateTime
//| return int value, max 0xffff
//+-------------------------------------------------------------------+
#ifndef __CORE_CPU_ATMEL
unsigned long GetTimeDelta(unsigned long _time)
{
unsigned long _dr;	

  // dopocitani delty zmeny - velikosti rozdilu	od posledni aktualizace casu
  if( _time > gDateTime.LONG.time )
   {
    // preslo to pres pulnoc - musi se dopocitat puvodni zbytek do pulnoci a k tomu novou cast po pulnoci
	// hodnota 0x17EFB = 23:59:59 + 1
    _dr = ( 0x00017EFC - _time );
	_dr += gDateTime.LONG.time;
   }
  else
    _dr = ( gDateTime.LONG.time - _time);


 return(_dr);
}
#endif


//+-------------------------------------------------------------------+
//| DateTimeUpdate()
//| convert the received date-time and store into gDateTime
//+-------------------------------------------------------------------+
#ifndef __CORE_CPU_ATMEL
int DateTimeUpdate(unsigned char *_dt)
{
tsRTC	_cdt;

  // ulozit puvodni strukturu pro vypocet delty zmeny
  memcpy(&_cdt, &gDateTime, sizeof(tsRTC));
  
  if(*(_dt) > 63) return(1);
  gDateTime.BYTE.yy 	= *_dt;
  
  _dt++;
  if(*_dt == 0 || *_dt > 12) return(1);
  gDateTime.BYTE.mm 	= *_dt;

  _dt++;
  if(*_dt == 0 || *_dt > 31) return(1);
  gDateTime.BYTE.dd 	= *_dt;

  _dt++;
  if(*_dt > 23) return(1);
  gDateTime.BYTE.h 	= *_dt;

  _dt++;
  if(*_dt > 59) return(1);
  gDateTime.BYTE.m 	= *_dt;

  _dt++;
  if(*_dt > 59) return(1);
  gDateTime.BYTE.s 	= *_dt;

  _dt++;
  if(*_dt > 7) return(1);
  gDateTime.BYTE.dow = *_dt;



  // toto je osetreni stavu po resetu, kdy prijde prvni casovy paket, musi se nastavit gTimeDelta na minimum 
  // pokud by to nebylo osetreno, tak v gTimeDelta je odchylka aktualniho RTC casu od 0000
  // a to by pak melo za nasledek spusteni reakce RTC funkci
  if (gDateTime.BYTE.x != 0xAA)		// jedna se o prvni pruchod
     gTimeDelta = 1;  				// nastavit gTimeDelta na zakladni jednotku 1s
  else
	gTimeDelta = GetTimeDelta(_cdt.LONG.time);   // dopocitani delty zmeny casu od posledni aktualizace
												 // nutno nasledne vynulovat po pruhodu kontrolou reakci vystupu, aby nedoslo k opakovanemu vyhodnoceni
	
	gDateTime.BYTE.x = 0xAA;					 // inicializovat, at se priste bere spravna gTimeDelta podle skutecneho stavu RTC
    


	
return(0);

} // end of DateTimeUpdate()
#endif


//****************************************************************************
// byte hex2dec
// convert BCD value to decimal
//***************************************************************************
unsigned char hex2dec( unsigned char hex )
{
   return (((hex & 0xf0) >> 4) * 10) + (hex & 0xf);
}

//****************************************************************************
// byte dec2hex
// convert decimal value to BCD
//***************************************************************************
unsigned char dec2hex( unsigned char dec )
{
   return ((dec / 10) << 4) + (dec % 10);
}



/////// end of MAIN AREA //////////////////////////////////////////////
