#include <stdio.h>
#include <sys/ioctl.h>
#include <assert.h>

#include <apci3600.h>

#include "apci3600-def.h"
#include "apci3600msg.h"

#define FAILURE() assert(0)

//--------------------------------------------------------------------------------
int apci3600_QueryAndPrintAnalogInputCyclicAcquisitionBufferSize(struct apci3600_card_t * pboard)
{
	unsigned long size = 0; 
	int ret = ioctl( pboard->base ,CMD_APCI3600_QueryAnalogInputCyclicAcquisitionBufferSize, &size) ;
	STATUS_MSG( ret, QueryAnalogInputCyclicAcquisitionBufferSize);
	printf("driver buffer size = %lu\n",size);
	return ret;
}


int i_Display_Board_Initialisation (struct apci3600_card_t * pboard)
{
	str_GetBoardInitialisation_t board_init;
	int ret = ioctl(pboard->base, CMD_APCI3600_GetBoardInitialisation, &board_init);

	STATUS_MSG(ret,GetBoardInitialisation);

	if(ret)
		return -1;

	printf ("\ni_PCI3600_GetBoardInitialisation OK");
	printf("\nBOARD INITIALISATION :");
	printf("\n+---------------------------------------------------------------------------+\n");
  
	switch(board_init.b_MasterSlaveMode)
	{
		case 0:
			printf("\nThe Board is initialised in MASTER mode\n");
			break;
		case 1:
			printf("\nThe Board is initialised in SLAVE mode\n");
			break;
		default:
			FAILURE();
	}

	printf("Divisor clock generator 1 :%hu -> %lfMHz\n",board_init.b_ClockGen1_Divisor,(double)(102.4 / (double)board_init.b_ClockGen1_Divisor));
	printf("Divisor clock generator 2 :%hu -> %lfMHz\n",board_init.b_ClockGen2_Divisor,(double)(102.4 / (double)board_init.b_ClockGen2_Divisor));
   
	/*Sampling Clock 1*/
	printf("\nSampling Clock 1 Selection :\n");
  
	switch (board_init.b_SamplingClock1_Selection)
	{
		case 0:
			printf(" 0:Clock Generator 1 : %lf MHz\n",(double)(102.4 / (double)board_init.b_ClockGen1_Divisor));
		 	break;
		case 1:
			printf(" 1:Clock Generator 2 : %lf MHz\n",(double)(102.4 / (double)board_init.b_ClockGen2_Divisor));
		 	break;
		case 2:
			printf(" 2:Chronometer 0 Latch input\n");
			break;
  		case 3:
			printf(" 3:Chronometer 1 Latch input\n");
			break;
		default:
			FAILURE();
	}
	/*Sampling Clock 2*/
	printf("\nSampling Clock 2 Selection :\n");

	switch (board_init.b_SamplingClock2_Selection)
	{
		case 0:
			printf(" 0:Clock Generator 1 : %lf MHz\n",(double)(102.4 / (double)board_init.b_ClockGen1_Divisor));
			break;
		case 1:
			printf(" 1:Clock Generator 2 : %lf MHz\n",(double)(102.4 / (double)board_init.b_ClockGen2_Divisor));
			break;
		case 2:
			printf(" 2:Chronometer 0 Latch input\n");
			break;
		case 3:
			printf(" 3:Chronometer 1 Latch input\n");
			break;
		default:
			FAILURE();
	}
	/* Test if Slave mode */
	switch(board_init.b_MasterSlaveMode)
	{
		case 0:
			break;
		case 1: /* slave mode */
			printf("\nSampling Clock 1 use for the slave board :");
 			{
			/*Sampling clock 1 for slave board*/
				switch (board_init.b_SlaveSamplingClock1_Selection)
				{
					case 0:
						printf(" 0: Ext Clock 1\n");
						break;
					case 1:
						printf(" 1: Ext Clock 2\n");
						break;
					default:
						FAILURE();				
				}
			}
			/*Sampling clock 2 for slave board*/
			printf("\nSampling Clock 2 use for the slave board :");
			 
			switch (board_init.b_SlaveSamplingClock2_Selection)
			{
				case 0:
					printf(" 0: Ext Clock 1\n");
					break;
				case 1:
					printf(" 1: Ext Clock 2\n");
					break;
				default:
					FAILURE();
			}

			break;
		default:
			FAILURE();
	}
	
	/*Chronometer master clock*/
	printf("\nChonometer master clock divisor selection :");
	
	switch (board_init.b_Chronometer_MasterClock_Divisor)
	{
		case 0:
			printf(" 0: Divisor = 1-> 102,4 MHz\n");
			break;
		case 1:
			printf(" 1: Divisor = 10-> 10,24 MHz\n");
			break;
		case 2:
			printf(" 2: Divisor = 100-> 1,024 MHz\n");
			break;
		default:
			FAILURE();
	 }
	apci3600_QueryAndPrintAnalogInputCyclicAcquisitionBufferSize(pboard);
	printf("\n+---------------------------------------------------------------------------+\n");

	return 0;
}
