1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2026-06-19 14:18:45 -04:00

TestMOSync: refactored Thread to Worker object moved to thread. Equivalent to FileInput changes

This commit is contained in:
f4exb
2020-07-12 11:20:46 +02:00
parent 437e0061f4
commit 8ef213d649
6 changed files with 77 additions and 85 deletions
+2 -2
View File
@@ -4,14 +4,14 @@ set(testmosync_SOURCES
testmosync.cpp
testmosyncplugin.cpp
testmosyncsettings.cpp
testmosyncthread.cpp
testmosyncworker.cpp
)
set(testmosync_HEADERS
testmosync.h
testmosyncplugin.h
testmosyncsettings.h
testmosyncthread.h
testmosyncworker.h
)
include_directories(
+37 -23
View File
@@ -31,7 +31,7 @@
#include "dsp/devicesamplesource.h"
#include "dsp/devicesamplesink.h"
#include "testmosyncthread.h"
#include "testmosyncworker.h"
#include "testmosync.h"
MESSAGE_CLASS_DEFINITION(TestMOSync::MsgConfigureTestMOSync, Message)
@@ -41,7 +41,7 @@ TestMOSync::TestMOSync(DeviceAPI *deviceAPI) :
m_deviceAPI(deviceAPI),
m_spectrumVis(SDR_TX_SCALEF),
m_settings(),
m_sinkThread(nullptr),
m_sinkWorker(nullptr),
m_deviceDescription("TestMOSync"),
m_runningTx(false),
m_masterTimer(deviceAPI->getMasterTimer()),
@@ -75,16 +75,17 @@ bool TestMOSync::startTx()
stopTx();
}
m_sinkThread = new TestMOSyncThread();
m_sinkWorker = new TestMOSyncWorker();
m_sinkWorker->moveToThread(&m_sinkWorkerThread);
m_sampleMOFifo.reset();
m_sinkThread->setFifo(&m_sampleMOFifo);
m_sinkThread->setFcPos(m_settings.m_fcPosTx);
m_sinkThread->setSamplerate(m_settings.m_sampleRate);
m_sinkThread->setLog2Interpolation(m_settings.m_log2Interp);
m_sinkThread->setSpectrumSink(&m_spectrumVis);
m_sinkThread->setFeedSpectrumIndex(m_feedSpectrumIndex);
m_sinkThread->connectTimer(m_masterTimer);
m_sinkThread->startWork();
m_sinkWorker->setFifo(&m_sampleMOFifo);
m_sinkWorker->setFcPos(m_settings.m_fcPosTx);
m_sinkWorker->setSamplerate(m_settings.m_sampleRate);
m_sinkWorker->setLog2Interpolation(m_settings.m_log2Interp);
m_sinkWorker->setSpectrumSink(&m_spectrumVis);
m_sinkWorker->setFeedSpectrumIndex(m_feedSpectrumIndex);
m_sinkWorker->connectTimer(m_masterTimer);
startWorker();
mutexLocker.unlock();
m_runningTx = true;
@@ -95,18 +96,31 @@ void TestMOSync::stopTx()
{
qDebug("TestMOSync::stopTx");
if (!m_sinkThread) {
if (!m_sinkWorker) {
return;
}
QMutexLocker mutexLocker(&m_mutex);
m_sinkThread->stopWork();
delete m_sinkThread;
m_sinkThread = nullptr;
stopWorker();
delete m_sinkWorker;
m_sinkWorker = nullptr;
m_runningTx = false;
}
void TestMOSync::startWorker()
{
m_sinkWorker->startWork();
m_sinkWorkerThread.start();
}
void TestMOSync::stopWorker()
{
m_sinkWorker->stopWork();
m_sinkWorkerThread.quit();
m_sinkWorkerThread.wait();
}
QByteArray TestMOSync::serialize() const
{
return m_settings.serialize();
@@ -232,8 +246,8 @@ void TestMOSync::setFeedSpectrumIndex(unsigned int feedSpectrumIndex)
{
m_feedSpectrumIndex = feedSpectrumIndex > 1 ? 1 : feedSpectrumIndex;
if (m_sinkThread) {
m_sinkThread->setFeedSpectrumIndex(m_feedSpectrumIndex);
if (m_sinkWorker) {
m_sinkWorker->setFeedSpectrumIndex(m_feedSpectrumIndex);
}
}
@@ -262,8 +276,8 @@ bool TestMOSync::applySettings(const TestMOSyncSettings& settings, bool force)
if ((m_settings.m_sampleRate != settings.m_sampleRate) || force)
{
if (m_sinkThread) {
m_sinkThread->setSamplerate(settings.m_sampleRate);
if (m_sinkWorker) {
m_sinkWorker->setSamplerate(settings.m_sampleRate);
}
forwardChangeTxDSP = true;
@@ -271,8 +285,8 @@ bool TestMOSync::applySettings(const TestMOSyncSettings& settings, bool force)
if ((m_settings.m_fcPosTx != settings.m_fcPosTx) || force)
{
if (m_sinkThread) {
m_sinkThread->setFcPos((int) settings.m_fcPosTx);
if (m_sinkWorker) {
m_sinkWorker->setFcPos((int) settings.m_fcPosTx);
}
forwardChangeTxDSP = true;
@@ -280,9 +294,9 @@ bool TestMOSync::applySettings(const TestMOSyncSettings& settings, bool force)
if ((m_settings.m_log2Interp != settings.m_log2Interp) || force)
{
if (m_sinkThread)
if (m_sinkWorker)
{
m_sinkThread->setLog2Interpolation(settings.m_log2Interp);
m_sinkWorker->setLog2Interpolation(settings.m_log2Interp);
qDebug() << "TestMOSync::applySettings: set interpolation to " << (1<<settings.m_log2Interp);
}
+6 -2
View File
@@ -23,13 +23,14 @@
#include <QString>
#include <QByteArray>
#include <QTimer>
#include <QThread>
#include "dsp/devicesamplemimo.h"
#include "dsp/spectrumvis.h"
#include "testmosyncsettings.h"
class DeviceAPI;
class TestMOSyncThread;
class TestMOSyncWorker;
class BasebandSampleSink;
class TestMOSync : public DeviceSampleMIMO {
@@ -152,12 +153,15 @@ private:
QMutex m_mutex;
SpectrumVis m_spectrumVis;
TestMOSyncSettings m_settings;
TestMOSyncThread* m_sinkThread;
TestMOSyncWorker* m_sinkWorker;
QThread m_sinkWorkerThread;
QString m_deviceDescription;
bool m_runningTx;
const QTimer& m_masterTimer;
unsigned int m_feedSpectrumIndex;
void startWorker();
void stopWorker();
bool applySettings(const TestMOSyncSettings& settings, bool force);
};
@@ -29,7 +29,7 @@
const PluginDescriptor TestMOSyncPlugin::m_pluginDescriptor = {
QString("TestMOSync"),
QString("Test Synchronous Multiple Output"),
QString("5.0.0"),
QString("5.7.10"),
QString("(c) Edouard Griffiths, F4EXB"),
QString("https://github.com/f4exb/sdrangel"),
true,
@@ -22,10 +22,10 @@
#include "dsp/basebandsamplesink.h"
#include "testmosyncsettings.h"
#include "testmosyncthread.h"
#include "testmosyncworker.h"
TestMOSyncThread::TestMOSyncThread(QObject* parent) :
QThread(parent),
TestMOSyncWorker::TestMOSyncWorker(QObject* parent) :
QObject(parent),
m_running(false),
m_buf(nullptr),
m_log2Interp(0),
@@ -36,13 +36,13 @@ TestMOSyncThread::TestMOSyncThread(QObject* parent) :
m_feedSpectrumIndex(0),
m_spectrumSink(nullptr)
{
qDebug("TestMOSyncThread::TestMOSyncThread");
qDebug("TestMOSyncWorker::TestMOSyncWorker");
setSamplerate(48000);
}
TestMOSyncThread::~TestMOSyncThread()
TestMOSyncWorker::~TestMOSyncWorker()
{
qDebug("TestMOSyncThread::~TestMOSyncThread");
qDebug("TestMOSyncWorker::~TestMOSyncWorker");
if (m_running) {
stopWork();
@@ -51,51 +51,30 @@ TestMOSyncThread::~TestMOSyncThread()
delete[] m_buf;
}
void TestMOSyncThread::startWork()
void TestMOSyncWorker::startWork()
{
qDebug("TestMOSyncThread::startWork");
m_startWaitMutex.lock();
qDebug("TestMOSyncWorker::startWork");
m_elapsedTimer.start();
start();
while(!m_running) {
m_startWaiter.wait(&m_startWaitMutex, 100);
}
m_startWaitMutex.unlock();
m_running = true;
}
void TestMOSyncThread::stopWork()
void TestMOSyncWorker::stopWork()
{
qDebug("TestMOSyncThread::stopWork");
qDebug("TestMOSyncWorker::stopWork");
m_running = false;
wait();
}
void TestMOSyncThread::run()
void TestMOSyncWorker::connectTimer(const QTimer& timer)
{
m_running = true;
m_startWaiter.wakeAll();
while(m_running) // actual work is in the tick() function
{
sleep(1);
}
m_running = false;
}
void TestMOSyncThread::connectTimer(const QTimer& timer)
{
qDebug() << "TestMOSyncThread::connectTimer";
qDebug() << "TestMOSyncWorker::connectTimer";
connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));
}
void TestMOSyncThread::setSamplerate(int samplerate)
void TestMOSyncWorker::setSamplerate(int samplerate)
{
if (samplerate != m_samplerate)
{
qDebug() << "TestMOSyncThread::setSamplerate:"
qDebug() << "TestMOSyncWorker::setSamplerate:"
<< " new:" << samplerate
<< " old:" << m_samplerate;
@@ -123,7 +102,7 @@ void TestMOSyncThread::setSamplerate(int samplerate)
}
}
void TestMOSyncThread::setLog2Interpolation(unsigned int log2Interpolation)
void TestMOSyncWorker::setLog2Interpolation(unsigned int log2Interpolation)
{
if ((log2Interpolation < 0) || (log2Interpolation > 6)) {
return;
@@ -151,22 +130,22 @@ void TestMOSyncThread::setLog2Interpolation(unsigned int log2Interpolation)
}
}
unsigned int TestMOSyncThread::getLog2Interpolation() const
unsigned int TestMOSyncWorker::getLog2Interpolation() const
{
return m_log2Interp;
}
void TestMOSyncThread::setFcPos(int fcPos)
void TestMOSyncWorker::setFcPos(int fcPos)
{
m_fcPos = fcPos;
}
int TestMOSyncThread::getFcPos() const
int TestMOSyncWorker::getFcPos() const
{
return m_fcPos;
}
void TestMOSyncThread::callback(qint16* buf, qint32 samplesPerChannel)
void TestMOSyncWorker::callback(qint16* buf, qint32 samplesPerChannel)
{
unsigned int iPart1Begin, iPart1End, iPart2Begin, iPart2End;
m_sampleFifo->readSync(samplesPerChannel/(1<<m_log2Interp), iPart1Begin, iPart1End, iPart2Begin, iPart2End);
@@ -184,7 +163,7 @@ void TestMOSyncThread::callback(qint16* buf, qint32 samplesPerChannel)
}
// Interpolate according to specified log2 (ex: log2=4 => decim=16). len is a number of samples (not a number of I or Q)
void TestMOSyncThread::callbackPart(qint16* buf, qint32 nSamples, int iBegin)
void TestMOSyncWorker::callbackPart(qint16* buf, qint32 nSamples, int iBegin)
{
for (unsigned int channel = 0; channel < 2; channel++)
{
@@ -282,7 +261,7 @@ void TestMOSyncThread::callbackPart(qint16* buf, qint32 nSamples, int iBegin)
}
}
void TestMOSyncThread::tick()
void TestMOSyncWorker::tick()
{
if (m_running)
{
@@ -309,7 +288,7 @@ void TestMOSyncThread::tick()
}
}
void TestMOSyncThread::callbackPart(std::vector<SampleVector>& data, unsigned int iBegin, unsigned int iEnd)
void TestMOSyncWorker::callbackPart(std::vector<SampleVector>& data, unsigned int iBegin, unsigned int iEnd)
{
unsigned int chunkSize = iEnd - iBegin;
@@ -358,7 +337,7 @@ void TestMOSyncThread::callbackPart(std::vector<SampleVector>& data, unsigned in
}
}
void TestMOSyncThread::feedSpectrum(int16_t *buf, unsigned int bufSize)
void TestMOSyncWorker::feedSpectrum(int16_t *buf, unsigned int bufSize)
{
if (!m_spectrumSink) {
return;
@@ -15,14 +15,12 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef PLUGINS_SAMPLEMIMO_TESTMOSYNC_TESTMOSYNCTHREAD_H_
#define PLUGINS_SAMPLEMIMO_TESTMOSYNC_TESTMOSYNCTHREAD_H_
#ifndef PLUGINS_SAMPLEMIMO_TESTMOSYNC_TESTMOSYNCWORKER_H_
#define PLUGINS_SAMPLEMIMO_TESTMOSYNC_TESTMOSYNCWORKER_H_
// configure two Tx
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QObject>
#include <QElapsedTimer>
#include "dsp/interpolators.h"
@@ -34,12 +32,12 @@ class QTimer;
class SampleMOFifo;
class BasebandSampleSink;
class TestMOSyncThread : public QThread {
class TestMOSyncWorker : public QObject {
Q_OBJECT
public:
TestMOSyncThread(QObject* parent = nullptr);
~TestMOSyncThread();
TestMOSyncWorker(QObject* parent = nullptr);
~TestMOSyncWorker();
void startWork();
void stopWork();
@@ -63,8 +61,6 @@ private:
int16_t m_imag;
};
#pragma pack(pop)
QMutex m_startWaitMutex;
QWaitCondition m_startWaiter;
bool m_running;
qint16 *m_buf; //!< Full buffer for SISO or MIMO operation
@@ -86,7 +82,6 @@ private:
IncrementalVector<Sample> m_samplesVector;
IncrementalVector<Sample> m_testVector;
void run();
unsigned int getNbFifos();
void callbackPart(qint16* buf, qint32 nSamples, int iBegin);
void callbackPart(std::vector<SampleVector>& data, unsigned int iBegin, unsigned int iEnd);
@@ -97,4 +92,4 @@ private slots:
void tick();
};
#endif // PLUGINS_SAMPLEMIMO_TESTMOSYNC_TESTMOSYNCTHREAD_H_
#endif // PLUGINS_SAMPLEMIMO_TESTMOSYNC_TESTMOSYNCWORKER_H_