/** @file apci3200-private.h
*
* @author Julien Krauth
*
* This header contains structure and function prototypes to demonstrate how to
* convert APCI-3200 values in temperatures.
*/

/** @par LICENCE
* @verbatim
Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code and the documentation.

ADDI-DATA GmbH
Dieselstrasse 3
D-77833 Ottersweier
Tel: +19(0)7223/9493-0
Fax: +49(0)7223/9493-92
http://www.addi-data-com
info@addi-data.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You shoud find the complete GPL in the COPYING file accompanying
this source code.
* @endverbatim
*/

#ifndef __APCI3200_CONVERT_H_
#define __APCI3200_CONVERT_H_

//------------------------------------------------------------------------------
/* Define the max number of value in the Thermocouple array */
#define	ADDIDATA_TH_TYPE_B_ARRAY_SIZE	9101
#define	ADDIDATA_TH_TYPE_E_ARRAY_SIZE	6351
#define	ADDIDATA_TH_TYPE_J_ARRAY_SIZE	7051
#define	ADDIDATA_TH_TYPE_K_ARRAY_SIZE	8211
#define	ADDIDATA_TH_TYPE_N_ARRAY_SIZE	7851
#define	ADDIDATA_TH_TYPE_R_ARRAY_SIZE	9091
#define	ADDIDATA_TH_TYPE_S_ARRAY_SIZE	9091
#define	ADDIDATA_TH_TYPE_T_ARRAY_SIZE	3001
#define	ADDIDATA_RTD_TYPE_PT_ARRAY_SIZE 1001
#define	ADDIDATA_RTD_TYPE_Ni_ARRAY_SIZE 1551

/* Define thermocpuple files names */
#define	ADDIDATA_TH_TYPE_B_FILE_NAME "B.CSV"
#define	ADDIDATA_TH_TYPE_E_FILE_NAME "E.CSV"
#define	ADDIDATA_TH_TYPE_J_FILE_NAME "J.CSV"
#define	ADDIDATA_TH_TYPE_K_FILE_NAME "K.CSV"
#define	ADDIDATA_TH_TYPE_N_FILE_NAME "N.CSV"
#define	ADDIDATA_TH_TYPE_R_FILE_NAME "R.CSV"
#define	ADDIDATA_TH_TYPE_S_FILE_NAME "S.CSV"
#define	ADDIDATA_TH_TYPE_T_FILE_NAME "T.CSV"
#define	ADDIDATA_RTD_TYPE_PT_FILE_NAME "PT1000.CSV"
#define	ADDIDATA_RTD_TYPE_Ni_FILE_NAME "Ni100.CSV"

/* Define the size of the convert file */
// Remove the file size test because the size is depending of the filesystem type.
//#define ADDIDATA_TH_TYPE_B_FILE_SIZE		55617
//#define ADDIDATA_TH_TYPE_E_FILE_SIZE		44831
//#define ADDIDATA_TH_TYPE_J_FILE_SIZE		49816
//#define ADDIDATA_TH_TYPE_K_FILE_SIZE		58036
//#define ADDIDATA_TH_TYPE_N_FILE_SIZE		54866
//#define ADDIDATA_TH_TYPE_R_FILE_SIZE		60120
//#define ADDIDATA_TH_TYPE_S_FILE_SIZE		59747
//#define ADDIDATA_TH_TYPE_T_FILE_SIZE		20240
//#define ADDIDATA_RTD_TYPE_PT_FILE_SIZE	 9305
//#define ADDIDATA_RTD_TYPE_Ni_FILE_SIZE	12302

/**@def ADDIDATA_TEMPERATURE_FILE_PATH
*
* This path is used by conversion functions to locate<br>
* the temperature CSV files. You can change this path<br>
* to your needs.
*
* @todo This string can be a parameter to path at the conversion function.
*
* @note the environment variable APCI3200_CSVDIR can be used to set this value at run time
*
*/
#ifndef ADDIDATA_CSVDIR
#define ADDIDATA_CSVDIR  "/etc/apci3200_temperature"
#endif

//------------------------------------------------------------------------------

typedef struct
{
	unsigned long dw_NumberOfValue;
	double *pd_VoltageValue;
	double *pd_TemperatureValue;
}str_APCI3200_THStruct,*pstr_APCI3200_THStruct;

typedef struct
{
	unsigned long dw_NumberOfValue;
	double *pd_ResistanceValue;
	double *pd_TemperatureValue;
}str_APCI3200_RTDStruct,*pstr_APCI3200_RTDStruct;

typedef struct
{
	unsigned char			      b_TypePTArrayLoaded;
	str_APCI3200_RTDStruct     s_TypePT_RTDStruct;
	unsigned char			      b_TypeNiArrayLoaded;
	str_APCI3200_RTDStruct     s_TypeNi_RTDStruct;
}
str_RTDBuffers;

typedef struct
{
	unsigned char			      b_TypeBArrayLoaded;
	str_APCI3200_THStruct      s_TypeB_THStruct;
	unsigned char			      b_TypeEArrayLoaded;
	str_APCI3200_THStruct      s_TypeE_THStruct;
	unsigned char			      b_TypeJArrayLoaded;
	str_APCI3200_THStruct      s_TypeJ_THStruct;
	unsigned char			      b_TypeKArrayLoaded;
	str_APCI3200_THStruct      s_TypeK_THStruct;
	unsigned char			      b_TypeNArrayLoaded;
	str_APCI3200_THStruct      s_TypeN_THStruct;
	unsigned char			      b_TypeRArrayLoaded;
	str_APCI3200_THStruct      s_TypeR_THStruct;
	unsigned char			      b_TypeSArrayLoaded;
	str_APCI3200_THStruct      s_TypeS_THStruct;
	unsigned char			      b_TypeTArrayLoaded;
	str_APCI3200_THStruct      s_TypeT_THStruct;
}
str_THBuffers;

typedef struct
{
	str_RTDBuffers s_RTDBuffers;
	str_THBuffers s_THBuffers;
}
str_TemperatureBuffers;

extern str_TemperatureBuffers s_TemperatureBuffers;

void v_APCI3200_InitGlobalStruct (void);

void v_APCI3200_FreeGlobalStruct (void);

/** Convert the digital temperature value (pdw_DigitalValue)<br>
*  into a real temperature value (pd_RealValue) from<br>
*  selected channel (w_Channel).
*
* @param [in]	w_AcquisitionType		Define the acquisition type.<br>
* 										When not scan mode use APCI3200_OTHER,<br>
* 										else APCI3200_SCAN.
* @param [in]	b_InputType				Type of the input (sensor):<br>
* 										- ADDIDATA_THERMOCOUPLE<br>
* 										- ADDIDATA_RTD
* @param [in]	b_TypePrecision			Type of the thermocouple or RTD.
*										Thermocouple:<br>
*										- ADDIDATA_THERMOCOUPLE_TYPE_B<br>
*										- ADDIDATA_THERMOCOUPLE_TYPE_E<br>
*										- ADDIDATA_THERMOCOUPLE_TYPE_J<br>
*										- ADDIDATA_THERMOCOUPLE_TYPE_K<br>
*										- ADDIDATA_THERMOCOUPLE_TYPE_N<br>
*										- ADDIDATA_THERMOCOUPLE_TYPE_R<br>
*										- ADDIDATA_THERMOCOUPLE_TYPE_S<br>
*										- ADDIDATA_THERMOCOUPLE_TYPE_T
*										RTD:
*										- ADDIDATA_RTD_TYPE_PT
*										- ADDIDATA_RTD_TYPE_Ni
* @param [in]	w_InputTypeValue		With thermocouple, 0.
* 										With RTD, 100 | 1000 .
* @param [in]	b_PolaritySelected		Connection polarity:<br>
*										- APCI3200_POLARITY_UNIPOLAR (0 to 2.5 V)<br>
*										- APCI3200_POLARITY_BIPOLAR (-2.5 to 2.5 V)
* @param [in]	w_GainSelected			The gain to use:<br>
*										- APCI3200_GAIN_1<br>
*										- APCI3200_GAIN_2<br>
*										- APCI3200_GAIN_4<br>
*										- APCI3200_GAIN_8<br>
*										- APCI3200_GAIN_16<br>
*										- APCI3200_GAIN_32<br>
*										- APCI3200_GAIN_128
* @param [in]	d_GainFactorValue		The gain factor that is returned by<br>
* 										do_CMD_APCI3200_ReadChannelCalibrationValue.
* 										This value has to be divided per / 1000000.0.
* @param [in]	d_CurrentSourceValue	The channel current source value that is returned by<br>
*										do_CMD_APCI3200_ReadChannelCalibrationValue<br>
*										This value has be divided per / 1000000000.0.
* @param [in]	d_CurrentSourceCJCValue	The CJC  current source value that is returned by<br>
*										do_CMD_APCI3200_ReadChannelCalibrationValue<br>
*										This value has be divided per / 1000000000.0.
* @param [in]	pdw_DigitalValue		A pointer of an array that will contain:<br>
*										pdw_DigitalValue [0] The channel value returned by do_CMD_APCI3200_Read1AnalogInputChannel.<br>
*										pdw_DigitalValue [1] The CJC value returned by do_CMD_APCI3200_ReadCJCValue.<br>
*										pdw_DigitalValue [2] The channel offset value returned by do_MD_APCI3200_ReadCalibrationOffsetValue.<br>
*										pdw_DigitalValue [3] The channel gain value returned by do_CMD_APCI3200_ReadCalibrationGainValue.<br>
*										pdw_DigitalValue [4] The CJC offset value returned by do_CMD_APCI3200_ReadCJCCalibrationOffsetValue.<br>
*										pdw_DigitalValue [5] The CJC gain value returned by do_CMD_APCI3200_ReadCJCCalibrationGainValue.
* @param [in]	pd_RealValue			Is a pointer on the temperature value in degree Celsius.
* @param [in]	d_ReferenceVoltage		Reference voltage as provided by CMD_APCI3200_ReadReferenceVoltage, but converted in Volt (1.25 or 2.5)
*
* @retval	0	No error.
* @retval	!=0	Error.
*/
int i_ADDIDATA_ConvertDigitalToRealTemperatureValueWithCorrectionParameters (
		unsigned int w_AcquisitionType,
		unsigned char b_InputType,
		unsigned char b_TypePrecision,
		unsigned int w_InputTypeValue,
		unsigned char b_PolaritySelected,
		unsigned int w_GainSelected,
		double d_GainFactorValue,
		double d_CurrentSourceValue,
		double d_CurrentSourceCJCValue,
		unsigned long *pdw_DigitalValue,
		double *pd_RealValue,
		double d_ReferenceVoltage /* 1.25 or 2.5 */
		);

#endif //#ifndef __APCI3200_CONVERT_H_
