/***************************************************************************
	ain_rdse_ch.c
   Z-World Inc 2003

	This sample program is intended for RabbitNet RN1100 Digital I/O boards

	Description
	===========
	This program reads and displays voltage and equivalent values of one
	single-ended analog input channel.  Coefficients are read from the
   RabbitNet board.  Computed raw data and equivalent voltages will be
   displayed.

	Instructions
	============
	1. Connect a power supply of 0-10 volts to AIN00 or AIN01.
   	Connect a power supply of 0-1 volts to AIN02.
	2. Compile and run this program.
	3. Follow the prompted directions of this program during execution.
	4. Values will continuously display.

***************************************************************************/
#class auto					/* Change local var storage default to "auto" */

#define STARTCHAN	0		//AIN00
#define ENDCHAN 2       //AIN02


void main()
{
	auto int rawdata, handle, status;
	auto int channel, keypress;
	auto float voltequ;
	auto char buffer[64];
   auto rn_AinData aindata;

	brdInit();                 //initialize controller
   rn_init(RN_PORTS, 1);      //initialize controller RN ports
   if ((handle = rn_device(0000)) == -1)  //device on port 0
   {
   	printf("\n no device found\n");
      exit(0);
   }

   // enable on all channels for conversions
	for(channel = 0; channel <= 2; channel++)
	{
		status = rn_anaInConfig(handle, channel, ENABLE, 0, 0);
   }

	while (1)
	{
		printf("\nChoose the AIN channel %d to %d .... ", STARTCHAN, ENDCHAN);
		gets(buffer);
		channel = atoi(buffer);

		printf("\nChoose:\n");
		printf("  1 to display raw data only\n");
		printf("  2 to display voltage only\n");
		printf("  3 to display both\n  .... ");
		gets(buffer);
		keypress = atoi(buffer);

		while (strcmp(buffer,"q") && strcmp(buffer,"Q"))
		{
			switch (keypress)
			{
				case 1:
            	//sample 10 times
					status = rn_anaIn(handle, channel, &rawdata, 10);
					printf("CH%2d raw data %d\n", channel, rawdata);
					break;
				case 2:
            	//sample 10 times before returning with voltage equivalent
					status = rn_anaInVolts(handle, channel, &voltequ, 10);
					printf("CH%2d is %.5f V\n", channel, voltequ);
					break;
				case 3:
            	//get calibrations
					status = rn_anaInRdCalib(handle, channel, SINGLE, 0, &aindata, 0);
               //sample 10 times before computing voltage equivalent
   				status = rn_anaIn(handle, channel, &rawdata, 10);
	            voltequ = (rawdata-aindata.offset)*aindata.gain;
					printf("CH%2d is %.5f V from raw data %d\n", channel, voltequ, rawdata);
					break;
				default:
				break;
			}
			printf("\npress enter key to read value again or 'Q' or read another channel\n\n");
			gets(buffer);
		}
	}
}	//end main

