ovasCDriverGenericOscillator.cpp 6.23 KB
Newer Older
1 2
#include "ovasCDriverGenericOscillator.h"
#include "ovasCConfigurationDriverGenericOscillator.h"
Jozef Legeny's avatar
Jozef Legeny committed
3

Jozef Legeny's avatar
Jozef Legeny committed
4
#include <toolkit/ovtk_all.h>
5
#include <openvibe/ovITimeArithmetics.h>
Jozef Legeny's avatar
Jozef Legeny committed
6

7
#include <system/ovCTime.h>
Jozef Legeny's avatar
Jozef Legeny committed
8 9 10 11 12 13 14 15 16 17 18 19

#include <cmath>

using namespace OpenViBEAcquisitionServer;
using namespace OpenViBE;
using namespace OpenViBE::Kernel;

//___________________________________________________________________//
//                                                                   //

CDriverGenericOscillator::CDriverGenericOscillator(IDriverContext& rDriverContext)
	:IDriver(rDriverContext)
20
	,m_oSettings("AcquisitionServer_Driver_GenericOscillator", m_rDriverContext.getConfigurationManager())
Jozef Legeny's avatar
Jozef Legeny committed
21 22
	,m_pCallback(NULL)
	,m_ui32SampleCountPerSentBlock(0)
23
	,m_ui64TotalSampleCount(0)
24
	,m_ui64StartTime(0)
25
	,m_bSendPeriodicStimulations(false)
Jozef Legeny's avatar
Jozef Legeny committed
26 27 28 29 30
{
	m_rDriverContext.getLogManager() << LogLevel_Trace << "CDriverGenericOscillator::CDriverGenericOscillator\n";

	m_oHeader.setSamplingFrequency(512);
	m_oHeader.setChannelCount(4);
31 32 33 34

	m_oSettings.add("Header", &m_oHeader);
	m_oSettings.add("SendPeriodicStimulations", &m_bSendPeriodicStimulations);
	m_oSettings.load();
Jozef Legeny's avatar
Jozef Legeny committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
}

void CDriverGenericOscillator::release(void)
{
	m_rDriverContext.getLogManager() << LogLevel_Trace << "CDriverGenericOscillator::release\n";

	delete this;
}

const char* CDriverGenericOscillator::getName(void)
{
	m_rDriverContext.getLogManager() << LogLevel_Trace << "CDriverGenericOscillator::getName\n";

	return "Generic Oscillator";
}

//___________________________________________________________________//
//                                                                   //

boolean CDriverGenericOscillator::initialize(
	const uint32 ui32SampleCountPerSentBlock,
	IDriverCallback& rCallback)
{
	m_rDriverContext.getLogManager() << LogLevel_Trace << "CDriverGenericOscillator::initialize\n";

	if(m_rDriverContext.isConnected()) { return false; }

	if(!m_oHeader.isChannelCountSet()
	 ||!m_oHeader.isSamplingFrequencySet())
	{
		return false;
	}

68
	for (uint32 i = 0; i < m_oHeader.getChannelCount(); i++)
69 70
	{
		m_oHeader.setChannelUnits(i, OVTK_UNIT_Volts, OVTK_FACTOR_Base);
71 72 73 74 75
		if (CString(m_oHeader.getChannelName(i)) == CString(""))
		{
			std::stringstream ss; ss << "Oscillator " << (i + 1);
			m_oHeader.setChannelName(i, ss.str().c_str());
		}
76 77
	}

78
	m_vSample.resize(m_oHeader.getChannelCount()*ui32SampleCountPerSentBlock);
Jozef Legeny's avatar
Jozef Legeny committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92

	m_pCallback=&rCallback;
	m_ui32SampleCountPerSentBlock=ui32SampleCountPerSentBlock;

	return true;
}

boolean CDriverGenericOscillator::start(void)
{
	m_rDriverContext.getLogManager() << LogLevel_Trace << "CDriverGenericOscillator::start\n";

	if(!m_rDriverContext.isConnected()) { return false; }
	if(m_rDriverContext.isStarted()) { return false; }

93
	m_ui64TotalSampleCount=0;
94
	m_ui64StartTime = System::Time::zgetTime();
Jozef Legeny's avatar
Jozef Legeny committed
95 96 97 98 99 100 101 102 103 104

	return true;
}

boolean CDriverGenericOscillator::loop(void)
{
	m_rDriverContext.getLogManager() << LogLevel_Debug << "CDriverGenericOscillator::loop\n";

	if(!m_rDriverContext.isConnected()) { return false; }

105
	if (m_rDriverContext.isStarted())
Jozef Legeny's avatar
Jozef Legeny committed
106
	{
107 108 109
		// Generate the contents we want to send next
		CStimulationSet l_oStimulationSet;
		if (m_bSendPeriodicStimulations)
110
		{
111 112 113 114 115
			l_oStimulationSet.setStimulationCount(1);
			l_oStimulationSet.setStimulationIdentifier(0, 0);
			l_oStimulationSet.setStimulationDate(0, 0);
			l_oStimulationSet.setStimulationDuration(0, 0);
		}
116

117
		const uint64 l_ui64Elapsed = System::Time::zgetTime() - m_ui64StartTime;
118 119
		const uint64 l_ui64SamplesNeededSoFar = ITimeArithmetics::timeToSampleCount(m_oHeader.getSamplingFrequency(), l_ui64Elapsed);
		if (l_ui64SamplesNeededSoFar <= m_ui64TotalSampleCount)
120
		{
121 122
			// Too early
			return true;
123
		}
124 125
		const uint32 l_ui32RemainingSamples = static_cast<uint32>(l_ui64SamplesNeededSoFar - m_ui64TotalSampleCount);
		if (l_ui32RemainingSamples * m_oHeader.getChannelCount() > m_vSample.size())
126
		{
127
			m_vSample.resize(l_ui32RemainingSamples * m_oHeader.getChannelCount());
128
		}
129

130 131 132 133 134 135 136 137 138 139 140 141 142
		// std::cout << "At " << ITimeArithmetics::timeToSeconds(l_ui64Elapsed) * 1000 << "ms, remaining " << l_ui32RemainingSamples << " samples\n";
		for (uint32 i = 0; i<l_ui32RemainingSamples; i++)
		{
			for (uint32 j = 0; j<m_oHeader.getChannelCount(); j++)
			{
				const float64 l_f64Value=
					::sin((m_ui64TotalSampleCount*(j+1)*12.3)/m_oHeader.getSamplingFrequency())+
					::sin((m_ui64TotalSampleCount*(j+1)* 4.5)/m_oHeader.getSamplingFrequency())+
					::sin((m_ui64TotalSampleCount*(j+1)*67.8)/m_oHeader.getSamplingFrequency());
				m_vSample[j*l_ui32RemainingSamples + i] = (float32)l_f64Value;
			}
			m_ui64TotalSampleCount++;
		}
143

144
		m_pCallback->setSamples(&m_vSample[0], l_ui32RemainingSamples);
145 146
		m_pCallback->setStimulationSet(l_oStimulationSet);
		m_rDriverContext.correctDriftSampleCount(m_rDriverContext.getSuggestedDriftCorrectionSampleCount());
147

Jozef Legeny's avatar
Jozef Legeny committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	}
	else
	{
		if(m_rDriverContext.isImpedanceCheckRequested())
		{
			for(uint32 j=0; j<m_oHeader.getChannelCount(); j++)
			{
				m_rDriverContext.updateImpedance(j, 1);
			}
		}
	}

	return true;
}

boolean CDriverGenericOscillator::stop(void)
{
	m_rDriverContext.getLogManager() << LogLevel_Trace << "CDriverGenericOscillator::stop\n";

	if(!m_rDriverContext.isConnected()) { return false; }
	if(!m_rDriverContext.isStarted()) { return false; }
	return true;
}

boolean CDriverGenericOscillator::uninitialize(void)
{
	m_rDriverContext.getLogManager() << LogLevel_Trace << "CDriverGenericOscillator::uninitialize\n";

	if(!m_rDriverContext.isConnected()) { return false; }
	if(m_rDriverContext.isStarted()) { return false; }

	m_pCallback=NULL;

	return true;
}

//___________________________________________________________________//
//                                                                   //

boolean CDriverGenericOscillator::isConfigurable(void)
{
	m_rDriverContext.getLogManager() << LogLevel_Trace << "CDriverGenericOscillator::isConfigurable\n";

	return true;
}

boolean CDriverGenericOscillator::configure(void)
{
	m_rDriverContext.getLogManager() << LogLevel_Trace << "CDriverGenericOscillator::configure\n";

198
	CConfigurationDriverGenericOscillator m_oConfiguration(m_rDriverContext, OpenViBE::Directories::getDataDir() + "/applications/acquisition-server/interface-Generic-Oscillator.ui", 
199
		m_bSendPeriodicStimulations);
200 201 202

	if(m_oConfiguration.configure(m_oHeader)) 
	{
203
		m_oSettings.save();
204 205 206 207
		return true;
	}

	return false;
Jozef Legeny's avatar
Jozef Legeny committed
208
}