/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
    Description:
	Classe d'interface avec la carte d'I/O PCI1756.

*/

/* ************************************************************************** */
/* Revision:
### 20121219 JFM
    Verision d'origine.

### YYYYMMDD Description du besoin ou du bug
    Description du changement.
 */

/* ************************************************************************** */

#include "USB4704Interface.h"
#include "USB4704Definitions.h"
#include <QString>
#include <QDebug>

CUSB4704Interface::CUSB4704Interface():
    mOpened(false)
{
	mInputBuf = 0;
}

CUSB4704Interface::~CUSB4704Interface()
{
	mInputCtrl->Dispose();
}


unsigned int CUSB4704Interface::OpenInterface()
{

    if(mOpened)
		return RET_ERROR;



	CEngLog::instance()->AddLogString(QString("Ouverture du module USB externe :"),3);

	mInputCtrl = AdxInstantAiCtrlCreate();
	ICollection<DeviceTreeNode> *supportedDevices = mInputCtrl->getSupportedDevices();
	if(supportedDevices->getCount() == 0)
	{
		CEngLog::instance()->AddLogString("L'ouverture du module USB a échouée (Aucun module n'est installé)",1);
		return RET_ERROR;
	}

	bool found = false;
	for (int i = 0; i < supportedDevices->getCount(); i++)
	{
		DeviceTreeNode const &node = supportedDevices->getItem(i);
		QString DeviceDescription(QString::fromWCharArray(node.Description));
		qDebug() << "Device Nbr: " << node.DeviceNumber << " : "<< DeviceDescription;
		if(QString::fromWCharArray(node.Description).contains(USB4704_DAQNAVI_NAME))
		{
			if(mInputCtrl->setSelectedDevice(DeviceInformation(node.Description)) != Success)
			{
				CEngLog::instance()->AddLogString("L'ouverture du module USB externe a échouée (Module occupé)",1);
				supportedDevices->Dispose();
				mInputCtrl->Dispose();
				return RET_ERROR;
			}
			CEngLog::instance()->AddLogString(QString("Module USB externe initialisé :") + QString::fromWCharArray(node.Description),3);

			found = true;
			break;
		}
	}
	if(found == false)
	{
		CEngLog::instance()->AddLogString(QString("").sprintf("Erreur. Incohérence du type de module USB externe"),1);
		return RET_ERROR;
	}

	supportedDevices->Dispose();
	CEngLog::instance()->AddLogString("Module USB externe ouvert avec succès",3);
	mOpened = true;
	return RET_OK;

}

unsigned int CUSB4704Interface::GetAnalogInput(int Channel,int *Data)
{
	if(mOpened == false)
	{
		*Data = 0;
		return RET_ERROR;
	}

	int Buf = 0;

	ErrorCode errorCode = Success;
	mMutex.lock();
	errorCode = mInputCtrl->Read(Channel,Buf);
	mMutex.unlock();
	if(errorCode != Success)
	{
		qDebug("USB read Error");
		*Data = 0;
		return RET_ERROR;
	}

	*Data = Buf;

	return RET_OK;
}

unsigned int CUSB4704Interface::GetAnalogInput(int Channel, double &Data)
{
	if(mOpened == false)
	{
		Data = 0.0;
		return RET_ERROR;
	}

	double Buf = 0;

	ErrorCode errorCode = Success;
	mMutex.lock();
	errorCode = mInputCtrl->Read(Channel,Buf);
	mMutex.unlock();
	if(errorCode != Success)
	{
		qDebug("USB read Error");
		Data = 0.;
		return RET_ERROR;
	}

	Data = Buf;

	return RET_OK;
}

