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

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

static int osWDT = 1;

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

	/* open device file */
	if(Open_Slot(SLOT9)){
		printf("open Slot %d failed!\n",SLOT9);
		return FAILURE;
	}

	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);
			Close_Slot(SLOT9);
			return FAILURE;
		}
	}
	else
	{
		printf("Open Slot %d failed!\n",slot);
		return FAILURE;
	}
	

	printf("Step 1: 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 2: Please Input AO value(range %s): ", GainStr[gain]);
	scanf("%f", &fAO);     // get AO output value
	printf("\n"); 
	
	printf("Step 3: Range of Channel\n");
	printf("\tFor I-9028U Channel index 0~7  \n");
	printf("\tFor I-9024U Channel index 0~3  \n");
	printf("\tPlease Input Channel Index for AO test: ");
	scanf("%d", &ch);
			
	i9028U_WriteAO(slotIndex, ch, gain, fAO);
	printf("\tChannel %d, Range %s, AO Output Value=%7.3f \n",ch, GainStr[gain], fAO);
	printf("\n"); 

	printf("Step 4: Please Input Safe value, range %s: ", GainStr[gain]);
	scanf("%f", &safeVal); 	// get safe value
		
	
	i9028U_WriteSafe_AO(slotIndex, ch, gain, safeVal);
	i9028U_ReadSafe_AO(slotIndex, ch, &gain, &safeVal);
	printf("\tChannel %d, Range %s, Read Safe Value=%6.3f\n", ch, GainStr[gain], safeVal);
	
	
	enStatus = 1;           // set WDT enable = true
	ifWDT_Overwrite = 1;  	// enable Output when WDT timeout


	printf("\nStep 5: Please Input WDT timeout value(Range: 1~16 seconds): ");
	scanf("%lu", &wdtTimeout);
	printf("\n"); 
	i9028U_SetModuleWDTConfig(slotIndex, enStatus, wdtTimeout,ifWDT_Overwrite); //enStauts>0, install interrupt
  
	// if program does not call i9028U_RefreshModuleWDT(slotIndex) within  wdtTimeout second, then 9028U/9024U, 8028U/8024U will WDT timeout, and enter safe value
	// if program does not call  RefreshWatchDog(osWDT) within wdtTimeout + 5 second,  then PAC OS will reset
	// when 9028U/9024U, 8028U/8024U will WDT timeout, please check the AO output value, it should be the same as safe value.

	EnableWDT(wdtTimeout * 1000 );	// msecond, enable watchdog timer 

	printf("Press Enter to refresh Watchdog timer and get Module WDT Status, or other key to stop demo.\n");
	printf("(If WDT timeout OS will reset then check the Output Value is Safe Value)\n");


	while (getchar()==10){   //'Enter' key
		wdtStatus = i9028U_GetModuleWDTStatus(slotIndex); //1: a Watchdog timeout is currently active
		printf("Watchdog status: %d \n", wdtStatus);      //0: a Watchdog timeout is not currently active
		printf("Refresh watchdog timer\n");	
		i9028U_RefreshModuleWDT(slotIndex); 	
		EnableWDT(10000);      	/* refresh watchdog timer 10s */	
	} 	  
	DisableWDT(); 
	ClearWDTSWEven(0xf); 
	
	Close_Slot(slot);
	Close_Slot(SLOT9);
	return 0;
}
