From 3b6a3ff94d82a6af592ab8e28ee4a947363e5830 Mon Sep 17 00:00:00 2001 From: AlexandreRouma Date: Wed, 7 Oct 2020 22:44:54 +0200 Subject: [PATCH] Fixed scripting + cleaner code + fixed RTLTCP --- CMakeLists.txt | 6 ++ core/src/config.cpp | 3 + core/src/config.h | 7 +- core/src/core.cpp | 11 +- core/src/dsp/block.h | 1 + core/src/gui/bandplan.cpp | 5 + core/src/gui/bandplan.h | 6 +- core/src/gui/dialogs/credits.cpp | 1 + core/src/gui/frequency_select.cpp | 1 + core/src/gui/frequency_select.h | 1 - core/src/gui/gui.h | 2 + core/src/gui/icons.cpp | 3 + core/src/gui/icons.h | 3 +- core/src/gui/main_window.cpp | 44 ++++++-- core/src/gui/main_window.h | 38 +------ core/src/gui/menu.cpp | 1 + core/src/gui/menu.h | 1 - core/src/gui/menus/scripting.cpp | 1 + core/src/gui/style.cpp | 3 + core/src/gui/style.h | 3 - core/src/gui/waterfall.cpp | 5 + core/src/gui/waterfall.h | 8 +- core/src/module.cpp | 10 +- core/src/module.h | 7 -- core/src/scripting.cpp | 18 ++++ core/src/scripting.h | 3 + core/src/signal_path/audio.h | 1 - core/src/signal_path/dsp.h | 1 - radio/src/main.cpp | 159 ++++++++++++++++++++++++++++- root_dev/config.json | 6 +- root_dev/module_list.json | 9 +- rtl_tcp_source/src/main.cpp | 4 +- rtl_tcp_source/src/rtltcp_client.h | 2 +- 33 files changed, 274 insertions(+), 100 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 49729ac..828e6f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,12 @@ add_subdirectory("file_source") add_subdirectory("rtl_tcp_source") add_subdirectory("demo") +if (MSVC) + set(CMAKE_CXX_FLAGS "-O2 /std:c++17") +else() + set(CMAKE_CXX_FLAGS "-O3 -std=c++17") +endif (MSVC) + add_executable(sdrpp "src/main.cpp" "win32/resources.rc") target_link_libraries(sdrpp PRIVATE sdrpp_core) diff --git a/core/src/config.cpp b/core/src/config.cpp index 39ad79f..3c167bb 100644 --- a/core/src/config.cpp +++ b/core/src/config.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include ConfigManager::ConfigManager() { diff --git a/core/src/config.h b/core/src/config.h index 6a327d8..e5cb96f 100644 --- a/core/src/config.h +++ b/core/src/config.h @@ -1,13 +1,8 @@ #pragma once #include -#include -#include -#include -#include -#include #include -#include #include +#include using nlohmann::json; diff --git a/core/src/core.cpp b/core/src/core.cpp index abc8f94..0e47295 100644 --- a/core/src/core.cpp +++ b/core/src/core.cpp @@ -16,12 +16,11 @@ #include #include #include -#include - -#include #define STB_IMAGE_RESIZE_IMPLEMENTATION #include +#include +#include #ifdef _WIN32 #include @@ -97,9 +96,9 @@ int sdrpp_main() { // duk_pop(ctx); core::scriptManager.createScript("TestScript 1", "test.js"); - core::scriptManager.createScript("TestScript 2", "test.js"); - core::scriptManager.createScript("TestScript 3", "test.js"); - core::scriptManager.createScript("TestScript 4", "test.js"); + //core::scriptManager.createScript("TestScript 2", "test.js"); + //core::scriptManager.createScript("TestScript 3", "test.js"); + //core::scriptManager.createScript("TestScript 4", "test.js"); // TESTING diff --git a/core/src/dsp/block.h b/core/src/dsp/block.h index 644d821..865897c 100644 --- a/core/src/dsp/block.h +++ b/core/src/dsp/block.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include namespace dsp { template diff --git a/core/src/gui/bandplan.cpp b/core/src/gui/bandplan.cpp index 79bfc44..af585b1 100644 --- a/core/src/gui/bandplan.cpp +++ b/core/src/gui/bandplan.cpp @@ -1,4 +1,9 @@ #include +#include +#include +#include +#include +#include namespace bandplan { std::map bandplans; diff --git a/core/src/gui/bandplan.h b/core/src/gui/bandplan.h index 315bc13..f7c46e0 100644 --- a/core/src/gui/bandplan.h +++ b/core/src/gui/bandplan.h @@ -1,11 +1,7 @@ #pragma once #include -#include -#include -#include -#include -#include #include +#include using nlohmann::json; diff --git a/core/src/gui/dialogs/credits.cpp b/core/src/gui/dialogs/credits.cpp index 060fc92..a8fa598 100644 --- a/core/src/gui/dialogs/credits.cpp +++ b/core/src/gui/dialogs/credits.cpp @@ -1,6 +1,7 @@ #include #include #include +#include namespace credits { ImFont* bigFont; diff --git a/core/src/gui/frequency_select.cpp b/core/src/gui/frequency_select.cpp index 711b8f0..25f25f3 100644 --- a/core/src/gui/frequency_select.cpp +++ b/core/src/gui/frequency_select.cpp @@ -1,4 +1,5 @@ #include +#include bool isInArea(ImVec2 val, ImVec2 min, ImVec2 max) { return val.x >= min.x && val.x < max.x && val.y >= min.y && val.y < max.y; diff --git a/core/src/gui/frequency_select.h b/core/src/gui/frequency_select.h index ef94c97..ebbe9ba 100644 --- a/core/src/gui/frequency_select.h +++ b/core/src/gui/frequency_select.h @@ -2,7 +2,6 @@ #include #include #include -#include class FrequencySelect { public: diff --git a/core/src/gui/gui.h b/core/src/gui/gui.h index 32d48b3..aa241d3 100644 --- a/core/src/gui/gui.h +++ b/core/src/gui/gui.h @@ -8,4 +8,6 @@ namespace gui { SDRPP_EXPORT ImGui::WaterFall waterfall; SDRPP_EXPORT FrequencySelect freqSelect; SDRPP_EXPORT Menu menu; + + void selectSource(std::string name); }; \ No newline at end of file diff --git a/core/src/gui/icons.cpp b/core/src/gui/icons.cpp index b6af76d..84b98f9 100644 --- a/core/src/gui/icons.cpp +++ b/core/src/gui/icons.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include #define STB_IMAGE_IMPLEMENTATION #include diff --git a/core/src/gui/icons.h b/core/src/gui/icons.h index f932c4d..748be74 100644 --- a/core/src/gui/icons.h +++ b/core/src/gui/icons.h @@ -1,8 +1,7 @@ #pragma once #include -#include #include -#include +#include namespace icons { extern ImTextureID LOGO; diff --git a/core/src/gui/main_window.cpp b/core/src/gui/main_window.cpp index dfe641f..3d10ad5 100644 --- a/core/src/gui/main_window.cpp +++ b/core/src/gui/main_window.cpp @@ -1,5 +1,40 @@ #include #include +#include "imgui.h" +#include "imgui_impl_glfw.h" +#include "imgui_impl_opengl3.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include std::thread worker; std::mutex fft_mtx; @@ -106,7 +141,6 @@ void windowInit() { gui::waterfall.setWaterfallMax(fftMax); float frequency = core::configManager.conf["frequency"]; - gui::freqSelect.setFrequency(frequency); gui::freqSelect.frequencyChanged = false; @@ -459,12 +493,4 @@ void drawWindow() { if (showCredits) { credits::show(); } -} - -void bindVolumeVariable(float* vol) { - volume = vol; -} - -void unbindVolumeVariable() { - volume = &dummyVolume; } \ No newline at end of file diff --git a/core/src/gui/main_window.h b/core/src/gui/main_window.h index 035717d..02f6ba6 100644 --- a/core/src/gui/main_window.h +++ b/core/src/gui/main_window.h @@ -1,43 +1,7 @@ #pragma once #include "imgui.h" -#include "imgui_impl_glfw.h" -#include "imgui_impl_opengl3.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #define WINDOW_FLAGS ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoBackground void windowInit(); -void drawWindow(); -void bindVolumeVariable(float* vol); -void unbindVolumeVariable(); \ No newline at end of file +void drawWindow(); \ No newline at end of file diff --git a/core/src/gui/menu.cpp b/core/src/gui/menu.cpp index c5611f7..74feb7e 100644 --- a/core/src/gui/menu.cpp +++ b/core/src/gui/menu.cpp @@ -1,4 +1,5 @@ #include +#include Menu::Menu() { diff --git a/core/src/gui/menu.h b/core/src/gui/menu.h index 1b3c8be..6e7965e 100644 --- a/core/src/gui/menu.h +++ b/core/src/gui/menu.h @@ -1,5 +1,4 @@ #pragma once -#include #include #include #include diff --git a/core/src/gui/menus/scripting.cpp b/core/src/gui/menus/scripting.cpp index fc02c8b..c17812c 100644 --- a/core/src/gui/menus/scripting.cpp +++ b/core/src/gui/menus/scripting.cpp @@ -1,6 +1,7 @@ #include #include #include +#include namespace scriptingmenu { void init() { diff --git a/core/src/gui/style.cpp b/core/src/gui/style.cpp index ea9a6a8..2541bd5 100644 --- a/core/src/gui/style.cpp +++ b/core/src/gui/style.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include namespace style { void setDefaultStyle() { diff --git a/core/src/gui/style.h b/core/src/gui/style.h index e588539..13e62c9 100644 --- a/core/src/gui/style.h +++ b/core/src/gui/style.h @@ -1,7 +1,4 @@ #pragma once -#include -#include -#include namespace style { void setDefaultStyle(); diff --git a/core/src/gui/waterfall.cpp b/core/src/gui/waterfall.cpp index 18cc1ea..3698bcc 100644 --- a/core/src/gui/waterfall.cpp +++ b/core/src/gui/waterfall.cpp @@ -1,4 +1,9 @@ #include +#include +#include +#include +#include +#include float COLOR_MAP[][3] = { {0x00, 0x00, 0x20}, diff --git a/core/src/gui/waterfall.h b/core/src/gui/waterfall.h index 3bcf830..ac48a66 100644 --- a/core/src/gui/waterfall.h +++ b/core/src/gui/waterfall.h @@ -1,12 +1,10 @@ #pragma once -#include -#include #include #include -#include -#include #include -#include +#include +#include +#include #define WATERFALL_RESOLUTION 1000000 diff --git a/core/src/module.cpp b/core/src/module.cpp index 8417473..05c9ed9 100644 --- a/core/src/module.cpp +++ b/core/src/module.cpp @@ -1,12 +1,14 @@ #include -#include -#include -#include +#include +#include +#include +#include + +using nlohmann::json; namespace mod { std::map modules; std::vector moduleNames; - ImGui::WaterFall* _wtf; void loadModule(std::string path, std::string name) { if (!std::filesystem::exists(path)) { diff --git a/core/src/module.h b/core/src/module.h index d366ad4..807f8b4 100644 --- a/core/src/module.h +++ b/core/src/module.h @@ -1,13 +1,6 @@ #pragma once #include #include -#include -#include -#include -#include -#include -#include -#include #include #ifdef _WIN32 diff --git a/core/src/scripting.cpp b/core/src/scripting.cpp index f9a769d..193b356 100644 --- a/core/src/scripting.cpp +++ b/core/src/scripting.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include ScriptManager::ScriptManager() { @@ -42,9 +44,16 @@ duk_context* ScriptManager::createContext(ScriptManager* _this, std::string name duk_idx_t sdrppBase = duk_push_object(ctx); + // API + duk_push_string(ctx, VERSION_STR); duk_put_prop_string(ctx, sdrppBase, "version"); + duk_push_c_function(ctx, ScriptManager::duk_setSource, 1); + duk_put_prop_string(ctx, sdrppBase, "selectSource"); + + // Modules + duk_idx_t modObjId = duk_push_object(ctx); for (const auto& [name, handler] : _this->handlers) { duk_idx_t objId = duk_push_object(ctx); @@ -72,4 +81,13 @@ void ScriptManager::Script::scriptWorker(Script* script) { void ScriptManager::bindScriptRunHandler(std::string name, ScriptManager::ScriptRunHandler_t handler) { // TODO: check if it exists and add a "unbind" function handlers[name] = handler; +} + +duk_ret_t ScriptManager::duk_setSource(duk_context* dukCtx) { + const char* name = duk_require_string(dukCtx, -1); + sigpath::sourceManager.selectSource(name); + + duk_pop_n(dukCtx, 1); // Pop demod name, this and context + + return 0; } \ No newline at end of file diff --git a/core/src/scripting.h b/core/src/scripting.h index 34c2304..dbc6c64 100644 --- a/core/src/scripting.h +++ b/core/src/scripting.h @@ -43,6 +43,9 @@ public: private: static duk_context* createContext(ScriptManager* _this, std::string name); + // API + static duk_ret_t duk_setSource(duk_context* ctx); + std::map handlers; }; \ No newline at end of file diff --git a/core/src/signal_path/audio.h b/core/src/signal_path/audio.h index 8f8fe9a..49ac6a1 100644 --- a/core/src/signal_path/audio.h +++ b/core/src/signal_path/audio.h @@ -3,7 +3,6 @@ #include #include #include -#include namespace audio { enum { diff --git a/core/src/signal_path/dsp.h b/core/src/signal_path/dsp.h index 850f854..215ecb2 100644 --- a/core/src/signal_path/dsp.h +++ b/core/src/signal_path/dsp.h @@ -8,7 +8,6 @@ #include #include #include -#include #include #include diff --git a/radio/src/main.cpp b/radio/src/main.cpp index feb565b..7d4a34c 100644 --- a/radio/src/main.cpp +++ b/radio/src/main.cpp @@ -129,6 +129,21 @@ private: duk_push_c_function(dukCtx, duk_setDemodulator, 1); duk_put_prop_string(dukCtx, objId, "setDemodulator"); + + duk_push_c_function(dukCtx, duk_getDemodulator, 0); + duk_put_prop_string(dukCtx, objId, "getDemodulator"); + + duk_push_c_function(dukCtx, duk_setBandwidth, 1); + duk_put_prop_string(dukCtx, objId, "setBandwidth"); + + duk_push_c_function(dukCtx, duk_getBandwidth, 0); + duk_put_prop_string(dukCtx, objId, "getBandwidth"); + + duk_push_c_function(dukCtx, duk_getMaxBandwidth, 0); + duk_put_prop_string(dukCtx, objId, "getMaxBandwidth"); + + duk_push_c_function(dukCtx, duk_getMinBandwidth, 0); + duk_put_prop_string(dukCtx, objId, "getMinBandwidth"); duk_push_pointer(dukCtx, ctx); duk_put_prop_string(dukCtx, objId, DUK_HIDDEN_SYMBOL("radio_ctx")); @@ -138,17 +153,159 @@ private: const char* str = duk_require_string(dukCtx, -1); std::string modName = str; - if (modName == "USB") { + duk_push_this(dukCtx); + if (!duk_get_prop_literal(dukCtx, -1, DUK_HIDDEN_SYMBOL("radio_ctx"))) { + printf("COULD NOT RETRIEVE POINTER\n"); + } + + RadioModule* _this = (RadioModule*)duk_require_pointer(dukCtx, -1); + + duk_pop_n(dukCtx, 3); // Pop demod name, this and context + + if (modName == "NFM") { + _this->demod = 0; + _this->bandWidth = 16000; + _this->bandWidthMin = 8000; + _this->bandWidthMax = 16000; + _this->sigPath.setDemodulator(SigPath::DEMOD_NFM, _this->bandWidth); + } + else if (modName == "WFM") { + _this->demod = 1; + _this->bandWidth = 200000; + _this->bandWidthMin = 100000; + _this->bandWidthMax = 200000; + _this->sigPath.setDemodulator(SigPath::DEMOD_FM, _this->bandWidth); + } + else if (modName == "AM") { + _this->demod = 2; + _this->bandWidth = 12500; + _this->bandWidthMin = 6250; + _this->bandWidthMax = 12500; + _this->sigPath.setDemodulator(SigPath::DEMOD_AM, _this->bandWidth); + } + else if (modName == "DSB") { + _this->demod = 3; + _this->bandWidth = 6000; + _this->bandWidthMin = 3000; + _this->bandWidthMax = 6000; + _this->sigPath.setDemodulator(SigPath::DEMOD_DSB, _this->bandWidth); + } + else if (modName == "USB") { _this->demod = 4; _this->bandWidth = 3000; _this->bandWidthMin = 1500; _this->bandWidthMax = 3000; _this->sigPath.setDemodulator(SigPath::DEMOD_USB, _this->bandWidth); } + else if (modName == "CW") { _this->demod = 5; } + else if (modName == "LSB") { + _this->demod = 6; + _this->bandWidth = 3000; + _this->bandWidthMin = 1500; + _this->bandWidthMax = 3000; + _this->sigPath.setDemodulator(SigPath::DEMOD_LSB, _this->bandWidth); + } + else if (modName == "RAW") { + _this->demod = 7; + _this->bandWidth = 10000; + _this->bandWidthMin = 3000; + _this->bandWidthMax = 10000; + _this->sigPath.setDemodulator(SigPath::DEMOD_RAW, _this->bandWidth); + } + else { + duk_push_string(dukCtx, "Invalid demodulator name"); + duk_throw(dukCtx); + } return 0; } + static duk_ret_t duk_getDemodulator(duk_context* dukCtx) { + duk_push_this(dukCtx); + if (!duk_get_prop_literal(dukCtx, -1, DUK_HIDDEN_SYMBOL("radio_ctx"))) { + printf("COULD NOT RETRIEVE POINTER\n"); + } + + RadioModule* _this = (RadioModule*)duk_require_pointer(dukCtx, -1); + + duk_pop_n(dukCtx, 2); // Pop demod name, this and context + + const char* demodNames[] = {"NFM", "WFM", "AM", "DSB", "USB", "CW", "LSB", "RAW"}; + + duk_push_string(dukCtx, demodNames[_this->demod]); + + return 1; + } + + static duk_ret_t duk_setBandwidth(duk_context* dukCtx) { + double bandwidth = duk_require_number(dukCtx, -1); + + duk_push_this(dukCtx); + if (!duk_get_prop_literal(dukCtx, -1, DUK_HIDDEN_SYMBOL("radio_ctx"))) { + printf("COULD NOT RETRIEVE POINTER\n"); + } + + RadioModule* _this = (RadioModule*)duk_require_pointer(dukCtx, -1); + + duk_pop_n(dukCtx, 3); // Pop demod name, this and context + + if (bandwidth > _this->bandWidthMax || bandwidth < _this->bandWidthMin) { + duk_push_string(dukCtx, "Bandwidth out of range"); + duk_throw(dukCtx); + } + + _this->bandWidth = bandwidth; + _this->bandWidth = std::clamp(_this->bandWidth, _this->bandWidthMin, _this->bandWidthMax); + _this->sigPath.setBandwidth(_this->bandWidth); + + return 0; + } + + static duk_ret_t duk_getBandwidth(duk_context* dukCtx) { + duk_push_this(dukCtx); + if (!duk_get_prop_literal(dukCtx, -1, DUK_HIDDEN_SYMBOL("radio_ctx"))) { + printf("COULD NOT RETRIEVE POINTER\n"); + } + + RadioModule* _this = (RadioModule*)duk_require_pointer(dukCtx, -1); + + duk_pop_n(dukCtx, 2); // Pop demod name, this and context + + duk_push_number(dukCtx, _this->bandWidth); + + return 1; + } + + static duk_ret_t duk_getMaxBandwidth(duk_context* dukCtx) { + duk_push_this(dukCtx); + if (!duk_get_prop_literal(dukCtx, -1, DUK_HIDDEN_SYMBOL("radio_ctx"))) { + printf("COULD NOT RETRIEVE POINTER\n"); + } + + RadioModule* _this = (RadioModule*)duk_require_pointer(dukCtx, -1); + + duk_pop_n(dukCtx, 2); // Pop demod name, this and context + + duk_push_number(dukCtx, _this->bandWidthMax); + + return 1; + } + + static duk_ret_t duk_getMinBandwidth(duk_context* dukCtx) { + duk_push_this(dukCtx); + if (!duk_get_prop_literal(dukCtx, -1, DUK_HIDDEN_SYMBOL("radio_ctx"))) { + printf("COULD NOT RETRIEVE POINTER\n"); + } + + RadioModule* _this = (RadioModule*)duk_require_pointer(dukCtx, -1); + + duk_pop_n(dukCtx, 2); // Pop demod name, this and context + + duk_push_number(dukCtx, _this->bandWidthMin); + + return 1; + } + std::string name; int demod = 1; int deemp = 0; diff --git a/root_dev/config.json b/root_dev/config.json index 190f7d8..b999d15 100644 --- a/root_dev/config.json +++ b/root_dev/config.json @@ -3,7 +3,7 @@ "bandPlan": "General", "bandPlanEnabled": true, "fftHeight": 300, - "frequency": 96571704, + "frequency": 96035792, "max": 0.0, "maximized": false, "menuOrder": [ @@ -21,7 +21,7 @@ "source": "", "sourceSettings": {}, "windowSize": { - "h": 720, - "w": 1280 + "h": 1053, + "w": 959 } } \ No newline at end of file diff --git a/root_dev/module_list.json b/root_dev/module_list.json index a64f080..5a29e31 100644 --- a/root_dev/module_list.json +++ b/root_dev/module_list.json @@ -1,7 +1,6 @@ { - "Radio": "./radio/Release/radio.dll", - "Recorder": "./recorder/Release/recorder.dll", - "Soapy": "./soapy/Release/soapy.dll", - "FileSource": "./file_source/Release/file_source.dll", - "RTLTCPSource": "./rtl_tcp_source/Release/rtl_tcp_source.dll" + "Radio": "./radio/radio.so", + "Recorder": "./recorder/recorder.so", + "Soapy": "./soapy/soapy.so", + "RTLTCPSource": "./rtl_tcp_source/rtl_tcp_source.so" } diff --git a/rtl_tcp_source/src/main.cpp b/rtl_tcp_source/src/main.cpp index a072432..e247afb 100644 --- a/rtl_tcp_source/src/main.cpp +++ b/rtl_tcp_source/src/main.cpp @@ -10,8 +10,8 @@ #define CONCAT(a, b) ((std::string(a) + b).c_str()) MOD_INFO { - /* Name: */ "fike_source", - /* Description: */ "File input module for SDR++", + /* Name: */ "rtl_tcp_source", + /* Description: */ "RTL-TCP input module for SDR++", /* Author: */ "Ryzerth", /* Version: */ "0.1.0" }; diff --git a/rtl_tcp_source/src/rtltcp_client.h b/rtl_tcp_source/src/rtltcp_client.h index 09d6818..5ffc771 100644 --- a/rtl_tcp_source/src/rtltcp_client.h +++ b/rtl_tcp_source/src/rtltcp_client.h @@ -87,7 +87,7 @@ public: bzero(&serv_addr, sizeof(struct sockaddr_in)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); - serv_addr.sin_port = port; + serv_addr.sin_port = htons(port); if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) { // TODO: log error return false;