// i8172W_FR_2054DIO.c : Defines the entry point for the console application.
//

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

int main(int argc, char **argv)
{
	int id, slot= 1, iRet=0, RetValue=0;
	int diData,group,i,channel,mode,status,port,doData,bitVal;
	
	RetValue = Open_Slot(slot);
  if (RetValue >0) {
		    printf("Open Slot %d failed!\n",slot);
		    return FAILURE;
  }else {
        id = i8172W_Init(slot);
        if (id==0)
           printf("\nThere is a I-8172W located at Slot %d.\n",slot);
        else {
           printf("There is no I-8172W located at LinPAC.\n");
           return FAILURE;
        }       
  }  
	
	printf("Please input the port number of i-8172W (Range: 0 or 1): ");
	scanf("%d",&port);
	printf("Please input the group index of FR-2054 (Range: 0~7): ");
	scanf("%d",&group);
	
  printf("\n<< Operation mode >>\n");
  printf("  0. Write single channel output \n");
  printf("  1. Write multi channel output \n");
  printf("  2. Read single channel input \n");
	printf("  3. Read all channel input \n");  
	printf("Input operation mode: ");
  scanf("%d",&mode);
  
  switch(mode)
  { 
		// for FRNet Output mode, group will be 0 ~ 7
    	case 0:
			      printf("\nPlease input channel number (Range: 0~7): ");            
            scanf("%d",&bitVal);

		      	printf("Please input status (0:OFF, 1:ON ): ");            
            scanf("%d",&status);
            i8172W_DIO_WriteDOBit(slot,port,group,bitVal,status);
            break;
            
     	case 1:            
			      printf("\nEnter DO Data (Range:0 ~ 0xff): ");		           
            scanf("%X",&doData);
            i8172W_DIO_WriteDOGroup(slot, port, group, doData & 0xff);    
          	break;

      case 2:
            diData=i8172W_DIO_ReadDIGroup(slot,port,group + 8);
            printf("DI data: %X\n\r",diData);
            break;
    	
		// for input mode, group will be 8 ~ 15
    	case 3:
			      printf("\nPlease select DI channel number: ");	                
            scanf("%d",&channel);
            iRet=i8172W_DIO_ReadDIBit(slot, port, group + 8, channel);            
            if(iRet)
                printf("\nThe Channel %d is on.",channel);
            else
                printf("\nThe Channel %d is off.",channel);
            break;
  }
    
  Close_Slot(slot);    
    
	return 0;
}

