mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2025-07-09 18:45:22 +02:00
pushing work for the future new source system
This commit is contained in:
@ -15,10 +15,6 @@ namespace sourcemenu {
|
||||
bool iqCorrection = false;
|
||||
bool invertIQ = false;
|
||||
|
||||
EventHandler<std::string> sourceRegisteredHandler;
|
||||
EventHandler<std::string> sourceUnregisterHandler;
|
||||
EventHandler<std::string> sourceUnregisteredHandler;
|
||||
|
||||
std::vector<std::string> sourceNames;
|
||||
std::string sourceNamesTxt;
|
||||
std::string selectedSource;
|
||||
@ -99,10 +95,10 @@ namespace sourcemenu {
|
||||
}
|
||||
sourceId = std::distance(sourceNames.begin(), it);
|
||||
selectedSource = sourceNames[sourceId];
|
||||
sigpath::sourceManager.selectSource(sourceNames[sourceId]);
|
||||
sigpath::sourceManager.select(sourceNames[sourceId]);
|
||||
}
|
||||
|
||||
void onSourceRegistered(std::string name, void* ctx) {
|
||||
void onSourceRegistered(std::string name) {
|
||||
refreshSources();
|
||||
|
||||
if (selectedSource.empty()) {
|
||||
@ -114,13 +110,13 @@ namespace sourcemenu {
|
||||
sourceId = std::distance(sourceNames.begin(), std::find(sourceNames.begin(), sourceNames.end(), selectedSource));
|
||||
}
|
||||
|
||||
void onSourceUnregister(std::string name, void* ctx) {
|
||||
void onSourceUnregister(std::string name) {
|
||||
if (name != selectedSource) { return; }
|
||||
|
||||
// TODO: Stop everything
|
||||
}
|
||||
|
||||
void onSourceUnregistered(std::string name, void* ctx) {
|
||||
void onSourceUnregistered(std::string name) {
|
||||
refreshSources();
|
||||
|
||||
if (sourceNames.empty()) {
|
||||
@ -153,12 +149,9 @@ namespace sourcemenu {
|
||||
selectSource(selected);
|
||||
sigpath::iqFrontEnd.setDecimation(1 << decimationPower);
|
||||
|
||||
sourceRegisteredHandler.handler = onSourceRegistered;
|
||||
sourceUnregisterHandler.handler = onSourceUnregister;
|
||||
sourceUnregisteredHandler.handler = onSourceUnregistered;
|
||||
sigpath::sourceManager.onSourceRegistered.bindHandler(&sourceRegisteredHandler);
|
||||
sigpath::sourceManager.onSourceUnregister.bindHandler(&sourceUnregisterHandler);
|
||||
sigpath::sourceManager.onSourceUnregistered.bindHandler(&sourceUnregisteredHandler);
|
||||
sigpath::sourceManager.onSourceRegistered.bind(onSourceRegistered);
|
||||
sigpath::sourceManager.onSourceUnregister.bind(onSourceUnregister);
|
||||
sigpath::sourceManager.onSourceUnregistered.bind(onSourceUnregistered);
|
||||
|
||||
core::configManager.release();
|
||||
}
|
||||
@ -179,7 +172,7 @@ namespace sourcemenu {
|
||||
|
||||
if (running) { style::endDisabled(); }
|
||||
|
||||
sigpath::sourceManager.showSelectedMenu();
|
||||
sigpath::sourceManager.showMenu();
|
||||
|
||||
if (ImGui::Checkbox("IQ Correction##_sdrpp_iq_corr", &iqCorrection)) {
|
||||
sigpath::iqFrontEnd.setDCBlocking(iqCorrection);
|
||||
|
@ -146,7 +146,7 @@ namespace server {
|
||||
// Load sourceId from config
|
||||
sourceId = 0;
|
||||
if (sourceList.keyExists(sourceName)) { sourceId = sourceList.keyId(sourceName); }
|
||||
sigpath::sourceManager.selectSource(sourceList[sourceId]);
|
||||
sigpath::sourceManager.select(sourceList[sourceId]);
|
||||
|
||||
// TODO: Use command line option
|
||||
std::string host = (std::string)core::args["addr"];
|
||||
@ -280,8 +280,7 @@ namespace server {
|
||||
}
|
||||
}
|
||||
else if (cmd == COMMAND_START) {
|
||||
sigpath::sourceManager.start();
|
||||
running = true;
|
||||
running = sigpath::sourceManager.start();
|
||||
}
|
||||
else if (cmd == COMMAND_STOP) {
|
||||
sigpath::sourceManager.stop();
|
||||
@ -309,14 +308,14 @@ namespace server {
|
||||
SmGui::FillWidth();
|
||||
SmGui::ForceSync();
|
||||
if (SmGui::Combo("##sdrpp_server_src_sel", &sourceId, sourceList.txt)) {
|
||||
sigpath::sourceManager.selectSource(sourceList[sourceId]);
|
||||
sigpath::sourceManager.select(sourceList[sourceId]);
|
||||
core::configManager.acquire();
|
||||
core::configManager.conf["source"] = sourceList.key(sourceId);
|
||||
core::configManager.release(true);
|
||||
}
|
||||
if (running) { SmGui::EndDisabled(); }
|
||||
|
||||
sigpath::sourceManager.showSelectedMenu();
|
||||
sigpath::sourceManager.showMenu();
|
||||
}
|
||||
|
||||
void renderUI(SmGui::DrawList* dl, std::string diffId, SmGui::DrawListElem diffValue) {
|
||||
|
@ -1,106 +1,186 @@
|
||||
#include <server.h>
|
||||
#include <signal_path/source.h>
|
||||
#include "source.h"
|
||||
#include <utils/flog.h>
|
||||
#include <signal_path/signal_path.h>
|
||||
#include <core.h>
|
||||
|
||||
SourceManager::SourceManager() {
|
||||
}
|
||||
void SourceManager::registerSource(const std::string& name, Source* source) {
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
|
||||
void SourceManager::registerSource(std::string name, SourceHandler* handler) {
|
||||
// Check arguments
|
||||
if (source || name.empty()) {
|
||||
flog::error("Invalid argument to register source", name);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check that a source with that name doesn't already exist
|
||||
if (sources.find(name) != sources.end()) {
|
||||
flog::error("Tried to register new source with existing name: {0}", name);
|
||||
flog::error("Tried to register source with existing name: {}", name);
|
||||
return;
|
||||
}
|
||||
sources[name] = handler;
|
||||
onSourceRegistered.emit(name);
|
||||
|
||||
// Add source to map
|
||||
sources[name] = source;
|
||||
|
||||
// Add source to lists
|
||||
sourceNames.push_back(name);
|
||||
onSourceRegistered(name);
|
||||
}
|
||||
|
||||
void SourceManager::unregisterSource(std::string name) {
|
||||
void SourceManager::unregisterSource(const std::string& name) {
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
|
||||
// Check that a source with that name exists
|
||||
if (sources.find(name) == sources.end()) {
|
||||
flog::error("Tried to unregister non existent source: {0}", name);
|
||||
flog::error("Tried to unregister a non-existent source: {}", name);
|
||||
return;
|
||||
}
|
||||
onSourceUnregister.emit(name);
|
||||
if (name == selectedName) {
|
||||
if (selectedHandler != NULL) {
|
||||
sources[selectedName]->deselectHandler(sources[selectedName]->ctx);
|
||||
}
|
||||
sigpath::iqFrontEnd.setInput(&nullSource);
|
||||
selectedHandler = NULL;
|
||||
}
|
||||
|
||||
// Notify event listeners of the imminent deletion
|
||||
onSourceUnregister(name);
|
||||
|
||||
// Delete from lists
|
||||
sourceNames.erase(std::find(sourceNames.begin(), sourceNames.end(), name));
|
||||
sources.erase(name);
|
||||
onSourceUnregistered.emit(name);
|
||||
|
||||
// Notify event listeners of the deletion
|
||||
onSourceUnregistered(name);
|
||||
}
|
||||
|
||||
std::vector<std::string> SourceManager::getSourceNames() {
|
||||
std::vector<std::string> names;
|
||||
for (auto const& [name, src] : sources) { names.push_back(name); }
|
||||
return names;
|
||||
const std::vector<std::string>& SourceManager::getSourceNames() {
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
return sourceNames;
|
||||
}
|
||||
|
||||
void SourceManager::selectSource(std::string name) {
|
||||
void SourceManager::select(const std::string& name) {
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
|
||||
// make sure that source isn't currently selected
|
||||
if (selectedSourceName == name) { return; }
|
||||
|
||||
// Deselect current source
|
||||
deselect();
|
||||
|
||||
// Check that a source with that name exists
|
||||
if (sources.find(name) == sources.end()) {
|
||||
flog::error("Tried to select non existent source: {0}", name);
|
||||
flog::error("Tried to select a non-existent source: {}", name);
|
||||
return;
|
||||
}
|
||||
if (selectedHandler != NULL) {
|
||||
sources[selectedName]->deselectHandler(sources[selectedName]->ctx);
|
||||
}
|
||||
selectedHandler = sources[name];
|
||||
selectedHandler->selectHandler(selectedHandler->ctx);
|
||||
selectedName = name;
|
||||
if (core::args["server"].b()) {
|
||||
server::setInput(selectedHandler->stream);
|
||||
}
|
||||
else {
|
||||
sigpath::iqFrontEnd.setInput(selectedHandler->stream);
|
||||
}
|
||||
// Set server input here
|
||||
|
||||
// Select the source
|
||||
selectedSourceName = name;
|
||||
selectedSource = sources[name];
|
||||
|
||||
// Call the selected source
|
||||
selectedSource->select();
|
||||
|
||||
// Retune to make sure the source has the latest frequency
|
||||
tune(frequency);
|
||||
}
|
||||
|
||||
void SourceManager::showSelectedMenu() {
|
||||
if (selectedHandler == NULL) {
|
||||
return;
|
||||
}
|
||||
selectedHandler->menuHandler(selectedHandler->ctx);
|
||||
const std::string& SourceManager::getSelected() {
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
return selectedSourceName;
|
||||
}
|
||||
|
||||
void SourceManager::start() {
|
||||
if (selectedHandler == NULL) {
|
||||
return;
|
||||
}
|
||||
selectedHandler->startHandler(selectedHandler->ctx);
|
||||
bool SourceManager::start() {
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
|
||||
// Check if not already running
|
||||
if (running) { return true; }
|
||||
|
||||
// Call source if selected and save if started
|
||||
running = (!selectedSource) ? false : selectedSource->start();
|
||||
|
||||
return running;
|
||||
}
|
||||
|
||||
void SourceManager::stop() {
|
||||
if (selectedHandler == NULL) {
|
||||
return;
|
||||
}
|
||||
selectedHandler->stopHandler(selectedHandler->ctx);
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
|
||||
// Check if running
|
||||
if (!running) { return; }
|
||||
|
||||
// Call source if selected and save state
|
||||
if (selectedSource) { selectedSource->stop(); }
|
||||
running = false;
|
||||
}
|
||||
|
||||
bool SourceManager::isRunning() {
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
return running;
|
||||
}
|
||||
|
||||
void SourceManager::tune(double freq) {
|
||||
if (selectedHandler == NULL) {
|
||||
return;
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
|
||||
// Save frequency
|
||||
frequency = freq;
|
||||
|
||||
// Call source if selected
|
||||
if (selectedSource) {
|
||||
selectedSource->tune(((mode == TUNING_MODE_NORMAL) ? freq : ifFrequency) + offset);
|
||||
}
|
||||
// TODO: No need to always retune the hardware in panadpter mode
|
||||
selectedHandler->tuneHandler(((tuneMode == TuningMode::NORMAL) ? freq : ifFreq) + tuneOffset, selectedHandler->ctx);
|
||||
onRetune.emit(freq);
|
||||
currentFreq = freq;
|
||||
}
|
||||
|
||||
void SourceManager::showMenu() {
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
|
||||
// Call source if selected
|
||||
if (selectedSource) { selectedSource->showMenu(); }
|
||||
}
|
||||
|
||||
double SourceManager::getSamplerate() {
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
return samplerate;
|
||||
}
|
||||
|
||||
// =========== TODO: These functions should not happen in this class ===========
|
||||
|
||||
void SourceManager::setTuningOffset(double offset) {
|
||||
tuneOffset = offset;
|
||||
tune(currentFreq);
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
|
||||
// Update offset
|
||||
this->offset = offset;
|
||||
|
||||
// Retune to take affect
|
||||
tune(frequency);
|
||||
}
|
||||
|
||||
void SourceManager::setTuningMode(TuningMode mode) {
|
||||
tuneMode = mode;
|
||||
tune(currentFreq);
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
|
||||
// Update mode
|
||||
this->mode = mode;
|
||||
|
||||
// Retune to take affect
|
||||
tune(frequency);
|
||||
}
|
||||
|
||||
void SourceManager::setPanadpterIF(double freq) {
|
||||
ifFreq = freq;
|
||||
tune(currentFreq);
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
|
||||
// Update offset
|
||||
ifFrequency = freq;
|
||||
|
||||
// Return to take affect if in panadapter mode
|
||||
if (mode == TUNING_MODE_PANADAPTER) { tune(frequency); }
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
void SourceManager::deselect() {
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
|
||||
// Call source if selected
|
||||
if (selectedSource) { selectedSource->deselect(); }
|
||||
|
||||
// Mark as deselected
|
||||
selectedSourceName.clear();
|
||||
selectedSource = NULL;
|
||||
}
|
||||
|
||||
void SourceManager::setSamplerate(double samplerate) {
|
||||
std::lock_guard<std::recursive_mutex> lck(mtx);
|
||||
|
||||
// Save samplerate and emit event
|
||||
this->samplerate = samplerate;
|
||||
onSamplerateChanged(samplerate);
|
||||
}
|
@ -1,56 +1,153 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <dsp/stream.h>
|
||||
#include <mutex>
|
||||
#include <dsp/types.h>
|
||||
#include <dsp/stream.h>
|
||||
#include <utils/event.h>
|
||||
|
||||
enum TuningMode {
|
||||
TUNING_MODE_NORMAL,
|
||||
TUNING_MODE_PANADAPTER
|
||||
};
|
||||
|
||||
class Source;
|
||||
|
||||
class SourceManager {
|
||||
friend Source;
|
||||
public:
|
||||
SourceManager();
|
||||
/**
|
||||
* Register a source.
|
||||
* @param name Name of the source.
|
||||
* @param source Pointer to the source instance.
|
||||
*/
|
||||
void registerSource(const std::string& name, Source* source);
|
||||
|
||||
struct SourceHandler {
|
||||
dsp::stream<dsp::complex_t>* stream;
|
||||
void (*menuHandler)(void* ctx);
|
||||
void (*selectHandler)(void* ctx);
|
||||
void (*deselectHandler)(void* ctx);
|
||||
void (*startHandler)(void* ctx);
|
||||
void (*stopHandler)(void* ctx);
|
||||
void (*tuneHandler)(double freq, void* ctx);
|
||||
void* ctx;
|
||||
};
|
||||
/**
|
||||
* Unregister a source.
|
||||
* @param name Name of the source.
|
||||
*/
|
||||
void unregisterSource(const std::string& name);
|
||||
|
||||
enum TuningMode {
|
||||
NORMAL,
|
||||
PANADAPTER
|
||||
};
|
||||
/**
|
||||
* Get a list of source names.
|
||||
* @return List of source names.
|
||||
*/
|
||||
const std::vector<std::string>& getSourceNames();
|
||||
|
||||
void registerSource(std::string name, SourceHandler* handler);
|
||||
void unregisterSource(std::string name);
|
||||
void selectSource(std::string name);
|
||||
void showSelectedMenu();
|
||||
void start();
|
||||
/**
|
||||
* Select a source.
|
||||
* @param name Name of the source.
|
||||
*/
|
||||
void select(const std::string& name);
|
||||
|
||||
/**
|
||||
* Get the name of the currently selected source.
|
||||
* @return Name of the source or empty if no source is selected.
|
||||
*/
|
||||
const std::string& getSelected();
|
||||
|
||||
/**
|
||||
* Start the radio.
|
||||
* @return True if the radio started successfully, false if not.
|
||||
*/
|
||||
bool start();
|
||||
|
||||
/**
|
||||
* Stop the radio.
|
||||
*/
|
||||
void stop();
|
||||
|
||||
/**
|
||||
* Check if the radio is running.
|
||||
* @return True if the radio is running, false if not.
|
||||
*/
|
||||
bool isRunning();
|
||||
|
||||
/**
|
||||
* Tune the radio.
|
||||
* @param freq Frequency in Hz.
|
||||
*/
|
||||
void tune(double freq);
|
||||
|
||||
/**
|
||||
* Tune the radio.
|
||||
* @param freq Frequency to tune the radio to.
|
||||
*/
|
||||
void showMenu();
|
||||
|
||||
/**
|
||||
* Get the current samplerate of the radio.
|
||||
* @return Samplerate in Hz.
|
||||
*/
|
||||
double getSamplerate();
|
||||
|
||||
// =========== TODO: These functions should not happen in this class ===========
|
||||
|
||||
/**
|
||||
* Set offset to add to the tuned frequency.
|
||||
* @param offset Offset in Hz.
|
||||
*/
|
||||
void setTuningOffset(double offset);
|
||||
|
||||
/**
|
||||
* Set tuning mode.
|
||||
* @param mode Tuning mode.
|
||||
*/
|
||||
void setTuningMode(TuningMode mode);
|
||||
|
||||
/**
|
||||
* Set panadapter mode IF frequency.
|
||||
* @param freq IF frequency in Hz.
|
||||
*/
|
||||
void setPanadpterIF(double freq);
|
||||
|
||||
std::vector<std::string> getSourceNames();
|
||||
// =============================================================================
|
||||
|
||||
// Emitted after a new source has been registered.
|
||||
Event<std::string> onSourceRegistered;
|
||||
|
||||
// Emitted when a source is about to be unregistered.
|
||||
Event<std::string> onSourceUnregister;
|
||||
|
||||
// Emitted after a source has been unregistered.
|
||||
Event<std::string> onSourceUnregistered;
|
||||
|
||||
// Emitted when the samplerate of the incoming IQ has changed.
|
||||
Event<double> onSamplerateChanged;
|
||||
|
||||
// Emitted when the source manager is instructed to tune the radio.
|
||||
Event<double> onRetune;
|
||||
|
||||
private:
|
||||
std::map<std::string, SourceHandler*> sources;
|
||||
std::string selectedName;
|
||||
SourceHandler* selectedHandler = NULL;
|
||||
double tuneOffset;
|
||||
double currentFreq;
|
||||
double ifFreq = 0.0;
|
||||
TuningMode tuneMode = TuningMode::NORMAL;
|
||||
dsp::stream<dsp::complex_t> nullSource;
|
||||
void deselect();
|
||||
void setSamplerate(double samplerate);
|
||||
|
||||
std::vector<std::string> sourceNames;
|
||||
std::map<std::string, Source*> sources;
|
||||
|
||||
std::string selectedSourceName = "";
|
||||
Source* selectedSource = NULL;
|
||||
|
||||
bool running = false;
|
||||
double samplerate = 1e6;
|
||||
double frequency = 100e6;
|
||||
double offset = 0;
|
||||
double ifFrequency = 8.830e6;
|
||||
TuningMode mode = TUNING_MODE_NORMAL;
|
||||
|
||||
std::recursive_mutex mtx;
|
||||
};
|
||||
|
||||
class Source {
|
||||
public:
|
||||
virtual void showMenu() {}
|
||||
virtual void select() = 0;
|
||||
virtual void deselect() {}
|
||||
virtual bool start() = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual void tune(double freq) {}
|
||||
|
||||
dsp::stream<dsp::complex_t> stream;
|
||||
};
|
@ -1,43 +1,51 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <utils/flog.h>
|
||||
#include <functional>
|
||||
#include <stdexcept>
|
||||
#include <mutex>
|
||||
#include <map>
|
||||
|
||||
template <class T>
|
||||
struct EventHandler {
|
||||
EventHandler() {}
|
||||
EventHandler(void (*handler)(T, void*), void* ctx) {
|
||||
this->handler = handler;
|
||||
this->ctx = ctx;
|
||||
}
|
||||
typedef int HandlerID;
|
||||
|
||||
void (*handler)(T, void*);
|
||||
void* ctx;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
template <typename... Args>
|
||||
class Event {
|
||||
using Handler = std::function<void(Args...)>;
|
||||
public:
|
||||
Event() {}
|
||||
~Event() {}
|
||||
|
||||
void emit(T value) {
|
||||
for (auto const& handler : handlers) {
|
||||
handler->handler(value, handler->ctx);
|
||||
}
|
||||
HandlerID bind(Handler handler) {
|
||||
std::lock_guard<std::mutex> lck(mtx);
|
||||
HandlerID id = genID();
|
||||
handlers[id] = handler;
|
||||
return id;
|
||||
}
|
||||
|
||||
void bindHandler(EventHandler<T>* handler) {
|
||||
handlers.push_back(handler);
|
||||
template<typename MHandler, class T>
|
||||
HandlerID bind(MHandler handler, T* ctx) {
|
||||
return bind([=](Args... args){
|
||||
(ctx->*handler)(args...);
|
||||
});
|
||||
}
|
||||
|
||||
void unbindHandler(EventHandler<T>* handler) {
|
||||
if (std::find(handlers.begin(), handlers.end(), handler) == handlers.end()) {
|
||||
flog::error("Tried to remove a non-existent event handler");
|
||||
return;
|
||||
void unbind(HandlerID id) {
|
||||
std::lock_guard<std::mutex> lck(mtx);
|
||||
if (handlers.find(id) == handlers.end()) {
|
||||
throw std::runtime_error("Could not unbind handler, unknown ID");
|
||||
}
|
||||
handlers.erase(id);
|
||||
}
|
||||
|
||||
void operator()(Args... args) {
|
||||
std::lock_guard<std::mutex> lck(mtx);
|
||||
for (const auto& [desc, handler] : handlers) {
|
||||
handler(args...);
|
||||
}
|
||||
handlers.erase(std::remove(handlers.begin(), handlers.end(), handler), handlers.end());
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<EventHandler<T>*> handlers;
|
||||
HandlerID genID() {
|
||||
int id;
|
||||
for (id = 1; handlers.find(id) != handlers.end(); id++);
|
||||
return id;
|
||||
}
|
||||
|
||||
std::map<HandlerID, Handler> handlers;
|
||||
std::mutex mtx;
|
||||
};
|
Reference in New Issue
Block a user