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

void print_usage()
{
	printf("Function: i9053_lpf\n");
	printf("Setup Low Pass Filter for I-9053P on CPU mode (by software control)\n");
	printf("Usage: i9053_lpf slot type lpf_value\n");
	printf("Example: i9053_lpf 1 1 35\n");
	printf("Write 35 ms for Low Pass Filter of slot 1, which jumper cap in CPU position \n");
}

int main(int argc, char **argv)
{
	int loop, RetValue,slot,type; //type==1---> software control, jumper cap in CPU position
    unsigned long start_time, end_time;
    DWORD read_lpf_Value, write_lpf_Value;
    
    if (argc != 4) {
		print_usage();
		return FAILURE;
	}
    
    slot=atoi(argv[1]);
    type=atoi(argv[2]);
    write_lpf_Value=atoi(argv[3]);
    
    if ((slot<1) || (slot>8) || (type>1) || (type<0)) {
    printf("The parameter is out of range.\n");
		return FAILURE;
    }
           
   	RetValue = Open_Slot(slot);
	if (RetValue >0) {
		printf("open Slot %d failed!\n",slot);
		return FAILURE;
	}
      
    RetValue= ReadDI_LPF(slot, &read_lpf_Value, type);  // type: 0 --> Hardware, 1 --> Software
    printf("ReadDI_LPF Value: \n");
    printf("    Response time = %d ms \n",read_lpf_Value);
    
    
    if (type ==1){  
        if (RetValue == -5){
            printf("\nIf you would like to setup LPF by software, it is necessary to move the jumper cap into CPU position. \n");
            printf("Please double check the position of Jumper cap for I-9053P. \n");
        }
        else {
               if (type ==1)
                    printf("The LPF is Software mode now. \n");
       
               RetValue=WriteDI_LPF(slot, write_lpf_Value);         //jumper cap in CPU position
               printf(">>> WriteDI_LPF = %d ms \n",write_lpf_Value);
            
               ReadDI_LPF(slot, &read_lpf_Value, type); 
               printf("ReadDI_LPF Value: \n");
               printf("    Response time = %d ms \n",read_lpf_Value);  
         }     
    } 
    else
         printf("\nIf you would like to setup LPF by software, please input <type> parameter = 1 \n");
        

	Close_Slot(slot);
	return SUCCESS;
}
