1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-09-28 15:56:33 -04:00

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

This commit is contained in:
f4exb 2020-07-12 02:39:14 +02:00
parent 4bad01280e
commit 059d0dc4f2
5 changed files with 100 additions and 91 deletions

View File

@ -5,7 +5,7 @@ set(airspyhf_SOURCES
airspyhfplugin.cpp
airspyhfsettings.cpp
airspyhfwebapiadapter.cpp
airspyhfthread.cpp
airspyhfworker.cpp
)
set(airspyhf_HEADERS
@ -13,7 +13,7 @@ set(airspyhf_HEADERS
airspyhfplugin.h
airspyhfsettings.h
airspyhfwebapiadapter.h
airspyhfthread.h
airspyhfworker.h
)
include_directories(

View File

@ -38,7 +38,7 @@
#include "airspyhfplugin.h"
#include "airspyhfsettings.h"
#include "airspyhfthread.h"
#include "airspyhfworker.h"
MESSAGE_CLASS_DEFINITION(AirspyHFInput::MsgConfigureAirspyHF, Message)
MESSAGE_CLASS_DEFINITION(AirspyHFInput::MsgStartStop, Message)
@ -53,7 +53,7 @@ AirspyHFInput::AirspyHFInput(DeviceAPI *deviceAPI) :
m_deviceAPI(deviceAPI),
m_settings(),
m_dev(0),
m_airspyHFThread(nullptr),
m_airspyHFWorker(nullptr),
m_deviceDescription("AirspyHF"),
m_running(false)
{
@ -167,9 +167,12 @@ bool AirspyHFInput::start()
return false;
}
if (m_running) { stop(); }
if (m_running) {
stop();
}
m_airspyHFThread = new AirspyHFThread(m_dev, &m_sampleFifo);
m_airspyHFWorker = new AirspyHFWorker(m_dev, &m_sampleFifo);
m_airspyHFWorker->moveToThread(&m_airspyHFWorkerThread);
int sampleRateIndex = m_settings.m_devSampleRateIndex;
if (m_settings.m_devSampleRateIndex >= m_sampleRates.size()) {
@ -177,21 +180,45 @@ bool AirspyHFInput::start()
}
if (sampleRateIndex >= 0) {
m_airspyHFThread->setSamplerate(m_sampleRates[sampleRateIndex]);
m_airspyHFWorker->setSamplerate(m_sampleRates[sampleRateIndex]);
}
m_airspyHFThread->setLog2Decimation(m_settings.m_log2Decim);
m_airspyHFThread->setIQOrder(m_settings.m_iqOrder);
m_airspyHFThread->startWork();
m_airspyHFWorker->setLog2Decimation(m_settings.m_log2Decim);
m_airspyHFWorker->setIQOrder(m_settings.m_iqOrder);
mutexLocker.unlock();
mutexLocker.unlock();
if (startWorker())
{
qDebug("AirspyHFInput::startInput: started");
applySettings(m_settings, true);
m_running = true;
}
else
{
m_running = false;
}
applySettings(m_settings, true);
return m_running;
}
qDebug("AirspyHFInput::startInput: started");
m_running = true;
bool AirspyHFInput::startWorker()
{
if (m_airspyHFWorker->startWork())
{
m_airspyHFWorkerThread.start();
return true;
}
else
{
return false;
}
}
return true;
void AirspyHFInput::stopWorker()
{
m_airspyHFWorker->stopWork();
m_airspyHFWorkerThread.quit();
m_airspyHFWorkerThread.wait();
}
void AirspyHFInput::closeDevice()
@ -211,11 +238,11 @@ void AirspyHFInput::stop()
qDebug("AirspyHFInput::stop");
QMutexLocker mutexLocker(&m_mutex);
if (m_airspyHFThread)
if (m_airspyHFWorker)
{
m_airspyHFThread->stopWork();
delete m_airspyHFThread;
m_airspyHFThread = nullptr;
stopWorker();
delete m_airspyHFWorker;
m_airspyHFWorker = nullptr;
}
m_running = false;
@ -446,10 +473,10 @@ bool AirspyHFInput::applySettings(const AirspyHFSettings& settings, bool force)
{
qCritical("AirspyHFInput::applySettings: could not set sample rate index %u (%d S/s)", sampleRateIndex, m_sampleRates[sampleRateIndex]);
}
else if (m_airspyHFThread)
else if (m_airspyHFWorker)
{
qDebug("AirspyHFInput::applySettings: sample rate set to index: %u (%d S/s)", sampleRateIndex, m_sampleRates[sampleRateIndex]);
m_airspyHFThread->setSamplerate(m_sampleRates[sampleRateIndex]);
m_airspyHFWorker->setSamplerate(m_sampleRates[sampleRateIndex]);
}
}
}
@ -459,9 +486,9 @@ bool AirspyHFInput::applySettings(const AirspyHFSettings& settings, bool force)
reverseAPIKeys.append("log2Decim");
forwardChange = true;
if (m_airspyHFThread)
if (m_airspyHFWorker)
{
m_airspyHFThread->setLog2Decimation(settings.m_log2Decim);
m_airspyHFWorker->setLog2Decimation(settings.m_log2Decim);
qDebug() << "AirspyInput: set decimation to " << (1<<settings.m_log2Decim);
}
}
@ -470,8 +497,8 @@ bool AirspyHFInput::applySettings(const AirspyHFSettings& settings, bool force)
{
reverseAPIKeys.append("iqOrder");
if (m_airspyHFThread) {
m_airspyHFThread->setIQOrder(settings.m_iqOrder);
if (m_airspyHFWorker) {
m_airspyHFWorker->setIQOrder(settings.m_iqOrder);
}
}
@ -487,7 +514,7 @@ bool AirspyHFInput::applySettings(const AirspyHFSettings& settings, bool force)
{
qCritical("AirspyHFInput::applySettings: could not set LO ppm correction to %f", settings.m_LOppmTenths / 10.0f);
}
else if (m_airspyHFThread)
else if (m_airspyHFWorker)
{
qDebug("AirspyHFInput::applySettings: LO ppm correction set to %f", settings.m_LOppmTenths / 10.0f);
}

View File

@ -21,6 +21,7 @@
#include <QString>
#include <QByteArray>
#include <QNetworkRequest>
#include <QThread>
#include <libairspyhf/airspyhf.h>
#include <dsp/devicesamplesource.h>
@ -30,7 +31,7 @@
class QNetworkAccessManager;
class QNetworkReply;
class DeviceAPI;
class AirspyHFThread;
class AirspyHFWorker;
class FileRecord;
class AirspyHFInput : public DeviceSampleSource {
@ -165,7 +166,8 @@ private:
QMutex m_mutex;
AirspyHFSettings m_settings;
airspyhf_device_t* m_dev;
AirspyHFThread* m_airspyHFThread;
AirspyHFWorker* m_airspyHFWorker;
QThread m_airspyHFWorkerThread;
QString m_deviceDescription;
std::vector<uint32_t> m_sampleRates;
bool m_running;
@ -173,6 +175,8 @@ private:
QNetworkAccessManager *m_networkManager;
QNetworkRequest m_networkRequest;
bool startWorker();
void stopWorker();
bool openDevice();
void closeDevice();
bool applySettings(const AirspyHFSettings& settings, bool force);

View File

@ -20,12 +20,12 @@
#include <errno.h>
#include "dsp/samplesinkfifo.h"
#include "airspyhfthread.h"
#include "airspyhfworker.h"
AirspyHFThread *AirspyHFThread::m_this = 0;
AirspyHFWorker *AirspyHFWorker::m_this = 0;
AirspyHFThread::AirspyHFThread(airspyhf_device_t* dev, SampleSinkFifo* sampleFifo, QObject* parent) :
QThread(parent),
AirspyHFWorker::AirspyHFWorker(airspyhf_device_t* dev, SampleSinkFifo* sampleFifo, QObject* parent) :
QObject(parent),
m_running(false),
m_dev(dev),
m_convertBuffer(AIRSPYHF_BLOCKSIZE),
@ -38,60 +38,34 @@ AirspyHFThread::AirspyHFThread(airspyhf_device_t* dev, SampleSinkFifo* sampleFif
m_this = this;
}
AirspyHFThread::~AirspyHFThread()
AirspyHFWorker::~AirspyHFWorker()
{
stopWork();
m_this = 0;
}
void AirspyHFThread::startWork()
bool AirspyHFWorker::startWork()
{
m_startWaitMutex.lock();
start();
while(!m_running)
m_startWaiter.wait(&m_startWaitMutex, 100);
m_startWaitMutex.unlock();
}
qDebug("AirspyThread::startWork");
airspyhf_error rc = (airspyhf_error) airspyhf_start(m_dev, rx_callback, 0);
void AirspyHFThread::stopWork()
{
qDebug("AirspyThread::stopWork");
m_running = false;
wait();
}
void AirspyHFThread::setSamplerate(uint32_t samplerate)
{
m_samplerate = samplerate;
}
void AirspyHFThread::setLog2Decimation(unsigned int log2_decim)
{
m_log2Decim = log2_decim;
}
void AirspyHFThread::run()
{
airspyhf_error rc;
m_running = true;
m_startWaiter.wakeAll();
rc = (airspyhf_error) airspyhf_start(m_dev, rx_callback, 0);
if (rc != AIRSPYHF_SUCCESS)
if (rc == AIRSPYHF_SUCCESS)
{
qCritical("AirspyHFFThread::run: failed to start Airspy HF Rx");
m_running = (airspyhf_is_streaming(m_dev) != 0);
}
else
{
while ((m_running) && (airspyhf_is_streaming(m_dev) != 0))
{
sleep(1);
}
qCritical("AirspyHFFThread::run: failed to start Airspy HF Rx");
m_running = false;
}
rc = (airspyhf_error) airspyhf_stop(m_dev);
return m_running;
}
void AirspyHFWorker::stopWork()
{
qDebug("AirspyThread::stopWork");
airspyhf_error rc = (airspyhf_error) airspyhf_stop(m_dev);
if (rc == AIRSPYHF_SUCCESS) {
qDebug("AirspyHFFThread::run: stopped Airspy HF Rx");
@ -99,11 +73,20 @@ void AirspyHFThread::run()
qDebug("AirspyHFFThread::run: failed to stop Airspy HF Rx");
}
m_running = false;
m_running = false;}
void AirspyHFWorker::setSamplerate(uint32_t samplerate)
{
m_samplerate = samplerate;
}
void AirspyHFWorker::setLog2Decimation(unsigned int log2_decim)
{
m_log2Decim = log2_decim;
}
// Decimate according to specified log2 (ex: log2=4 => decim=16)
void AirspyHFThread::callbackIQ(const float* buf, qint32 len)
void AirspyHFWorker::callbackIQ(const float* buf, qint32 len)
{
SampleVector::iterator it = m_convertBuffer.begin();
@ -137,7 +120,7 @@ void AirspyHFThread::callbackIQ(const float* buf, qint32 len)
m_sampleFifo->write(m_convertBuffer.begin(), it);
}
void AirspyHFThread::callbackQI(const float* buf, qint32 len)
void AirspyHFWorker::callbackQI(const float* buf, qint32 len)
{
SampleVector::iterator it = m_convertBuffer.begin();
@ -171,7 +154,7 @@ void AirspyHFThread::callbackQI(const float* buf, qint32 len)
m_sampleFifo->write(m_convertBuffer.begin(), it);
}
int AirspyHFThread::rx_callback(airspyhf_transfer_t* transfer)
int AirspyHFWorker::rx_callback(airspyhf_transfer_t* transfer)
{
qint32 nbIAndQ = transfer->sample_count * 2;

View File

@ -15,35 +15,31 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_AIRSPYHFTHREAD_H
#define INCLUDE_AIRSPYHFTHREAD_H
#ifndef INCLUDE_AIRSPYHFWORKER_H
#define INCLUDE_AIRSPYHFWORKER_H
#include <dsp/decimatorsfi.h>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QObject>
#include <libairspyhf/airspyhf.h>
#include "dsp/samplesinkfifo.h"
#define AIRSPYHF_BLOCKSIZE (1<<17)
class AirspyHFThread : public QThread {
class AirspyHFWorker : public QObject {
Q_OBJECT
public:
AirspyHFThread(airspyhf_device_t* dev, SampleSinkFifo* sampleFifo, QObject* parent = 0);
~AirspyHFThread();
AirspyHFWorker(airspyhf_device_t* dev, SampleSinkFifo* sampleFifo, QObject* parent = 0);
~AirspyHFWorker();
void startWork();
bool startWork();
void stopWork();
void setSamplerate(uint32_t samplerate);
void setLog2Decimation(unsigned int log2_decim);
void setIQOrder(bool iqOrder) { m_iqOrder = iqOrder; }
private:
QMutex m_startWaitMutex;
QWaitCondition m_startWaiter;
bool m_running;
airspyhf_device_t* m_dev;
@ -54,15 +50,14 @@ private:
int m_samplerate;
unsigned int m_log2Decim;
bool m_iqOrder;
static AirspyHFThread *m_this;
static AirspyHFWorker *m_this;
DecimatorsFI<true> m_decimatorsIQ;
DecimatorsFI<false> m_decimatorsQI;
void run();
void callbackIQ(const float* buf, qint32 len);
void callbackQI(const float* buf, qint32 len);
static int rx_callback(airspyhf_transfer_t* transfer);
};
#endif // INCLUDE_AIRSPYHFTHREAD_H
#endif // INCLUDE_AIRSPYHFWORKER_H