/****************************************************************************
	Knihovna pro rizeni AD prevodniku.

*****************************************************************************/
#include "Drivers.h"
#ifndef PCSIM
	#include <ctl_api.h>
	#include <targets/LPC2478.h>
#endif

#define BATTERY_MEASURE_COUNT 8						//Pocet mereni pro vypocet plovouciho prumeru
#define BATTERY_INITIAL 180							//Vychozi nastaveni pole

int batteryRawValue[BATTERY_MEASURE_COUNT];			//Pole pro realizaci vypoctu plovouciho prumeru
int batteryCounter = 0;								//Pocitadlo posledni pozice mereni


/****************************************************************************
	Vypocet hodnoty napeti baterie pomoci plovouciho prumeru. Napeti neni
	vypocteno exaktne ve voltech, je vracena hodnota registru AD prevodniku.

	Toto reseni je zvoleno, protoze doposud nebyla pro zarizeni vybrana vhodna
	baterie.

	Vstup: 
		neni

	Vystup
		int:		Hodnota napeti na baterii

*****************************************************************************/
int GetBatteryValue(void)
{
	int sum = 0;
	for (int i = 0; i < BATTERY_MEASURE_COUNT; i++)			//Soucet prvku v poli
	{
		sum += batteryRawValue[i];					
	}
	return integerDiv(sum,BATTERY_MEASURE_COUNT);			//Vydeleni delkou pole
}


/****************************************************************************
	Aktivuje AD prevodnik za ucelem zmereni nove hodnoty.

	Vstup: 
		neni

	Vystup
		neni

*****************************************************************************/
void updateBattery(void)
{
	batteryCounter = (batteryCounter+1)%BATTERY_MEASURE_COUNT;		//vypocet pozice v poli (kruhovy zasobnik)
#ifndef PCSIM
  AD0CR |= (1<<AD0CR_START_BIT);									//Zacatek mereni
#endif
}

#ifndef PCSIM
/****************************************************************************
	Rutina preruseni pro AD prevodnik

	Vstup: 
		neni

	Vystup
		neni

*****************************************************************************/
void adcISR(void)
{
  int flag = AD0STAT;
  flag = AD0DR6;
  if (AD0GDR_DONE_MASK & AD0GDR)												//POkud je mereni dokonceno
  {
    batteryRawValue[batteryCounter] = (1023&(AD0GDR>>AD0GDR_RESULT_BIT));		//Vlozeni zmerene hodnoty do pole
	
  }
}
#endif

/****************************************************************************
	Inicializacni funkce AD prevodniku a ovladace mereni baterie

	Vstup: 
		neni

	Vystup
		neni

*****************************************************************************/
void adcInit(void)
{
#ifndef PCSIM
  PCONP |= (1<<PCONP_PCAD_BIT) ;							//Enable power
  PCLKSEL0|= (3<<PCLKSEL0_PCLK_ADC_BIT);					//1/8 system clock
  AD0CR |= (1<<AD0CR_PDN_BIT)|(1<<6)|(1 << AD0CR_CLKDIV_BIT);
  PINSEL0 |= (3<<PINSEL0_P0_12_BIT);						//Inicializace pinu
  PINMODE0 |= (2<<PINSEL0_P0_12_BIT);
  AD0INTEN |= (1<<8) | (1<<6);								//Inicializace preruseni

  ctl_set_isr(18, 31, CTL_ISR_TRIGGER_FIXED, adcISR, 0);	//Nastaveni preruseni ve VIC
  ctl_unmask_isr(18);										//Povoleni preruseni
#endif
  for (int i = 0; i < BATTERY_MEASURE_COUNT; i++)			//Inicializace pole hodnot	
	  batteryRawValue[i] = BATTERY_INITIAL;
}
