//----------------------------------------------------
/**
	\file		thermocouple.c

	\brief		Knihovna staraciji se o termoclanek
				<<< ALL >>>

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

#include "thermocouple.h"
#include "appiodef.h"
#include "misc.h"



void ThermocoupleVoltToTemp( THERMOCOUPLE_CTX * _ctx, float_t _volt)
{
	float x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;

	x9 = _volt * (_ctx->A9);
	x8 = _volt * (_ctx->A8 + x9);
	x7 = _volt * (_ctx->A7 + x8);
	x6 = _volt * (_ctx->A6 + x7);
	x5 = _volt * (_ctx->A5 + x6);
	x4 = _volt * (_ctx->A4 + x5);
	x3 = _volt * (_ctx->A3 + x4);
	x2 = _volt * (_ctx->A2 + x3);
	x1 = _volt * (_ctx->A1 + x2);
	x0 = _ctx->A0 + x1;

	_ctx->TempOnThermoucouple = x0;
}


float_t ThermocoupleVoltOnJunction( THERMOCOUPLE_CTX * _ctx, aiodata_t _volt )
{
	float_t ret = 0;
	float_t voltJunction = _volt * VOLT_ON_DIV;
	float_t mvolt = voltJunction *  1000;

	_ctx->mVoltOnJunction = mvolt;
	_ctx->TempOnJunction = mvolt / _ctx->JunctionTemp_on_Volt;

	ret = _ctx->TempOnJunction * _ctx->coefNear25;


	return ret;
}


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

	\param		_ctx			kontext knihovny
				_thermocouple	napeti na termoclaneku
				_junction		napeti na prechodu na desku
				_aX				X-ty koeficient rovnice pro vypocet teploty 
*/
//----------------------------------------------------

void ThermoUpdateConstant( THERMOCOUPLE_CTX * _ctx, float_t	_thermocoupleGain, float_t _junctionTemp_on_Volt,
						  float_t _AmpflOffset, float_t _filtrConst, float_t _coefnear25, float_t _a0, 
						  float_t _a1, float_t _a2, float_t _a3, float_t _a4, float_t _a5, float_t _a6, 
						  float_t _a7, float_t _a8, float_t _a9)
{
	_ctx->ThermocoupleGain = _thermocoupleGain;
	_ctx->JunctionTemp_on_Volt = _junctionTemp_on_Volt;
	_ctx->AmpflOffset = _AmpflOffset;

	_ctx->A0 = _a0;
	_ctx->A1 = _a1;
	_ctx->A2 = _a2;
	_ctx->A3 = _a3;
	_ctx->A4 = _a4;
	_ctx->A5 = _a5;
	_ctx->A6 = _a6;
	_ctx->A7 = _a7;
	_ctx->A8 = _a8;
	_ctx->A9 = _a9;

	_ctx->FilterKonstant = _filtrConst;
	_ctx->coefNear25 = (float_t) (_coefnear25 * 1e-6);
}


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

	\param		_ctx			kontext knihovny
				_thermocouple	napeti na termoclaneku
				_junction		napeti na prechodu na desku
*/
//----------------------------------------------------

void ThermocoupleTick( THERMOCOUPLE_CTX * _ctx, aiodata_t _thermocouple, aiodata_t _junction)
{
	ticks_t time = tmTicks();

	_ctx->oldThermo = KFilter(_thermocouple, _ctx->oldThermo, _ctx->FilterKonstant);
	_ctx->oldJunction = KFilter(_junction, _ctx->oldJunction, _ctx->FilterKonstant);
	
	float_t voltTh = (_ctx->oldThermo * VOLT_ON_DIV) - (_ctx->AmpflOffset / 1000);
	
	voltTh = voltTh / _ctx->ThermocoupleGain;
	voltTh = voltTh + ThermocoupleVoltOnJunction( _ctx, _ctx->oldJunction );

	ThermocoupleVoltToTemp( _ctx, voltTh); 

	_ctx->mVoltOnThermoucouple = voltTh * 1000;
	_ctx->Timer = tmDuration( time );
}


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

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

void ThermocoupleInit( THERMOCOUPLE_CTX * _ctx )
{
	memset( _ctx, 0, sizeof( THERMOCOUPLE_CTX ));

	_ctx->FilterKonstant = 0.8;
}


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

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

void ThermocoupleFinalize( THERMOCOUPLE_CTX * _ctx )
{
	memset( _ctx, 0, sizeof( THERMOCOUPLE_CTX ));
}

