1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-26 17:58:43 -05:00
sdrangel/plugins/channelrx/demodvor/vordemodgui.cpp

479 lines
16 KiB
C++
Raw Normal View History

2020-11-27 22:10:27 -05:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2016 Edouard Griffiths, F4EXB //
// Copyright (C) 2020 Jon Beniston, M7RCE //
// //
// 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 //
// (at your option) any later version. //
// //
// 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 <limits>
#include <QDebug>
2020-11-27 22:10:27 -05:00
#include "device/deviceuiset.h"
#include "dsp/dspengine.h"
#include "dsp/dspcommands.h"
#include "dsp/dspengine.h"
2020-11-27 22:10:27 -05:00
#include "plugin/pluginapi.h"
#include "util/simpleserializer.h"
#include "util/db.h"
#include "util/morse.h"
#include "util/units.h"
#include "gui/basicchannelsettingsdialog.h"
#include "gui/devicestreamselectiondialog.h"
#include "gui/crightclickenabler.h"
#include "gui/audioselectdialog.h"
#include "gui/dialpopup.h"
#include "gui/dialogpositioner.h"
2020-11-27 22:10:27 -05:00
#include "channel/channelwebapiutils.h"
#include "maincore.h"
#include "ui_vordemodgui.h"
#include "vordemod.h"
#include "vordemodreport.h"
#include "vordemodgui.h"
2020-11-27 22:10:27 -05:00
VORDemodGUI* VORDemodGUI::create(PluginAPI* pluginAPI, DeviceUISet *deviceUISet, BasebandSampleSink *rxChannel)
2020-11-27 22:10:27 -05:00
{
VORDemodGUI* gui = new VORDemodGUI(pluginAPI, deviceUISet, rxChannel);
2020-11-27 22:10:27 -05:00
return gui;
}
void VORDemodGUI::destroy()
2020-11-27 22:10:27 -05:00
{
delete this;
}
void VORDemodGUI::resetToDefaults()
2020-11-27 22:10:27 -05:00
{
m_settings.resetToDefaults();
displaySettings();
applySettings(true);
}
QByteArray VORDemodGUI::serialize() const
2020-11-27 22:10:27 -05:00
{
return m_settings.serialize();
}
bool VORDemodGUI::deserialize(const QByteArray& data)
2020-11-27 22:10:27 -05:00
{
if(m_settings.deserialize(data)) {
displaySettings();
applySettings(true);
return true;
} else {
resetToDefaults();
return false;
}
}
bool VORDemodGUI::handleMessage(const Message& message)
2020-11-27 22:10:27 -05:00
{
if (VORDemod::MsgConfigureVORDemod::match(message))
2020-11-27 22:10:27 -05:00
{
qDebug("VORDemodGUI::handleMessage: VORDemod::MsgConfigureVORDemod");
const VORDemod::MsgConfigureVORDemod& cfg = (VORDemod::MsgConfigureVORDemod&) message;
2020-11-27 22:10:27 -05:00
m_settings = cfg.getSettings();
blockApplySettings(true);
displaySettings();
blockApplySettings(false);
return true;
}
else if (DSPSignalNotification::match(message))
{
DSPSignalNotification& notif = (DSPSignalNotification&) message;
m_deviceCenterFrequency = notif.getCenterFrequency();
2020-11-27 22:10:27 -05:00
m_basebandSampleRate = notif.getSampleRate();
ui->deltaFrequency->setValueRange(false, 7, -m_basebandSampleRate/2, m_basebandSampleRate/2);
ui->deltaFrequencyLabel->setToolTip(tr("Range %1 %L2 Hz").arg(QChar(0xB1)).arg(m_basebandSampleRate/2));
updateAbsoluteCenterFrequency();
2020-11-27 22:10:27 -05:00
return true;
}
else if (VORDemodReport::MsgReportRadial::match(message))
2020-11-27 22:10:27 -05:00
{
VORDemodReport::MsgReportRadial& report = (VORDemodReport::MsgReportRadial&) message;
2020-11-27 22:10:27 -05:00
// Display radial and signal magnitudes
2020-11-27 22:10:27 -05:00
Real varMagDB = std::round(20.0*std::log10(report.getVarMag()));
Real refMagDB = std::round(20.0*std::log10(report.getRefMag()));
bool validRadial = (refMagDB > m_settings.m_refThresholdDB) && (varMagDB > m_settings.m_varThresholdDB);
ui->radialText->setText(tr("%1°").arg(std::round(report.getRadial())));
2020-11-27 22:10:27 -05:00
if (validRadial) {
ui->radialText->setStyleSheet("QLabel { color: white }");
} else {
ui->radialText->setStyleSheet("QLabel { color: red }");
}
2020-11-27 22:10:27 -05:00
ui->refText->setText(tr("%1 dB").arg(refMagDB));
2020-11-27 22:10:27 -05:00
if (refMagDB > m_settings.m_refThresholdDB) {
ui->refText->setStyleSheet("QLabel { color: white }");
} else {
ui->refText->setStyleSheet("QLabel { color: red }");
}
2020-11-27 22:10:27 -05:00
ui->varText->setText(tr("%1 dB").arg(varMagDB));
2020-11-27 22:10:27 -05:00
if (varMagDB > m_settings.m_varThresholdDB) {
ui->varText->setStyleSheet("QLabel { color: white }");
} else {
ui->varText->setStyleSheet("QLabel { color: red }");
2020-11-27 22:10:27 -05:00
}
return true;
2020-11-27 22:10:27 -05:00
}
else if (MorseDemod::MsgReportIdent::match(message))
2020-11-27 22:10:27 -05:00
{
MorseDemod::MsgReportIdent& report = (MorseDemod::MsgReportIdent&) message;
2020-11-27 22:10:27 -05:00
QString ident = report.getIdent();
QString identString = Morse::toString(ident); // Convert Morse to a string
ui->identText->setText(identString);
ui->morseText->setText(Morse::toSpacedUnicode(ident));
2020-11-27 22:10:27 -05:00
// Idents should only be two or three characters, so filter anything else
// other than TEST which indicates a VOR is under maintainance (may also be TST)
if (((identString.size() >= 2) && (identString.size() <= 3)) || (identString == "TEST"))
{
ui->identText->setStyleSheet("QLabel { color: white }");
ui->morseText->setStyleSheet("QLabel { color: white }");
2020-11-27 22:10:27 -05:00
}
else
{
ui->identText->setStyleSheet("QLabel { color: yellow }");
ui->morseText->setStyleSheet("QLabel { color: yellow }");
2020-11-27 22:10:27 -05:00
}
2020-11-27 22:10:27 -05:00
return true;
}
return false;
}
void VORDemodGUI::handleInputMessages()
2020-11-27 22:10:27 -05:00
{
Message* message;
while ((message = getInputMessageQueue()->pop()) != 0)
{
if (handleMessage(*message))
{
delete message;
}
}
}
void VORDemodGUI::channelMarkerChangedByCursor()
2020-11-27 22:10:27 -05:00
{
ui->deltaFrequency->setValue(m_channelMarker.getCenterFrequency());
m_settings.m_inputFrequencyOffset = m_channelMarker.getCenterFrequency();
applySettings();
2020-11-27 22:10:27 -05:00
}
void VORDemodGUI::channelMarkerHighlightedByCursor()
2020-11-27 22:10:27 -05:00
{
setHighlighted(m_channelMarker.getHighlighted());
2020-11-27 22:10:27 -05:00
}
void VORDemodGUI::on_deltaFrequency_changed(qint64 value)
{
m_channelMarker.setCenterFrequency(value);
m_settings.m_inputFrequencyOffset = m_channelMarker.getCenterFrequency();
updateAbsoluteCenterFrequency();
applySettings();
}
void VORDemodGUI::on_thresh_valueChanged(int value)
2020-11-27 22:10:27 -05:00
{
ui->threshText->setText(QString("%1").arg(value / 10.0, 0, 'f', 1));
m_settings.m_identThreshold = value / 10.0;
applySettings();
}
void VORDemodGUI::on_volume_valueChanged(int value)
2020-11-27 22:10:27 -05:00
{
ui->volumeText->setText(QString("%1").arg(value / 10.0, 0, 'f', 1));
m_settings.m_volume = value / 10.0;
applySettings();
}
void VORDemodGUI::on_squelch_valueChanged(int value)
2020-11-27 22:10:27 -05:00
{
ui->squelchText->setText(QString("%1 dB").arg(value));
m_settings.m_squelch = value;
applySettings();
}
void VORDemodGUI::on_audioMute_toggled(bool checked)
2020-11-27 22:10:27 -05:00
{
m_settings.m_audioMute = checked;
applySettings();
}
void VORDemodGUI::on_identBandpassEnable_toggled(bool checked)
{
m_settings.m_identBandpassEnable = checked;
applySettings();
}
void VORDemodGUI::onWidgetRolled(QWidget* widget, bool rollDown)
2020-11-27 22:10:27 -05:00
{
(void) widget;
(void) rollDown;
getRollupContents()->saveState(m_rollupState);
applySettings();
2020-11-27 22:10:27 -05:00
}
void VORDemodGUI::onMenuDialogCalled(const QPoint &p)
2020-11-27 22:10:27 -05:00
{
if (m_contextMenuType == ContextMenuChannelSettings)
{
BasicChannelSettingsDialog dialog(&m_channelMarker, this);
dialog.setUseReverseAPI(m_settings.m_useReverseAPI);
dialog.setReverseAPIAddress(m_settings.m_reverseAPIAddress);
dialog.setReverseAPIPort(m_settings.m_reverseAPIPort);
dialog.setReverseAPIDeviceIndex(m_settings.m_reverseAPIDeviceIndex);
dialog.setReverseAPIChannelIndex(m_settings.m_reverseAPIChannelIndex);
dialog.setDefaultTitle(m_displayedName);
if (m_deviceUISet->m_deviceMIMOEngine)
{
dialog.setNumberOfStreams(m_vorDemod->getNumberOfDeviceStreams());
dialog.setStreamIndex(m_settings.m_streamIndex);
}
2020-11-27 22:10:27 -05:00
dialog.move(p);
new DialogPositioner(&dialog, false);
2020-11-27 22:10:27 -05:00
dialog.exec();
m_settings.m_rgbColor = m_channelMarker.getColor().rgb();
m_settings.m_title = m_channelMarker.getTitle();
m_settings.m_useReverseAPI = dialog.useReverseAPI();
m_settings.m_reverseAPIAddress = dialog.getReverseAPIAddress();
m_settings.m_reverseAPIPort = dialog.getReverseAPIPort();
m_settings.m_reverseAPIDeviceIndex = dialog.getReverseAPIDeviceIndex();
m_settings.m_reverseAPIChannelIndex = dialog.getReverseAPIChannelIndex();
setWindowTitle(m_settings.m_title);
setTitle(m_channelMarker.getTitle());
2020-11-27 22:10:27 -05:00
setTitleColor(m_settings.m_rgbColor);
if (m_deviceUISet->m_deviceMIMOEngine)
{
m_settings.m_streamIndex = dialog.getSelectedStreamIndex();
m_channelMarker.clearStreamIndexes();
m_channelMarker.addStreamIndex(m_settings.m_streamIndex);
updateIndexLabel();
}
2020-11-27 22:10:27 -05:00
applySettings();
}
resetContextMenuType();
}
VORDemodGUI::VORDemodGUI(PluginAPI* pluginAPI, DeviceUISet *deviceUISet, BasebandSampleSink *rxChannel, QWidget* parent) :
2020-11-27 22:10:27 -05:00
ChannelGUI(parent),
ui(new Ui::VORDemodGUI),
2020-11-27 22:10:27 -05:00
m_pluginAPI(pluginAPI),
m_deviceUISet(deviceUISet),
m_channelMarker(this),
m_deviceCenterFrequency(0),
2020-11-27 22:10:27 -05:00
m_doApplySettings(true),
m_squelchOpen(false),
m_tickCount(0)
2020-11-27 22:10:27 -05:00
{
setAttribute(Qt::WA_DeleteOnClose, true);
m_helpURL = "plugins/channelrx/demodvorsc/readme.md";
RollupContents *rollupContents = getRollupContents();
ui->setupUi(rollupContents);
setSizePolicy(rollupContents->sizePolicy());
rollupContents->arrangeRollups();
connect(rollupContents, SIGNAL(widgetRolled(QWidget*,bool)), this, SLOT(onWidgetRolled(QWidget*,bool)));
2020-11-27 22:10:27 -05:00
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onMenuDialogCalled(const QPoint &)));
m_vorDemod = reinterpret_cast<VORDemod*>(rxChannel);
2020-11-27 22:10:27 -05:00
m_vorDemod->setMessageQueueToGUI(getInputMessageQueue());
connect(&MainCore::instance()->getMasterTimer(), SIGNAL(timeout()), this, SLOT(tick())); // 50 ms
CRightClickEnabler *audioMuteRightClickEnabler = new CRightClickEnabler(ui->audioMute);
connect(audioMuteRightClickEnabler, SIGNAL(rightClick(const QPoint &)), this, SLOT(audioSelect(const QPoint &)));
2020-11-27 22:10:27 -05:00
ui->deltaFrequencyLabel->setText(QString("%1f").arg(QChar(0x94, 0x03)));
ui->deltaFrequency->setColorMapper(ColorMapper(ColorMapper::GrayGold));
ui->deltaFrequency->setValueRange(false, 7, -9999999, 9999999);
2020-11-27 22:10:27 -05:00
ui->channelPowerMeter->setColorTheme(LevelMeterSignalDB::ColorGreenAndBlue);
m_channelMarker.blockSignals(true);
m_channelMarker.setColor(Qt::yellow);
m_channelMarker.setBandwidth(2*VORDemodSettings::VORDEMOD_CHANNEL_BANDWIDTH);
m_channelMarker.setCenterFrequency(0);
2020-11-27 22:10:27 -05:00
m_channelMarker.setTitle("VOR Demodulator");
m_channelMarker.blockSignals(false);
m_channelMarker.setVisible(true); // activate signal on the last setting only
setTitleColor(m_channelMarker.getColor());
m_settings.setChannelMarker(&m_channelMarker);
m_settings.setRollupState(&m_rollupState);
2020-11-27 22:10:27 -05:00
m_deviceUISet->addChannelMarker(&m_channelMarker);
connect(&m_channelMarker, SIGNAL(changedByCursor()), this, SLOT(channelMarkerChangedByCursor()));
connect(&m_channelMarker, SIGNAL(highlightedByCursor()), this, SLOT(channelMarkerHighlightedByCursor()));
connect(getInputMessageQueue(), SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()));
displaySettings();
makeUIConnections();
2020-11-27 22:10:27 -05:00
applySettings(true);
DialPopup::addPopupsToChildDials(this);
2020-11-27 22:10:27 -05:00
}
VORDemodGUI::~VORDemodGUI()
2020-11-27 22:10:27 -05:00
{
delete ui;
}
void VORDemodGUI::blockApplySettings(bool block)
2020-11-27 22:10:27 -05:00
{
m_doApplySettings = !block;
}
void VORDemodGUI::applySettings(bool force)
2020-11-27 22:10:27 -05:00
{
if (m_doApplySettings)
{
VORDemod::MsgConfigureVORDemod* message = VORDemod::MsgConfigureVORDemod::create( m_settings, force);
2020-11-27 22:10:27 -05:00
m_vorDemod->getInputMessageQueue()->push(message);
}
}
void VORDemodGUI::displaySettings()
2020-11-27 22:10:27 -05:00
{
m_channelMarker.blockSignals(true);
m_channelMarker.setCenterFrequency(m_settings.m_inputFrequencyOffset);
m_channelMarker.setBandwidth(2*VORDemodSettings::VORDEMOD_CHANNEL_BANDWIDTH);
2020-11-27 22:10:27 -05:00
m_channelMarker.setTitle(m_settings.m_title);
m_channelMarker.blockSignals(false);
m_channelMarker.setColor(m_settings.m_rgbColor); // activate signal on the last setting only
setTitleColor(m_settings.m_rgbColor);
setWindowTitle(m_channelMarker.getTitle());
setTitle(m_channelMarker.getTitle());
2020-11-27 22:10:27 -05:00
blockApplySettings(true);
ui->deltaFrequency->setValue(m_channelMarker.getCenterFrequency());
2020-11-27 22:10:27 -05:00
ui->thresh->setValue(m_settings.m_identThreshold * 10.0);
ui->threshText->setText(QString("%1").arg(m_settings.m_identThreshold, 0, 'f', 1));
ui->volume->setValue(m_settings.m_volume * 10.0);
ui->volumeText->setText(QString("%1").arg(m_settings.m_volume, 0, 'f', 1));
ui->squelch->setValue(m_settings.m_squelch);
ui->squelchText->setText(QString("%1 dB").arg(m_settings.m_squelch));
ui->audioMute->setChecked(m_settings.m_audioMute);
ui->identBandpassEnable->setChecked(m_settings.m_identBandpassEnable);
2020-11-27 22:10:27 -05:00
updateIndexLabel();
2020-11-27 22:10:27 -05:00
getRollupContents()->restoreState(m_rollupState);
updateAbsoluteCenterFrequency();
2020-11-27 22:10:27 -05:00
blockApplySettings(false);
}
void VORDemodGUI::leaveEvent(QEvent* event)
2020-11-27 22:10:27 -05:00
{
m_channelMarker.setHighlighted(false);
ChannelGUI::leaveEvent(event);
2020-11-27 22:10:27 -05:00
}
void VORDemodGUI::enterEvent(EnterEventType* event)
2020-11-27 22:10:27 -05:00
{
m_channelMarker.setHighlighted(true);
ChannelGUI::enterEvent(event);
2020-11-27 22:10:27 -05:00
}
void VORDemodGUI::audioSelect(const QPoint& p)
2020-11-27 22:10:27 -05:00
{
qDebug("VORDemodGUI::audioSelect");
2020-11-27 22:10:27 -05:00
AudioSelectDialog audioSelect(DSPEngine::instance()->getAudioDeviceManager(), m_settings.m_audioDeviceName);
audioSelect.move(p);
2020-11-27 22:10:27 -05:00
audioSelect.exec();
if (audioSelect.m_selected)
{
m_settings.m_audioDeviceName = audioSelect.m_audioDeviceName;
applySettings();
}
}
void VORDemodGUI::tick()
2020-11-27 22:10:27 -05:00
{
double magsqAvg, magsqPeak;
int nbMagsqSamples;
m_vorDemod->getMagSqLevels(magsqAvg, magsqPeak, nbMagsqSamples);
double powDbAvg = CalcDb::dbPower(magsqAvg);
double powDbPeak = CalcDb::dbPower(magsqPeak);
ui->channelPowerMeter->levelChanged(
(100.0f + powDbAvg) / 100.0f,
(100.0f + powDbPeak) / 100.0f,
nbMagsqSamples);
if (m_tickCount % 4 == 0) {
ui->channelPower->setText(QString::number(powDbAvg, 'f', 1));
}
int audioSampleRate = m_vorDemod->getAudioSampleRate();
bool squelchOpen = m_vorDemod->getSquelchOpen();
if (squelchOpen != m_squelchOpen)
{
if (audioSampleRate < 0) {
ui->audioMute->setStyleSheet("QToolButton { background-color : red; }");
} else if (squelchOpen) {
ui->audioMute->setStyleSheet("QToolButton { background-color : green; }");
} else {
ui->audioMute->setStyleSheet("QToolButton { background:rgb(79,79,79); }");
}
m_squelchOpen = squelchOpen;
}
m_tickCount++;
}
void VORDemodGUI::makeUIConnections()
{
QObject::connect(ui->deltaFrequency, &ValueDialZ::changed, this, &VORDemodGUI::on_deltaFrequency_changed);
QObject::connect(ui->thresh, &QDial::valueChanged, this, &VORDemodGUI::on_thresh_valueChanged);
QObject::connect(ui->volume, &QDial::valueChanged, this, &VORDemodGUI::on_volume_valueChanged);
QObject::connect(ui->squelch, &QDial::valueChanged, this, &VORDemodGUI::on_squelch_valueChanged);
QObject::connect(ui->audioMute, &QToolButton::toggled, this, &VORDemodGUI::on_audioMute_toggled);
QObject::connect(ui->identBandpassEnable, &QToolButton::toggled, this, &VORDemodGUI::on_identBandpassEnable_toggled);
}
void VORDemodGUI::updateAbsoluteCenterFrequency()
{
setStatusFrequency(m_deviceCenterFrequency + m_settings.m_inputFrequencyOffset);
}