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

//////
// Search criteria
//////
#define MATCHFLAG RN_MATCH_PRDID	//set flag to search for product ID
#define MATCHPID  RN1200			//match ADC board product id


//#define NUMSAMPLES 1				// Set sample parameter to do
                                 // one conversion.


#define NUMSAMPLES 10				// Set sample parameter to do
                                 // ten conversions with averaging
                                 // the result.


const char vstr[][16] = {
	"0\t0 - 20 \n",
	"1\t0 - 10\n",
	"2\t0 - 5\n",
	"3\t0 - 4\n",
	"4\t0 - 2.5\n",
	"5\t0 - 2\n",
	"6\t0 - 1.25\n",
	"7\t0 - 1\n\n"
   };


// 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 printrange()
{
	auto int i;
   auto char buffer[256];
   auto int index;

   i = 2;
	DispStr(2,i++,"Gain_code\tVoltage range");
	DispStr(2,i++,"---------\t-------------");

   for (index = 0 ; index < 8; index++)
   {
   	sprintf(buffer, "\t%s", vstr[index]);
		DispStr(2,i++, buffer);
   }
   DispStr(2,++i, "Choose gain code for channel 0 - 7.... ");

}

void  blankScreen(int start, int end)
{
	auto char buffer[256];
   auto int i;

   memset(buffer, 0x00, sizeof(buffer));
 	memset(buffer, ' ', sizeof(buffer));
   buffer[sizeof(buffer)-1] = '\0';
   for(i=start; i < end; i++)
   {
   	DispStr(start, i, buffer);
   }

}


void main ()
{
	auto int device0, status;
 	auto rn_search newdev;
   auto rn_AinData aindata;
   auto float voltage;
   auto int channel;
   auto int gaincode;
   auto int key;
   auto int skip;
	auto char s[80];


	brdInit();
   rn_init(RN_PORTS, 1);      //initialize controller RN ports

   //search for device match
	newdev.flags = MATCHFLAG;
	newdev.productid = MATCHPID;
   if ((device0 = rn_find(&newdev)) == -1)
   {
   	printf("\n no device found\n");
      exit(0);
   }

   for(;;)
	{
   	blankScreen(0, 18);
		// display the voltage that was read on the A/D channels
     	printrange();
 	  	do
	  	{
	  		gaincode = getchar();
	  	} while (!( (gaincode >= '0') && (gaincode <= '7')) );
	  	gaincode = gaincode - 0x30;
	  	printf("%d", gaincode);
	  	while(kbhit()) getchar();

      // Must enable all channels for conversions again after reset
		for(channel = 0; channel < 8; channel++)
		{
			status = rn_anaInConfig(device0, channel, RNSINGLE, gaincode, 0);
         if(status != RNREADY)
         {
           	sprintf(s, "\nRabbitnet config error! Status = %x\n", status);
            DispStr(2,20, s);
            channel--;
         }
 		}
      blankScreen(0, 20);
      sprintf(s, "A/D input voltages for channels 0 - %d", 8 - 1 );
		DispStr(2, 2, s);
		DispStr(2, 3, "--------------------------------------");
      DispStr(2, 16, "Press Q or q to select another gain option.");

     for(;;)
     {
   		skip = 4;
			for(channel = 0; channel < 8; channel++)
			{
      		status = rn_anaInVolts(device0, channel, &voltage, NUMSAMPLES, 0);
            if(voltage != ADOVERFLOW)
         		sprintf(s, "Channel = %2d Voltage = %.3f                    ", channel, voltage);
            else
               sprintf(s, "Channel = %2d Voltage = Exceeded Voltage Range  ", channel);
            DispStr(2,channel + skip, s);
            if(status != RNREADY)
            {
             	sprintf(s, "\nRabbitnet ADC Read Error! Status = %x\n", status);
               DispStr(2,21, s);
               channel--;
            }
			}
         if(kbhit())
			{
				key = getchar();
				if (key == 'Q' || key == 'q')		// check if it's the q or Q key
				{
					while(kbhit()) getchar();
      			break;
     			}
			}
	  }
   }
}
///////////////////////////////////////////////////////////////////////////