/************	
	Author: Martin Hsu
	Email: service@icpdas.com
	Date 2008/03/10
	
	it can set Encode Mode as 1: cw/ccw ; 2: pulse/dir ; 3: A/B phase
	i8093_Read32BitEncoder(int slot,int ch, long* EnCode32) will get hardware 24 Bit value + 8 bit software value and get total 32 bit Encoder value , 
	positive direction 0 + 1 ===> 0x00000001 +++++ ===> 0xffffffff +1 ===> 0	 
	negative direction 0 -1  ===> 0xffffffff ----- ===> 0x00000000 -1 ===>  0		
	
#define OK			0
#define ID_ERROR		-1
#define SLOT_OUT_RANGE		-2
#define CHANNEL_OUT_RANGE	-3
#define SELECT_CHANNEL_ERROR 	-4
#define MODE_ERROR		-5
*******************************/

/********************************
	8093 Error Code definition	
*******************************/

#include "msw.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "i8093W.h"

char title[]="Demo for I-8093W Encoder";

void showMenu(void)
{   
  printf("\n\n****************************************************\n\r");   
	printf("Press '1': Step 1 to Set Encoder Mode.\n\r");    	
	printf("Press '2': Step 2 to Set Xor Bit.\n\r");    	
	printf("Press '3': Step 3 to Set Preset Value.\n\r");
	printf("Press '4': Step 4 to Read 32 Bit Encoder Value.\n\r");     	
	printf("Press '5': Step 5 to Reset Encoder Value.\n\r"); 
	printf("Press '6': Step 6 to Read Index.\n\r"); 
	printf("Press '7': Step 7 to Read Line Status.\n\r");   
	printf("Press '8': Step 8 to Read Frequency.\n\r");	  
	printf("Press '9': Step 9 to Set Index Latch Status.\n\r");	
	printf("Press 'a': Step a to Read Latched Index.\n\r");	
	printf("Press 'b': Step b to Clear Latched Index.\n\r");
 	printf("Press 'Q' or 'q' to Exit the program.\n\r");
}


int main(void)
{
   	int ret, c, c1, ch, RetValue, i, no, TOP=0, CHCNT=3, latched=0, enableLatchStatus=0 ;
    int slotIndex=1, trans_flag[3]={0,0,0};
    long enVal[3]={0,0,0},Val;
    long Pre_enVal[3]={0,0,0};
    long Now_enVal[3]={0,0,0};
    long Pre_delta_enVal[3]={0,0,0};
    long Now_delta_enVal[3]={0,0,0};
    long PresetVal[3]={0,0,0};
    int A_Status=0,B_Status=0,C_Status=0,Mode=0,Xor=0,indexVal=-1;
    char LatchStr[2][32]={"Not Latched", "Latched"};
    char modeStr[4][32]={{"Stop"}, {"CW/CCW"}, {"Pulse/Dir"},{"A/B Phase"}};
    char buf[32];
    float freq=0;
    
	RetValue = Open_Slot(slotIndex);
	if (RetValue >0) {
		printf("Open Slot %d failed!\n",slotIndex);
		return FAILURE;
	}else {
		ret = i8093W_Init(slotIndex);
		if (ret==0)
			printf("\nThere is a I-8093W located at Slot %d\n",slotIndex);
		else {
			printf("There is no I-8093W located at LinPAC.\n");
			return FAILURE;
		}
	}

	printf("\n****************************");
	printf("\n%s",title);
	printf("\nLibrary Version =: %04X",i8093W_GetLibVersion());
	printf("\n****************************\n");

	showMenu();
	scanf("%d",&c);
      
	switch(c)
	{
		case 1: // Set Encoder Mode
			printf("\n\n******** ~ SET Encoder MODE ~ **********\n\r");
			printf("Press '1' to Set CW/CCW Mode \n");
			printf("Press '2' to Set Pulse/Dir Mode \n");
			printf("Press '3' to Set A/B Phase Mode \n");
			scanf("%d",&c1);
			if ((c1==1)||(c1==2)||(c1==3)){
				for(ch=TOP;ch<=CHCNT;ch++)
				{
					ret=i8093W_SetMode(slotIndex,ch,c1);
					printf("ret= %d Set Channel[%d] Mode %s \n",ret,ch, modeStr[c1]);
				}
			}else
				printf("Out of range!\n");

		break;
		case 2: //Set Xor
			printf("\n******** ~ SET XOR ~ *******\n\r");
			printf("Press '0' to Set XOR bit = 0\n");
			printf("Press '1' to Set XOR bit = 1\n");

			scanf("%d",&c1);
			if ((c1==0)||(c1==1)){
				for(ch=TOP;ch<=CHCNT;ch++)
				{
					ret=i8093W_SetXOR(slotIndex,ch,c1);
					printf("ret= %d Set Channel[%d] Xor %d \n",ret,ch, c1);	
				}
			} else
				printf("Out of range !\n");
		break;

		case 3:	//SET PRESET VALUE
			printf("\n******* ~ Set PRESET VALUE ~ *******\n");
			printf("Input CH0 (Hex Data Range 0 ~ 0xffffffff): ");
			scanf("%lx",&Val);
			printf("\n");

			i8093W_SetPresetValue(slotIndex,0,Val);
			printf("Set CH:0 Preset %lX\n",Val);

			printf("Input CH1 (Hex Data Range 0 ~ 0xffffffff): ");
			scanf("%lx",&Val);
			printf("\n");

			i8093W_SetPresetValue(slotIndex,1,Val);
			printf("Set CH:1 Preset %lX\n",Val);

			printf("Input CH2 (Hex Data Range 0 ~ 0xffffffff): ");
			scanf("%lx",&Val);
			printf("\n");

			i8093W_SetPresetValue(slotIndex,2,Val);
			printf("Set CH:2 Preset %lX\n",Val);

			usleep(100000);                       //100sec

			break;  
                              
		case 4:	//READ 32 BIT ENCODER VALUE
			printf("\n******* ~ READ 32 BIT ENCODER VALUE ~ *******\n");
			for(ch=TOP;ch<=CHCNT;ch++)
			{
				if (Pre_enVal[ch]>=Now_enVal[ch])
					Pre_delta_enVal[ch]=Pre_enVal[ch]-enVal[ch];
				else
					Pre_delta_enVal[ch]=enVal[ch]-Pre_enVal[ch];

				Pre_enVal[ch]=enVal[ch];

				ret=i8093W_Read32BitEncoder(slotIndex,ch,&enVal[ch]);
				printf("S[%d]C[%d] Encode32 %010ld %08lX\n",slotIndex,ch,enVal[ch],enVal[ch]);
			}

			usleep(500000);                       //500sec

		break;

		case 5:	//RESET ENCODER VALUE
			printf("\n******* ~ RESET ENCODER VALUE ~ *******\n\r");
			for(ch=TOP;ch<=CHCNT;ch++)
			{
				ret=i8093W_ResetEncoder(slotIndex,ch);
				printf("ret= %d Reset Channel[%d] \n",ret,ch);
			}

		break;

		case 6:	//READ INDEX STATUS
			printf("\n******* ~ READ INDEX STATUS ~ *******\n\r");
			for(ch=TOP;ch<=CHCNT;ch++)
			{
				ret=i8093W_GetIndex(slotIndex,ch,&indexVal);
				printf("S[%d]C[%d] index status %d\n",slotIndex,ch,indexVal);
			}

			usleep(500000);                       //500sec

		break;     

		case 7:	//READ LINE STATUS
			printf("\n******* ~ READ LINE STATUS ~ *******\n\r");
			for(ch=TOP;ch<=CHCNT;ch++)
			{
				ret=i8093W_GetLineStatus(slotIndex, ch, &A_Status,&B_Status,&C_Status);
				printf("S[%d]C[%d] A_Status[%d] B_Status[%d] C_Status[%d] \n",slotIndex,ch,A_Status,B_Status,C_Status);
			}

			usleep(500000);                       //500sec

		break; 

		case 8:	//READ Frequency
			printf("\n******* ~ READ Frequency ~ *******\n\r");
			for(ch=TOP;ch<=CHCNT;ch++)
			{
				freq=0;
				ret=i8093W_ReadFreq(slotIndex, ch, &freq);
				printf("S[%d]C[%d] Freq= %.3f  \t",slotIndex,ch,freq);
			}

			usleep(500000);                       //500sec

		break;

		case 9:	//SET Ch Index Latch Status
			printf("\n******* ~ SET Channel Index Latch Status ~ *******\n\r");
			printf("Press '0' to Disable Index Latch Status\n");
			printf("Press '1' to Enable Index Latch Status\n");
			scanf("%d",&c1);
			if (c1==0){
				for(ch=TOP;ch<=CHCNT;ch++)
				{
					ret=i8093W_SetIndexLatchStatus(slotIndex,ch,0);
					printf("\nret= %d Disable Channel[%d] Index Latched Status \n",ret,ch);
				}
			}else{
				for(ch=TOP;ch<=CHCNT;ch++)
				{
					ret=i8093W_SetIndexLatchStatus(slotIndex,ch,1);	
					printf("\nret= %d Enable Channel[%d] Index Latched Status \n",ret,ch);
				}
			}

			usleep(100000);                       //100sec

		break;

		case 'a':	 //READ Index Latched STATUS
			printf("\n******* ~ READ Index Latched STATUS ~ *******\n\r");
			for(ch=TOP;ch<=CHCNT;ch++)
			{
				ret=i8093W_GetIndexLatchStatus(slotIndex , ch, &latched, &enableLatchStatus);
				printf("S[%d]C[%d] Z index %s , status 0x%X \n",slotIndex,ch,LatchStr[latched],enableLatchStatus);
			}

			usleep(500000);                       //500sec

		break;

		case 'b':	 //Clear Index Latched Status
			printf("\n******* ~ Clear Index Latched Status ~ *******\n\r");
			printf("Press '0~2' to Clear Channel Index Latched Status\n");
			scanf("%d",&c1);

			ret=i8093W_ClearLatchedIndex(slotIndex, ch);
			printf("Clear Latched Index: S[%d]C[%d] \n",slotIndex,ch);

              break;


		case 'Q':case 'q':

			break;
	}

	Close_Slot(slotIndex);
	return 0;
}



