mirror of
https://github.com/f4exb/sdrangel.git
synced 2025-02-03 09:44:01 -05:00
Airspy: refactoring: device open close moved in the constructor and destructor respectively of the input object
This commit is contained in:
parent
0c972455da
commit
bb812b4b47
@ -38,7 +38,10 @@ AirspyGui::AirspyGui(DeviceSourceAPI *deviceAPI, QWidget* parent) :
|
||||
m_sampleSource(0),
|
||||
m_lastEngineState((DSPDeviceSourceEngine::State)-1)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
m_sampleSource = new AirspyInput(m_deviceAPI);
|
||||
m_deviceAPI->setSource(m_sampleSource);
|
||||
|
||||
ui->setupUi(this);
|
||||
ui->centerFrequency->setColorMapper(ColorMapper(ColorMapper::ReverseGold));
|
||||
ui->centerFrequency->setValueRange(7, 24000U, 1900000U);
|
||||
|
||||
@ -48,11 +51,9 @@ AirspyGui::AirspyGui(DeviceSourceAPI *deviceAPI, QWidget* parent) :
|
||||
|
||||
displaySettings();
|
||||
|
||||
m_sampleSource = new AirspyInput(m_deviceAPI);
|
||||
m_rates = ((AirspyInput*) m_sampleSource)->getSampleRates();
|
||||
displaySampleRates();
|
||||
connect(m_sampleSource->getOutputMessageQueueToGUI(), SIGNAL(messageEnqueued()), this, SLOT(handleSourceMessages()));
|
||||
m_deviceAPI->setSource(m_sampleSource);
|
||||
|
||||
char recFileNameCStr[30];
|
||||
sprintf(recFileNameCStr, "test_%d.sdriq", m_deviceAPI->getDeviceUID());
|
||||
@ -123,17 +124,18 @@ bool AirspyGui::deserialize(const QByteArray& data)
|
||||
|
||||
bool AirspyGui::handleMessage(const Message& message)
|
||||
{
|
||||
if (AirspyInput::MsgReportAirspy::match(message))
|
||||
{
|
||||
qDebug() << "AirspyGui::handleMessage: MsgReportAirspy";
|
||||
m_rates = ((AirspyInput::MsgReportAirspy&) message).getSampleRates();
|
||||
displaySampleRates();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
// if (AirspyInput::MsgReportAirspy::match(message))
|
||||
// {
|
||||
// qDebug() << "AirspyGui::handleMessage: MsgReportAirspy";
|
||||
// m_rates = ((AirspyInput::MsgReportAirspy&) message).getSampleRates();
|
||||
// displaySampleRates();
|
||||
// return true;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
}
|
||||
|
||||
void AirspyGui::handleDSPMessages()
|
||||
|
@ -28,99 +28,114 @@
|
||||
#include "airspythread.h"
|
||||
|
||||
MESSAGE_CLASS_DEFINITION(AirspyInput::MsgConfigureAirspy, Message)
|
||||
MESSAGE_CLASS_DEFINITION(AirspyInput::MsgReportAirspy, Message)
|
||||
//MESSAGE_CLASS_DEFINITION(AirspyInput::MsgReportAirspy, Message)
|
||||
|
||||
AirspyInput::AirspyInput(DeviceSourceAPI *deviceAPI) :
|
||||
m_deviceAPI(deviceAPI),
|
||||
m_settings(),
|
||||
m_dev(0),
|
||||
m_airspyThread(0),
|
||||
m_deviceDescription("Airspy")
|
||||
m_deviceDescription("Airspy"),
|
||||
m_running(false)
|
||||
{
|
||||
m_sampleRates.push_back(10000000);
|
||||
m_sampleRates.push_back(2500000);
|
||||
openDevice();
|
||||
}
|
||||
|
||||
AirspyInput::~AirspyInput()
|
||||
{
|
||||
stop();
|
||||
if (m_running) stop();
|
||||
closeDevice();
|
||||
}
|
||||
|
||||
bool AirspyInput::openDevice()
|
||||
{
|
||||
if (m_dev != 0)
|
||||
{
|
||||
closeDevice();
|
||||
}
|
||||
|
||||
airspy_error rc;
|
||||
|
||||
rc = (airspy_error) airspy_init();
|
||||
|
||||
if (rc != AIRSPY_SUCCESS)
|
||||
{
|
||||
qCritical("AirspyInput::start: failed to initiate Airspy library %s", airspy_error_name(rc));
|
||||
}
|
||||
|
||||
if (!m_sampleFifo.setSize(1<<19))
|
||||
{
|
||||
qCritical("AirspyInput::start: could not allocate SampleFifo");
|
||||
return false;
|
||||
}
|
||||
|
||||
int device = m_deviceAPI->getSampleSourceSequence();
|
||||
|
||||
if ((m_dev = open_airspy_from_sequence(device)) == 0)
|
||||
{
|
||||
qCritical("AirspyInput::start: could not open Airspy #%d", device);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef LIBAIRSPY_DYN_RATES
|
||||
uint32_t nbSampleRates;
|
||||
uint32_t *sampleRates;
|
||||
|
||||
airspy_get_samplerates(m_dev, &nbSampleRates, 0);
|
||||
|
||||
sampleRates = new uint32_t[nbSampleRates];
|
||||
|
||||
airspy_get_samplerates(m_dev, sampleRates, nbSampleRates);
|
||||
|
||||
if (nbSampleRates == 0)
|
||||
{
|
||||
qCritical("AirspyInput::start: could not obtain Airspy sample rates");
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug("AirspyInput::start: %d sample rates", nbSampleRates);
|
||||
}
|
||||
|
||||
m_sampleRates.clear();
|
||||
|
||||
for (int i=0; i<nbSampleRates; i++)
|
||||
{
|
||||
m_sampleRates.push_back(sampleRates[i]);
|
||||
qDebug("AirspyInput::start: sampleRates[%d] = %u Hz", i, sampleRates[i]);
|
||||
}
|
||||
|
||||
delete[] sampleRates;
|
||||
#else
|
||||
qDebug("AirspyInput::start: detault rates");
|
||||
m_sampleRates.clear();
|
||||
m_sampleRates.push_back(10000000);
|
||||
m_sampleRates.push_back(2500000);
|
||||
#endif
|
||||
|
||||
// MsgReportAirspy *message = MsgReportAirspy::create(m_sampleRates);
|
||||
// getOutputMessageQueueToGUI()->push(message);
|
||||
|
||||
rc = (airspy_error) airspy_set_sample_type(m_dev, AIRSPY_SAMPLE_INT16_IQ);
|
||||
|
||||
if (rc != AIRSPY_SUCCESS)
|
||||
{
|
||||
qCritical("AirspyInput::start: could not set sample type to INT16_IQ");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AirspyInput::start(int device)
|
||||
{
|
||||
QMutexLocker mutexLocker(&m_mutex);
|
||||
airspy_error rc;
|
||||
|
||||
rc = (airspy_error) airspy_init();
|
||||
if (!m_dev) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (rc != AIRSPY_SUCCESS)
|
||||
{
|
||||
qCritical("AirspyInput::start: failed to initiate Airspy library %s", airspy_error_name(rc));
|
||||
}
|
||||
|
||||
if (m_dev != 0)
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
if (!m_sampleFifo.setSize(1<<19))
|
||||
{
|
||||
qCritical("AirspyInput::start: could not allocate SampleFifo");
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((m_dev = open_airspy_from_sequence(device)) == 0)
|
||||
{
|
||||
qCritical("AirspyInput::start: could not open Airspy #%d", device);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef LIBAIRSPY_DYN_RATES
|
||||
uint32_t nbSampleRates;
|
||||
uint32_t *sampleRates;
|
||||
|
||||
airspy_get_samplerates(m_dev, &nbSampleRates, 0);
|
||||
|
||||
sampleRates = new uint32_t[nbSampleRates];
|
||||
|
||||
airspy_get_samplerates(m_dev, sampleRates, nbSampleRates);
|
||||
|
||||
if (nbSampleRates == 0)
|
||||
{
|
||||
qCritical("AirspyInput::start: could not obtain Airspy sample rates");
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug("AirspyInput::start: %d sample rates", nbSampleRates);
|
||||
}
|
||||
|
||||
m_sampleRates.clear();
|
||||
|
||||
for (int i=0; i<nbSampleRates; i++)
|
||||
{
|
||||
m_sampleRates.push_back(sampleRates[i]);
|
||||
qDebug("AirspyInput::start: sampleRates[%d] = %u Hz", i, sampleRates[i]);
|
||||
}
|
||||
|
||||
delete[] sampleRates;
|
||||
#else
|
||||
qDebug("AirspyInput::start: detault rates");
|
||||
m_sampleRates.clear();
|
||||
m_sampleRates.push_back(10000000);
|
||||
m_sampleRates.push_back(2500000);
|
||||
#endif
|
||||
|
||||
MsgReportAirspy *message = MsgReportAirspy::create(m_sampleRates);
|
||||
getOutputMessageQueueToGUI()->push(message);
|
||||
|
||||
rc = (airspy_error) airspy_set_sample_type(m_dev, AIRSPY_SAMPLE_INT16_IQ);
|
||||
|
||||
if (rc != AIRSPY_SUCCESS)
|
||||
{
|
||||
qCritical("AirspyInput::start: could not set sample type to INT16_IQ");
|
||||
return false;
|
||||
}
|
||||
if (m_running) stop();
|
||||
|
||||
if((m_airspyThread = new AirspyThread(m_dev, &m_sampleFifo)) == 0)
|
||||
{
|
||||
@ -136,30 +151,37 @@ bool AirspyInput::start(int device)
|
||||
applySettings(m_settings, true);
|
||||
|
||||
qDebug("AirspyInput::startInput: started");
|
||||
m_running = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void AirspyInput::closeDevice()
|
||||
{
|
||||
if (m_dev != 0)
|
||||
{
|
||||
airspy_stop_rx(m_dev);
|
||||
airspy_close(m_dev);
|
||||
m_dev = 0;
|
||||
}
|
||||
|
||||
m_deviceDescription.clear();
|
||||
airspy_exit();
|
||||
}
|
||||
|
||||
void AirspyInput::stop()
|
||||
{
|
||||
qDebug("AirspyInput::stop");
|
||||
QMutexLocker mutexLocker(&m_mutex);
|
||||
|
||||
if(m_airspyThread != 0)
|
||||
if (m_airspyThread != 0)
|
||||
{
|
||||
m_airspyThread->stopWork();
|
||||
delete m_airspyThread;
|
||||
m_airspyThread = 0;
|
||||
}
|
||||
|
||||
if(m_dev != 0)
|
||||
{
|
||||
airspy_stop_rx(m_dev);
|
||||
airspy_close(m_dev);
|
||||
m_dev = 0;
|
||||
}
|
||||
|
||||
airspy_exit();
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
const QString& AirspyInput::getDeviceDescription() const
|
||||
|
@ -48,25 +48,25 @@ public:
|
||||
{ }
|
||||
};
|
||||
|
||||
class MsgReportAirspy : public Message {
|
||||
MESSAGE_CLASS_DECLARATION
|
||||
|
||||
public:
|
||||
const std::vector<uint32_t>& getSampleRates() const { return m_sampleRates; }
|
||||
|
||||
static MsgReportAirspy* create(const std::vector<uint32_t>& sampleRates)
|
||||
{
|
||||
return new MsgReportAirspy(sampleRates);
|
||||
}
|
||||
|
||||
protected:
|
||||
std::vector<uint32_t> m_sampleRates;
|
||||
|
||||
MsgReportAirspy(const std::vector<uint32_t>& sampleRates) :
|
||||
Message(),
|
||||
m_sampleRates(sampleRates)
|
||||
{ }
|
||||
};
|
||||
// class MsgReportAirspy : public Message {
|
||||
// MESSAGE_CLASS_DECLARATION
|
||||
//
|
||||
// public:
|
||||
// const std::vector<uint32_t>& getSampleRates() const { return m_sampleRates; }
|
||||
//
|
||||
// static MsgReportAirspy* create(const std::vector<uint32_t>& sampleRates)
|
||||
// {
|
||||
// return new MsgReportAirspy(sampleRates);
|
||||
// }
|
||||
//
|
||||
// protected:
|
||||
// std::vector<uint32_t> m_sampleRates;
|
||||
//
|
||||
// MsgReportAirspy(const std::vector<uint32_t>& sampleRates) :
|
||||
// Message(),
|
||||
// m_sampleRates(sampleRates)
|
||||
// { }
|
||||
// };
|
||||
|
||||
AirspyInput(DeviceSourceAPI *deviceAPI);
|
||||
virtual ~AirspyInput();
|
||||
@ -82,6 +82,8 @@ public:
|
||||
virtual bool handleMessage(const Message& message);
|
||||
|
||||
private:
|
||||
bool openDevice();
|
||||
void closeDevice();
|
||||
bool applySettings(const AirspySettings& settings, bool force);
|
||||
struct airspy_device *open_airspy_from_sequence(int sequence);
|
||||
void setCenterFrequency(quint64 freq);
|
||||
@ -93,6 +95,7 @@ private:
|
||||
AirspyThread* m_airspyThread;
|
||||
QString m_deviceDescription;
|
||||
std::vector<uint32_t> m_sampleRates;
|
||||
bool m_running;
|
||||
};
|
||||
|
||||
#endif // INCLUDE_AIRSPYINPUT_H
|
||||
|
@ -44,7 +44,6 @@ RTLSDRInput::RTLSDRInput(DeviceSourceAPI *deviceAPI) :
|
||||
|
||||
RTLSDRInput::~RTLSDRInput()
|
||||
{
|
||||
//stop();
|
||||
if (m_running) stop();
|
||||
closeDevice();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user