This example shows a fictional oscilloscope with 2 channels: one channel acquires a sinusoidal wave while the other channel acquires a square wave.
The code shows how to add two channels to a root node by using Node::addChild() and how to use the DataAcquisition node.
#include <functional>
#include <math.h>
#include <unistd.h>
#include <iostream>
#include <thread>
class Oscilloscope
{
public:
private:
void switchOnSinWave();
void switchOffSinWave();
void startSinWave();
void stopSinWave();
void recoverSinWave();
void switchOnSquareWave();
void switchOffSquareWave();
void startSquareWave();
void stopSquareWave();
void recoverSquareWave();
void acquireSinusoidalWave();
void acquireSquareWave();
std::thread m_acquisitionThreadSinWave;
volatile bool m_bStopAcquisitionSinWave;
std::thread m_acquisitionThreadSquareWave;
volatile bool m_bStopAcquisitionSquareWave;
};
{
"SinWave",
100,
std::bind(&Oscilloscope::switchOnSinWave, this),
std::bind(&Oscilloscope::switchOffSinWave, this),
std::bind(&Oscilloscope::startSinWave, this),
std::bind(&Oscilloscope::stopSinWave, this),
std::bind(&Oscilloscope::recoverSinWave, this),
std::bind(&Oscilloscope::allowChange,
this,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3)
));
"SquareWave",
100,
std::bind(&Oscilloscope::switchOnSquareWave, this),
std::bind(&Oscilloscope::switchOffSquareWave, this),
std::bind(&Oscilloscope::startSquareWave, this),
std::bind(&Oscilloscope::stopSquareWave, this),
std::bind(&Oscilloscope::recoverSquareWave, this),
std::bind(&Oscilloscope::allowChange,
this,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3)
));
rootNode.initialize(this, factory);
}
void Oscilloscope::switchOnSinWave()
{
}
void Oscilloscope::switchOffSinWave()
{
}
void Oscilloscope::startSinWave()
{
m_bStopAcquisitionSinWave = false;
m_acquisitionThreadSinWave = std::thread(std::bind(&Oscilloscope::acquireSinusoidalWave, this));
}
void Oscilloscope::stopSinWave()
{
m_bStopAcquisitionSinWave = true;
m_acquisitionThreadSinWave.join();
}
void Oscilloscope::recoverSinWave()
{
}
void Oscilloscope::switchOnSquareWave()
{
}
void Oscilloscope::switchOffSquareWave()
{
}
void Oscilloscope::startSquareWave()
{
m_bStopAcquisitionSquareWave = false;
m_acquisitionThreadSquareWave = std::thread(std::bind(&Oscilloscope::acquireSquareWave, this));
}
void Oscilloscope::stopSquareWave()
{
m_bStopAcquisitionSquareWave = true;
m_acquisitionThreadSquareWave.join();
}
void Oscilloscope::recoverSquareWave()
{
}
{
return true;
}
void Oscilloscope::acquireSinusoidalWave()
{
std::vector<std::int32_t> outputData(m_acquisitionSinWave.getMaxElements());
std::int64_t angle(0);
while(!m_bStopAcquisitionSinWave)
{
size_t maxAmplitude = m_sinWaveAmplitude.getValue();
for(size_t scanVector(0); scanVector != outputData.size(); ++scanVector)
{
outputData[scanVector] = (double)maxAmplitude * sin((double)(angle++) / 10.0f);
}
m_acquisitionSinWave.push(m_acquisitionSinWave.getTimestamp(), outputData);
::usleep(100000);
}
}
void Oscilloscope::acquireSquareWave()
{
std::vector<std::int32_t> outputData(m_acquisitionSquareWave.getMaxElements());
std::int64_t angle(0);
while(!m_bStopAcquisitionSquareWave)
{
size_t maxAmplitude = m_squareWaveAmplitude.getValue();
for(size_t scanVector(0); scanVector != outputData.size(); ++scanVector)
{
outputData[scanVector] = ((angle & 0xff) < 128) ? maxAmplitude : - maxAmplitude;
}
m_acquisitionSquareWave.push(m_acquisitionSquareWave.getTimestamp(), outputData);
::usleep(100000);
}
}