/***************************************************************************************************************:')

ESCfg.h

ES distance sensor handling.

Fabrice Le Bars

Created: 2009-03-28

Version status: Not finished

***************************************************************************************************************:)*/

#ifndef ESCFG_H
#define ESCFG_H

#include "ESCore.h"

/*
Debug macros specific to ESCfg.
*/
#ifdef _DEBUG_MESSAGES_ESUTILS
#	define _DEBUG_MESSAGES_ESCFG
#endif // _DEBUG_MESSAGES_ESUTILS

#ifdef _DEBUG_WARNINGS_ESUTILS
#	define _DEBUG_WARNINGS_ESCFG
#endif // _DEBUG_WARNINGS_ESUTILS

#ifdef _DEBUG_ERRORS_ESUTILS
#	define _DEBUG_ERRORS_ESCFG
#endif // _DEBUG_ERRORS_ESUTILS

#ifdef _DEBUG_MESSAGES_ESCFG
#	define PRINT_DEBUG_MESSAGE_ESCFG(params) PRINT_DEBUG_MESSAGE(params)
#else
#	define PRINT_DEBUG_MESSAGE_ESCFG(params)
#endif // _DEBUG_MESSAGES_ESCFG

#ifdef _DEBUG_WARNINGS_ESCFG
#	define PRINT_DEBUG_WARNING_ESCFG(params) PRINT_DEBUG_WARNING(params)
#else
#	define PRINT_DEBUG_WARNING_ESCFG(params)
#endif // _DEBUG_WARNINGS_ESCFG

#ifdef _DEBUG_ERRORS_ESCFG
#	define PRINT_DEBUG_ERROR_ESCFG(params) PRINT_DEBUG_ERROR(params)
#else
#	define PRINT_DEBUG_ERROR_ESCFG(params)
#endif // _DEBUG_ERRORS_ESCFG

typedef HANDLE HES;

#define INVALID_HES_VALUE INVALID_HANDLE_VALUE

/*
Open a ES.

HES* phES : (INOUT) Valid pointer that will receive an identifier of the
ES opened.
char* szDevice : (IN) ES serial port to open.

Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int OpenES(HES* phES, char* szDevice)
{
	if (OpenRS232Port(phES, szDevice) != EXIT_SUCCESS)
	{
		PRINT_DEBUG_ERROR_ESCFG(("OpenES error (%s) : %s"
								   "(szDevice=%s)\n", 
								   strtime_m(), 
								   "Cannot open the serial port. ", 
								   szDevice));
		return EXIT_FAILURE;
	}

	if (SetOptionsRS232Port(*phES, 9600, NOPARITY, FALSE, 8, ONESTOPBIT, 500) != EXIT_SUCCESS)
	{
		PRINT_DEBUG_ERROR_ESCFG(("OpenES error (%s) : %s"
								   "(szDevice=%s)\n", 
								   strtime_m(), 
								   "Cannot set the options of the serial port. ", 
								   szDevice));
		CloseRS232Port(phES);
		return EXIT_FAILURE;
	}

	if (PurgeRS232Port(*phES) != EXIT_SUCCESS)
	{
		PRINT_DEBUG_ERROR_ESCFG(("OpenES error (%s) : %s"
								   "(szDevice=%s)\n", 
								   strtime_m(), 
								   "Cannot purge the serial port. ", 
								   szDevice));
		CloseRS232Port(phES);
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}

/*
Get the distance from a ES.

HES hES : (IN) Identifier of the ES.
double* pDistance : (INOUT) Valid pointer receiving the distance (in m).

Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int GetDistanceES(HES hES, double* pDistance)
{

	if (ReadChannelES(hES, pDistance) != EXIT_SUCCESS)
	{
		PRINT_DEBUG_ERROR_ESCFG(("GetDistanceES error (%s) : %s"
								   "(hES=%#x)\n", 
								   strtime_m(), 
								   "Error getting data from the device. ", 
								   hES));
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}

/*
Close a ES.

HES* phES : (INOUT) Valid pointer to the identifier of the ES.

Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int CloseES(HES* phES)
{
	if (CloseRS232Port(phES) != EXIT_SUCCESS)
	{
		PRINT_DEBUG_ERROR_ESCFG(("CloseES error (%s) : %s"
								   "(*phES=%#x)\n", 
								   strtime_m(), 
								   "Error closing the serial port. ", 
								   *phES));
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}

#endif // ESCFG_H
