/*
	Release : libTCUSB18A 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/18A
		- Control the selected Output
		- Control the selected Analog Output
		- Read Inputs
		- Read Input Counters

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "libtcusb18a.h"
// include libTCUSB18A 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,N=0;
double fData=0.0;
long int pos=0;
DWORD InputValue=0;
BYTE CTR=0,InputBitValue=0,OutputBitValue=0;
error=TCUSB18A_Refresh(1);

if (error==0)
{
	printf   ("**************************************\n");
	printf   ("****   Input State                ****\n");
	printf   ("**************************************\n");
 	error=TCUSB18A_Input(1, &InputValue);
	printf ("%lx\n",InputValue);
	for (N=1;N<9;N++)
	{
	error=TCUSB18A_BitInput(1,N,&InputBitValue);                /* Lecture d'une entr� du module  */
	if (N==5) printf("\n");
	printf("E%02d:%d ", N,InputBitValue);
	}
	printf("\n");
	printf   ("**************************************\n");
	printf   ("****   Counter Value              ****\n");
	printf   ("**************************************\n");
	for (N=1;N<9;N++)
	{
	if (N==5) printf("\n");
	error=TCUSB18A_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<5;N++)
	{
	error=TCUSB18A_BitReadBack(1,N,&OutputBitValue);             /* Relecture d'une sortie (1 �4) du module */
	printf("O%d:%d ", N,OutputBitValue);
	}
	printf("\n");
	printf   ("**************************************\n");
	printf   ("****   Analog Values              ****\n");
	printf   ("**************************************\n");	
	TCUSB18A_AnaInVolts (1,1,&fData);
	printf   ("CH1 (V)=%2.2f",fData);
	TCUSB18A_AnaInVolts (1,2,&fData);
	printf   ("   CH2 (V)=%2.2f",fData);
	TCUSB18A_AnaInVolts (1,3,&fData);
	printf   ("   CH3 (V)=%2.2f",fData);
	TCUSB18A_AnaInVolts (1,4,&fData);
	printf   ("   CH4 (V)=%2.2f",fData);
	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- Set Analog ouput   	   	     *\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;
float fEtat=0.0;

BYTE dllV=0;
BYTE firmV=0;


TCUSB18A_DeviceList();
///////////////////////////////////
/////////////OPEN FUNCTION/////////
// error=OPEN_WITH_CHAR();
 // OR
 error=OPEN_WITH_DWORD();
///////////////////////////////////
/////////////////////////////////// 
 
 
if (error == 0){
	
	printf("**************************************\n");
	printf("************** TCUSB18A *************\n");
	printf("**************************************\n");
	printf("\n");
	printf("**************************************\n");
	error=TCUSB18A_Refresh(1);					// First Refresh
	error=TCUSB18A_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<=4))
			{
			if ((Etat==0) || (Etat==1)) TCUSB18A_BitOutput(1,Sortie,Etat);
			TCUSB18A_Refresh(1);
			}
		ResfreshData();
		}
		

	
	
	if (choix == 2){
		printf("--- Control the selected Analog Output ---\n");
		printf("  Enter the Analog Output Number 1/2 : "); 
		scanf ("%d",&Sortie);
		printf("  Enter the State : -10.0/+10.0 "); 
		scanf ("%f",&fEtat);
		if ((Sortie>=1) && (Sortie<=2))
			{
			if ((fEtat<=10.0) || (fEtat>=-10.0)) TCUSB18A_AnaOutVolts(1,Sortie,fEtat);
			TCUSB18A_Refresh(1);
			}
		ResfreshData();
		
	}


	if (choix == 3){
		error=TCUSB18A_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=TCUSB18A_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=TCUSB18A_Open(1,&tst);
return error;
////////////////////////////////////////////////////////
}


