You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

528 lines
19 KiB

///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2021-2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 <QDockWidget>
#include <QMainWindow>
#include <QDebug>
#include <QAction>
#include <QClipboard>
#include <QFileDialog>
#include <QScrollBar>
#include <QMenu>
#include "psk31demodgui.h"
#include "device/deviceuiset.h"
#include "dsp/dspengine.h"
#include "dsp/dspcommands.h"
#include "ui_psk31demodgui.h"
#include "plugin/pluginapi.h"
#include "util/db.h"
#include "util/rtty.h"
#include "gui/basicchannelsettingsdialog.h"
#include "dsp/dspengine.h"
#include "dsp/glscopesettings.h"
#include "gui/dialogpositioner.h"
#include "maincore.h"
#include "psk31demod.h"
PSK31DemodGUI* PSK31DemodGUI::create(PluginAPI* pluginAPI, DeviceUISet *deviceUISet, BasebandSampleSink *rxChannel)
{
PSK31DemodGUI* gui = new PSK31DemodGUI(pluginAPI, deviceUISet, rxChannel);
return gui;
}
void PSK31DemodGUI::destroy()
{
delete this;
}
void PSK31DemodGUI::resetToDefaults()
{
m_settings.resetToDefaults();
displaySettings();
applySettings(QStringList({"streamIndex"}), true);
}
QByteArray PSK31DemodGUI::serialize() const
{
return m_settings.serialize();
}
bool PSK31DemodGUI::deserialize(const QByteArray& data)
{
if(m_settings.deserialize(data)) {
displaySettings();
applySettings(QStringList({"streamIndex"}), true);
return true;
} else {
resetToDefaults();
return false;
}
}
void PSK31DemodGUI::characterReceived(QString c)
{
// Is the scroll bar at the bottom?
int scrollPos = ui->text->verticalScrollBar()->value();
bool atBottom = scrollPos >= ui->text->verticalScrollBar()->maximum();
// Move cursor to end where we want to append new text
// (user may have moved it by clicking / highlighting text)
ui->text->moveCursor(QTextCursor::End);
// Restore scroll position
ui->text->verticalScrollBar()->setValue(scrollPos);
const QChar character = c.isEmpty() ? QChar() : c[0];
if (character == '\b')
{
ui->text->textCursor().deletePreviousChar();
}
else
{
const QString text = PSK31Demod::displayText(character, m_previousCharacter);
if (!text.isEmpty()) {
ui->text->insertPlainText(text);
}
}
m_previousCharacter = character;
// Scroll to bottom, if we we're previously at the bottom
if (atBottom) {
ui->text->verticalScrollBar()->setValue(ui->text->verticalScrollBar()->maximum());
}
}
bool PSK31DemodGUI::handleMessage(const Message& message)
{
if (PSK31Demod::MsgConfigurePSK31Demod::match(message))
{
qDebug("PSK31DemodGUI::handleMessage: PSK31Demod::MsgConfigurePSK31Demod");
const PSK31Demod::MsgConfigurePSK31Demod& cfg = (PSK31Demod::MsgConfigurePSK31Demod&) message;
m_settings = cfg.getSettings();
blockApplySettings(true);
ui->scopeGUI->updateSettings();
m_channelMarker.updateSettings(static_cast<const ChannelMarker*>(m_settings.m_channelMarker));
displaySettings();
blockApplySettings(false);
return true;
}
else if (DSPSignalNotification::match(message))
{
DSPSignalNotification& notif = (DSPSignalNotification&) message;
m_deviceCenterFrequency = notif.getCenterFrequency();
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();
return true;
}
else if (PSK31Demod::MsgCharacter::match(message))
{
PSK31Demod::MsgCharacter& report = (PSK31Demod::MsgCharacter&) message;
QString c = report.getCharacter();
characterReceived(c);
return true;
}
else if (PSK31Demod::MsgDemodReport::match(message))
{
PSK31Demod::MsgDemodReport& report = (PSK31Demod::MsgDemodReport&) message;
const QString status = report.isLocked() ? tr("Locked") : tr("Unlocked");
if (report.isLocked())
{
const QString offsetValue = QStringLiteral("%1%2 Hz")
.arg(report.getFrequencyOffset() >= 0.0f ? QStringLiteral("+") : QString())
.arg(report.getFrequencyOffset(), 0, 'f', 1);
const QString offset = tr("Frequency offset: %1 Hz")
.arg(report.getFrequencyOffset(), 0, 'f', 1);
const QString snrValue = tr("%1 dB SNR").arg(report.getSNR(), 0, 'f', 1);
const QString snr = tr("SNR: %1 dB").arg(report.getSNR(), 0, 'f', 1);
ui->carrierStatus->setText(tr("%1 %2 %3").arg(status, offsetValue, snrValue));
ui->carrierStatus->setToolTip(tr("%1\n%2").arg(offset, snr));
ui->channelPower->setToolTip(tr("Carrier: %1\n%2\n%3").arg(status, offset, snr));
}
else
{
ui->carrierStatus->setText(status);
ui->carrierStatus->setToolTip(status);
ui->channelPower->setToolTip(tr("Carrier: %1").arg(status));
}
return true;
}
return false;
}
void PSK31DemodGUI::handleInputMessages()
{
Message* message;
while ((message = getInputMessageQueue()->pop()))
{
if (!handleMessage(*message)) {
qDebug("%s: unhandled message: %s", Q_FUNC_INFO, message->getIdentifier());
}
delete message;
}
}
void PSK31DemodGUI::channelMarkerChangedByCursor()
{
ui->deltaFrequency->setValue(m_channelMarker.getCenterFrequency());
m_settings.m_inputFrequencyOffset = m_channelMarker.getCenterFrequency();
applySettings(QStringList({"inputFrequencyOffset"}));
}
void PSK31DemodGUI::channelMarkerHighlightedByCursor()
{
setHighlighted(m_channelMarker.getHighlighted());
}
void PSK31DemodGUI::on_deltaFrequency_changed(qint64 value)
{
m_channelMarker.setCenterFrequency(value);
m_settings.m_inputFrequencyOffset = m_channelMarker.getCenterFrequency();
updateAbsoluteCenterFrequency();
applySettings(QStringList({"inputFrequencyOffset"}));
}
void PSK31DemodGUI::on_rfBW_valueChanged(int value)
{
float bw = value;
ui->rfBWText->setText(formatFrequency((int)bw));
m_channelMarker.setBandwidth(bw);
m_settings.m_rfBandwidth = bw;
applySettings(QStringList({"rfBandwidth"}));
}
void PSK31DemodGUI::on_clearTable_clicked()
{
ui->text->clear();
}
void PSK31DemodGUI::on_udpEnabled_clicked(bool checked)
{
m_settings.m_udpEnabled = checked;
applySettings(QStringList({"udpEnabled"}));
}
void PSK31DemodGUI::on_udpAddress_editingFinished()
{
m_settings.m_udpAddress = ui->udpAddress->text();
applySettings(QStringList({"udpAddress"}));
}
void PSK31DemodGUI::on_udpPort_editingFinished()
{
m_settings.m_udpPort = ui->udpPort->text().toInt();
applySettings(QStringList({"udpPort"}));
}
void PSK31DemodGUI::onWidgetRolled(QWidget* widget, bool rollDown)
{
(void) widget;
(void) rollDown;
getRollupContents()->saveState(m_rollupState);
applySettings(QStringList());
}
void PSK31DemodGUI::onMenuDialogCalled(const QPoint &p)
{
if (m_contextMenuType == 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_psk31Demod->getNumberOfDeviceStreams());
dialog.setStreamIndex(m_settings.m_streamIndex);
}
dialog.move(p);
new DialogPositioner(&dialog, false);
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());
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();
}
applySettings(QStringList({
"rgbColor",
"title",
"useReverseAPI",
"reverseAPIAddress",
"reverseAPIPort",
"reverseAPIDeviceIndex",
"reverseAPIChannelIndex",
"streamIndex"
}));
}
resetContextMenuType();
}
PSK31DemodGUI::PSK31DemodGUI(PluginAPI* pluginAPI, DeviceUISet *deviceUISet, BasebandSampleSink *rxChannel, QWidget* parent) :
ChannelGUI(parent),
ui(new Ui::PSK31DemodGUI),
m_pluginAPI(pluginAPI),
m_deviceUISet(deviceUISet),
m_channelMarker(this),
m_deviceCenterFrequency(0),
m_doApplySettings(true),
m_tickCount(0)
{
setAttribute(Qt::WA_DeleteOnClose, true);
m_helpURL = "plugins/channelrx/demodpsk31/readme.md";
RollupContents *rollupContents = getRollupContents();
ui->setupUi(rollupContents);
setSizePolicy(rollupContents->sizePolicy());
rollupContents->arrangeRollups();
connect(rollupContents, SIGNAL(widgetRolled(QWidget*,bool)), this, SLOT(onWidgetRolled(QWidget*,bool)));
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onMenuDialogCalled(const QPoint &)));
m_psk31Demod = reinterpret_cast<PSK31Demod*>(rxChannel);
m_psk31Demod->setMessageQueueToGUI(getInputMessageQueue());
connect(&MainCore::instance()->getMasterTimer(), SIGNAL(timeout()), this, SLOT(tick())); // 50 ms
ui->deltaFrequencyLabel->setText(QString("%1f").arg(QChar(0x94, 0x03)));
ui->deltaFrequency->setColorMapper(ColorMapper(ColorMapper::GrayGold));
ui->deltaFrequency->setValueRange(false, 7, -9999999, 9999999);
ui->channelPowerMeter->setColorTheme(LevelMeterSignalDB::ColorGreenAndBlue);
m_scopeVis = m_psk31Demod->getScopeSink();
m_scopeVis->setGLScope(ui->glScope);
ui->glScope->connectTimer(MainCore::instance()->getMasterTimer());
ui->scopeGUI->setBuddies(m_scopeVis->getInputMessageQueue(), m_scopeVis, ui->glScope);
// Scope settings to display the IQ waveforms
ui->scopeGUI->setPreTrigger(1);
GLScopeSettings::TraceData traceDataI, traceDataQ;
traceDataI.m_projectionType = Projector::ProjectionReal;
traceDataI.m_amp = 1.0; // for -1 to +1
traceDataI.m_ofs = 0.0; // vertical offset
traceDataQ.m_projectionType = Projector::ProjectionImag;
traceDataQ.m_amp = 1.0;
traceDataQ.m_ofs = 0.0;
ui->scopeGUI->changeTrace(0, traceDataI);
ui->scopeGUI->addTrace(traceDataQ);
ui->scopeGUI->setDisplayMode(GLScopeSettings::DisplayXYV);
ui->scopeGUI->focusOnTrace(0); // re-focus to take changes into account in the GUI
GLScopeSettings::TriggerData triggerData;
triggerData.m_triggerLevel = 0.1;
triggerData.m_triggerLevelCoarse = 10;
triggerData.m_triggerPositiveEdge = true;
ui->scopeGUI->changeTrigger(0, triggerData);
ui->scopeGUI->focusOnTrigger(0); // re-focus to take changes into account in the GUI
m_scopeVis->setLiveRate(PSK31DemodSettings::PSK31DEMOD_CHANNEL_SAMPLE_RATE);
m_scopeVis->configure(500, 1, 0, 0, true);
m_channelMarker.blockSignals(true);
m_channelMarker.setColor(QColor(25, 180, 200));
m_channelMarker.setBandwidth(m_settings.m_rfBandwidth);
m_channelMarker.setCenterFrequency(m_settings.m_inputFrequencyOffset);
m_channelMarker.setTitle("PSK31 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.setScopeGUI(ui->scopeGUI);
m_settings.setRollupState(&m_rollupState);
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()));
ui->scopeContainer->setVisible(false);
ui->carrierStatusLabel->setText(tr("Carrier"));
ui->carrierStatus->setText(tr("Unlocked"));
ui->carrierStatus->setToolTip(tr("Unlocked"));
ui->text->addAcronyms(Rtty::m_acronyms);
displaySettings();
makeUIConnections();
applySettings(QStringList(), true);
m_resizer.enableChildMouseTracking();
}
PSK31DemodGUI::~PSK31DemodGUI()
{
delete ui;
}
void PSK31DemodGUI::blockApplySettings(bool block)
{
m_doApplySettings = !block;
}
void PSK31DemodGUI::applySettings(const QStringList& settingsKeys, bool force)
{
if (m_doApplySettings)
{
PSK31Demod::MsgConfigurePSK31Demod* message = PSK31Demod::MsgConfigurePSK31Demod::create(settingsKeys, m_settings, force);
m_psk31Demod->getInputMessageQueue()->push(message);
}
}
QString PSK31DemodGUI::formatFrequency(int frequency) const
{
return QString("%1 Hz").arg(frequency);
}
void PSK31DemodGUI::displaySettings()
{
m_channelMarker.blockSignals(true);
m_channelMarker.setBandwidth(m_settings.m_rfBandwidth);
m_channelMarker.setCenterFrequency(m_settings.m_inputFrequencyOffset);
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());
blockApplySettings(true);
ui->deltaFrequency->setValue(m_channelMarker.getCenterFrequency());
ui->rfBWText->setText(formatFrequency((int)m_settings.m_rfBandwidth));
ui->rfBW->setValue(m_settings.m_rfBandwidth);
updateIndexLabel();
ui->udpEnabled->setChecked(m_settings.m_udpEnabled);
ui->udpAddress->setText(m_settings.m_udpAddress);
ui->udpPort->setText(QString::number(m_settings.m_udpPort));
ui->logFilename->setToolTip(QString(".txt log filename: %1").arg(m_settings.m_logFilename));
ui->logEnable->setChecked(m_settings.m_logEnabled);
getRollupContents()->restoreState(m_rollupState);
updateAbsoluteCenterFrequency();
blockApplySettings(false);
}
void PSK31DemodGUI::leaveEvent(QEvent* event)
{
m_channelMarker.setHighlighted(false);
ChannelGUI::leaveEvent(event);
}
void PSK31DemodGUI::enterEvent(EnterEventType* event)
{
m_channelMarker.setHighlighted(true);
ChannelGUI::enterEvent(event);
}
void PSK31DemodGUI::tick()
{
double magsqAvg, magsqPeak;
int nbMagsqSamples;
m_psk31Demod->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));
}
m_tickCount++;
}
void PSK31DemodGUI::on_logEnable_clicked(bool checked)
{
m_settings.m_logEnabled = checked;
applySettings(QStringList({"logEnabled"}));
}
void PSK31DemodGUI::on_logFilename_clicked()
{
// Get filename to save to
QFileDialog fileDialog(nullptr, "Select file to log received text to", "", "*.txt");
fileDialog.setAcceptMode(QFileDialog::AcceptSave);
if (fileDialog.exec())
{
QStringList fileNames = fileDialog.selectedFiles();
if (fileNames.size() > 0)
{
m_settings.m_logFilename = fileNames[0];
ui->logFilename->setToolTip(QString(".txt log filename: %1").arg(m_settings.m_logFilename));
applySettings(QStringList({"logFilename"}));
}
}
}
void PSK31DemodGUI::makeUIConnections()
{
QObject::connect(ui->deltaFrequency, &ValueDialZ::changed, this, &PSK31DemodGUI::on_deltaFrequency_changed);
QObject::connect(ui->rfBW, &QSlider::valueChanged, this, &PSK31DemodGUI::on_rfBW_valueChanged);
QObject::connect(ui->clearTable, &QPushButton::clicked, this, &PSK31DemodGUI::on_clearTable_clicked);
QObject::connect(ui->udpEnabled, &QCheckBox::clicked, this, &PSK31DemodGUI::on_udpEnabled_clicked);
QObject::connect(ui->udpAddress, &QLineEdit::editingFinished, this, &PSK31DemodGUI::on_udpAddress_editingFinished);
QObject::connect(ui->udpPort, &QLineEdit::editingFinished, this, &PSK31DemodGUI::on_udpPort_editingFinished);
QObject::connect(ui->logEnable, &ButtonSwitch::clicked, this, &PSK31DemodGUI::on_logEnable_clicked);
QObject::connect(ui->logFilename, &QToolButton::clicked, this, &PSK31DemodGUI::on_logFilename_clicked);
}
void PSK31DemodGUI::updateAbsoluteCenterFrequency()
{
setStatusFrequency(m_deviceCenterFrequency + m_settings.m_inputFrequencyOffset);
}