//----------------------------------------------------
/**
	\file		lambda.c

	\brief		Knihovna staraciji se ovyhodnoceni Lamba sondy
				<<< ALL >>>

	\author		ConTeK s.r.o., Jakub Kastner
	\version	01.01
	\date		22.12.2015
*/
//----------------------------------------------------

#include "lambda.h"
#include "appiodef.h"
#include "misc.h"


//----------------------------------------------------
/**
	\brief		Funkce pro obnoceni konstant

	\param		_ctx			kontext knihovny
				_AmpflierGain	Zesileni zesilovace
				_AmpfOffset		Offset zesilovace
*/
//----------------------------------------------------

void LambdaUpdateConstant( LAMBDA_CTX * _ctx, float_t _AmpflierGain, float_t _AmpfOffset, 
						  float_t _filtCoef, float_t _x1, float_t _y1, float_t _x2, float_t _y2)
{
	_ctx->AmpflierGain = _AmpflierGain;
	_ctx->AmpfOffset = _AmpfOffset;
	_ctx->FilterConstant = _filtCoef;

	_ctx->X1 = round( _x1 / VOLT_ON_DIV);
	_ctx->X2 = round( _x2 / VOLT_ON_DIV);

	_ctx->Y1 = _y1;
	_ctx->Y2 = _y2;
}


//----------------------------------------------------
/**
	\brief		Periodicky volaná funkce

	\param		_ctx			kontext knihovny
*/
//----------------------------------------------------

void LambdaTick( LAMBDA_CTX * _ctx )
{
	float_t mvoltTmp = (sGetAInp(AI_LAMBDA) * VOLT_ON_DIV) * 1000;
		mvoltTmp = mvoltTmp - _ctx->AmpfOffset;
		mvoltTmp = mvoltTmp / _ctx->AmpflierGain;

	float_t power = LinIntrpl( sGetAInp(AI_LAMBDA_CURRENT), _ctx->X1, _ctx->Y1, _ctx->X2, _ctx->Y2);
	
	_ctx->PowerOn = sGetDOutp( LAMBDA_POWER );

	_ctx->LambdaVoltage = KFilter( mvoltTmp, _ctx->LambdaVoltage, _ctx->FilterConstant);
	_ctx->LambdaPower = KFilter( power, _ctx->LambdaPower, _ctx->FilterConstant);
}

//----------------------------------------------------
/**
	\brief		Inicializace knihovny

	\param		_ctx			kontext knihovny
*/
//----------------------------------------------------

void LambdaInit( LAMBDA_CTX * _ctx )
{
	memset( _ctx, 0, sizeof( LAMBDA_CTX ));
}

//----------------------------------------------------
/**
	\brief		Finalize knihovny

	\param		_ctx			kontext knihovny
*/
//----------------------------------------------------

void LambdaFinalize( LAMBDA_CTX * _ctx )
{
	memset( _ctx, 0, sizeof( LAMBDA_CTX ));
}

