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

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "libtcusb16io.h"
// include libTCUSB16IO 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);


void ResfreshData(void)
{
int error=0,OutputBitValue=0,N=0,InputBitValue;
long  pos=0;
DWORD InputValue=0;
BYTE CTR=0;
error=TCUSB16IO_Refresh(1);

if (error==0)
{
	printf   ("**************************************\n");
	printf   ("****   Input State                ****\n");
	printf   ("**************************************\n");
 	error=TCUSB16IO_Input(1, &InputValue);
	printf ("%lx\n",InputValue);
	for (N=1;N<12;N++)
	{
	InputBitValue=TCUSB16IO_BitInput(1,N);                /* Lecture d'une entr� du module  */
	if (N==7) printf("\n");
	printf("E%02d:%d ", N,InputBitValue);
	}
	printf("\n");
	printf   ("**************************************\n");
	printf   ("****   Counter Value              ****\n");
	printf   ("**************************************\n");
	for (N=1;N<7;N++)
	{
	error=TCUSB16IO_Counter(1,N,&CTR);     /* Lecture du compteur de l'entr� du module indiqu�*/
	printf("CT%d:%d ", N,CTR);
	}
	printf("\n");
	printf   ("**************************************\n");
	printf   ("****   Output State               ****\n");
	printf   ("**************************************\n");
	for (N=1;N<7;N++)
	{
	OutputBitValue=TCUSB16IO_BitReadBack(1,N);             /* Relecture d'une sortie (1 �6) du module */
	printf("O%d:%d ", N,OutputBitValue);
	}
	printf("\n");
	printf   ("**************************************\n");
	printf   ("****   Encoder Value              ****\n");
	printf   ("**************************************\n");	
	error=TCUSB16IO_Pos(1,&pos); 
	printf   ("POS=%ld",pos);
	printf("\n"); 

}
else
{
	printf(" !!! Error Refresh: %d !!!\n",error);
}



}


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

int Menu(void)
{
int choix;

printf("\n");
printf("**************** Menu ****************\n");
printf("1- activate output 	             *\n");
printf("2- reset encoder   	   	     *\n");
printf("3- Close Module                      *\n");
printf("**************************************\n");

printf("Choix = ");
scanf ("%d",&choix); 

if ((choix>=1) && (choix<= 3)){
switch (choix) {
              case 1 : choix = 1; break;
              case 2 : choix = 2; break;
	      case 3 : choix = 3; 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;

BYTE dllV=0;
BYTE firmV=0;


TCUSB16IO_DeviceList();
///////////////////////////////////
/////////////OPEN FUNCTION/////////
// error=OPEN_WITH_CHAR();
 // OR
 error=OPEN_WITH_DWORD();
///////////////////////////////////
/////////////////////////////////// 
 
 
if (error == 0){
	
	printf("**************************************\n");
	printf("************** TCUSB16IO *************\n");
	printf("**************************************\n");
	printf("\n");
	printf("**************************************\n");
	error=TCUSB16IO_Refresh(1);					// First Refresh
	error=TCUSB16IO_Version(1,&firmV,&dllV);
	printf("  VERSION DLL: %d VERSION FIRMWARE:%d\n",dllV,firmV);
	printf("**************************************\n");
	
	if (error > 0) printf(" !!! Error Refresh: %d !!!\n",error);

choix=1;

while ((choix != 0) && (choix <=3)){

	choix=Menu();
	
	if (choix == 1){
		printf("--- Control the selected Output ---\n");
		printf("  Enter the Output Number : "); 
		scanf ("%d",&Sortie);
		printf("  Enter the State : 0/1 "); 
		scanf ("%d",&Etat);
		if ((Sortie>=1) && (Sortie<=6))
			{
			if ((Etat==0) || (Etat==1)) TCUSB16IO_BitOutput(1,Sortie,Etat);
			TCUSB16IO_Refresh(1);
			}
		ResfreshData();
		}
		

	
	
	if (choix == 2){
		error=TCUSB16IO_ResetPos(1);
		TCUSB16IO_Refresh(1);
		ResfreshData();
		
	}


	if (choix == 3){
		error=TCUSB16IO_Close(1);
		if (error > 0) {printf("- !!! Error Close: %d\n",error);return 1;}
		else{
			printf("--------*** Module Closed ***---------\n");
			printf("**************************************\n");
		return 1;
		}
	}
}
}
else printf(" !!! Closing prog, Opening Error: %d !!!\n",error);



}


int OPEN_WITH_DWORD(void)
{
DWORD numserieNB=0;
int error=0; 
///////////////////WITH DWORD IN PARAMETER///////////////
printf("Enter Module Serial Number : "); 
scanf ("%lu",&numserieNB);	
//printf("\n value entered %lu\n\n",numserieNB);	// Get Serial Number
//numserieNB=16001046;
error=TCUSB16IO_OpenN(1,&numserieNB);
return error;
////////////////////////////////////////////////////////
}


int OPEN_WITH_CHAR(void)
{
int error=0;
char num[10],*tst;
memset(num,0,sizeof(num));
//////////////////WITH CHAR * IN PARAMETER///////////////
printf("Enter Module Serial Number : "); 
scanf ("%s",num);
tst=num;

//tst="16001046";

error=TCUSB16IO_Open(1,&tst);
return error;
////////////////////////////////////////////////////////
}


