summaryrefslogtreecommitdiffstats
path: root/plugins/samplesource/gnuradio/gnuradiogui.cpp
blob: 3291aa4fe132031156b2b4e3fec7dd2446bd6373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
// written by Christian Daniel                                                   //
// Copyright (C) 2013 by Dimitri Stolnikov <horiz0n@gmx.net>                     //
//                                                                               //
// 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/>.          //
///////////////////////////////////////////////////////////////////////////////////

#include "gnuradiogui.h"
#include "ui_gnuradiogui.h"

#include <osmosdr/device.h>
#include <iostream>
#include <plugin/pluginapi.h>

#include <QHBoxLayout>
#include <QLabel>
#include <QSlider>

GNURadioGui::GNURadioGui(PluginAPI* pluginAPI, QWidget* parent) :
	QWidget(parent),
	ui(new Ui::GNURadioGui),
	m_pluginAPI(pluginAPI),
	m_settings(),
	m_sampleSource(NULL)
{
	ui->setupUi(this);
	connect(&m_updateTimer, SIGNAL(timeout()), this, SLOT(updateHardware()));
	displaySettings();

	m_sampleSource = new GNURadioInput(m_pluginAPI->getMainWindowMessageQueue());
	m_pluginAPI->setSampleSource(m_sampleSource);
}

GNURadioGui::~GNURadioGui()
{
	delete ui;
}

void GNURadioGui::destroy()
{
	delete this;
}

void GNURadioGui::setName(const QString& name)
{
	setObjectName(name);
}

void GNURadioGui::resetToDefaults()
{
	m_generalSettings.resetToDefaults();
	m_settings.resetToDefaults();
	displaySettings();
	sendSettings();
}

QByteArray GNURadioGui::serializeGeneral() const
{
	return m_generalSettings.serialize();
}

bool GNURadioGui::deserializeGeneral(const QByteArray&data)
{
	if(m_generalSettings.deserialize(data)) {
		displaySettings();
		sendSettings();
		return true;
	} else {
		resetToDefaults();
		return false;
	}
}

quint64 GNURadioGui::getCenterFrequency() const
{
	return m_generalSettings.m_centerFrequency;
}

QByteArray GNURadioGui::serialize() const
{
	return m_settings.serialize();
}

bool GNURadioGui::deserialize(const QByteArray& data)
{
	if(m_settings.deserialize(data)) {
		displaySettings();
		sendSettings();
		return true;
	} else {
		resetToDefaults();
		return false;
	}
}

bool GNURadioGui::handleMessage(Message* message)
{
	if(message->id() == GNURadioInput::MsgReportGNURadio::ID()) {
		GNURadioInput::MsgReportGNURadio* rep = (GNURadioInput::MsgReportGNURadio*)message;
		m_namedGains = rep->getNamedGains();
		m_freqMin = rep->getFreqMin();
		m_freqMax = rep->getFreqMax();
		m_freqCorr = rep->getFreqCorr();
		m_sampRates = rep->getSampRates();
		m_antennas = rep->getAntennas();
		m_dcoffs = rep->getDCOffs();
		m_iqbals = rep->getIQBals();
		m_bandwidths = rep->getBandwidths();
		/* insert 0 which will become "Auto" in the combo box */
		m_bandwidths.insert(m_bandwidths.begin(), 0);
		displaySettings();
		return true;
	} else {
		return false;
	}
}

void GNURadioGui::displaySettings()
{
	int oldIndex = 0;

	oldIndex = ui->cboDevices->currentIndex();
	ui->cboDevices->clear();

	QString oldArgs = ui->txtDeviceArgs->text();

	osmosdr::devices_t devices = osmosdr::device::find();

	for ( int i = 0; i < devices.size(); i++ )
	{
		osmosdr::device_t dev = devices[i];

		QString label;

		if ( dev.count( "label" ) )
		{
			label = QString(dev[ "label" ].c_str());
			dev.erase("label");
		}

		QPair< QString, QString > pair(label, dev.to_string().c_str());
		m_devs.append(pair);

		ui->cboDevices->addItem(label);
	}

	if ( ui->cboDevices->count() && oldIndex >= 0 )
	{
		if ( oldIndex > ui->cboDevices->count() - 1 )
			oldIndex = 0;

		ui->cboDevices->setCurrentIndex(oldIndex);

		if ( oldArgs.length() == 0 )
			ui->txtDeviceArgs->setText( m_devs[oldIndex].second );
	}

	if ( oldArgs.length() )
		ui->txtDeviceArgs->setText( oldArgs );

	ui->centerFrequency->setValueRange(7,
					   unsigned(m_freqMin / 1000.0),
					   unsigned(m_freqMax / 1000.0));

	ui->centerFrequency->setValue(m_generalSettings.m_centerFrequency / 1000);

	ui->sldFreqCorr->setRange(-100, +100);
	ui->sldFreqCorr->setValue( m_freqCorr );
	ui->lblFreqCorr->setText(tr("%1").arg(ui->sldFreqCorr->value()));

	m_gainControls.clear();
	QVBoxLayout *layoutGains = ui->verticalLayoutGains;
	QLayoutItem *layoutItem;

	while ( ( layoutItem = layoutGains->takeAt( 0 ) ) != NULL )
	{
		QLayout *layout = layoutItem->layout();

		if ( !layout )
			continue;

		while ( ( layoutItem = layout->takeAt( 0 ) ) != NULL )
		{
			delete layoutItem->widget();
			delete layoutItem;
		}

		delete layout;
	}

	for ( int i = 0; i < m_namedGains.size(); i++ )
	{
		std::pair< QString, std::vector<double> > pair = m_namedGains[i];

		QHBoxLayout *layout = new QHBoxLayout();
		QLabel *gainName = new QLabel( pair.first + " Gain" );
		QSlider *gainSlider = new QSlider(Qt::Horizontal);
		QLabel *gainLabel = new QLabel("0");
		gainLabel->setMinimumWidth(30);
		gainLabel->setAlignment(Qt::AlignHCenter | Qt::AlignHCenter);

		QPair< QSlider*, QLabel* > pair2( gainSlider, gainLabel );
		m_gainControls.push_back( pair2 );

		connect(gainSlider, SIGNAL(valueChanged(int)),
			this, SLOT(on_sldGain_valueChanged(int)));

		layout->addWidget(gainName);
		layout->addWidget(gainSlider);
		layout->addWidget(gainLabel);

		layoutGains->addLayout(layout);

		std::vector<double> gain_values = pair.second;

		if ( gain_values.size() ) {
			gainSlider->setRange(0, gain_values.size() - 1);
			gainSlider->setValue(gain_values.size() / 4);
			gainSlider->setEnabled(true);
		} else {
			gainSlider->setEnabled(false);
		}
	}

	oldIndex = ui->cboSampleRate->currentIndex();
	ui->cboSampleRate->clear();

	for ( int i = 0; i < m_sampRates.size(); i++ )
		ui->cboSampleRate->addItem( QString::number(m_sampRates[i] / 1e6) );

	if ( oldIndex > ui->cboSampleRate->count() - 1 )
		oldIndex = 0;

	if ( ui->cboSampleRate->count() && oldIndex >= 0 )
		ui->cboSampleRate->setCurrentIndex(oldIndex);

	if ( ui->cboSampleRate->count() ) {
		ui->cboSampleRate->setEnabled(true);
	} else {
		ui->cboSampleRate->setEnabled(false);
	}

	oldIndex = ui->cboAntennas->currentIndex();
	ui->cboAntennas->clear();

	if ( m_antennas.size() ) {
		for ( int i = 0; i < m_antennas.size(); i++ )
			ui->cboAntennas->addItem( m_antennas[i] );

		if ( oldIndex > ui->cboAntennas->count() - 1 )
			oldIndex = 0;

		if ( ui->cboAntennas->count() && oldIndex >= 0 )
			ui->cboAntennas->setCurrentIndex(oldIndex);

		ui->cboAntennas->setEnabled(true);
	} else {
		ui->cboAntennas->setEnabled(false);
	}

	oldIndex = ui->cboDCOffset->currentIndex();
	ui->cboDCOffset->clear();

	if ( m_dcoffs.size() ) {
		for ( int i = 0; i < m_dcoffs.size(); i++ )
			ui->cboDCOffset->addItem( m_dcoffs[i] );

		if ( ui->cboDCOffset->count() && oldIndex >= 0 )
			ui->cboDCOffset->setCurrentIndex(oldIndex);

		ui->cboDCOffset->setEnabled(true);
	} else {
		ui->cboDCOffset->setEnabled(false);
	}

	oldIndex = ui->cboIQBalance->currentIndex();
	ui->cboIQBalance->clear();

	if ( m_iqbals.size() ) {
		for ( int i = 0; i < m_iqbals.size(); i++ )
			ui->cboIQBalance->addItem( m_iqbals[i] );

		if ( ui->cboIQBalance->count() && oldIndex >= 0 )
			ui->cboIQBalance->setCurrentIndex(oldIndex);

		ui->cboIQBalance->setEnabled(true);
	} else {
		ui->cboIQBalance->setEnabled(false);
	}

	oldIndex = ui->cboBandwidth->currentIndex();
	ui->cboBandwidth->clear();

	for ( int i = 0; i < m_bandwidths.size(); i++ )
		if ( 0.0 == m_bandwidths[i] )
			ui->cboBandwidth->addItem( "Auto" );
		else
			ui->cboBandwidth->addItem( QString::number(m_bandwidths[i] / 1e6) );

	if ( oldIndex > ui->cboBandwidth->count() - 1 )
		oldIndex = 0;

	if ( ui->cboBandwidth->count() && oldIndex >= 0 )
		ui->cboBandwidth->setCurrentIndex(oldIndex);

	if ( ui->cboBandwidth->count() ) {
		ui->cboBandwidth->setEnabled(true);
	} else {
		ui->cboBandwidth->setEnabled(false);
	}
}

void GNURadioGui::sendSettings()
{
	if(!m_updateTimer.isActive())
		m_updateTimer.start(100);
}

void GNURadioGui::updateHardware()
{
	m_updateTimer.stop();
	GNURadioInput::MsgConfigureGNURadio* msg = GNURadioInput::MsgConfigureGNURadio::create(m_generalSettings, m_settings);
	msg->submit(m_pluginAPI->getDSPEngineMessageQueue());
}

void GNURadioGui::on_cboDevices_currentIndexChanged(int index)
{
	if ( index < 0 || index >= m_devs.count() )
		return;

	ui->txtDeviceArgs->setText( m_devs[index].second );
}

void GNURadioGui::on_txtDeviceArgs_textChanged(const QString &arg1)
{
	m_settings.m_args = arg1;
	sendSettings();
}

void GNURadioGui::on_centerFrequency_changed(quint64 value)
{
	m_generalSettings.m_centerFrequency = value * 1000;
	sendSettings();
}

void GNURadioGui::on_sldFreqCorr_valueChanged(int value)
{
	ui->lblFreqCorr->setText(tr("%1").arg(value));
	m_settings.m_freqCorr = value;
	sendSettings();
}

void GNURadioGui::on_sldGain_valueChanged(int value)
{
	m_settings.m_namedGains.clear();

	for ( int i = 0; i < m_gainControls.size(); i++ )
	{
		QPair< QSlider*, QLabel* > controls = m_gainControls[i];

		QSlider *slider = controls.first;
		QLabel *label = controls.second;

		std::pair< QString, std::vector<double> > named_gain = m_namedGains[ i ];

		int index = slider->value();
		double gain = named_gain.second[index];
		label->setText(tr("%1").arg(gain));

		QPair< QString, double > named_gain2( named_gain.first, gain );
		m_settings.m_namedGains.push_back( named_gain2 );
	}

	sendSettings();
}

void GNURadioGui::on_cboSampleRate_currentIndexChanged(int index)
{
	if ( index < 0 || index >= m_sampRates.size() )
		return;

	m_settings.m_sampRate = m_sampRates[index];
	sendSettings();
}

void GNURadioGui::on_cboAntennas_currentIndexChanged(const QString &arg1)
{
	m_settings.m_antenna = arg1;
	sendSettings();
}

void GNURadioGui::on_cboDCOffset_currentIndexChanged(const QString &arg1)
{
	m_settings.m_dcoff = arg1;
	sendSettings();
}

void GNURadioGui::on_cboIQBalance_currentIndexChanged(const QString &arg1)
{
	m_settings.m_iqbal = arg1;
	sendSettings();
}

void GNURadioGui::on_cboBandwidth_currentIndexChanged(int index)
{
	if ( index < 0 || index >= m_bandwidths.size() )
		return;

	m_settings.m_bandwidth = m_bandwidths[index];
	sendSettings();
}