1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2025-11-10 08:10:25 -05:00
sdrangel/plugins/channelrx/demodlora/lorademodgui.cpp

403 lines
12 KiB
C++
Raw Normal View History

2020-02-08 18:21:38 +01:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2019-2020 Edouard Griffiths, F4EXB //
// //
// 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 "device/deviceuiset.h"
#include <QScrollBar>
#include <QDebug>
2015-01-11 00:12:58 +00:00
#include "ui_lorademodgui.h"
#include "dsp/spectrumvis.h"
2020-02-08 18:21:38 +01:00
#include "dsp/dspengine.h"
#include "dsp/dspcommands.h"
2015-01-11 00:12:58 +00:00
#include "gui/glspectrum.h"
2020-02-08 18:21:38 +01:00
#include "gui/glspectrumgui.h"
2015-01-11 00:12:58 +00:00
#include "plugin/pluginapi.h"
#include "util/simpleserializer.h"
#include "mainwindow.h"
2017-10-08 00:28:42 +02:00
#include "lorademod.h"
#include "lorademodgui.h"
2015-01-11 00:12:58 +00:00
LoRaDemodGUI* LoRaDemodGUI::create(PluginAPI* pluginAPI, DeviceUISet *deviceUISet, BasebandSampleSink *rxChannel)
2015-01-11 00:12:58 +00:00
{
LoRaDemodGUI* gui = new LoRaDemodGUI(pluginAPI, deviceUISet, rxChannel);
2015-01-11 00:12:58 +00:00
return gui;
}
void LoRaDemodGUI::destroy()
{
delete this;
}
void LoRaDemodGUI::setName(const QString& name)
{
setObjectName(name);
}
QString LoRaDemodGUI::getName() const
{
return objectName();
}
qint64 LoRaDemodGUI::getCenterFrequency() const {
return m_channelMarker.getCenterFrequency();
}
void LoRaDemodGUI::setCenterFrequency(qint64 centerFrequency)
{
m_channelMarker.setCenterFrequency(centerFrequency);
applySettings();
}
2015-01-11 00:12:58 +00:00
void LoRaDemodGUI::resetToDefaults()
{
2017-10-08 00:28:42 +02:00
m_settings.resetToDefaults();
displaySettings();
applySettings(true);
2015-01-11 00:12:58 +00:00
}
QByteArray LoRaDemodGUI::serialize() const
{
2017-10-08 00:28:42 +02:00
return m_settings.serialize();
2015-01-11 00:12:58 +00:00
}
bool LoRaDemodGUI::deserialize(const QByteArray& data)
{
2020-02-08 18:21:38 +01:00
if (m_settings.deserialize(data))
{
2017-10-08 00:28:42 +02:00
displaySettings();
applySettings(true);
return true;
2020-02-08 18:21:38 +01:00
}
else
{
2017-10-08 00:28:42 +02:00
resetToDefaults();
return false;
}
2015-01-11 00:12:58 +00:00
}
bool LoRaDemodGUI::handleMessage(const Message& message)
2015-01-11 00:12:58 +00:00
{
2020-02-08 18:21:38 +01:00
if (DSPSignalNotification::match(message))
{
DSPSignalNotification& notif = (DSPSignalNotification&) message;
int basebandSampleRate = notif.getSampleRate();
qDebug() << "LoRaDemodGUI::handleMessage: DSPSignalNotification: m_basebandSampleRate: " << basebandSampleRate;
if (basebandSampleRate != m_basebandSampleRate)
{
m_basebandSampleRate = basebandSampleRate;
setBandwidths();
}
return true;
}
else if (LoRaDemod::MsgReportDecodeBytes::match(message))
{
const LoRaDemod::MsgReportDecodeBytes& msg = (LoRaDemod::MsgReportDecodeBytes&) message;
QByteArray bytes = msg.getBytes();
ui->hexText->setText(bytes.toHex());
ui->syncWord->setText((tr("%1").arg(msg.getSyncWord(), 2, 16)));
ui->sText->setText(tr("%1").arg(msg.getSingalDb(), 0, 'f', 1));
ui->snrText->setText(tr("%1").arg(msg.getSingalDb() - msg.getNoiseDb(), 0, 'f', 1));
}
else if (LoRaDemod::MsgReportDecodeString::match(message))
{
const LoRaDemod::MsgReportDecodeString& msg = (LoRaDemod::MsgReportDecodeString&) message;
addText(msg.getString());
ui->syncWord->setText((tr("%1").arg(msg.getSyncWord(), 2, 16)));
ui->sText->setText(tr("%1").arg(msg.getSingalDb(), 0, 'f', 1));
ui->snrText->setText(tr("%1").arg(msg.getSingalDb() - msg.getNoiseDb(), 0, 'f', 1));
}
2020-02-08 18:21:38 +01:00
else
{
return false;
}
}
void LoRaDemodGUI::handleInputMessages()
{
Message* message;
while ((message = getInputMessageQueue()->pop()) != 0)
{
if (handleMessage(*message)) {
delete message;
}
}
2015-01-11 00:12:58 +00:00
}
2020-02-08 18:21:38 +01:00
void LoRaDemodGUI::channelMarkerChangedByCursor()
2015-01-11 00:12:58 +00:00
{
2020-02-08 18:21:38 +01:00
ui->deltaFrequency->setValue(m_channelMarker.getCenterFrequency());
m_settings.m_inputFrequencyOffset = m_channelMarker.getCenterFrequency();
2015-01-11 00:12:58 +00:00
applySettings();
}
2020-02-08 18:21:38 +01:00
void LoRaDemodGUI::on_deltaFrequency_changed(qint64 value)
{
m_channelMarker.setCenterFrequency(value);
m_settings.m_inputFrequencyOffset = m_channelMarker.getCenterFrequency();
applySettings();
}
void LoRaDemodGUI::channelMarkerHighlightedByCursor()
{
setHighlighted(m_channelMarker.getHighlighted());
}
2015-01-11 00:12:58 +00:00
void LoRaDemodGUI::on_BW_valueChanged(int value)
{
2017-10-08 00:28:42 +02:00
if (value < 0) {
m_settings.m_bandwidthIndex = 0;
2020-02-08 18:21:38 +01:00
} else if (value < LoRaDemodSettings::nbBandwidths) {
2017-10-08 00:28:42 +02:00
m_settings.m_bandwidthIndex = value;
} else {
2020-02-08 18:21:38 +01:00
m_settings.m_bandwidthIndex = LoRaDemodSettings::nbBandwidths - 1;
2017-10-08 00:28:42 +02:00
}
int thisBW = LoRaDemodSettings::bandwidths[value];
2015-01-12 14:25:16 +00:00
ui->BWText->setText(QString("%1 Hz").arg(thisBW));
m_channelMarker.setBandwidth(thisBW);
2020-02-08 18:21:38 +01:00
ui->glSpectrum->setSampleRate(thisBW);
ui->glSpectrum->setCenterFrequency(thisBW/2);
2017-10-08 00:28:42 +02:00
2015-01-11 00:12:58 +00:00
applySettings();
}
void LoRaDemodGUI::on_Spread_valueChanged(int value)
2015-01-12 14:25:16 +00:00
{
2020-01-30 18:26:43 +01:00
m_settings.m_spreadFactor = value;
ui->SpreadText->setText(tr("%1").arg(value));
2020-02-08 18:21:38 +01:00
ui->spectrumGUI->setFFTSize(m_settings.m_spreadFactor);
2020-01-30 18:26:43 +01:00
applySettings();
2015-01-12 14:25:16 +00:00
}
2020-02-08 18:21:38 +01:00
void LoRaDemodGUI::on_deBits_valueChanged(int value)
{
m_settings.m_deBits = value;
ui->deBitsText->setText(tr("%1").arg(m_settings.m_deBits));
applySettings();
}
void LoRaDemodGUI::on_scheme_currentIndexChanged(int index)
{
m_settings.m_codingScheme = (LoRaDemodSettings::CodingScheme) index;
applySettings();
}
void LoRaDemodGUI::on_mute_toggled(bool checked)
{
m_settings.m_decodeActive = !checked;
applySettings();
}
void LoRaDemodGUI::on_clear_clicked(bool checked)
{
(void) checked;
ui->messageText->clear();
}
void LoRaDemodGUI::on_eomSquelch_valueChanged(int value)
{
m_settings.m_eomSquelchTenths = value;
displaySquelch();
applySettings();
}
void LoRaDemodGUI::on_messageLength_valueChanged(int value)
{
m_settings.m_nbSymbolsMax = value;
ui->messageLengthText->setText(tr("%1").arg(m_settings.m_nbSymbolsMax));
applySettings();
}
void LoRaDemodGUI::onWidgetRolled(QWidget* widget, bool rollDown)
2015-01-11 00:12:58 +00:00
{
(void) widget;
(void) rollDown;
2015-01-11 00:12:58 +00:00
}
LoRaDemodGUI::LoRaDemodGUI(PluginAPI* pluginAPI, DeviceUISet *deviceUISet, BasebandSampleSink *rxChannel, QWidget* parent) :
2015-01-11 00:12:58 +00:00
RollupWidget(parent),
ui(new Ui::LoRaDemodGUI),
m_pluginAPI(pluginAPI),
m_deviceUISet(deviceUISet),
m_channelMarker(this),
2020-02-08 18:21:38 +01:00
m_basebandSampleRate(250000),
m_doApplySettings(true),
m_tickCount(0)
2015-01-11 00:12:58 +00:00
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose, true);
connect(this, SIGNAL(widgetRolled(QWidget*,bool)), this, SLOT(onWidgetRolled(QWidget*,bool)));
m_spectrumVis = new SpectrumVis(SDR_RX_SCALEF, ui->glSpectrum);
m_LoRaDemod = (LoRaDemod*) rxChannel; //new LoRaDemod(m_deviceUISet->m_deviceSourceAPI);
2017-10-08 01:42:18 +02:00
m_LoRaDemod->setSpectrumSink(m_spectrumVis);
2020-02-08 18:21:38 +01:00
m_LoRaDemod->setMessageQueueToGUI(getInputMessageQueue());
2015-01-11 00:12:58 +00:00
connect(&MainWindow::getInstance()->getMasterTimer(), SIGNAL(timeout()), this, SLOT(tick()));
ui->glSpectrum->setDisplayWaterfall(true);
2015-01-11 00:12:58 +00:00
ui->glSpectrum->setDisplayMaxHold(true);
2020-02-08 18:21:38 +01:00
ui->deltaFrequencyLabel->setText(QString("%1f").arg(QChar(0x94, 0x03)));
ui->deltaFrequency->setColorMapper(ColorMapper(ColorMapper::GrayGold));
ui->deltaFrequency->setValueRange(false, 7, -9999999, 9999999);
ui->messageText->setReadOnly(true);
ui->syncWord->setReadOnly(true);
ui->hexText->setReadOnly(true);
2020-02-08 18:21:38 +01:00
m_channelMarker.setMovable(true);
m_channelMarker.setVisible(true);
2020-02-08 18:21:38 +01:00
connect(&m_channelMarker, SIGNAL(changedByCursor()), this, SLOT(channelMarkerChangedByCursor()));
connect(&m_channelMarker, SIGNAL(highlightedByCursor()), this, SLOT(channelMarkerHighlightedByCursor()));
m_deviceUISet->registerRxChannelInstance(LoRaDemod::m_channelIdURI, this);
m_deviceUISet->addChannelMarker(&m_channelMarker);
m_deviceUISet->addRollupWidget(this);
2015-01-11 00:12:58 +00:00
2017-10-08 01:42:18 +02:00
ui->spectrumGUI->setBuddies(m_spectrumVis->getInputMessageQueue(), m_spectrumVis, ui->glSpectrum);
2015-01-11 00:12:58 +00:00
2017-10-08 01:42:18 +02:00
m_settings.setChannelMarker(&m_channelMarker);
m_settings.setSpectrumGUI(ui->spectrumGUI);
2020-02-08 18:21:38 +01:00
connect(getInputMessageQueue(), SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()));
setBandwidths();
2017-10-08 01:42:18 +02:00
displaySettings();
applySettings(true);
2015-01-11 00:12:58 +00:00
}
LoRaDemodGUI::~LoRaDemodGUI()
{
m_deviceUISet->removeRxChannelInstance(this);
delete m_LoRaDemod; // TODO: check this: when the GUI closes it has to delete the demodulator
2015-01-11 00:12:58 +00:00
delete m_spectrumVis;
delete ui;
}
void LoRaDemodGUI::blockApplySettings(bool block)
{
m_doApplySettings = !block;
}
2017-10-08 00:28:42 +02:00
void LoRaDemodGUI::applySettings(bool force)
2015-01-11 00:12:58 +00:00
{
if (m_doApplySettings)
{
2017-10-08 01:42:18 +02:00
setTitleColor(m_channelMarker.getColor());
LoRaDemod::MsgConfigureLoRaDemod* message = LoRaDemod::MsgConfigureLoRaDemod::create( m_settings, force);
m_LoRaDemod->getInputMessageQueue()->push(message);
}
2015-01-11 00:12:58 +00:00
}
2017-10-08 00:28:42 +02:00
void LoRaDemodGUI::displaySettings()
{
int thisBW = LoRaDemodSettings::bandwidths[m_settings.m_bandwidthIndex];
2017-10-08 01:42:18 +02:00
m_channelMarker.blockSignals(true);
2020-02-08 18:21:38 +01:00
m_channelMarker.setTitle(m_settings.m_title);
m_channelMarker.setCenterFrequency(m_settings.m_inputFrequencyOffset);
2017-10-08 01:42:18 +02:00
m_channelMarker.setBandwidth(thisBW);
2020-02-08 18:21:38 +01:00
m_channelMarker.blockSignals(false);
2017-10-08 01:42:18 +02:00
m_channelMarker.setColor(m_settings.m_rgbColor);
setTitleColor(m_settings.m_rgbColor);
2020-02-08 18:21:38 +01:00
ui->glSpectrum->setSampleRate(thisBW);
ui->glSpectrum->setCenterFrequency(thisBW/2);
2017-10-12 01:21:30 +02:00
blockApplySettings(true);
2020-02-08 18:21:38 +01:00
ui->deltaFrequency->setValue(m_channelMarker.getCenterFrequency());
2017-10-12 01:21:30 +02:00
ui->BWText->setText(QString("%1 Hz").arg(thisBW));
ui->BW->setValue(m_settings.m_bandwidthIndex);
2020-02-08 18:21:38 +01:00
ui->Spread->setValue(m_settings.m_spreadFactor);
2020-01-30 18:26:43 +01:00
ui->SpreadText->setText(tr("%1").arg(m_settings.m_spreadFactor));
2020-02-08 18:21:38 +01:00
ui->deBits->setValue(m_settings.m_deBits);
ui->deBitsText->setText(tr("%1").arg(m_settings.m_deBits));
ui->scheme->setCurrentIndex((int) m_settings.m_codingScheme);
ui->messageLengthText->setText(tr("%1").arg(m_settings.m_nbSymbolsMax));
ui->messageLength->setValue(m_settings.m_nbSymbolsMax);
2020-02-08 18:21:38 +01:00
ui->spectrumGUI->setFFTSize(m_settings.m_spreadFactor);
displaySquelch();
2017-10-12 01:21:30 +02:00
blockApplySettings(false);
2017-10-08 00:28:42 +02:00
}
2020-02-08 18:21:38 +01:00
void LoRaDemodGUI::displaySquelch()
{
ui->eomSquelch->setValue(m_settings.m_eomSquelchTenths);
if (m_settings.m_eomSquelchTenths == ui->eomSquelch->maximum()) {
ui->eomSquelchText->setText("---");
} else {
ui->eomSquelchText->setText(tr("%1").arg(m_settings.m_eomSquelchTenths / 10.0, 0, 'f', 1));
}
}
2020-02-08 18:21:38 +01:00
void LoRaDemodGUI::setBandwidths()
{
2020-02-12 13:17:15 +01:00
int maxBandwidth = m_basebandSampleRate/LoRaDemodSettings::oversampling;
2020-02-08 18:21:38 +01:00
int maxIndex = 0;
for (; (maxIndex < LoRaDemodSettings::nbBandwidths) && (LoRaDemodSettings::bandwidths[maxIndex] <= maxBandwidth); maxIndex++)
{}
if (maxIndex != 0)
{
qDebug("LoRaDemodGUI::setBandwidths: avl: %d max: %d", maxBandwidth, LoRaDemodSettings::bandwidths[maxIndex-1]);
ui->BW->setMaximum(maxIndex - 1);
int index = ui->BW->value();
ui->BWText->setText(QString("%1 Hz").arg(LoRaDemodSettings::bandwidths[index]));
}
}
void LoRaDemodGUI::addText(const QString& text)
{
QDateTime dt = QDateTime::currentDateTime();
QString dateStr = dt.toString("HH:mm:ss");
QTextCursor cursor = ui->messageText->textCursor();
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
if (!ui->messageText->document()->isEmpty()) {
cursor.insertText("\n");
}
cursor.insertText(tr("%1 %2").arg(dateStr).arg(text));
ui->messageText->verticalScrollBar()->setValue(ui->messageText->verticalScrollBar()->maximum());
}
void LoRaDemodGUI::tick()
{
if (m_tickCount < 10)
{
m_tickCount++;
}
else
{
m_tickCount = 0;
if (m_LoRaDemod->getDemodActive()) {
ui->mute->setStyleSheet("QToolButton { background-color : green; }");
} else {
ui->mute->setStyleSheet("QToolButton { background:rgb(79,79,79); }");
}
}
}