/** @file temperature.c

@brief functions commonly used by sample.


@par LICENCE
@verbatim
Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code of this module.

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 should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

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


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <sys/sysmacros.h> /* major() minor() */
#define NDEBUG
#include <assert.h>
#include <apci3200.h>
#include "convert.h"
#include "findboards.h"
#include "input.h"
#include "errorlib.h"
#include "errormsg.h"
#include "libapci3200sample.h"
//--------------------------------------------------------------------------------
/** this function look up for cards in the /proc/sys/apci3200/ directory
* @param cards A pointer to the card area to fill
* @return The number of cards detected (may be zero if nor cards found).
*/
int apci3200_find_cards(int ** cards)
{
	return apci_find_boards("apci3200", cards);
}
//--------------------------------------------------------------------------------
/* utility function to convert numerical unit value in symbol */
const char* apci3200_unit2symb(unsigned int val)
{
	switch(val)
	{
	case APCI3200_NANOVOLT:
		return "nV";
	case APCI3200_MICROVOLT:
		return "uV";
	case APCI3200_MILLIVOLT:
		return "mV";
	case APCI3200_VOLT:
		return "V";
	case APCI3200_NANOAMPERE:
		return "nA";
	case APCI3200_MICROAMPERE:
		return "uA";
	case APCI3200_MILLIAMPERE:
		return "mA";
	case APCI3200_AMPERE:
		return "A";
	default:
		return "?";
	}
}
//--------------------------------------------------------------------------------
int apci3200_ask_frequency(void)
{
	printf("acquisition frequency ?\n");
	printf("1: 20Hz/50ms\n");
	printf("2: 40Hz/25ms\n");
	printf("3: 80Hz/12.5ms\n");
	printf("4: 160Hz/6.25ms\n");
	switch(get_integer(1,4))
	{
	case 1:
		return APCI3200_FREQUENCE_20_HZ;
	case 2:
		return APCI3200_FREQUENCE_40_HZ;
	case 3:
		return APCI3200_FREQUENCE_80_HZ;
	case 4:
		return APCI3200_FREQUENCE_160_HZ;
	default:
		exit(1);
	}
}
//--------------------------------------------------------------------------------
void apci3200_dump_sensor(struct acquisition_t *acquisition)
{
	switch(acquisition->SensorClass)
	{
	case ADDIDATA_THERMOCOUPLE: /* */
		printf("termocouple ");
		switch(acquisition->SensorType)
		{
		case 0:
			printf("type B\n");
			break;
		case 1:
			printf("type E\n");
			break;
		case 2:
			printf("type J\n");
			break;
		case 3:
			printf("type K\n");
			break;
		case 4:
			printf("type N\n");
			break;
		case 5:
			printf("type R\n");
			break;
		case 6:
			printf("type S\n");
			break;
		case 7:
			printf("type T\n");
			break;
		default:
			printf("UNKNOWN");
		}
		break;
	case ADDIDATA_RTD:
		switch(acquisition->SensorType)
		{
		case 0:
			printf("PT\n");
			break;
		case 1:
			printf("Ni\n");
			break;
		default:
			printf("UNKNOWN\n");
			break;
		}
		break;
	default:
		printf("class UNKNOWN\n");
	}
}
//--------------------------------------------------------------------------------
void apci3200_ask_sensortype(int ch, struct acquisition_t *acquisition)
{
	printf("channel %d: class of sensor [0: Thermocouple 1:RTD]\n",ch);
	switch(get_integer(0,1))
	{
	case 0:
		acquisition->SensorClass=ADDIDATA_THERMOCOUPLE;
		break;
	case 1:
		acquisition->SensorClass=ADDIDATA_RTD;
		break;
	default:
		exit(1);
	}

	switch(acquisition->SensorClass)
	{
	case ADDIDATA_THERMOCOUPLE: /* */
		printf("\n");
		printf("0: type B\n");
		printf("1: type E\n");
		printf("2: type J\n");
		printf("3: type K\n");
		printf("4: type N\n");
		printf("5: type R\n");
		printf("6: type S\n");
		printf("7: type T\n");
		switch(get_integer(0,7))
		{
		case 0:
			acquisition->SensorType=ADDIDATA_THERMOCOUPLE_TYPE_B;
			break;
		case 1:
			acquisition->SensorType=ADDIDATA_THERMOCOUPLE_TYPE_E;
			break;
		case 2:
			acquisition->SensorType=ADDIDATA_THERMOCOUPLE_TYPE_J;
			break;
		case 3:
			acquisition->SensorType=ADDIDATA_THERMOCOUPLE_TYPE_K;
			break;
		case 4:
			acquisition->SensorType=ADDIDATA_THERMOCOUPLE_TYPE_N;
			break;
		case 5:
			acquisition->SensorType=ADDIDATA_THERMOCOUPLE_TYPE_R;
			break;
		case 6:
			acquisition->SensorType=ADDIDATA_THERMOCOUPLE_TYPE_S;
			break;
		case 7:
			acquisition->SensorType=ADDIDATA_THERMOCOUPLE_TYPE_T;
			break;
		default:
			exit(1);
		};
		break;
	case ADDIDATA_RTD:
		printf("0: type PT 1: type Ni");
		switch(get_integer(0,1))
		{
		case 0:
			acquisition->SensorType = ADDIDATA_RTD_TYPE_PT;
			break;
		case 1:
			acquisition->SensorType = ADDIDATA_RTD_TYPE_Ni;
			break;
		default:
			exit(1);
		}
		{
			int tmp;
			printf("RTD Value [0:100 1:1000]");
			tmp=get_integer(0,1);
			switch(tmp)
			{
			case 0:
				acquisition->RTDValue = 100;
				break;
			case 1:
				acquisition->RTDValue = 1000;
			default:
				assert(0);
			}
		}
		break;
	}
}
//--------------------------------------------------------------------------------
unsigned int apci3200_ask_gain(void)
{
	printf("value\tgain:\n");
	printf("0\t1\n");
	printf("1\t2\n");
	printf("2\t4\n");
	printf("3\t8\n");
	printf("4\t16\n");
	printf("5\t32\n");
	printf("6\t64\n");
	printf("7\t128\n");
	printf("choice");
	switch(get_integer(0,7))
	{
	case 0:
		return APCI3200_GAIN_1;
		break;
	case 1:
		return APCI3200_GAIN_2;
		break;
	case 2:
		return APCI3200_GAIN_4;
		break;
	case 3:
		return APCI3200_GAIN_8;
		break;
	case 4:
		return APCI3200_GAIN_16;
		break;
	case 5:
		return APCI3200_GAIN_32;
		break;
	case 6:
		return APCI3200_GAIN_64;
		break;
	case 7:
		return APCI3200_GAIN_128;
		break;
	default:
		exit(1);
	}
}
//--------------------------------------------------------------------------------
unsigned int apci3200_ask_polarity(void)
{
	printf("polarity [unipolar:%d bipolar:%d]",APCI3200_POLARITY_UNIPOLAR,APCI3200_POLARITY_BIPOLAR);
	return get_integer(APCI3200_POLARITY_UNIPOLAR,APCI3200_POLARITY_BIPOLAR);
}
//--------------------------------------------------------------------------------
unsigned int apci3200_ask_connection_type(void)
{
	printf("connection type [single:%d differential:%d]",APCI3200_CONNECTION_TYPE_SINGLE,APCI3200_CONNECTION_TYPE_DIFFERENTIAL);
	return get_integer(APCI3200_CONNECTION_TYPE_SINGLE,APCI3200_CONNECTION_TYPE_DIFFERENTIAL);
}
