From ebc90ea5b23d3909e156f74099ff333e1236648c Mon Sep 17 00:00:00 2001 From: John Greb Date: Sun, 11 Jan 2015 00:12:58 +0000 Subject: [PATCH] Start LoRa. --- plugins/channel/CMakeLists.txt | 1 + plugins/channel/lora/CMakeLists.txt | 47 +++++++ plugins/channel/lora/lorademod.cpp | 102 ++++++++++++++++ plugins/channel/lora/lorademod.h | 75 ++++++++++++ plugins/channel/lora/lorademodgui.cpp | 168 ++++++++++++++++++++++++++ plugins/channel/lora/lorademodgui.h | 56 +++++++++ plugins/channel/lora/lorademodgui.ui | 136 +++++++++++++++++++++ plugins/channel/lora/loraplugin.cpp | 53 ++++++++ plugins/channel/lora/loraplugin.h | 29 +++++ 9 files changed, 667 insertions(+) create mode 100644 plugins/channel/lora/CMakeLists.txt create mode 100644 plugins/channel/lora/lorademod.cpp create mode 100644 plugins/channel/lora/lorademod.h create mode 100644 plugins/channel/lora/lorademodgui.cpp create mode 100644 plugins/channel/lora/lorademodgui.h create mode 100644 plugins/channel/lora/lorademodgui.ui create mode 100644 plugins/channel/lora/loraplugin.cpp create mode 100644 plugins/channel/lora/loraplugin.h diff --git a/plugins/channel/CMakeLists.txt b/plugins/channel/CMakeLists.txt index f1506c59a..a57fa036d 100644 --- a/plugins/channel/CMakeLists.txt +++ b/plugins/channel/CMakeLists.txt @@ -1,5 +1,6 @@ project(demod) +add_subdirectory(lora) add_subdirectory(nfm) add_subdirectory(ssb) add_subdirectory(tcpsrc) diff --git a/plugins/channel/lora/CMakeLists.txt b/plugins/channel/lora/CMakeLists.txt new file mode 100644 index 000000000..209874b0b --- /dev/null +++ b/plugins/channel/lora/CMakeLists.txt @@ -0,0 +1,47 @@ +project(lora) + +set(lora_SOURCES + lorademod.cpp + lorademodgui.cpp + loraplugin.cpp +) + +set(lora_HEADERS + lorademod.h + lorademodgui.h + loraplugin.h +) + +set(lora_FORMS + lorademodgui.ui +) + +include_directories( + . + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_SOURCE_DIR}/include + ${CMAKE_SOURCE_DIR}/include-gpl + ${OPENGL_INCLUDE_DIR} +) + +#include(${QT_USE_FILE}) +add_definitions(${QT_DEFINITIONS}) +add_definitions(-DQT_PLUGIN) +add_definitions(-DQT_SHARED) + +#qt5_wrap_cpp(lora_HEADERS_MOC ${lora_HEADERS}) +qt5_wrap_ui(lora_FORMS_HEADERS ${lora_FORMS}) + +add_library(demodlora SHARED + ${lora_SOURCES} + ${lora_HEADERS_MOC} + ${lora_FORMS_HEADERS} +) + +target_link_libraries(demodlora + ${QT_LIBRARIES} + ${OPENGL_LIBRARIES} + sdrbase +) + +qt5_use_modules(demodlora Core Widgets OpenGL Multimedia) diff --git a/plugins/channel/lora/lorademod.cpp b/plugins/channel/lora/lorademod.cpp new file mode 100644 index 000000000..e7fd3650c --- /dev/null +++ b/plugins/channel/lora/lorademod.cpp @@ -0,0 +1,102 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany // +// written by Christian Daniel // +// (c) 2015 John Greb +// // +// 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 . // +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include "lorademod.h" +#include "dsp/dspcommands.h" + +MESSAGE_CLASS_DEFINITION(LoRaDemod::MsgConfigureLoRaDemod, Message) + +LoRaDemod::LoRaDemod(SampleSink* sampleSink) : + m_sampleSink(sampleSink) +{ + m_Bandwidth = 7813; + m_sampleRate = 96000; + m_frequency = 0; + m_nco.setFreq(m_frequency, m_sampleRate); + m_interpolator.create(16, m_sampleRate, m_Bandwidth/2.0); + m_sampleDistanceRemain = (Real)m_sampleRate / m_Bandwidth; + + m_freq = 0; + m_angle = 0; +} + +LoRaDemod::~LoRaDemod() +{ +} + +void LoRaDemod::configure(MessageQueue* messageQueue, Real Bandwidth) +{ + Message* cmd = MsgConfigureLoRaDemod::create(Bandwidth); + cmd->submit(messageQueue, this); +} + +void LoRaDemod::feed(SampleVector::const_iterator begin, SampleVector::const_iterator end, bool pO) +{ + Complex ci; + + m_sampleBuffer.clear(); + for(SampleVector::const_iterator it = begin; it < end; ++it) { + Complex c(it->real() / 32768.0, it->imag() / 32768.0); + c *= m_nco.nextIQ(); + + if(m_interpolator.interpolate(&m_sampleDistanceRemain, c, &ci)) { + m_freq = (m_freq + 1) & 255; + m_angle = (m_angle + m_freq) & 255; + Complex cangle(cos(M_PI*m_angle/128),-sin(M_PI*m_angle/128)); + ci *= cangle; + m_sampleBuffer.push_back(Sample(ci.real() * 32000, ci.imag() * 32000)); + m_sampleDistanceRemain += (Real)m_sampleRate / m_Bandwidth; + } + } + if(m_sampleSink != NULL) + m_sampleSink->feed(m_sampleBuffer.begin(), m_sampleBuffer.end(), true); +} + +void LoRaDemod::start() +{ +} + +void LoRaDemod::stop() +{ +} + +bool LoRaDemod::handleMessage(Message* cmd) +{ + if(DSPSignalNotification::match(cmd)) { + DSPSignalNotification* signal = (DSPSignalNotification*)cmd; + m_sampleRate = signal->getSampleRate(); + m_nco.setFreq(-signal->getFrequencyOffset(), m_sampleRate); + m_interpolator.create(16, m_sampleRate, m_Bandwidth/2.0); + m_sampleDistanceRemain = m_sampleRate / m_Bandwidth; + cmd->completed(); + return true; + } else if(MsgConfigureLoRaDemod::match(cmd)) { + MsgConfigureLoRaDemod* cfg = (MsgConfigureLoRaDemod*)cmd; + m_Bandwidth = cfg->getBandwidth(); + m_interpolator.create(16, m_sampleRate, m_Bandwidth/2.0); + cmd->completed(); + return true; + } else { + if(m_sampleSink != NULL) + return m_sampleSink->handleMessage(cmd); + else + return false; + } +} diff --git a/plugins/channel/lora/lorademod.h b/plugins/channel/lora/lorademod.h new file mode 100644 index 000000000..8e1068bbc --- /dev/null +++ b/plugins/channel/lora/lorademod.h @@ -0,0 +1,75 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany // +// (C) 2015 John Greb // +// // +// 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 . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef INCLUDE_LoRaDEMOD_H +#define INCLUDE_LoRaDEMOD_H + +#include +#include "dsp/samplesink.h" +#include "dsp/nco.h" +#include "dsp/interpolator.h" +#include "util/message.h" + +class LoRaDemod : public SampleSink { +public: + LoRaDemod(SampleSink* sampleSink); + ~LoRaDemod(); + + void configure(MessageQueue* messageQueue, Real Bandwidth); + + void feed(SampleVector::const_iterator begin, SampleVector::const_iterator end, bool pO); + void start(); + void stop(); + bool handleMessage(Message* cmd); + +private: + class MsgConfigureLoRaDemod : public Message { + MESSAGE_CLASS_DECLARATION + + public: + Real getBandwidth() const { return m_Bandwidth; } + + static MsgConfigureLoRaDemod* create(Real Bandwidth) + { + return new MsgConfigureLoRaDemod(Bandwidth); + } + + private: + Real m_Bandwidth; + + MsgConfigureLoRaDemod(Real Bandwidth) : + Message(), + m_Bandwidth(Bandwidth) + { + } + }; + + Real m_Bandwidth; + int m_sampleRate; + int m_frequency; + int m_freq; + int m_angle; + + NCO m_nco; + Interpolator m_interpolator; + Real m_sampleDistanceRemain; + + SampleSink* m_sampleSink; + SampleVector m_sampleBuffer; +}; + +#endif // INCLUDE_LoRaDEMOD_H diff --git a/plugins/channel/lora/lorademodgui.cpp b/plugins/channel/lora/lorademodgui.cpp new file mode 100644 index 000000000..1d1c30264 --- /dev/null +++ b/plugins/channel/lora/lorademodgui.cpp @@ -0,0 +1,168 @@ +#include +#include +#include "lorademodgui.h" +#include "ui_lorademodgui.h" +#include "lorademodgui.h" +#include "ui_lorademodgui.h" +#include "dsp/threadedsamplesink.h" +#include "dsp/channelizer.h" +#include "lorademod.h" +#include "dsp/spectrumvis.h" +#include "gui/glspectrum.h" +#include "plugin/pluginapi.h" +#include "util/simpleserializer.h" +#include "gui/basicchannelsettingswidget.h" + +LoRaDemodGUI* LoRaDemodGUI::create(PluginAPI* pluginAPI) +{ + LoRaDemodGUI* gui = new LoRaDemodGUI(pluginAPI); + return gui; +} + +void LoRaDemodGUI::destroy() +{ + delete this; +} + +void LoRaDemodGUI::setName(const QString& name) +{ + setObjectName(name); +} + +void LoRaDemodGUI::resetToDefaults() +{ + ui->BW->setValue(0); + applySettings(); +} + +QByteArray LoRaDemodGUI::serialize() const +{ + SimpleSerializer s(1); + s.writeS32(1, m_channelMarker->getCenterFrequency()); + s.writeS32(2, ui->BW->value()); + s.writeBlob(3, ui->spectrumGUI->serialize()); + s.writeU32(4, m_channelMarker->getColor().rgb()); + return s.final(); +} + +bool LoRaDemodGUI::deserialize(const QByteArray& data) +{ + SimpleDeserializer d(data); + + if(!d.isValid()) { + resetToDefaults(); + return false; + } + + if(d.getVersion() == 1) { + QByteArray bytetmp; + quint32 u32tmp; + qint32 tmp; + d.readS32(1, &tmp, 0); + m_channelMarker->setCenterFrequency(tmp); + d.readS32(2, &tmp, 0); + ui->BW->setValue(tmp); + d.readBlob(3, &bytetmp); + ui->spectrumGUI->deserialize(bytetmp); + if(d.readU32(4, &u32tmp)) + m_channelMarker->setColor(u32tmp); + applySettings(); + return true; + } else { + resetToDefaults(); + return false; + } +} + +bool LoRaDemodGUI::handleMessage(Message* message) +{ + return false; +} + +void LoRaDemodGUI::viewChanged() +{ + applySettings(); +} + +void LoRaDemodGUI::on_BW_valueChanged(int value) +{ + value = 7813; + ui->BWText->setText(QString("%1 Hz").arg(value)); + m_channelMarker->setBandwidth(value); + applySettings(); +} + +void LoRaDemodGUI::onWidgetRolled(QWidget* widget, bool rollDown) +{ + /* + if((widget == ui->spectrumContainer) && (m_LoRaDemod != NULL)) + m_LoRaDemod->setSpectrum(m_threadedSampleSink->getMessageQueue(), rollDown); + */ +} + +void LoRaDemodGUI::onMenuDoubleClicked() +{ + if(!m_basicSettingsShown) { + m_basicSettingsShown = true; + BasicChannelSettingsWidget* bcsw = new BasicChannelSettingsWidget(m_channelMarker, this); + bcsw->show(); + } +} + +LoRaDemodGUI::LoRaDemodGUI(PluginAPI* pluginAPI, QWidget* parent) : + RollupWidget(parent), + ui(new Ui::LoRaDemodGUI), + m_pluginAPI(pluginAPI), + m_basicSettingsShown(false) +{ + ui->setupUi(this); + setAttribute(Qt::WA_DeleteOnClose, true); + connect(this, SIGNAL(widgetRolled(QWidget*,bool)), this, SLOT(onWidgetRolled(QWidget*,bool))); + connect(this, SIGNAL(menuDoubleClickEvent()), this, SLOT(onMenuDoubleClicked())); + + m_spectrumVis = new SpectrumVis(ui->glSpectrum); + m_LoRaDemod = new LoRaDemod(m_spectrumVis); + m_channelizer = new Channelizer(m_LoRaDemod); + m_threadedSampleSink = new ThreadedSampleSink(m_channelizer); + m_pluginAPI->addSampleSink(m_threadedSampleSink); + + ui->glSpectrum->setCenterFrequency(0); + ui->glSpectrum->setSampleRate(7813); + ui->glSpectrum->setDisplayWaterfall(true); + ui->glSpectrum->setDisplayMaxHold(true); + + m_channelMarker = new ChannelMarker(this); + m_channelMarker->setColor(Qt::magenta); + m_channelMarker->setBandwidth(7813); + m_channelMarker->setCenterFrequency(0); + m_channelMarker->setVisible(true); + connect(m_channelMarker, SIGNAL(changed()), this, SLOT(viewChanged())); + m_pluginAPI->addChannelMarker(m_channelMarker); + + ui->spectrumGUI->setBuddies(m_threadedSampleSink->getMessageQueue(), m_spectrumVis, ui->glSpectrum); + + applySettings(); +} + +LoRaDemodGUI::~LoRaDemodGUI() +{ + m_pluginAPI->removeChannelInstance(this); + m_pluginAPI->removeSampleSink(m_threadedSampleSink); + delete m_threadedSampleSink; + delete m_channelizer; + delete m_LoRaDemod; + delete m_spectrumVis; + delete m_channelMarker; + delete ui; +} + +void LoRaDemodGUI::applySettings() +{ + const int loraBW[] = {7813, 7813, 10417, 20833 }; + int thisBW = loraBW[ui->BW->value()]; + setTitleColor(m_channelMarker->getColor()); + m_channelizer->configure(m_threadedSampleSink->getMessageQueue(), + thisBW, + m_channelMarker->getCenterFrequency()); + m_LoRaDemod->configure(m_threadedSampleSink->getMessageQueue(), thisBW); +} diff --git a/plugins/channel/lora/lorademodgui.h b/plugins/channel/lora/lorademodgui.h new file mode 100644 index 000000000..f64eeed9d --- /dev/null +++ b/plugins/channel/lora/lorademodgui.h @@ -0,0 +1,56 @@ +#ifndef INCLUDE_LoRaDEMODGUI_H +#define INCLUDE_LoRaDEMODGUI_H + +#include "gui/rollupwidget.h" +#include "plugin/plugingui.h" + +class PluginAPI; +class ChannelMarker; +class ThreadedSampleSink; +class Channelizer; +class LoRaDemod; +class SpectrumVis; + +namespace Ui { + class LoRaDemodGUI; +} + +class LoRaDemodGUI : public RollupWidget, public PluginGUI { + Q_OBJECT + +public: + static LoRaDemodGUI* create(PluginAPI* pluginAPI); + void destroy(); + + void setName(const QString& name); + + void resetToDefaults(); + QByteArray serialize() const; + bool deserialize(const QByteArray& data); + + bool handleMessage(Message* message); + +private slots: + void viewChanged(); + void on_BW_valueChanged(int value); + void onWidgetRolled(QWidget* widget, bool rollDown); + void onMenuDoubleClicked(); + +private: + Ui::LoRaDemodGUI* ui; + PluginAPI* m_pluginAPI; + ChannelMarker* m_channelMarker; + bool m_basicSettingsShown; + + ThreadedSampleSink* m_threadedSampleSink; + Channelizer* m_channelizer; + LoRaDemod* m_LoRaDemod; + SpectrumVis* m_spectrumVis; + + explicit LoRaDemodGUI(PluginAPI* pluginAPI, QWidget* parent = NULL); + ~LoRaDemodGUI(); + + void applySettings(); +}; + +#endif // INCLUDE_LoRaDEMODGUI_H diff --git a/plugins/channel/lora/lorademodgui.ui b/plugins/channel/lora/lorademodgui.ui new file mode 100644 index 000000000..97afe9b35 --- /dev/null +++ b/plugins/channel/lora/lorademodgui.ui @@ -0,0 +1,136 @@ + + + LoRaDemodGUI + + + + 0 + 0 + 302 + 410 + + + + LoRa Demodulator + + + + + 35 + 35 + 242 + 96 + + + + Settings + + + + 2 + + + 3 + + + + + Bandwidth + + + + + + + 0 + + + 1 + + + 1 + + + 0 + + + Qt::Horizontal + + + + + + + + 50 + 0 + + + + 7813 Hz + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + 40 + 140 + 218 + 184 + + + + Channel Spectrum + + + + 2 + + + 3 + + + + + + 200 + 150 + + + + + + + + + + + + + GLSpectrum + QWidget +
gui/glspectrum.h
+ 1 +
+ + GLSpectrumGUI + QWidget +
gui/glspectrumgui.h
+ 1 +
+ + RollupWidget + QWidget +
gui/rollupwidget.h
+ 1 +
+
+ + +
diff --git a/plugins/channel/lora/loraplugin.cpp b/plugins/channel/lora/loraplugin.cpp new file mode 100644 index 000000000..88bd41140 --- /dev/null +++ b/plugins/channel/lora/loraplugin.cpp @@ -0,0 +1,53 @@ +#include +#include +#include "plugin/pluginapi.h" +#include "loraplugin.h" +#include "lorademodgui.h" + +const PluginDescriptor LoRaPlugin::m_pluginDescriptor = { + QString("LoRa Demodulator"), + QString("0.1"), + QString("(c) 2015 John Greb"), + QString("http://www.maintech.de"), + true, + QString("github.com/hexameron/rtl-sdrangelove") +}; + +LoRaPlugin::LoRaPlugin(QObject* parent) : + QObject(parent) +{ +} + +const PluginDescriptor& LoRaPlugin::getPluginDescriptor() const +{ + return m_pluginDescriptor; +} + +void LoRaPlugin::initPlugin(PluginAPI* pluginAPI) +{ + m_pluginAPI = pluginAPI; + + // register demodulator + QAction* action = new QAction(tr("&LoRa Demodulator"), this); + connect(action, SIGNAL(triggered()), this, SLOT(createInstanceLoRa())); + m_pluginAPI->registerChannel("de.maintech.sdrangelove.channel.lora", this, action); +} + +PluginGUI* LoRaPlugin::createChannel(const QString& channelName) +{ + if(channelName == "de.maintech.sdrangelove.channel.lora") { + LoRaDemodGUI* gui = LoRaDemodGUI::create(m_pluginAPI); + m_pluginAPI->registerChannelInstance("de.maintech.sdrangelove.channel.lora", gui); + m_pluginAPI->addChannelRollup(gui); + return gui; + } else { + return NULL; + } +} + +void LoRaPlugin::createInstanceLoRa() +{ + LoRaDemodGUI* gui = LoRaDemodGUI::create(m_pluginAPI); + m_pluginAPI->registerChannelInstance("de.maintech.sdrangelove.channel.lora", gui); + m_pluginAPI->addChannelRollup(gui); +} diff --git a/plugins/channel/lora/loraplugin.h b/plugins/channel/lora/loraplugin.h new file mode 100644 index 000000000..f63b6a1fe --- /dev/null +++ b/plugins/channel/lora/loraplugin.h @@ -0,0 +1,29 @@ +#ifndef INCLUDE_LoRaPLUGIN_H +#define INCLUDE_LoRaPLUGIN_H + +#include +#include "plugin/plugininterface.h" + +class LoRaPlugin : public QObject, PluginInterface { + Q_OBJECT + Q_INTERFACES(PluginInterface) + Q_PLUGIN_METADATA(IID "de.maintech.sdrangelove.channel.lora") + +public: + explicit LoRaPlugin(QObject* parent = NULL); + + const PluginDescriptor& getPluginDescriptor() const; + void initPlugin(PluginAPI* pluginAPI); + + PluginGUI* createChannel(const QString& channelName); + +private: + static const PluginDescriptor m_pluginDescriptor; + + PluginAPI* m_pluginAPI; + +private slots: + void createInstanceLoRa(); +}; + +#endif // INCLUDE_LoRaPLUGIN_H