/* Example of I-7565-H1/H2 module

   Author: Golden Wang

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

/* File level history (record changes for this file here.)
   v 0.0.1 10 Jan 2024 by Winson Chen
     Add I7565DNM_SendExplicitMSG_W, I7565DNM_IsExplicitMSGRespOK, I7565DNM_GetExplicitMSGRespValue

   v 0.0.0 1 Nov 2009 by Golden Wang
     create, blah blah... */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include "i7565DNM.h"

#define kbhit()   (int)getchar()
char Kbhit(void);

char Kbhit(void)
{
  int n=0;

  ioctl(0,FIONREAD,&n);
  if(n) return kbhit();

  return n;
}

int main(void)
{
	int usb, devtype, can1_baud = 0, can2_baud = 0, init = 0;
	char command;
	int i, wRetVal;
        DWORD RxMsgCnt=0, j=0;
        DWORD temp;
	BYTE DevPort, DesMACID = 1, ConType = ConType_Poll;
        WORD Ret;

	printf("\r\n");

	printf("1 : ttyUSB0\r\n");
        printf("2 : ttyUSB1\r\n");
       	printf("3 : ttyUSB2\r\n");
        printf("4 : ttyUSB3\r\n");
	printf("USB-Serial Device Name(1~4) :");
	scanf("%d",&usb);
	
	switch(usb)
        {
                case   1:
			DevPort = USB0; break;
                case   2:
			DevPort = USB1; break;
                case   3:
			DevPort = USB2; break;
                case   4:
			DevPort = USB3; break;
                default :
			printf("USB-Serial Device Name Error\r\n");
			return 0;
        }

	getchar();

	wRetVal = I7565DNM_ActiveModule(DevPort);

        if (wRetVal > 0) 
	{
                printf("open %d I7565DNM COM port failed! Erro : %d\r\n",  DevPort, wRetVal);
                return 0;
        }

	printf("a. Get I-7565DNM Firmware Version:\r\n");	
	printf("b. Reset I-7565DNM Firmware:\r\n");	
	printf("c. Get I-7565DNM CAN Bus Baud Rate:\r\n");  
	printf("d. Set I-7565DNM CAN Bus Baud Rate:\r\n");  
	printf("e. Get I-7565DNM MAC ID:\r\n"); 
	printf("f. Set I-7565DNM MAC ID:\r\n"); 
	printf("g. Clear EEPROM Configuration:\r\n"); 
	printf("h. Add Slave(MACID 12) Device:\r\n"); 
	printf("i. Remove Slave(MACID 12) Device:\r\n"); 
	printf("j. Add Slave(MACID 12) Poll I/O Connection:\r\n"); 
	printf("k. Remove Slave(MACID 12) PollI/O Connection:\r\n"); 
	printf("l. Start Slave(MACID 12) Device:\r\n"); 
	printf("m. Stop Slave(MACID 12) Device:\r\n"); 
	printf("n. Read Slave(MACID 12) Input Data:\r\n"); 
	printf("o. Write Slave(MACID 12) Output Data:\r\n"); 
	printf("p. SendExplicitMSG Slave(MACID 12) Device:\r\n"); 
	printf("q. IsExplicitMSGRespOK Slave(MACID 12) Device:\r\n"); 
	printf("r. GetExplicitMSGRespValue Slave(MACID 12) Device:\r\n"); 
	printf("y. Show All I-7565DNM's Demo Options:\r\n");
	printf("z. Shutdown and exit:\r\n");
    
	while(1)
	{
		command = Kbhit();

		if(command)
		{
			switch(command)
			{
				case 'a':
				{
					DWORD Result;
					getchar();

                                        Result = I7565DNM_GetFirmwareVersion(DevPort);

                                        if(Result == I7565DNM_GetFirmwareVerErr)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
                                                printf("I-7565DNM Version : 0x%x\r\n", Result);
                                        }
      				}
      				break;

				case 'b':
                                {
                                        DWORD Result;
                                        getchar();

					Result = I7565DNM_ResetFirmware(DevPort);
					
                                        if(Result == I7565DNM_ResetFirmwareErr)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
                                                printf("Reset I-7565DNM Firmware ... OK\r\n");
                                        }
                                }
                                break;

				case 'c':
				{
					DWORD Result, Baud;
                                        getchar();

					Result = I7565DNM_GetBaudRate(DevPort);

                                        if(Result == I7565DNM_GetBaudRateErr)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
						if(Result == CANBaud_125K)
							Baud = 125;
						else if (Result == CANBaud_250K)
							Baud = 250;
						else if (Result == CANBaud_500K)
							Baud = 500;

                                                printf("I-7565DNM CAN Baud Rate: %dKbps\r\n", Baud);
                                        }
      				}
      				break;


				case 'd':
				{
					int option;
					DWORD Result, Baud;

					printf("Set I-7565DNM CAN Bus Baud Rate(1~3)\r\n");
					printf("1 : 125 Kbps\r\n");
					printf("2 : 250 Kbps\r\n");
					printf("3 : 500 Kbps\r\n");
					scanf("%d",&option);

					switch(option)
					{
						case   1: Baud = CANBaud_125K; break;
						case   2: Baud = CANBaud_250K; break;
						case   3: Baud = CANBaud_500K; break;
						default  : Baud = CANBaud_125K; break;
					}

					getchar();

                                        Result = I7565DNM_SetBaudRate(DevPort, Baud);
					
                                        if(Result)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
                                                printf("Set I-7565DNM CAN Baud Rate ... OK\r\n");
                                        }
      				}
				break;
	
				case 'e':
				{
					DWORD Result;
                                        getchar();

                                        Result = I7565DNM_GetMasterMACID(DevPort);

                                        if(Result == I7565DNM_GetMasterMACIDErr)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
                                                printf("I-7565DNM MAC ID: %d\r\n", Result);
                                        }
      				}
      				break;

				case 'f':
				{
					BYTE MACID;
                                        DWORD Result;

                                        printf("Set I-7565DNM MAC ID(0~63)\r\n");
                                        scanf("%hhd",&MACID);

                                        getchar();
					
					Result = I7565DNM_SetMasterMACID(DevPort, MACID);

                                        if(Result)
       	                                {
               	                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                       	                }
                               	        else
                                       	{
                                               	printf("Set I-7565DNM MAC ID ... OK\r\n");
                                        }
				}
				break;
	
				case 'g':
                                {
                                        DWORD Result;

                                        getchar();

                                        Result = I7565DNM_ClearAllConfig(DevPort);

                                        if(Result)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
                                                printf("Clear I7565DNM EEPROM ... OK\r\n");
                                        }
                                }
                                break;


				case 'h':
				{
                                        DWORD Result;
/*
                                        printf("Set Slave MAC ID(0~63)\r\n");
                                        scanf("%d",&DesMACID);
*/
                                        getchar();

                                        Result = I7565DNM_AddDevice(DevPort, DesMACID, 1000);

                                        if(Result)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
                                                printf("Add Slave(MACID %d) Device... OK\r\n", DesMACID);
                                        }
      				}
				break;

				case 'i':
				{
                                        DWORD Result;

/*
                                        printf("Set Slave MAC ID(0~63)\r\n");
                                        scanf("%d",&DesMACID);
*/
                                        getchar();

                                        Result = I7565DNM_RemoveDevice(DevPort, DesMACID);

                                        if(Result)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
                                                printf("Remove Slave(MACID %d) Device... OK\r\n", DesMACID);
                                        }
	
      				}
				break;

				case 'j':
                                {
                                        DWORD Result;

/*
                                        printf("Set Slave MAC ID(0~63)\r\n");
                                        scanf("%d",&DesMACID);
*/
                                        getchar();

                                        Result = I7565DNM_AddIOConnection(DevPort, DesMACID, ConType, 1, 1, 10);

                                        if(Result)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
                                                printf("Add Slave(MACID %d) Device Poll I/O Connection Type(%d) ... OK\r\n", DesMACID, ConType);
                                        }

				}
				break;

				case 'k':
                                {
                                        DWORD Result;

/*
                                        printf("Set Slave MAC ID(0~63)\r\n");
                                        scanf("%d",&DesMACID);
*/
                                        getchar();

                                        Result = I7565DNM_RemoveIOConnection(DevPort,DesMACID,ConType);;

                                        if(Result)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
                                                printf("Remove Slave(MACID %d) Device Poll I/O Connection Type(%d) ... OK\r\n", DesMACID, ConType);
                                        }
                                }
                                break;

				case 'l':
                                {
                                        DWORD Result;

/*
                                        printf("Set Slave MAC ID(0~63)\r\n");
                                        scanf("%d",&DesMACID);
*/
                                        getchar();

                                        Result = I7565DNM_StartDevice(DevPort, DesMACID);

                                        if(Result)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
                                                printf("Start Slave(MACID %d) Device... OK\r\n", DesMACID);
                                        }


				}
				break;

				case 'm':
				{
                                        DWORD Result;

/*
                                        printf("Set Slave MAC ID(0~63)\r\n");
                                        scanf("%d",&DesMACID);
*/
                                        getchar();

                                        Result = I7565DNM_StopDevice(DevPort, DesMACID);

                                        if(Result)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
                                                printf("Stop Slave(MACID %d) Device... OK\r\n", DesMACID);
                                        }
				}
				break;

				case 'n':
                                {
					DWORD Result;
					WORD IOLen;
					BYTE IODATA[2];

/*
                                        printf("Set Slave MAC ID(0~63)\r\n");
                                        scanf("%d",&MACID);
*/
                                        getchar();

					Result = I7565DNM_ReadInputData(DevPort, DesMACID, ConType, &IOLen, IODATA);

					if(Result)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
						printf("Read Slave(MACID %d) Device Input Data : 0x%x\n", DesMACID, IODATA[0]);
                                        }


                                }
                                break;

				case 'o':
                                {
                                        DWORD Result;
                                        WORD IOLen;
                                        BYTE IODATA[8];

/*
                                        printf("Set Slave MAC ID(0~63)\r\n");
                                        scanf("%d",&DesMACID);
*/

                                        getchar();

					IODATA[0] = 0;
					printf("Write Output Data(0x00 ~ 0xff)\r\n");
					//scanf("%2x",IODATA);
					scanf("%hhx",&IODATA[0]);
                                        getchar();
					
					Result = I7565DNM_WriteOutputData(DevPort, DesMACID, ConType, 1, IODATA);

					if(Result)
                                        {
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        }
                                        else
                                        {
						printf("Write Slave(MACID %d) Device Output Data : 0x%x\n", DesMACID, IODATA[0]);
					}
                                }
				break;

				case 'p':
                                {
					DWORD Result;
					BYTE ServiceID = 0x0e, ClassID = 1, InstanceID = 1, DATA[2];
					WORD DataLen = 2;

					getchar();
					DATA[0] = 3;	//get CAN-2054D product code
					
					printf("ServiceID 0x%x, ClassID %d, InstanceID %d, data %d\n",ServiceID, ClassID, InstanceID, DATA[0]);
					Result = I7565DNM_SendExplicitMSG_W(DevPort, DesMACID, ServiceID, ClassID, InstanceID, DataLen, DATA);

					if(Result)
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        else
						printf("SendExplicitMSG Slave(MACID %d) Device OK\n", DesMACID);

				}
                                break;

				case 'q':
                                {
					DWORD Result;

					getchar();

					Result = I7565DNM_IsExplicitMSGRespOK(DevPort, DesMACID);

					if(Result)
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        else
						printf("IsExplicitMSGRespOK Slave(MACID %d) Device OK\n", DesMACID);

				}
                                break;

				case 'r':
                                {
					DWORD Result;
					WORD DataLen;
					BYTE DATA[10];
					int i;

					getchar();

					Result = I7565DNM_GetExplicitMSGRespValue(DevPort, DesMACID, &DataLen, DATA);

					if(Result)
                                                printf("I-7565DNM Error : Error Code = %d\r\n", Result);
                                        else
					{
						printf("GetExplicitMSGRespValue Slave(MACID %d) DataLen is %d DATA is ", DesMACID, DataLen);
						for(i = 0; i < DataLen; i++)
                                                	printf("0x%x ",DATA[i]);
        	                                printf("\n");
					}
				}
                                break;

				case 'y':
				{
					getchar();
					printf("a. Get I-7565DNM Firmware Version:\r\n");
					printf("b. Reset I-7565DNM Firmware:\r\n");
					printf("c. Get I-7565DNM CAN Bus Baud Rate:\r\n");
					printf("d. Set I-7565DNM CAN Bus Baud Rate:\r\n");
					printf("e. Get I-7565DNM MAC ID:\r\n");
					printf("f. Set I-7565DNM MAC ID:\r\n");
					printf("g. Clear EEPROM Configuration:\r\n");
					printf("h. Add Slave(MACID 12) Device:\r\n");
					printf("i. Remove Slave(MACID 12) Device:\r\n");
					printf("j. Add Slave(MACID 12) Poll I/O Connection:\r\n");
					printf("k. Remove Slave(MACID 12) PollI/O Connection:\r\n");
					printf("l. Start Slave(MACID 12) Device:\r\n");
					printf("m. Stop Slave(MACID 12) Device:\r\n");
					printf("n. Read Slave(MACID 12) Input Data:\r\n");
					printf("o. Write Slave(MACID 12) Output Data:\r\n");
					printf("p. SendExplicitMSG Slave(MACID 12) Device:\r\n"); 
					printf("q. IsExplicitMSGRespOK Slave(MACID 12) Device:\r\n"); 
					printf("r. GetExplicitMSGRespValue Slave(MACID 12) Device:\r\n"); 
					printf("y. Show All I-7565DNM's Demo Options:\r\n");
					printf("z. Shutdown and exit:\r\n");


				}
				break;

				case 'z':
				{

					getchar();
					Ret = I7565DNM_CloseModule(DevPort);
				
					if(Ret)
					{
						printf("ShutdownMaster Error Code = %d\r\n",Ret);
					}
					else
					{
						printf("ShutdownMaster OK\r\n");
						return 0;
					}

				}
			        break;
				default:
				        printf("Unknow command = %X\r\n",command);
			        break;
			}
		}
	}
}
