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

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

	This program demonstrates how to use the analog input functions on voltage
   inputs.  The program will continuously display the voltage (average
 	of 10 samples) that is present on the A/D channels.

	Instructions:
	-------------
	1. Connect a voltmeter to the output of the power supply that you will
   monitor.

	2. Preset the voltage on the power supply to be within the voltage
	range of the A/D converter channel that your going to test. For
	input channels AIN00 and AIN01 input voltage range is 0 to +10V.
   AIN02 input voltage is 0 to +1V and AIN03 input voltage is +/-0.25V.

	3. Power-on the controller.

	4. Connect the output of power supply to one of the AIN channels.

	5. Compile and run this program.

	6. Vary the voltage on a AIN channel, the voltage displayed in the STDIO
	window and voltmeter should track each other.


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

#if FLASH_COMPILE == 0
#error  "MUST COMPILE TO FLASH!"
#endif

#define NUMSAMPLES 10
#define STARTCHAN 0
#define ENDCHAN 3
#define GAINSET 1

// set the STDIO cursor location and display a string
void DispStr(int x, int y, char *s)
{
   x += 0x20;
   y += 0x20;
   printf ("\x1B=%c%c%s", x, y, s);
}


///////////////////////////////////////////////////////////////////////////

void main ()
{
	auto float voltage;
	auto int channel, chanpos, status, handle;
	auto unsigned int avg_sample;
	auto char s[80];
	auto float ad_inputs[ENDCHAN+1];
	static float start_time, end_time, total_time;

	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 = STARTCHAN; channel <= ENDCHAN; channel++)
	{
		status = rn_anaInConfig(handle, channel, ENABLE, GAINSET, 0);
   }

	sprintf(s, "A/D input voltage for channels %d - %d", STARTCHAN, ENDCHAN );
	DispStr(2, 2, s);
	DispStr(2, 3, "--------------------------------------");

	DispStr(2, 5, "Voltage range 0 to +");

	for(;;)
	{
		for (channel = STARTCHAN; channel < ENDCHAN; channel++)
		{
			// sample each channel
			status = rn_anaInVolts(handle, channel, &ad_inputs[channel], NUMSAMPLES);
		}
      // read channel 3
		status = rn_anaInDiff(handle, channel, &ad_inputs[channel], NUMSAMPLES);

		for(channel = STARTCHAN; channel <= ENDCHAN; channel++)
		{
			voltage = ad_inputs[channel];
			sprintf(s, "Channel = %2d Voltage = %.3f  ", channel, voltage);
			DispStr(2, channel+ 6, s);
		}
	}
}
///////////////////////////////////////////////////////////////////////////

