diff --git a/plugins/pqm/include/pqm/acquisitionmanager.h b/plugins/pqm/include/pqm/acquisitionmanager.h index 5bf9f28950..9f0e24724c 100644 --- a/plugins/pqm/include/pqm/acquisitionmanager.h +++ b/plugins/pqm/include/pqm/acquisitionmanager.h @@ -29,15 +29,11 @@ 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(); @@ -45,17 +41,17 @@ private Q_SLOTS: iio_context *m_ctx; iio_buffer *m_buffer; - QTimer *m_dataRefreshTimer; QFutureWatcher *m_readFw; - QFutureWatcher *m_readBufferFw; QFutureWatcher *m_setFw; + QMutex mutex; QVector m_chnlsName; QMap> m_pqmAttr; QMap> m_bufferData; QMap m_tools = {{"rms", false}, {"harmonics", false}, {"waveform", false}, {"settings", false}}; bool m_attrHaveBeenRead = false; + bool m_buffHaveBeenRead = false; }; } // namespace scopy::pqm diff --git a/plugins/pqm/src/acquisitionmanager.cpp b/plugins/pqm/src/acquisitionmanager.cpp index 29385e9d93..9b47a5bb39 100644 --- a/plugins/pqm/src/acquisitionmanager.cpp +++ b/plugins/pqm/src/acquisitionmanager.cpp @@ -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(this); - m_readBufferFw = new QFutureWatcher(this); m_setFw = new QFutureWatcher(this); iio_device *dev = iio_context_find_device(m_ctx, DEVICE_PQM); if(dev) { @@ -24,13 +22,8 @@ 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::finished, this, &AcquisitionManager::onSetFinished); - connect(m_readFw, &QFutureWatcher::finished, this, &AcquisitionManager::onReadFinished); - connect(m_readBufferFw, &QFutureWatcher::finished, this, - &AcquisitionManager::onReadBufferFinished, Qt::QueuedConnection); + connect(m_readFw, &QFutureWatcher::finished, this, &AcquisitionManager::onReadFinished, + Qt::QueuedConnection); } else { qWarning(CAT_PQM_ACQ) << "The PQM device is not available!"; } @@ -38,17 +31,6 @@ AcquisitionManager::AcquisitionManager(iio_context *ctx, QObject *parent) 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(); @@ -83,21 +65,13 @@ void AcquisitionManager::toolEnabled(bool en, QString toolName) { m_tools[toolName] = en; QMap::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(); } } @@ -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() @@ -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 f = QtConcurrent::run(this, &AcquisitionManager::readBufferedData); - m_readBufferFw->setFuture(f); - } -} - bool AcquisitionManager::readBufferedData() { if(!m_buffer) { @@ -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::const_iterator it = std::find(m_tools.cbegin(), m_tools.cend(), true); + if(it != m_tools.cend()) { + futureReadData(); } } @@ -217,18 +183,9 @@ 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> attr) { if(!m_setFw->isRunning()) { - prepareForSet(); QFuture f = QtConcurrent::run(this, &AcquisitionManager::setData, attr); m_setFw->setFuture(f); } @@ -236,6 +193,7 @@ void AcquisitionManager::setConfigAttr(QMap> att void AcquisitionManager::setData(QMap> attr) { + mutex.lock(); iio_device *dev = iio_context_find_device(m_ctx, DEVICE_PQM); if(!dev) return; @@ -248,12 +206,7 @@ void AcquisitionManager::setData(QMap> 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"