1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-09 01:56:05 -05:00
sdrangel/plugins/samplesource/fcd/fcdinput.cpp

251 lines
5.6 KiB
C++
Raw Normal View History

2014-11-29 13:09:35 -05:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
// written by Christian Daniel //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
// FIXME: FCD is handled very badly!
#include <QDebug>
2014-11-29 13:09:35 -05:00
#include <string.h>
#include <errno.h>
#include "fcdinput.h"
#include "fcdthread.h"
#include "fcdgui.h"
2015-08-17 02:29:34 -04:00
#include "qthid.h"
#include "dsp/dspcommands.h"
#include "fcdserializer.h"
2014-11-29 13:09:35 -05:00
MESSAGE_CLASS_DEFINITION(FCDInput::MsgConfigureFCD, Message)
//MESSAGE_CLASS_DEFINITION(FCDInput::MsgReportFCD, Message)
2014-12-02 09:23:21 -05:00
FCDInput::Settings::Settings() :
2015-08-17 02:29:34 -04:00
centerFrequency(435000000),
2014-12-02 09:23:21 -05:00
range(0),
gain(0),
bias(0)
2014-11-29 13:09:35 -05:00
{
}
void FCDInput::Settings::resetToDefaults()
{
2015-08-17 02:29:34 -04:00
centerFrequency = 435000000;
2014-12-02 09:23:21 -05:00
range = 0;
gain = 0;
bias = 0;
2014-11-29 13:09:35 -05:00
}
QByteArray FCDInput::Settings::serialize() const
{
FCDSerializer::FCDData data;
data.m_data.m_lnaGain = gain;
data.m_data.m_frequency = centerFrequency;
data.m_range = range;
data.m_bias = bias;
QByteArray byteArray;
FCDSerializer::writeSerializedData(data, byteArray);
return byteArray;
/*
2014-11-29 13:09:35 -05:00
SimpleSerializer s(1);
2015-08-17 02:29:34 -04:00
s.writeU64(1, centerFrequency);
s.writeS32(2, range);
s.writeS32(3, gain);
s.writeS32(4, bias);
return s.final();*/
2014-11-29 13:09:35 -05:00
}
bool FCDInput::Settings::deserialize(const QByteArray& serializedData)
2014-11-29 13:09:35 -05:00
{
FCDSerializer::FCDData data;
bool valid = FCDSerializer::readSerializedData(serializedData, data);
gain = data.m_data.m_lnaGain;
centerFrequency = data.m_data.m_frequency;
range = data.m_range;
bias = data.m_bias;
return valid;
/*
2014-12-02 09:23:21 -05:00
SimpleDeserializer d(data);
2015-08-17 02:29:34 -04:00
if (d.isValid() && d.getVersion() == 1)
{
d.readU64(1, &centerFrequency, 435000000);
d.readS32(2, &range, 0);
d.readS32(3, &gain, 0);
d.readS32(4, &bias, 0);
2014-12-02 09:23:21 -05:00
return true;
}
2015-08-17 02:29:34 -04:00
2014-11-29 13:09:35 -05:00
resetToDefaults();
return true;*/
2014-11-29 13:09:35 -05:00
}
2015-08-17 02:29:34 -04:00
FCDInput::FCDInput() :
2014-11-29 13:09:35 -05:00
m_settings(),
2015-08-17 02:29:34 -04:00
m_FCDThread(0),
2014-11-29 13:09:35 -05:00
m_deviceDescription()
{
}
FCDInput::~FCDInput()
{
2015-08-17 02:29:34 -04:00
stop();
}
bool FCDInput::init(const Message& cmd)
{
return false;
2014-11-29 13:09:35 -05:00
}
2015-08-17 02:29:34 -04:00
bool FCDInput::start(int device)
2014-11-29 13:09:35 -05:00
{
QMutexLocker mutexLocker(&m_mutex);
2015-08-17 02:29:34 -04:00
if (m_FCDThread)
{
2014-11-29 13:09:35 -05:00
return false;
2015-08-17 02:29:34 -04:00
}
2014-12-10 13:24:01 -05:00
/* Apply settings before streaming to avoid bus contention;
* there is very little spare bandwidth on a full speed USB device.
* Failure is harmless if no device is found */
2014-11-29 13:09:35 -05:00
2015-08-17 02:29:34 -04:00
applySettings(m_settings, true);
if(!m_sampleFifo.setSize(96000*4))
2015-08-17 02:29:34 -04:00
{
2014-11-29 13:09:35 -05:00
qCritical("Could not allocate SampleFifo");
return false;
}
2015-08-17 02:29:34 -04:00
if ((m_FCDThread = new FCDThread(&m_sampleFifo)) == NULL)
{
2014-11-29 13:09:35 -05:00
qFatal("out of memory");
return false;
}
m_deviceDescription = QString("Funcube Dongle");
2015-08-17 02:29:34 -04:00
qDebug("FCDInput::start");
2014-11-29 13:09:35 -05:00
return true;
}
2015-08-17 02:29:34 -04:00
void FCDInput::stop()
2014-11-29 13:09:35 -05:00
{
QMutexLocker mutexLocker(&m_mutex);
2015-08-17 02:29:34 -04:00
if (m_FCDThread)
{
2014-11-29 13:09:35 -05:00
m_FCDThread->stopWork();
// wait for thread to quit ?
delete m_FCDThread;
2015-08-17 02:29:34 -04:00
m_FCDThread = 0;
2014-11-29 13:09:35 -05:00
}
2015-08-17 02:29:34 -04:00
2014-11-29 13:09:35 -05:00
m_deviceDescription.clear();
}
const QString& FCDInput::getDeviceDescription() const
{
return m_deviceDescription;
}
int FCDInput::getSampleRate() const
{
2015-08-17 02:29:34 -04:00
return 96000;
2014-11-29 13:09:35 -05:00
}
quint64 FCDInput::getCenterFrequency() const
{
2015-08-17 02:29:34 -04:00
return m_settings.centerFrequency;
2014-11-29 13:09:35 -05:00
}
2015-08-17 02:29:34 -04:00
bool FCDInput::handleMessage(const Message& message)
2014-11-29 13:09:35 -05:00
{
2015-08-17 02:29:34 -04:00
if(MsgConfigureFCD::match(message))
{
qDebug() << "FCDInput::handleMessage: MsgConfigureFCD";
2015-08-17 02:29:34 -04:00
MsgConfigureFCD& conf = (MsgConfigureFCD&) message;
applySettings(conf.getSettings(), false);
2014-11-29 13:09:35 -05:00
return true;
2015-08-17 02:29:34 -04:00
}
else
{
2014-11-29 13:09:35 -05:00
return false;
}
}
2015-08-17 02:29:34 -04:00
void FCDInput::applySettings(const Settings& settings, bool force)
2014-11-29 13:09:35 -05:00
{
bool signalChange = false;
2014-12-02 09:23:21 -05:00
2015-08-17 02:29:34 -04:00
if ((m_settings.centerFrequency != settings.centerFrequency))
{
qDebug() << "FCDInput::applySettings: fc: " << settings.centerFrequency;
2015-08-17 02:29:34 -04:00
m_settings.centerFrequency = settings.centerFrequency;
set_center_freq((double) m_settings.centerFrequency);
signalChange = true;
2014-12-02 10:39:37 -05:00
}
if ((m_settings.gain != settings.gain) || force)
2015-08-17 02:29:34 -04:00
{
set_lna_gain(settings.gain > 0);
m_settings.gain = settings.gain;
}
if ((m_settings.bias != settings.bias) || force)
{
2015-08-17 02:29:34 -04:00
set_bias_t(settings.bias > 0);
m_settings.bias = settings.bias;
}
if (signalChange)
{
DSPSignalNotification *notif = new DSPSignalNotification(96000, m_settings.centerFrequency);
getOutputMessageQueue()->push(notif);
}
2015-08-17 02:29:34 -04:00
}
void FCDInput::set_center_freq(double freq)
{
if (fcdAppSetFreq(freq) == FCD_MODE_NONE)
{
qDebug("No FCD HID found for frquency change");
}
}
void FCDInput::set_bias_t(bool on)
{
quint8 cmd = on ? 1 : 0;
fcdAppSetParam(FCD_CMD_APP_SET_BIAS_TEE, &cmd, 1);
}
void FCDInput::set_lna_gain(bool on)
{
quint8 cmd = on ? 1 : 0;
2014-12-02 14:00:12 -05:00
2015-08-17 02:29:34 -04:00
fcdAppSetParam(FCD_CMD_APP_SET_LNA_GAIN, &cmd, 1);
2014-11-29 13:09:35 -05:00
}
2014-12-02 09:23:21 -05:00
2014-12-02 14:00:12 -05:00