// AVDS2MATLAB.cpp: implementation of the CAVDS2MATLAB class.
//
//////////////////////////////////////////////////////////////////////
 /*      
 *          Copyright (C) 2000 RasSimTech Ltd. 
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version. 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details. 
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 */

#include "AVDS2MATLAB.h"

extern "C"
{
	int iInitTransfer(int iID,int iSize);	// returns index of new instance or -1 if error occurs
	void DeleteTransferAll();
	void TransferSetData(int iInstance,int iIndex,double dValue);
	double dTransferGetData(int iInstance,int iIndex);
	void TransferReadData(int iInstance);
	void TransferWriteData(int iInstance);
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAVDS2MATLAB::CAVDS2MATLAB()
{
	m_bConnectionOpen = FALSE;
	m_iIndexToAVDS = -1;
	m_iIndexToMATLAB = -1;
	m_iNumberToAVDS = -1;
	m_iNumberToMATLAB = -1;
	m_bUseRealTimeFlag = FALSE;

}

CAVDS2MATLAB::~CAVDS2MATLAB()
{

}


BOOL CAVDS2MATLAB::bOpenConnection(int iToAVDSID,int iNumberToAVDS,int iToMATLABID,int iNumberToMATLAB)
{
	m_iNumberToAVDS = iNumberToAVDS;
	m_iNumberToMATLAB = iNumberToMATLAB;
	DeleteTransferAll();	/* clear old version of transfer arrays, if they exist */
	m_iIndexToAVDS = iInitTransfer(iToAVDSID,iNumberToAVDS);
	m_iIndexToMATLAB = iInitTransfer(iToMATLABID,iNumberToMATLAB);
	if((m_iIndexToAVDS==-1)||(m_iIndexToMATLAB==-1))
	{
		m_bConnectionOpen = FALSE;
	}
	else
	{
		m_bConnectionOpen = TRUE;
	}
	return (m_bConnectionOpen);
}

BOOL CAVDS2MATLAB::bSendData(double const * const pdSendData)
{
	if(m_bConnectionOpen)
	{
		for(int iIndex = 0;iIndex < m_iNumberToAVDS;iIndex++)
		{
			TransferSetData(m_iIndexToAVDS,iIndex,pdSendData[iIndex]);
		}
		TransferWriteData(m_iIndexToAVDS);
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

BOOL CAVDS2MATLAB::bReceiveData(double* pdReceiveData)
{
	if(m_bConnectionOpen)
	{
		TransferReadData(m_iIndexToMATLAB);
		for(int iIndex = 0;iIndex < m_iNumberToMATLAB;iIndex++)
		{
			pdReceiveData[iIndex] = dTransferGetData(m_iIndexToMATLAB,iIndex);
		}
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}


void CAVDS2MATLAB::CloseConnection()
{
	if(m_bConnectionOpen)
	{
		for(int iIndex = 0;iIndex < m_iNumberToAVDS;iIndex++)
		{
			TransferSetData(m_iIndexToAVDS,iIndex,0.0);
		}
		TransferWriteData(m_iIndexToAVDS);
	}
	DeleteTransferAll();	/* clear old version of transfer arrays, if they exist */
}
