// i8172W_ReadAI.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"
#include "i8172W.h"

int main(int argc, char **argv)
{
	//Declaration and default value assignment
	int RetValue, id, slot = 2, port = 0, group = 0;
	short bitMode = 12, typeCode = 0x08;  // -10 ~ +10 V
	short channel = 0, jumper =0, maxCh=8;
	float AIFloat = 0.0F;

	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("\nPlease input the group index(Dip switch) of FR-2017 (Range:0~7): ");
	scanf("%d",&group);

	printf("\n<<Jumper select of FR-2017>> \n");
	printf("   Differential = 0 \n");
	printf("   Single-Ended = 1 \n");
	printf("Please input jumper of FR-2017: ");
	scanf("%hd",&jumper);
	if(jumper ==0)
		maxCh=8 ; // default
	else
		maxCh=16;

	printf("\n\n<<Bit mode of FR-2017>>\n");
	printf("   12 :  for 12-bit \n");
	printf("   16 :  for 16-bit \n");
	printf("Please input bit mode for FR-2017: \n");
	scanf("%hd",&bitMode);

	printf("\n\n<<Type code of FR-2017>>\n");
	printf("   0x1a: 0 ~ 20 mA\n");
	printf("   0x7 : 4 ~ 20 mA\n");
	printf("   0x8 : +/-10 V  \n");
	printf("   0x9 : +/-5 V   \n");
	printf("   0xa : +/-1 V   \n");
	printf("   0xb : +/-500 mV\n");
	printf("   0xc : +/-150 mV\n");
	printf("   0xd : +/-20 mA \n");  
	printf("Please slect type code for FR-2017: ");
	scanf("%hd",&typeCode);

	if((0x1a != typeCode) && (typeCode < 7 || typeCode > 0xd))
		typeCode = 0x08;              // -10 ~ +10 V

	if(channel > 7 || channel < 0)  //differential mode
		channel = 0;

	for(channel=0;channel<maxCh;channel++)
	{
		i8172W_ReadAI_Ch(slot, port, group, bitMode, typeCode, channel, &AIFloat);
		printf("\nAI[%d] = %f",channel, AIFloat);
	}

	Close_Slot(slot);
	return 0;
}
