//----------------------------------------------------
/**
	\file		slowtick.c

	\brief		Funkce pomaleho ticku
				<<< ALL >>>

	\author		ConTeK s.r.o., Pavel Vavra
	\version	01.01
	\date		24.5.2012
*/
//----------------------------------------------------

#include "slowtick.h"

#include "hwboard.h"
#include "timeticks.h"

#include "gvariables.h"
#include "XmlVariables.h"
#include "appiodef.h"

#include "thermocouple.h"
#include "lambda.h"
#include "photox.h"


bool_t PreGlow;
bool_t Glow;
bool_t AfterGlow;
bool_t Blow;
lticks_t lTimerGlow = 0;

//----------------------------------------------------
/**
	\brief		Blika ledkou ridici desky
*/
//----------------------------------------------------

void BoardLedTick(void)
{
	static ticks_t lTimer = 0;

	if(tmElapsed(lTimer))
	{
		SwapLed();
		lTimer = tmTimeout(500);
	}
}


//----------------------------------------------------
/**
	\brief		Funkce pomalého ticku
*/
//----------------------------------------------------

void SlowTick(void)
{
	//! reset watch dog timeru
	wdtReset();

	//! Blikani led diodou desky ridici jednotky
	BoardLedTick();

	ThermocoupleTick( &gThermocouple, sGetAInp(AI_THERMOCOUPLE), sGetAInp(AI_CONNTEMP) );

	gParam.State.Temp.TempCouple = gThermocouple.TempOnThermoucouple;
	gParam.State.Temp.mVCouple = gThermocouple.mVoltOnThermoucouple;
	gParam.State.Temp.TempJunction = gThermocouple.TempOnJunction;
	gParam.State.Temp.mVJunction = gThermocouple.mVoltOnJunction;

	LambdaTick( &gLambda );

	gParam.State.Lambda.LambdaCurrent = gLambda.LambdaPower;
	gParam.State.Lambda.LambdaVolt = gLambda.LambdaVoltage;


	PhotoTick( &gPhoto );
	gParam.State.Photo.RES = gPhoto.Res;
	gParam.State.Photo.RESVOLT = gPhoto.VoltRes;
	gParam.State.Photo.TRANS = gPhoto.VoltTrans;


	if (gParam.Debug.Out.DO0 || gParam.State.ComComand.GLOW)
	{
		if(!PreGlow && !Glow)
		{
			AfterGlow = FALSE;
			PreGlow = TRUE;
			Blow = TRUE;
			lTimerGlow = ltmTimeout(gParam.Pst.Conf.Vyhrivani.TimeBefore);
		}

		if(PreGlow && ltmElapsed(lTimerGlow) )
		{
			Blow = TRUE;
			PreGlow = FALSE;
			Glow = TRUE;
		}
	}
	else
	{
		if(Glow)
		{
			Glow = FALSE;
			AfterGlow = TRUE;
			lTimerGlow = ltmTimeout(gParam.Pst.Conf.Vyhrivani.TimeAfter);
		}

		if( AfterGlow )
		{
			if( ltmElapsed(lTimerGlow) )
			{
				AfterGlow = FALSE;
				Blow = FALSE;
			}
		}
		else
		{
			Blow = gParam.Debug.Out.DO1 || gParam.State.ComComand.BLOW;
		}
	}



	if(sGetDInp( DI_2 ))
		sSetDOutp( DO_0, Glow );
	else
		sSetDOutp( DO_0, FALSE );

	sSetDOutp( DO_1, Blow );
	sSetDOutp( DO_2, gParam.Debug.Out.DO2 );
	sSetDOutp( DO_3, gParam.Debug.Out.DO3 );
	sSetDOutp( DO_4, gParam.Debug.Out.DO4 );
	sSetDOutp( DO_5, gParam.Debug.Out.DO5 );
	sSetDOutp( DO_6, gParam.Debug.Out.DO6 );
	sSetDOutp( DO_7, gParam.Debug.Out.DO7 );

	sSetDOutp( nOUT_EN, gParam.Debug.Out.DO8 );
	sSetDOutp( LAMBDA_POWER, gParam.Debug.Out.DO9 );

	gParam.Debug.Inp.DI0 = sGetDInp( DI_0 );
	gParam.Debug.Inp.DI1 = sGetDInp( DI_1 );
	gParam.Debug.Inp.DI2 = sGetDInp( DI_2 );
	gParam.Debug.Inp.DI3 = sGetDInp( DI_3 );
	gParam.Debug.Inp.DI4 = sGetDInp( DI_4 );
	gParam.Debug.Inp.DI5 = sGetDInp( DI_5 );
	gParam.Debug.Inp.DI6 = sGetDInp( DI_6 );
	gParam.Debug.Inp.DI7 = sGetDInp( DI_7 );
	gParam.Debug.Inp.DI8 = sGetDInp( STS );

	gParam.State.ComComand.STATUS = PhotoGetFire( &gPhoto ) | ( Glow && sGetDInp( DI_2 )) << 1 | sGetDInp( DI_0 ) << 2 | sGetDInp( DI_1 ) << 3;
	
	//debug vypis An input
	gParam.Debug.Inp.AI0 = sGetAInp(AI_THERMOCOUPLE);
	gParam.Debug.Inp.AI1 = sGetAInp(AI_CONNTEMP);
	gParam.Debug.Inp.AI2 = sGetAInp(AI_LAMBDA_CURRENT);
	gParam.Debug.Inp.AI3 = sGetAInp(AI_RESERVED);
	gParam.Debug.Inp.AI4 = sGetAInp(AI_LAMBDA);
	gParam.Debug.Inp.AI5 = sGetAInp(AI_PHOTORES);
	gParam.Debug.Inp.AI6 = sGetAInp(AI_PHOTOTRANS);

	//debug vypis An Out
	sSetAOutp(AO_0, gParam.Debug.Out.AO0);

	//! mereni doby slow ticku
	gParam.Debug.Timing.LastSlowTickTime = (uint16_t)gTimeTickCtx.SlowTickTime;
	gParam.Debug.Timing.MaxSlowTickTime = (uint16_t)gTimeTickCtx.SlowTickMaxTime;
}
