/*
	Release : libTCUSB48IO v1.0.0
	Date    : 12/11/2015
	Author  : 3C Concept SA
	
	
	Simple example to create a console application 
	Functions :
		- Open/Close the module TCUSB/48IO
		- Read the selected Input
		- Control the selected Output
		- Read Input Counters
	

	To build use the command "make" 
	(assuming you have the libTCUSB48IO library in the /usr/lib directory).
	
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


//#include "libTCUSB48IO.h"
//include libTCUSB48IO header for C++ project

typedef  unsigned long int DWORD ;     //4 octet
typedef  unsigned char BYTE ;          //1 octet

int Menu(void);
void ResfreshData(void);
int OPEN_WITH_DWORD(void);
int OPEN_WITH_CHAR(void);

////////////////////////////////////////////////////////////////////////////
// Menu Function

int Menu(void)
{
int choix;

printf("\n");
printf("**************** Menu ****************\n");
printf("1- Read the selected Input           *\n");
printf("2- Control the selected Output       *\n");
printf("3- Read Input Counters               *\n");
printf("4- Close Module                      *\n");
printf("**************************************\n");

printf("Choix = ");
scanf ("%d",&choix); 
printf("\n");
if ((choix>=1) && (choix<= 4)){
switch (choix) {
              case 1 : choix = 1; break;
              case 2 : choix = 2; break;
	      case 3 : choix = 3; break;
	      case 4 : choix = 4; break;
              default : return 1;
              }
}
else choix = 0;

return choix;
}


////////////////////////////////////////////////////////////////////////////
// Main

int main(int argc, char * arg[])
{

int choix;
int error=0;

int Entree;
int Sortie;
int Etat;
int In;			// Input Status
unsigned char Counter;	// Counter Value
unsigned char dll_version;	// Counter Value

error=OPEN_WITH_DWORD();
//ou
//error=OPEN_WITH_CHAR();
		
		
if (error==0)		
{
	TCUSB48IO_Version(1,&dll_version);
	printf("\nVersion  lib TCUSB48IO :%d",dll_version);		
}
		
choix=1;
if (error==0)		
{
while ((choix != 0) && (choix <=4)){

	choix=Menu();
	
	if (choix == 1){
		error=TCUSB48IO_Refresh(1);						// Refresh Datas
		if (error > 0) printf(" !!! Error Refresh: %d !!!\n",error);
		else{
			printf("------ Read the selected Input -----\n");
			printf("  Enter the Input Number : "); 
			scanf ("%d",&Entree);
			if ((Entree>=1) && (Entree<=32)){
				In=TCUSB48IO_BitInput(1,Entree);
				printf("  Input %d => State = %d\n",Entree,In);
			}
		}
	}
	
	if (choix == 2){
		printf("--- Control the selected Output ---\n");
		printf("  Enter the Output Number : "); 
		scanf ("%d",&Sortie);
		printf("  Enter the State : "); 
		scanf ("%d",&Etat);
		if ((Sortie>=1) && (Sortie<=16)){
			if ((Etat==0) || (Etat==1)) TCUSB48IO_BitOutput(1,Sortie,Etat);
		}
		error=TCUSB48IO_Refresh(1);						// Refresh Datas
		if (error > 0) printf(" !!! Error Refresh: %d !!!\n",error);
	}
	
	if (choix == 3){
		error=TCUSB48IO_Refresh(1);						// Refresh Datas
		if (error > 0) printf(" !!! Error Refresh: %d !!!\n",error);
		else{
			printf("------ Read Input Counters  ------\n");
			printf("  Enter the Input Number : "); 
			scanf ("%d",&Entree);
			if ((Entree>=1) && (Entree<=32)){
				TCUSB48IO_Counter(1,Entree-1,&Counter);
				printf("  Counter %d => Value = %d\n",Entree,Counter);
			}
		}
	}
	
	if (choix == 4){
		error=TCUSB48IO_Close(1);
		if (error > 0) printf("- !!! Error Close: %d\n",error);
		else{
			printf("--------*** Module Closed ***---------\n");
			printf("**************************************\n");
		return 1;
		}
	}
}
}


}


int OPEN_WITH_DWORD(void)
{	
	int error=0;
	DWORD numNB=0;	
	printf("Enter Module Serial Number : "); 
	scanf ("%lu",&numNB);	// Get Serial Number
	printf("\n");
	
	error=TCUSB48IO_OpenN(1,&numNB);
	if (error == 0){
		printf("**************************************\n");
		printf("****** Module '%lu' Opened ******\n",numNB);
		error=TCUSB48IO_Refresh(1);// First Refresh
		if (error > 0) printf(" !!! Error Refresh: %d !!!\n",error);
	}
	else printf(" !!! Opening Error: %d !!!\n",error);
return error;
}

int OPEN_WITH_CHAR(void)
{
	int error=0;
	char num[8],*tst;	
	
	printf("**************************************\n");
	printf("************** TCUSB48IO *************\n");
	printf("\n");

	TCUSB48IO_DeviceList();

	printf("Enter Module Serial Number : "); 
	scanf ("%s",num);	// Get Serial Number
	tst=num;
	
	error=TCUSB48IO_Open(1,&tst);
	if (error == 0)
	{
		printf("**************************************\n");
		printf("****** Module '%s' Opened ******\n",num);
	
		error=TCUSB48IO_Refresh(1);// First Refresh
		if (error > 0) printf(" !!! Error Refresh: %d !!!\n",error);
	}
	else printf(" !!! Opening Error: %d !!!\n",error);
			
return error;
}


