/*   
   v 1.0.0 24 Aug 2018 by Winson
     Create
 */

#include<stdio.h>
#include<stdlib.h>
#include "msw.h"
#include "i87H17W.h"

void Show();

int main()
{
	int i, wRetVal, slot;
	BYTE HLState, ch;
	char status[80] = {0}, InBuf[80] = {0};
	long iRet = 0;
	short iCheckSum = 0;
	WORD wTimeout = 1, Wstatus;
	int options;
	BYTE Enwdt, times;
	char GetConfig[10] = {0}, *SetConfig;
	WORD hVal, ahVal[8];
	float fVal, afVal[8];
	HTSendFrame SetFrame;
	HTRcvFrame GetFrame;
 
	wRetVal = Open_Com(ttySA0, 115200, Data8Bit, NonParity, OneStopBit);
	if (wRetVal > 0) {
		printf("open port failed!\n");
		return (-1);
	}

	//Check Open_Slot
	wRetVal = Open_Slot(0);
	if (wRetVal > 0) {
		printf("open Slot failed!\n");
		return (-1);
	}

	printf("Enter I-87H17W slot number\n");
	scanf("%d",&slot);
	ChangeToSlot(slot);

	Show();

	while(1)
	{
		printf("\nPress ESC to exit.\n\n");

		scanf("%d",&options);
		if(getchar() == 27)
			break;

		switch(options)
		{
			case 1:
				iRet = hart_GetModuleName(InBuf, iCheckSum, wTimeout);
				if(iRet)
				{
					printf("Error number %ld\n",iRet);
					return iRet;
				}
				else
					printf("Get module name: %s\n", InBuf);
			break;
			case 2:
				iRet = hart_GetFwVersion(InBuf, iCheckSum, wTimeout);
				if(iRet)
				{
					printf("Error number %ld\n",iRet);
					return iRet;
				}
				else
					printf("Get firmware version: %s\n",InBuf);
			break;
			case 3:
				iRet = hart_GetModRstSta(status, iCheckSum, wTimeout);
				if(iRet)
				{
					printf("Error number %ld\n",iRet);
					return iRet;
				}
				else
				{
					printf("Get module reset status: %d\n",*status);
					if(*status)
						printf("Module has been reset, and value will be 0 after reading.\n");
					else
						printf("Module has not been reset.\n");
				}
			break;
			case 4:
				iRet = hart_GetModDBSta(status, iCheckSum, wTimeout);
				if(iRet)
				{
					printf("Error number %ld\n",iRet);
					return iRet;
				}
				else
				{
					printf("Get module daughter board Staus: ");
					if(*status)
						printf("disconnected.\n");
					else
						printf("connected.\n");
				}
			break;
			case 5:
				iRet = hart_GetWDTStatus(&Wstatus, iCheckSum, wTimeout);
				if(iRet)
				{
					printf("Error number %ld\n",iRet);
					return iRet;
				}
				else
				{
					printf("Get watch dog timer status: %d\n",Wstatus);
					printf("[Bit 7]\n");
					printf("0: indicates that the host watchdog is disabled\n");
					printf("1: indicates the host watchdog is enabled\n");
					printf("[Bit 2]\n");
					printf("0: indicates that no host watchdog timeout occurred\n");
					printf("1: indicates that a host watchdog timeout occurred\n");
				}
			break;
			case 6:
				iRet = hart_RstWDTStatus(iCheckSum, wTimeout);
				if(iRet)
				{
					printf("Error number %ld\n",iRet);
					return iRet;
				}
				else
					printf("Reset watch dog timer status success\n");
			break;
			case 7:
				Enwdt = 1;	//1 WDT enable, 0 WDT disable
				times = 255;	//unit 0.1sec, EX: 01 means 0.1 seconds, ff means 25.5 seconds.
				iRet = hart_SetWDTConfig(Enwdt, times, iCheckSum, wTimeout);
				if(iRet)
				{
					printf("Error number %ld\n",iRet);
					return iRet;
				}
				else
					printf("Set watch dog timer configure success\n");
			break;
			case 8:
				iRet = hart_GetWDTConfig(&Enwdt, &times, iCheckSum, wTimeout);
				if(iRet)
				{
					printf("Error number %ld\n",iRet);
					return iRet;
				}
				else
				{
					if(Enwdt)
						printf("Get watch dog timer configure WDT enable, timeout value %.1f seconds\n", (float)times/10);
					else
						printf("Get watch dog timer configure WDT disable, timeout value %.1f seconds\n", (float)times/10);
				}
			break;
			case 9:
				ch = 0;
				wTimeout = 3;
				iRet = hart_EnableCal(iCheckSum, wTimeout);
				if(iRet)
				{
					printf("Error number %ld\n",iRet);
					return iRet;
				}
				else
				{
					iRet = hart_SetZeroCal(ch, iCheckSum, wTimeout);
					if(iRet)
					{
						printf("Error number %ld\n",iRet);
						return iRet;
					}
					else
					{
						printf("Set zero calibration success\n");
						hart_DisableCal(iCheckSum, wTimeout);
					}
				}
			break;
			case 10:
				ch = 0;
				wTimeout = 3;
				iRet = hart_EnableCal(iCheckSum, wTimeout);
				if(iRet)
				{
					printf("Error number %ld\n",iRet);
					return iRet;
				}
				else
				{
					iRet = hart_SetFullCal(ch, iCheckSum, wTimeout);
					if(iRet)
					{
						printf("Error number %ld\n",iRet);
						return iRet;
					}
					else
					{
						printf("Set zero calibration success\n");
						if(ch == 7)
							hart_DisableCal(iCheckSum, wTimeout);
					}
				}
			break;
			case 11:
				HLState = 1;	//1 get the maximum AI data, 0 get the minimum AI data
				ch = 1;

				//Get Raw Data Format Setting
				iRet = hart_GetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				//Set New Data Format Setting
				SetConfig = "00";	//00 float data format, 02 hexadecimal data format
				iRet = hart_SetConfiguration(SetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				iRet = hart_ReadChHLfVal(HLState, ch, &fVal, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				if(HLState)
					printf("Read CH%d maximum AI float value %.3f\n",ch, fVal);
				else
					printf("Read CH%d minimum AI float value %.3f\n",ch, fVal);
	
				//Recover to Raw Data Format Setting
				iRet = hart_SetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;
		
			break;
			case 12:
				HLState = 1;	//1 get the maximum AI data, 0 get the minimum AI data
				ch = 0;

				//Get Raw Data Format Setting
				iRet = hart_GetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				//Set New Data Format Setting
				SetConfig = "02";	//00 float data format, 02 hexadecimal data format
				iRet = hart_SetConfiguration(SetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				iRet = hart_ReadChHLhVal(HLState, ch, &hVal, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				if(HLState)
					printf("Read CH%d maximum AI hexadecimal value 0x%x\n",ch, hVal);
				else
					printf("Read CH%d minimum AI hexadecimal value 0x%x\n",ch, hVal);
	
				//Recover to Raw Data Format Setting
				iRet = hart_SetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;
		
			break;
			case 13:
				HLState = 1;	//1 get the maximum AI data, 0 get the minimum AI data

				//Get Raw Data Format Setting
				iRet = hart_GetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				//Set New Data Format Setting
				SetConfig = "00";	//00 float data format, 02 hexadecimal data format
				iRet = hart_SetConfiguration(SetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				iRet = hart_ReadAllChHLfVal(HLState, afVal, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				if(HLState)
					printf("Read all channels maximum AI float value:\n");
				else
					printf("Read all channels minimum AI float value:\n");

				for(i = 0; i < 8; i++)
					printf("CH%d %.3f\n", i, afVal[i]);

				//Recover to Raw Data Format Setting
				iRet = hart_SetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;
			break;
			case 14:
				HLState = 1;	//1 get the maximum AI data, 0 get the minimum AI data

				//Get Raw Data Format Setting
				iRet = hart_GetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				//Set New Data Format Setting
				SetConfig = "02";	//00 float data format, 02 hexadecimal data format
				iRet = hart_SetConfiguration(SetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				iRet = hart_ReadAllChHLhVal(HLState, ahVal, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				if(HLState)
					printf("Read all channels maximum AI hexadecimal value:\n");
				else
					printf("Read all channels minimum AI hexadecimal value:\n");

				for(i = 0; i < 8; i++)
					printf("CH%d 0x%x\n", i, ahVal[i]);

				//Recover to Raw Data Format Setting
				iRet = hart_SetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;
			break;
			case 15:
				HLState = 1;	//1 Clear CH maximum AI data, 0 Clear CH minimum AI data
				ch = 0;
				iRet = hart_ClrChHLVal(HLState, ch, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				if(HLState)
					printf("Clear CH%d maximum AI value.\n", ch);
				else
					printf("Clear CH%d minimum AI value.\n", ch);
			break;
			case 16:
				HLState = 1;	//1 Clear maximum AI data, 0 Clear minimum AI data
				iRet = hart_ClrAllChHLVal(HLState, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				if(HLState)
					printf("Clear maximum AI value for all channels.\n");
				else
					printf("Clear minimum AI value for all channels.\n");
			break;
			case 17:
				//Get Raw Data Format Setting
				iRet = hart_GetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				//Set New Data Format Setting
				SetConfig = "00";	//00 float data format, 02 hexadecimal data format
				iRet = hart_SetConfiguration(SetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				ch = 0;
				iRet = hart_ReadChfVal(ch, &fVal, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				printf("Read CH%d AI float value %.3f\n",ch, fVal);

				//Recover to Raw Data Format Setting
				iRet = hart_SetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;
			break;
			case 18:
				//Get Raw Data Format Setting
			        iRet = hart_GetConfiguration(GetConfig, iCheckSum, wTimeout);
			        if(iRet!=_NOERR)
			                return iRet;

			        //Set New Data Format Setting
				SetConfig = "02";	//00 float data format, 02 hexadecimal data format
			        iRet = hart_SetConfiguration(SetConfig, iCheckSum, wTimeout);
			        if(iRet!=_NOERR)
		                return iRet;

			        ch = 1;
			        iRet = hart_ReadChhVal(ch, &hVal, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				printf("Read CH%d AI hex value 0x%x\n",ch, hVal);

			        //Recover to Raw Data Format Setting
			        iRet = hart_SetConfiguration(GetConfig, iCheckSum, wTimeout);
			        if(iRet!=_NOERR)
			                return iRet;	
			break;
			case 19:
				//Get Raw Data Format Setting
				iRet = hart_GetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				//Set New Data Format Setting
				SetConfig = "00";	//00 float data format, 02 hexadecimal data format
				iRet = hart_SetConfiguration(SetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				iRet = hart_ReadAllfVal(afVal, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				printf("Read All channels AI float value:\n");
				for(i = 0; i < 8; i++)
				{
					printf("CH%d %.3f  ", i, afVal[i]);
					if( i % 4 == 3)
					        printf("\n");
				}

				//Recover to Raw Data Format Setting
				iRet = hart_SetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;
			break;
			case 20:
				//Get Raw Data Format Setting
				iRet = hart_GetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				//Set New Data Format Setting
				SetConfig = "02";	//00 float data format, 02 hexadecimal data format
				iRet = hart_SetConfiguration(SetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				iRet = hart_ReadAllhVal(ahVal, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;

				printf("Read All channels AI hex value:\n");
				for(i = 0; i < 8; i++)
				{
					printf("CH%d 0x%x  ", i, ahVal[i]);
					if( i % 4 == 3)
					        printf("\n");
				}

				//Recover to Raw Data Format Setting
				iRet = hart_SetConfiguration(GetConfig, iCheckSum, wTimeout);
				if(iRet!=_NOERR)
					return iRet;
			break;
			case 21:
				ch = 7;
				printf("Your value should turn to Hexadecimal from Hexadecimal.\n");
				printf("Ex. you want to input value 0x80, you should input 128\n");

				printf("preamble:");
				scanf("%hhu",&SetFrame.preamble);
				printf("delimeter:");
				scanf("%hhu",&SetFrame.delimeter);
				if((SetFrame.delimeter >> 7) & 0x1)
				{
					for(i = 0; i < 5; i++)
					printf("addr[%d]: ", i);
					scanf("%hhu",&SetFrame.addr[i]);
				}
				else
				{
					printf("addr:");
					scanf("%hhu",SetFrame.addr);
				}
				printf("cmd:");
				scanf("%hhu",&SetFrame.cmd);
				
				printf("data:(If no data, press ESC then Enter)");
				scanf("%s",SetFrame.data);

				SetFrame.SdataLen = strlen(SetFrame.data);
					
				hart_Send(&SetFrame, ch, iCheckSum, wTimeout);
			break;
			case 22:
				ch = 7;
				hart_Recv(&GetFrame, ch, iCheckSum, wTimeout);
				printf("preamble %d\n",GetFrame.preamble);
				printf("delimeter %d\n",GetFrame.delimeter);
				for(i = 0; i < 5; i++)
					printf("addr[%d] 0x%x\n",i, GetFrame.addr[i]);
				printf("cmd %d\n", GetFrame.cmd);
				printf("datalen %d\n", GetFrame.Rdatalen);
				printf("response code %d\n", GetFrame.response_code);
				for(i = 0; i < GetFrame.Rdatalen-2; i++)
					printf("data 0x%x\n", GetFrame.data[i]);
				
			break;
			default:
				Show();
			break;
		}
	}

 	Close_Com(ttySA0);
	Close_SlotAll();
	return 0;
}

void Show()
{
	printf("1.  Get module name.\n");
	printf("2.  Get module firmware version.\n");
	printf("3.  Get module reset status.\n");
	printf("4.  Get the Connection status of daughter board.\n");
	printf("5.  Get the host watchdog time out status of a module.\n");
	printf("6.  Reset the host watchdog time out status of a module.\n");
	printf("7.  Set the host watchdog time out setting of a module.\n");
	printf("8.  Get the host watchdog time out setting of a module.\n");
	printf("9.  Run the zero calibration of the specified channel.\n");
	printf("10. Run the span calibration of the specified channel.\n");
	printf("11. Get the maximum or minimum analog input value with float format of the specified channel.\n");
	printf("12. Get the maximum or minimum analog input value with hex format of the specified channel. \n");
	printf("13. Get the maximum or minimum analog input value with float format of all channels. \n");
	printf("14. Get the maximum or minimum analog input value with hex format of all channels. \n");
	printf("15. Clear the maximum or minimum analog input value of the specified channel. \n");
	printf("16. Clear the maximum or minimum analog input value of all channels.\n");
	printf("17. Get the current analog input value with float format of the specified channel.\n");
	printf("18. Get the current analog input value with hex format of the specified channel.\n");
	printf("19. Get the current analog input value with float format of all channels.\n");
	printf("20. Get the current analog input value with hex format of all channels.\n");
	printf("21. Send the HART frame to HART device.\n");
	printf("22. Receive the HART frame from HART device.\n");
}
