RTL-SDR: refactoring: device open close moved in the constructor and destructor respectively of the input object

pull/27/head
f4exb 2017-04-13 01:21:25 +02:00
rodzic 934e73f7a7
commit bb5fd95f33
5 zmienionych plików z 216 dodań i 139 usunięć

Wyświetl plik

@ -60,6 +60,8 @@ RTLSDRGui::RTLSDRGui(DeviceSourceAPI *deviceAPI, QWidget* parent) :
m_deviceAPI->addSink(m_fileSink);
connect(m_deviceAPI->getDeviceOutputMessageQueue(), SIGNAL(messageEnqueued()), this, SLOT(handleDSPMessages()), Qt::QueuedConnection);
queryDeviceReport(); // will reply with MsgReportRTLSDR to report gain list
}
RTLSDRGui::~RTLSDRGui()
@ -67,6 +69,7 @@ RTLSDRGui::~RTLSDRGui()
m_deviceAPI->removeSink(m_fileSink);
delete m_fileSink;
delete ui;
delete m_sampleSource;
}
void RTLSDRGui::destroy()
@ -124,6 +127,7 @@ bool RTLSDRGui::deserialize(const QByteArray& data)
{
if(m_settings.deserialize(data))
{
displayGains();
displaySettings();
sendSettings();
return true;
@ -141,7 +145,7 @@ bool RTLSDRGui::handleMessage(const Message& message)
{
qDebug() << "RTLSDRGui::handleMessage: MsgReportRTLSDR";
m_gains = ((RTLSDRInput::MsgReportRTLSDR&) message).getGains();
displaySettings();
displayGains();
return true;
}
else
@ -194,6 +198,35 @@ void RTLSDRGui::updateSampleRateAndFrequency()
ui->deviceRateText->setText(tr("%1k").arg(QString::number(m_sampleRate / 1000.0f, 'g', 5)));
}
void RTLSDRGui::displayGains()
{
if (m_gains.size() > 0)
{
int dist = abs(m_settings.m_gain - m_gains[0]);
int pos = 0;
for (uint i = 1; i < m_gains.size(); i++)
{
if (abs(m_settings.m_gain - m_gains[i]) < dist)
{
dist = abs(m_settings.m_gain - m_gains[i]);
pos = i;
}
}
ui->gainText->setText(tr("%1.%2").arg(m_gains[pos] / 10).arg(abs(m_gains[pos] % 10)));
ui->gain->setMaximum(m_gains.size() - 1);
ui->gain->setEnabled(true);
ui->gain->setValue(pos);
}
else
{
ui->gain->setMaximum(0);
ui->gain->setEnabled(false);
ui->gain->setValue(0);
}
}
void RTLSDRGui::displaySettings()
{
ui->centerFrequency->setValue(m_settings.m_centerFrequency / 1000);
@ -204,32 +237,6 @@ void RTLSDRGui::displaySettings()
ui->ppmText->setText(tr("%1").arg(m_settings.m_loPpmCorrection));
ui->decim->setCurrentIndex(m_settings.m_log2Decim);
ui->fcPos->setCurrentIndex((int) m_settings.m_fcPos);
if (m_gains.size() > 0)
{
int dist = abs(m_settings.m_gain - m_gains[0]);
int pos = 0;
for (uint i = 1; i < m_gains.size(); i++)
{
if (abs(m_settings.m_gain - m_gains[i]) < dist)
{
dist = abs(m_settings.m_gain - m_gains[i]);
pos = i;
}
}
ui->gainText->setText(tr("%1.%2").arg(m_gains[pos] / 10).arg(abs(m_gains[pos] % 10)));
ui->gain->setMaximum(m_gains.size() - 1);
ui->gain->setEnabled(true);
ui->gain->setValue(pos);
}
else
{
ui->gain->setMaximum(0);
ui->gain->setEnabled(false);
ui->gain->setValue(0);
}
}
void RTLSDRGui::sendSettings()
@ -334,6 +341,12 @@ void RTLSDRGui::on_record_toggled(bool checked)
}
}
void RTLSDRGui::queryDeviceReport()
{
RTLSDRInput::MsgQueryRTLSDR* message = RTLSDRInput::MsgQueryRTLSDR::create();
m_sampleSource->getInputMessageQueue()->push(message);
}
void RTLSDRGui::updateHardware()
{
RTLSDRInput::MsgConfigureRTLSDR* message = RTLSDRInput::MsgConfigureRTLSDR::create(m_settings);

Wyświetl plik

@ -61,6 +61,8 @@ private:
quint64 m_deviceCenterFrequency; //!< Center frequency in device
int m_lastEngineState;
void queryDeviceReport();
void displayGains();
void displaySettings();
void sendSettings();
void updateSampleRateAndFrequency();

Wyświetl plik

@ -28,6 +28,7 @@
#include "dsp/dspengine.h"
MESSAGE_CLASS_DEFINITION(RTLSDRInput::MsgConfigureRTLSDR, Message)
MESSAGE_CLASS_DEFINITION(RTLSDRInput::MsgQueryRTLSDR, Message)
MESSAGE_CLASS_DEFINITION(RTLSDRInput::MsgReportRTLSDR, Message)
RTLSDRInput::RTLSDRInput(DeviceSourceAPI *deviceAPI) :
@ -35,107 +36,128 @@ RTLSDRInput::RTLSDRInput(DeviceSourceAPI *deviceAPI) :
m_settings(),
m_dev(0),
m_rtlSDRThread(0),
m_deviceDescription()
m_deviceDescription(),
m_running(false)
{
openDevice();
}
RTLSDRInput::~RTLSDRInput()
{
stop();
//stop();
if (m_running) stop();
closeDevice();
}
bool RTLSDRInput::start(int device)
bool RTLSDRInput::openDevice()
{
if (m_dev != 0)
{
closeDevice();
}
char vendor[256];
char product[256];
char serial[256];
int res;
int numberOfGains;
if (!m_sampleFifo.setSize(96000 * 4))
{
qCritical("RTLSDRInput::openDevice: Could not allocate SampleFifo");
return false;
}
int device;
if ((device = rtlsdr_get_index_by_serial(qPrintable(m_deviceAPI->getSampleSourceSerial()))) < 0)
{
qCritical("RTLSDRInput::openDevice: could not get RTLSDR serial number");
return false;
}
if ((res = rtlsdr_open(&m_dev, device)) < 0)
{
qCritical("RTLSDRInput::openDevice: could not open RTLSDR #%d: %s", device, strerror(errno));
return false;
}
vendor[0] = '\0';
product[0] = '\0';
serial[0] = '\0';
if ((res = rtlsdr_get_usb_strings(m_dev, vendor, product, serial)) < 0)
{
qCritical("RTLSDRInput::openDevice: error accessing USB device");
stop();
return false;
}
qWarning("RTLSDRInput::openDevice: open: %s %s, SN: %s", vendor, product, serial);
m_deviceDescription = QString("%1 (SN %2)").arg(product).arg(serial);
if ((res = rtlsdr_set_sample_rate(m_dev, 1152000)) < 0)
{
qCritical("RTLSDRInput::openDevice: could not set sample rate: 1024k S/s");
stop();
return false;
}
if ((res = rtlsdr_set_tuner_gain_mode(m_dev, 1)) < 0)
{
qCritical("RTLSDRInput::openDevice: error setting tuner gain mode");
stop();
return false;
}
if ((res = rtlsdr_set_agc_mode(m_dev, 0)) < 0)
{
qCritical("RTLSDRInput::openDevice: error setting agc mode");
stop();
return false;
}
numberOfGains = rtlsdr_get_tuner_gains(m_dev, NULL);
if (numberOfGains < 0)
{
qCritical("RTLSDRInput::openDevice: error getting number of gain values supported");
stop();
return false;
}
m_gains.resize(numberOfGains);
if (rtlsdr_get_tuner_gains(m_dev, &m_gains[0]) < 0)
{
qCritical("RTLSDRInput::openDevice: error getting gain values");
stop();
return false;
}
else
{
qDebug() << "RTLSDRInput::openDevice: " << m_gains.size() << "gains";
}
if ((res = rtlsdr_reset_buffer(m_dev)) < 0)
{
qCritical("RTLSDRInput::openDevice: could not reset USB EP buffers: %s", strerror(errno));
stop();
return false;
}
return true;
}
bool RTLSDRInput::start(int device) // TODO: remove device parameter
{
QMutexLocker mutexLocker(&m_mutex);
if (m_dev != 0)
{
stop();
if (!m_dev) {
return false;
}
char vendor[256];
char product[256];
char serial[256];
int res;
int numberOfGains;
if (!m_sampleFifo.setSize(96000 * 4))
{
qCritical("RTLSDRInput::start: Could not allocate SampleFifo");
return false;
}
if ((res = rtlsdr_open(&m_dev, device)) < 0)
{
qCritical("RTLSDRInput::start: could not open RTLSDR #%d: %s", device, strerror(errno));
return false;
}
vendor[0] = '\0';
product[0] = '\0';
serial[0] = '\0';
if ((res = rtlsdr_get_usb_strings(m_dev, vendor, product, serial)) < 0)
{
qCritical("RTLSDRInput::start: error accessing USB device");
stop();
return false;
}
qWarning("RTLSDRInput::start: open: %s %s, SN: %s", vendor, product, serial);
m_deviceDescription = QString("%1 (SN %2)").arg(product).arg(serial);
if ((res = rtlsdr_set_sample_rate(m_dev, 1152000)) < 0)
{
qCritical("RTLSDRInput::start: could not set sample rate: 1024k S/s");
stop();
return false;
}
if ((res = rtlsdr_set_tuner_gain_mode(m_dev, 1)) < 0)
{
qCritical("RTLSDRInput::start: error setting tuner gain mode");
stop();
return false;
}
if ((res = rtlsdr_set_agc_mode(m_dev, 0)) < 0)
{
qCritical("RTLSDRInput::start: error setting agc mode");
stop();
return false;
}
numberOfGains = rtlsdr_get_tuner_gains(m_dev, NULL);
if (numberOfGains < 0)
{
qCritical("RTLSDRInput::start: error getting number of gain values supported");
stop();
return false;
}
m_gains.resize(numberOfGains);
if (rtlsdr_get_tuner_gains(m_dev, &m_gains[0]) < 0)
{
qCritical("RTLSDRInput::start: error getting gain values");
stop();
return false;
}
else
{
qDebug() << "RTLSDRInput::start: " << m_gains.size() << "gains";
MsgReportRTLSDR *message = MsgReportRTLSDR::create(m_gains);
getOutputMessageQueueToGUI()->push(message);
}
if ((res = rtlsdr_reset_buffer(m_dev)) < 0)
{
qCritical("RTLSDRInput::start: could not reset USB EP buffers: %s", strerror(errno));
stop();
return false;
}
if (m_running) stop();
if ((m_rtlSDRThread = new RTLSDRThread(m_dev, &m_sampleFifo)) == NULL)
{
@ -144,15 +166,31 @@ bool RTLSDRInput::start(int device)
return false;
}
m_rtlSDRThread->setSamplerate(m_settings.m_devSampleRate);
m_rtlSDRThread->setLog2Decimation(m_settings.m_log2Decim);
m_rtlSDRThread->setFcPos((int) m_settings.m_fcPos);
m_rtlSDRThread->startWork();
mutexLocker.unlock();
applySettings(m_settings, true);
m_running = true;
return true;
}
void RTLSDRInput::closeDevice()
{
if (m_dev != 0)
{
rtlsdr_close(m_dev);
m_dev = 0;
}
m_deviceDescription.clear();
}
void RTLSDRInput::stop()
{
QMutexLocker mutexLocker(&m_mutex);
@ -164,13 +202,7 @@ void RTLSDRInput::stop()
m_rtlSDRThread = 0;
}
if (m_dev != 0)
{
rtlsdr_close(m_dev);
m_dev = 0;
}
m_deviceDescription.clear();
m_running = false;
}
const QString& RTLSDRInput::getDeviceDescription() const
@ -205,6 +237,16 @@ bool RTLSDRInput::handleMessage(const Message& message)
return true;
}
else if (MsgQueryRTLSDR::match(message))
{
MsgQueryRTLSDR& conf = (MsgQueryRTLSDR&) message;
qDebug() << "RTLSDRInput::handleMessage: MsgQueryRTLSDR";
MsgReportRTLSDR *message = MsgReportRTLSDR::create(m_gains);
getOutputMessageQueueToGUI()->push(message);
return true;
}
else
{
return false;
@ -268,7 +310,7 @@ bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force)
}
else
{
m_rtlSDRThread->setSamplerate(settings.m_devSampleRate);
if (m_rtlSDRThread) m_rtlSDRThread->setSamplerate(settings.m_devSampleRate);
qDebug("RTLSDRInput::applySettings: sample rate set to %d", m_settings.m_devSampleRate);
}
}
@ -279,7 +321,7 @@ bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force)
m_settings.m_log2Decim = settings.m_log2Decim;
forwardChange = true;
if(m_dev != 0)
if (m_rtlSDRThread != 0)
{
m_rtlSDRThread->setLog2Decimation(settings.m_log2Decim);
}
@ -314,7 +356,7 @@ bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force)
}
}
if(m_dev != 0)
if (m_dev != 0)
{
if (rtlsdr_set_center_freq( m_dev, deviceCenterFrequency ) != 0)
{
@ -335,7 +377,7 @@ bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force)
{
m_settings.m_fcPos = settings.m_fcPos;
if(m_dev != 0)
if (m_rtlSDRThread != 0)
{
m_rtlSDRThread->setFcPos((int) m_settings.m_fcPos);
qDebug() << "RTLSDRInput: set fc pos (enum) to " << (int) m_settings.m_fcPos;

Wyświetl plik

@ -49,26 +49,41 @@ public:
{ }
};
class MsgReportRTLSDR : public Message {
class MsgQueryRTLSDR : public Message {
MESSAGE_CLASS_DECLARATION
public:
const std::vector<int>& getGains() const { return m_gains; }
static MsgReportRTLSDR* create(const std::vector<int>& gains)
static MsgQueryRTLSDR* create()
{
return new MsgReportRTLSDR(gains);
return new MsgQueryRTLSDR();
}
protected:
std::vector<int> m_gains;
MsgReportRTLSDR(const std::vector<int>& gains) :
Message(),
m_gains(gains)
MsgQueryRTLSDR() :
Message()
{ }
};
class MsgReportRTLSDR : public Message {
MESSAGE_CLASS_DECLARATION
public:
const std::vector<int>& getGains() const { return m_gains; }
static MsgReportRTLSDR* create(const std::vector<int>& gains)
{
return new MsgReportRTLSDR(gains);
}
protected:
std::vector<int> m_gains;
MsgReportRTLSDR(const std::vector<int>& gains) :
Message(),
m_gains(gains)
{ }
};
RTLSDRInput(DeviceSourceAPI *deviceAPI);
virtual ~RTLSDRInput();
@ -81,6 +96,7 @@ public:
virtual bool handleMessage(const Message& message);
const std::vector<int>& getGains() const { return m_gains; }
void set_ds_mode(int on);
private:
@ -91,7 +107,10 @@ private:
RTLSDRThread* m_rtlSDRThread;
QString m_deviceDescription;
std::vector<int> m_gains;
bool m_running;
bool openDevice();
void closeDevice();
bool applySettings(const RTLSDRSettings& settings, bool force);
};

Wyświetl plik

@ -19,7 +19,7 @@
#include <errno.h>
#include "rtlsdrthread.h"
#include "../../../sdrbase/dsp/samplesinkfifo.h"
#include "dsp/samplesinkfifo.h"
#define FCD_BLOCKSIZE 16384
@ -30,7 +30,8 @@ RTLSDRThread::RTLSDRThread(rtlsdr_dev_t* dev, SampleSinkFifo* sampleFifo, QObjec
m_convertBuffer(FCD_BLOCKSIZE),
m_sampleFifo(sampleFifo),
m_samplerate(288000),
m_log2Decim(4)
m_log2Decim(4),
m_fcPos(0)
{
}