Skip to content

Commit

Permalink
pqm: Rolling mode changes.
Browse files Browse the repository at this point in the history
Signed-off-by: andrei.danila <[email protected]>
  • Loading branch information
andreidanila1 committed Jul 2, 2024
1 parent 91f361d commit 6097841
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 78 deletions.
8 changes: 2 additions & 6 deletions plugins/pqm/include/pqm/acquisitionmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,29 @@ public Q_SLOTS:

private Q_SLOTS:
void futureReadData();
void futureBufferRead();
void onReadFinished();
void onReadBufferFinished();
void onSetFinished();

private:
double convertFromHwToHost(int value, QString chnlId);
void enableBufferChnls(iio_device *dev);
void prepareForSet();
void readData();
bool readPqmAttributes();
bool readBufferedData();
void setData(QMap<QString, QMap<QString, QString>>);

iio_context *m_ctx;
iio_buffer *m_buffer;
QTimer *m_dataRefreshTimer;
QFutureWatcher<void> *m_readFw;
QFutureWatcher<bool> *m_readBufferFw;
QFutureWatcher<void> *m_setFw;

QMutex mutex;
QVector<QString> m_chnlsName;
QMap<QString, QMap<QString, QString>> m_pqmAttr;
QMap<QString, QVector<double>> m_bufferData;
QMap<QString, bool> m_tools = {{"rms", false}, {"harmonics", false}, {"waveform", false}, {"settings", false}};

bool m_attrHaveBeenRead = false;
bool m_buffHaveBeenRead = false;
};
} // namespace scopy::pqm

Expand Down
97 changes: 25 additions & 72 deletions plugins/pqm/src/acquisitionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ using namespace scopy::pqm;
AcquisitionManager::AcquisitionManager(iio_context *ctx, QObject *parent)
: QObject(parent)
, m_ctx(ctx)
, m_dataRefreshTimer(nullptr)
, m_buffer(nullptr)
{
m_readFw = new QFutureWatcher<void>(this);
m_readBufferFw = new QFutureWatcher<bool>(this);
m_setFw = new QFutureWatcher<void>(this);
iio_device *dev = iio_context_find_device(m_ctx, DEVICE_PQM);
if(dev) {
Expand All @@ -24,31 +22,15 @@ AcquisitionManager::AcquisitionManager(iio_context *ctx, QObject *parent)
if(!m_buffer) {
qWarning(CAT_PQM_ACQ) << "Cannot create the buffer!";
}
m_dataRefreshTimer = new QTimer(this);
m_dataRefreshTimer->setInterval(500);
connect(m_dataRefreshTimer, &QTimer::timeout, this, &AcquisitionManager::futureReadData);
connect(m_setFw, &QFutureWatcher<void>::finished, this, &AcquisitionManager::onSetFinished);
connect(m_readFw, &QFutureWatcher<void>::finished, this, &AcquisitionManager::onReadFinished);
connect(m_readBufferFw, &QFutureWatcher<bool>::finished, this,
&AcquisitionManager::onReadBufferFinished, Qt::QueuedConnection);
connect(m_readFw, &QFutureWatcher<void>::finished, this, &AcquisitionManager::onReadFinished,
Qt::QueuedConnection);
} else {
qWarning(CAT_PQM_ACQ) << "The PQM device is not available!";
}
}

AcquisitionManager::~AcquisitionManager()
{
if(m_dataRefreshTimer) {
m_dataRefreshTimer->stop();
m_dataRefreshTimer->deleteLater();
m_dataRefreshTimer = nullptr;
}
if(m_readBufferFw) {
m_readBufferFw->waitForFinished();
m_readBufferFw->cancel();
m_readBufferFw->deleteLater();
m_readBufferFw = nullptr;
}
if(m_readFw) {
m_readFw->waitForFinished();
m_readFw->deleteLater();
Expand Down Expand Up @@ -83,21 +65,13 @@ void AcquisitionManager::toolEnabled(bool en, QString toolName)
{
m_tools[toolName] = en;
QMap<QString, bool>::const_iterator it = std::find(m_tools.cbegin(), m_tools.cend(), true);
if(!m_dataRefreshTimer) {
qWarning(CAT_PQM_ACQ) << "Unable to start data acquisition!";
return;
}
if(it != m_tools.cend()) {
if(m_tools["waveform"]) {
futureBufferRead();
}
if(!m_dataRefreshTimer->isActive()) {
m_dataRefreshTimer->start();
if(!m_readFw->isRunning()) {
futureReadData();
}
} else {
m_readBufferFw->waitForFinished();
m_readBufferFw->cancel();
m_dataRefreshTimer->stop();
m_readFw->waitForFinished();
m_readFw->cancel();
}
}

Expand All @@ -111,12 +85,14 @@ void AcquisitionManager::futureReadData()

void AcquisitionManager::readData()
{
mutex.lock();
if(m_tools["rms"] || m_tools["harmonics"] || m_tools["settings"]) {
m_readBufferFw->waitForFinished();
m_readBufferFw->pause();
m_attrHaveBeenRead = readPqmAttributes();
m_readBufferFw->resume();
}
if(m_tools["waveform"]) {
m_buffHaveBeenRead = readBufferedData();
}
mutex.unlock();
}

bool AcquisitionManager::readPqmAttributes()
Expand Down Expand Up @@ -149,22 +125,6 @@ bool AcquisitionManager::readPqmAttributes()
return true;
}

void AcquisitionManager::onReadFinished()
{
if(m_attrHaveBeenRead) {
m_attrHaveBeenRead = false;
Q_EMIT pqmAttrsAvailable(m_pqmAttr);
}
}

void AcquisitionManager::futureBufferRead()
{
if(!m_readBufferFw->isRunning()) {
QFuture<bool> f = QtConcurrent::run(this, &AcquisitionManager::readBufferedData);
m_readBufferFw->setFuture(f);
}
}

bool AcquisitionManager::readBufferedData()
{
if(!m_buffer) {
Expand Down Expand Up @@ -195,13 +155,19 @@ bool AcquisitionManager::readBufferedData()
return true;
}

void AcquisitionManager::onReadBufferFinished()
void AcquisitionManager::onReadFinished()
{
bool readResult = m_readBufferFw->result();
if(m_tools["waveform"]) {
if(readResult)
Q_EMIT bufferDataAvailable(m_bufferData);
futureBufferRead();
if(m_attrHaveBeenRead) {
m_attrHaveBeenRead = false;
Q_EMIT pqmAttrsAvailable(m_pqmAttr);
}
if(m_buffHaveBeenRead) {
m_attrHaveBeenRead = false;
Q_EMIT bufferDataAvailable(m_bufferData);
}
QMap<QString, bool>::const_iterator it = std::find(m_tools.cbegin(), m_tools.cend(), true);
if(it != m_tools.cend()) {
futureReadData();
}
}

Expand All @@ -217,25 +183,17 @@ double AcquisitionManager::convertFromHwToHost(int value, QString chnlId)
return result;
}

void AcquisitionManager::prepareForSet()
{
m_readFw->waitForFinished();
m_readFw->pause();
m_readBufferFw->waitForFinished();
m_readBufferFw->pause();
}

void AcquisitionManager::setConfigAttr(QMap<QString, QMap<QString, QString>> attr)
{
if(!m_setFw->isRunning()) {
prepareForSet();
QFuture<void> f = QtConcurrent::run(this, &AcquisitionManager::setData, attr);
m_setFw->setFuture(f);
}
}

void AcquisitionManager::setData(QMap<QString, QMap<QString, QString>> attr)
{
mutex.lock();
iio_device *dev = iio_context_find_device(m_ctx, DEVICE_PQM);
if(!dev)
return;
Expand All @@ -248,12 +206,7 @@ void AcquisitionManager::setData(QMap<QString, QMap<QString, QString>> attr)
iio_device_attr_write(dev, key.toStdString().c_str(), newVal.toStdString().c_str());
}
}
}

void AcquisitionManager::onSetFinished()
{
m_readFw->resume();
m_readBufferFw->resume();
mutex.lock();
}

#include "moc_acquisitionmanager.cpp"

0 comments on commit 6097841

Please sign in to comment.