#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include "msw.h"
#include "i8026W.h"

int slotIndex=-1,runFlag=0;
char sTemp[20];
void TestDIO();
void TestAI();
void TestAO();

/*int main()
{printf("\nThis demo show I-8026W firmware and lib information.\n");
  i8026W_hello();
}*/

int main()
{
	int slot;
	int function=0,c;
	short firmware, RetValue;

	printf("\nThis demo show I-8026W firmware and lib information.\n");

	for(slot=1;slot<9;slot++)
	{
		RetValue = Open_Slot(slot);
		if (RetValue >0) {
			printf("open Slot %d failed!\n",slot);
		}
		else
		{
			if(i8026W_Init(slot)==0)
			{
				slotIndex=slot;
				break;
			}
		}
		Close_Slot(slot);
	}

	if(slotIndex==-1)
	{
		printf("There is no I-8026W at LinPAC!\n");
		exit(0);
	}
	else
		printf("There is an I-8026W at slot %d\n",slotIndex);
	
	printf("\n****************************");

	firmware = i8026W_GetFirmwareVer(slotIndex);
	printf("\nFirmware Version =: %04X",firmware);    
	printf("\nLibrary Version =: %04X",i8026W_GetLibVersion());
	printf("\n****************************\n");
menu:
	printf("Choice Menu:\n\r");
	printf("	0: Test DIO\n\r");
	printf("	1: Test AI\n\r");
	printf("	2: Test AO\n\r");
	printf("	3: to quit the program\n\r");
	printf("========================================\n");
	printf("  Please choose (0~3):");
	scanf("%d",&function);
	switch(function)
	{
    	case 0:
    		TestDIO();
    		break;
    	case 1:
    		TestAI();
    		break;
    	case 2:
    		TestAO();
    		break;
    	case 3:
    		exit(0);
    		break;
	}

	printf("\nPress '0' to quit the program, others to menu: ");
	scanf("%d",&c);
	if(c==0){
		Close_Slot(slotIndex);
    	exit(0);
	}
	else
    		goto menu;

	return 0;
}

void TestDIO()
{
	int ch=0;
	short diVal=0,doVal=0,bitVal=0;
	unsigned char diBitArr[2],doBitArr[2];
	int option=0;

	printf("\nChoice Function:\n\r");
	printf("    0: Write DO \n\r");
	printf("    1: Write DO Bit\n\r");
	printf("    2: Readback DIO\n\r");
	printf("  Please choose (0~2):");
	scanf("%d",&option);

	switch(option)
	{
	case 0:
		printf("\nWrite DO Value 0~3: ");
		scanf("%hd",&doVal);
		i8026W_WriteDO(slotIndex, doVal);
   		break;
   		
	case 1:
		printf("\nSelect channel to write DO:  \n\r");
		printf("    0: Channel 0\n\r");
		printf("    1: Channel 1\n\r");
		printf("Please choose(0~1): ");
		scanf("%d",&ch);

		printf("\nInput DO status: \n\r");
	 	printf("    0: Turn OFF Channel %d \n\r",ch);
		printf("    1: Turn ON Channel %d\n\r",ch);
		printf("Please choose(0~1): ");
		scanf("%hd",&bitVal);
		i8026W_WriteDOBit(slotIndex,ch,bitVal);
		break;

	default:
		i8026W_ReadDIO(slotIndex, &diVal,&doVal,diBitArr,doBitArr);
		printf("DI= %d[%X], DO= %d[%X]\n", diVal,diVal,doVal,doVal);
		printf("Bit status: DI0[%d], DI1[%d], DO0[%d], DO1[%d]\n\r",diBitArr[0] ,diBitArr[1],doBitArr[0],doBitArr[1]);
		break;
	}
}


void TestAI()
{
	int i=0, ch=0, j, gain=0,format=0;
	float fVal=0;
	short hVal=0;
	float f[6];
	short h[6];
	unsigned long T,dT;
	char GainStr[5][32]={"+/-10V","+/-5V","+/-2.5V","+/-1.25V", "+/-20mA"};

	printf("\nGain:\n\r");
	printf("    0: +/-10V\n\r");
	printf("    1: +/-5V\n\r");
	printf("    2: +/-2.5V\n\r");
	printf("    3: +/-1.25V\n\r");
	printf("    4: +/-20mA\n\r");
	printf("  Please choose (0~4):");
	scanf("%d",&gain);

	printf("\nSelect Format: \n\r");
	printf("    0: Engineering format\n\r");
	printf("    1: Hex format\n\r");
	printf("Please choose(0~1): ");
	scanf("%d",&format);
	runFlag=1;

	for(ch=0;ch<6;ch++)
	{
		if(format==0)
		{
			i8026W_ReadAI(slotIndex,ch,gain,&fVal);
			f[ch]= fVal;
		}
		else
		{
			i8026W_ReadAIHex(slotIndex,ch,gain,&hVal);
			h[ch]= hVal;
		}
	}

	if(format==0)
		printf("\n%s ==> Ch0[%5.3f], Ch1[%5.3f], Ch2[%5.3f], Ch3[%5.3f], Ch4[%5.3f], Ch5[%5.3f]\n",GainStr[gain],f[0],f[1],f[2],f[3],f[4],f[5]);
	else
		printf("\n%s ==> Ch0[%04X], Ch1[%04X], Ch2[%04X], Ch3[%04X], Ch4[%04X], Ch5[%04X]\n",GainStr[gain],h[0],h[1],h[2],h[3],h[4],h[5]);
}

void TestAO()
{
	float fVal=0.0;
	char GainStr[5][32]={"+/-10V","+/-5V","+10V","+5V", "0~20mA"};
	int hVal=0,ch=0,format=0,gain=0;

	printf("\nGain: \n\r");
	printf("    0: +/-10V\n\r");
	printf("    1: +/-5V\n\r");
	printf("    2: +10V\n\r");
	printf("    3: +5V\n\r");
	printf("    4: +0~20mA\n\r");
	printf("  Please choose (0~4):");
	scanf("%d",&gain);

	printf("\nSelect Format: \n\r");
	printf("    0: Engineering format\n\r");
	printf("    1: Hex format\n\r");
	printf("  Please choose (0~1):");
	scanf("%d",&format);

	printf("\nSelect channel to write AO: \n\r");
	printf("    0: Channel 0\n\r");
	printf("    1: Channel 1\n\r");
	printf("  Please choose (0~1):");
	scanf("%d",&ch);

	if(format==0)
	{
		printf("\nInput AO value (%s): ", GainStr[gain]);
 		scanf("%f",&fVal);
		i8026W_WriteAO(slotIndex,ch,gain,fVal);
	}
	else
	{
		if(gain>=2)
			printf("\nInput AO value 0x0000~0xffff(Min~Max): ");
		else
			printf("\nInput AO value 0x8000~0x7fff(Min~Max): ");

 		scanf("%x",&hVal);
		i8026W_WriteAOHex(slotIndex,ch,gain,hVal);
	}
}

void TestAIO()
{
	float fVal=0;
	short hVal=0,gain=0;
	char GainStr[5][32]={"+/-10V","+/-5V","10V","5V", "0~20mA"};
	int i,ch=0,format=0;
	/*
	printf("\nGain: \n\r");
	printf("    0: +/-10V\n\r");
	printf("    1: +/-5V\n\r");
	printf("    2: +10V\n\r");
	printf("    3: +5V\n\r");
	printf("    4: +0~20mA\n\r");
	printf("  Please choose (0~4):");
	scanf("%d",&gain);

	printf("\nSelect Format: \n\r");
	printf("    0: Engineering format\n\r");
	printf("    1: Hex format\n\r");
	printf("  Please choose (0~1):");
	scanf("%d",&format);

	printf("\nSelect channel to write AO: \n\r");
	printf("    0: Channel 0\n\r");
	printf("    1: Channel 1\n\r");
	printf("  Please choose (0~1):");
	scanf("%d",&ch);

	if(format==0)
	{
		printf("\nInput AO value (%s): ", GainStr[gain]);
 		scanf("%f",&fVal);
		i8026W_WriteAO(slotIndex,ch,gain,fVal);
	}
	else
	{
		if(gain>=2)
			printf("\nInput AO value  0x0000~ 0xffff: \r");
		else
			printf("\nInput AO value  0x8000~ 0x7fff: \r");

 		scanf("%x",&hVal);
		i8026W_WriteAOHex(slotIndex,ch,gain,hVal);
	}
	*/

	short h[6];
	float f[6];
 // gain=2;
	for(ch=0;ch<2;ch++)
	{
	 	switch(ch)
		{
		    case 0:
		      fVal=4.5;
		      gain=1;
		    break;

		    case 1:
		      fVal=9.5;
		      gain=0;
		    break;
		}
		printf("write AO ch%d: GAIN=%d, fVal=%f \n",ch, gain, fVal);
		i8026W_WriteAO(slotIndex,ch, gain, fVal);
	}

	for(ch=0;ch<6;ch++)
	{
		switch(ch)
		{
		    case 0: case 5:
		       gain=0;
		    break;
		    case 1: case 4:
		       gain=1;
		    break;
		    case 2: case 3:
		       gain=2;
		    break;
		}

		for (i=0;i<3;i++){
			if(format==0)
			{
				i8026W_ReadAI(slotIndex,ch,gain,&fVal);
				f[ch]= fVal;
				// printf("%s ==> Ch%d[%5.3f]\n",GainStr[gain],ch, f[ch]);
				printf("%s ==> Ch0[%5.3f], Ch1[%5.3f], Ch2[%5.3f], Ch3[%5.3f], Ch4[%5.3f], Ch5[%5.3f]\n",GainStr[gain],f[0],f[1],f[2],f[3],f[4],f[5]);
			}
			else
			{
				i8026W_ReadAIHex(slotIndex,ch,gain,&hVal);
				h[ch]= hVal;
				printf("%s ==> Ch0[%04X], Ch1[%04X], Ch2[%04X], Ch3[%04X], Ch4[%04X], Ch5[%04X]\n\r",GainStr[gain],h[0],h[1],h[2],h[3],h[4],h[5]);
			}
		}
	}

/*	if(format==0)
			printf("%s ==> Ch0[%5.3f], Ch1[%5.3f], Ch2[%5.3f], Ch3[%5.3f], Ch4[%5.3f], Ch5[%5.3f]\n",GainStr[gain],f[0],f[1],f[2],f[3],f[4],f[5]);
	else
			printf("%s ==> Ch0[%04X], Ch1[%04X], Ch2[%04X], Ch3[%04X], Ch4[%04X], Ch5[%04X]\n\r",GainStr[gain],h[0],h[1],h[2],h[3],h[4],h[5]);
*/
}
