// power_on i9028.c: Defines the entry point for the console application.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "msw.h"
#include "i8028U.h"

int main()
{
	char GainStr[6][32]={"0~ 5V", "0~ 10V", "+/- 5V", "+/- 10V", "4~ 20mA", "0~ 20mA"};
	int enStatus= 1,slotIndex =-1, ret=-1, slot=2, RetValue, ch, block;
	short gain= 0;
	char temp[128];
	float powerVal=0, f, fAO;

	RetValue = Open_Slot(slot);
	if (RetValue ==0) 
	{
		ret=i9028U_Init(slot);
		if(ret== 0){
			slotIndex = slot;
			printf("I-9024U/9028U plug-in Slot: %d\n", slot);
		}
		else{
			printf("I-9024U/9028U_Init error, ret= %d \n",ret);
			Close_Slot(slot);
			return FAILURE;
		}   
	}
	else
	{
		printf("open Slot %d failed!\n",slot);
		return FAILURE;
	}

	printf("Step 1: Input 1 to test Power On Value\n");
	printf("\tInput 2 to test Power On as Last Output Value(Retentive or Battery Backup Output)\n");
	printf("\tPlease Input status: ");
	scanf("%d", &enStatus);
	printf("\n"); 
		
	i9028U_SetPowerOnEnStatus(slotIndex, enStatus);
	
	printf("Step 2: Input '0' to test 0~ 5V AO  \tInput '3' to test +/- 10V AO\n");
	printf("\tInput '1' to test 0~ 10V AO \tInput '4' to test 4~ 20mA AO\n");
	printf("\tInput '2' to test +/- 5V AO \tInput '5' to test 0~ 20mA AO\n");
	printf("\tPlease Input gain of AO range: "); // get select gain
	scanf( "%hd", &gain);
	if(gain >5)
		gain = 5;
   
	printf("\nStep 3: Please Input AO value(range %s): ", GainStr[gain]);
	scanf( "%f", &fAO);
	printf("\tSet Output AO Value:%7.3f \n\n",  fAO);

	printf("Step 4: Range of Channel\n");
	printf("\tFor 9028U Channel index 0~7 \n");
	printf("\tFor 9024U Channel index 0~3 \n");
	printf("\tPlease Input Channel Index for AO test: ");
	scanf("%d", &ch);
	printf("\n"); 
	i9028U_WriteAO(slotIndex, ch ,gain, fAO);
	
	
	if(enStatus ==1)  //test Power On Value
	{
		printf("Step 5: Please Input Power On Value(Gain %s): ", GainStr[gain]);
		scanf( "%f", &powerVal);
    
		printf("\tSet Power On Value:%7.3f \n", powerVal);
		
		i9028U_WritePowerOn_AO(slotIndex , ch, gain, powerVal);
	
		i9028U_ReadPowerOn_AO(slotIndex , ch, &gain, &powerVal);
		printf("\tSet Channel %d, Gain %s, Power On Value %6.3f\n", ch, GainStr[gain], powerVal);
		
		printf("\nPlease Reset the Power of PAC and measure the AO output value to be as Power On Value\n");
		
	}
	else if(enStatus ==2) //test Power On as Last Output Value
	{
		printf("Please Reset the Power of PAC and measure the AO output value to be as Last Output Value \n");
		printf("Or Restart the program and measure the AO output value to be as Last Output Value\n");
	}

	Close_Slot(slot);

	return 0;
}

