ovasCDriverGenericSawTooth.cpp 4.7 KB
Newer Older
1 2 3
#include "ovasCDriverGenericSawTooth.h"
#include "../ovasCConfigurationBuilder.h"

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

7
#include <system/ovCTime.h>
8 9 10 11 12 13 14 15 16 17 18 19 20

#include <cmath>

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

//___________________________________________________________________//
//                                                                   //

CDriverGenericSawTooth::CDriverGenericSawTooth(IDriverContext& rDriverContext)
	:IDriver(rDriverContext)
	,m_pCallback(NULL)
21 22 23
	,m_ui32ExternalBlockSize(0)
	,m_vSample(NULL)
	,m_ui64TotalSampleCount(0)
24
	,m_ui64StartTime(0)
25 26 27 28 29
{
	m_rDriverContext.getLogManager() << LogLevel_Trace << "CDriverGenericSawTooth::CDriverGenericSawTooth\n";

	m_oHeader.setSamplingFrequency(512);
	m_oHeader.setChannelCount(1);
30 31
	m_oHeader.setChannelName(0, "Sawtooth");
	m_oHeader.setChannelUnits(0, OVTK_UNIT_Volts, OVTK_FACTOR_Base);
32 33 34 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
}

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

	delete this;
}

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

	return "Generic Saw Tooth";
}

//___________________________________________________________________//
//                                                                   //

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

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

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

65 66

	m_vSample.resize(m_oHeader.getChannelCount()*ui32SampleCountPerSentBlock);
67 68

	m_pCallback=&rCallback;
69 70

	m_ui32ExternalBlockSize=ui32SampleCountPerSentBlock;
71 72 73 74 75 76 77 78 79 80 81

	return true;
}

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

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

82
	m_ui64TotalSampleCount=0;
83
	m_ui64StartTime = System::Time::zgetTime();
84 85 86 87

	return true;
}

88 89
#include <iostream>

90 91 92 93 94 95 96
boolean CDriverGenericSawTooth::loop(void)
{
	m_rDriverContext.getLogManager() << LogLevel_Debug << "CDriverGenericSawTooth::loop\n";

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

97
	// Find out how many samples to send
98
	const uint64 l_ui64Elapsed = System::Time::zgetTime() - m_ui64StartTime;
99 100
	const uint64 l_ui64SamplesNeededSoFar = ITimeArithmetics::timeToSampleCount(m_oHeader.getSamplingFrequency(), l_ui64Elapsed);
	if (l_ui64SamplesNeededSoFar <= m_ui64TotalSampleCount)
101
	{
102 103
		// Too early
		return true;
104
	}
105 106
	const uint32 l_ui32RemainingSamples = static_cast<uint32>(l_ui64SamplesNeededSoFar - m_ui64TotalSampleCount);
	if (l_ui32RemainingSamples * m_oHeader.getChannelCount() > m_vSample.size())
107
	{
108
		m_vSample.resize(l_ui32RemainingSamples * m_oHeader.getChannelCount());
109
	}
110

111 112 113 114 115 116 117 118 119 120
	// Generate the data
	// The result should be a linear ramp between [0,1] for each block sent *out* by the acquisition server
	for(uint32 i=0; i<l_ui32RemainingSamples; i++)
	{
		for (uint32 j = 0; j<m_oHeader.getChannelCount(); j++)
		{
			m_vSample[j*l_ui32RemainingSamples + i] = float32(m_ui64TotalSampleCount % m_ui32ExternalBlockSize) / (m_ui32ExternalBlockSize - 1);
		}
		m_ui64TotalSampleCount++;
	}
121

122
	m_pCallback->setSamples(&m_vSample[0],l_ui32RemainingSamples);
123 124 125

	m_rDriverContext.correctDriftSampleCount(m_rDriverContext.getSuggestedDriftCorrectionSampleCount());

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	return true;
}

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

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

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

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

	m_pCallback=NULL;

	return true;
}

//___________________________________________________________________//
//                                                                   //

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

	return false;
}

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

	return false;
}