/* Knihovna pro praci s kapacitnim teplotnim cidlem vcetne vypoctu a kompenzaci RH */

/* Standard includes. */
#include <stdio.h>

/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

#include "Humidity.h"
#include "DS18B20.h"
#include "PIN_config.h"

/* STD Knihovny */
#include "stm32f10x_tim.h"
#include "stm32f10x_rcc.h"

// Globalni promenne
extern uint16_t HumidityRAW=0;
extern uint8_t Humidity=HUMIDITY_ERROR;
extern uint8_t HumidityHistorie[2]={HUMIDITY_ERROR,HUMIDITY_ERROR};

extern int16_t Teplota[MAXSENSORS];

/* ---------------------------------------------------------
 Inicializace casovace pro mereni relativni vlhkosti
 TIM4 - kanal 2
 -------------------------------------------------------- */
void Humidity_Init(void)
{	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE);				// Povoleni hodin do TIM4
 	HUM_IN();															// Nastaveni pinu pro mereni vlhkosti jako vstup

	TI2_Config(TIM4, TIM_ICPolarity_Rising, TIM_ICSelection_DirectTI, 0x0);	// Nastaveni na nabeznou hranu a vypnutou filtraci
	TIM_TIxExternalClockConfig(TIM4, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0x0);	// Nastaveni externiho hodinoveho vstupu na kanal 2
	TIM_Cmd(TIM4, ENABLE);
}

/* -----------------------------------------
 Hlavni funkce slouzici ke zmereni a vypoctu
 relativni vzdusne vlhkosti z kmitoctu od 
 kapacitniho cidla
 ---------------------------------------- */
void HumidityZmerit(void)
{
	HumidityRAW=TIM4->CNT;					// Zjisteni hodnoty v registru
	TIM_SetCounter(TIM4,0x00);				// Vyresetovani hodnoty v registru

	/* Korekce kmitoctu vuci teplote 25°C. +1°C = +73 Hz */
	if(Teplota[TEPLOTA_EXT_ID]!=DS18X20_INVALID_TEMP)
	{
		HumidityRAW = HumidityRAW+(HUMIDITY_TEMP_COR*((Teplota[TEPLOTA_EXT_ID]-250)/10));
	}
	
	if((HumidityRAW<HUMIDITY_FREQ_FOR_0_CAL)&(HumidityRAW>HUMIDITY_FREQ_FOR_100_CAL))
	{
		Humidity = (uint16_t)(((uint16_t)HUMIDITY_FREQ_FOR_0_CAL-HumidityRAW)/(uint16_t)HUMIDITY_FREQ_DELTA_1_CAL);	// Prepocet vlhkosti z kmitoctu na procenta 0-100%
		if (HumidityHistorie[0]==255)					// Pokud je pamet minima a maxima prazdna - ulozit bez porovnavani
			HumidityHistorie[0] = Humidity;
		else if(Humidity>HumidityHistorie[0])			// Pokud je aktualni vlhkost vetsi nez maximum - ulozit
			HumidityHistorie[0] = Humidity;
		if (HumidityHistorie[1]==255)					// Pokud je pamet minima a maxima prazdna - ulozit bez porovnavani
			HumidityHistorie[1] = Humidity;
		else if(Humidity<HumidityHistorie[1])			// Pokud je aktualni vlhkost mensi nez maximum - ulozit
			HumidityHistorie[1] = Humidity;
	}
	else
	{
		Humidity = 255;
	}
}

/* ----------------------------------------------------------
 Pomocna funkce pro nastaveni citace pro mereni relativni
 vzdusne vlhkosti (vytazena z knihovny stm32f10x_tim
 --------------------------------------------------------- */
static void TI2_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
                       uint16_t TIM_ICFilter)
{
  uint16_t tmpccmr1 = 0, tmpccer = 0, tmp = 0;
  /* Disable the Channel 2: Reset the CC2E Bit */
  TIMx->CCER &= (uint16_t)~((uint16_t)TIM_CCER_CC2E);
  tmpccmr1 = TIMx->CCMR1;
  tmpccer = TIMx->CCER;
  tmp = (uint16_t)(TIM_ICPolarity << 4);
  /* Select the Input and set the filter */
  tmpccmr1 &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCMR1_CC2S)) & ((uint16_t)~((uint16_t)TIM_CCMR1_IC2F)));
  tmpccmr1 |= (uint16_t)(TIM_ICFilter << 12);
  tmpccmr1 |= (uint16_t)(TIM_ICSelection << 8);
  
  if((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) ||
     (TIMx == TIM4) ||(TIMx == TIM5))
  {
    /* Select the Polarity and set the CC2E Bit */
    tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC2P));
    tmpccer |=  (uint16_t)(tmp | (uint16_t)TIM_CCER_CC2E);
  }
  else
  {
    /* Select the Polarity and set the CC2E Bit */
    tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC2P | TIM_CCER_CC2NP));
    tmpccer |= (uint16_t)(TIM_ICPolarity | (uint16_t)TIM_CCER_CC2E);
  }
  
  /* Write to TIMx CCMR1 and CCER registers */
  TIMx->CCMR1 = tmpccmr1 ;
  TIMx->CCER = tmpccer;
}

/* Konec souboru */
