diff --git a/CMakeLists.txt b/CMakeLists.txt index 306812f6..39bf477d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,8 +8,9 @@ add_subdirectory("recorder") add_subdirectory("soapy") add_subdirectory("file_source") add_subdirectory("rtl_tcp_source") -add_subdirectory("audio_sink") +# add_subdirectory("audio_sink") add_subdirectory("rx888_source") +add_subdirectory("plutosdr_source") add_subdirectory("demo") if (MSVC) diff --git a/plutosdr_source/CMakeLists.txt b/plutosdr_source/CMakeLists.txt new file mode 100644 index 00000000..38480820 --- /dev/null +++ b/plutosdr_source/CMakeLists.txt @@ -0,0 +1,32 @@ +cmake_minimum_required(VERSION 3.13) +project(plutosdr_source) + +if (MSVC) + set(CMAKE_CXX_FLAGS "-O2 /std:c++17") +else() + set(CMAKE_CXX_FLAGS "-O3 -std=c++17 -fpermissive") +endif (MSVC) + +include_directories("src/") + +file(GLOB SRC "src/*.cpp") + +add_library(plutosdr_source SHARED ${SRC}) +target_link_libraries(plutosdr_source PRIVATE sdrpp_core) +set_target_properties(plutosdr_source PROPERTIES PREFIX "") + +if (MSVC) + # Lib path + target_link_directories(sdrpp_core PUBLIC "C:/Program Files/PothosSDR/lib/") + target_include_directories(sdrpp_core PUBLIC "C:/Program Files/PothosSDR/include/") + + target_link_libraries(plutosdr_source PUBLIC libiio) +else (MSVC) + find_package(PkgConfig) + + pkg_check_modules(SOAPY REQUIRED iio) + + target_include_directories(plutosdr_source PUBLIC ${IIO_INCLUDE_DIRS}) + target_link_directories(plutosdr_source PUBLIC ${IIO_LIBRARY_DIRS}) + target_link_libraries(plutosdr_source PUBLIC ${IIO_LIBRARIES}) +endif (MSVC) \ No newline at end of file diff --git a/plutosdr_source/src/main.cpp b/plutosdr_source/src/main.cpp new file mode 100644 index 00000000..882d48e0 --- /dev/null +++ b/plutosdr_source/src/main.cpp @@ -0,0 +1,222 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define CONCAT(a, b) ((std::string(a) + b).c_str()) + +MOD_INFO { + /* Name: */ "plutosdr_source", + /* Description: */ "PlutoSDR input module for SDR++", + /* Author: */ "Ryzerth", + /* Version: */ "0.1.0" +}; + +const char* gainModes[] = { + "manual", "fast_attack", "slow_attack", "hybrid" +}; + +const char* gainModesTxt = "Manual\0Fast Attack\0Slow Attack\0Hybrid\0"; + +class PlutoSDRSourceModule { +public: + PlutoSDRSourceModule(std::string name) { + this->name = name; + + sampleRate = 4000000.0; + + handler.ctx = this; + handler.selectHandler = menuSelected; + handler.deselectHandler = menuDeselected; + handler.menuHandler = menuHandler; + handler.startHandler = start; + handler.stopHandler = stop; + handler.tuneHandler = tune; + handler.stream = &stream; + sigpath::sourceManager.registerSource("PlutoSDR", &handler); + + spdlog::info("PlutoSDRSourceModule '{0}': Instance created!", name); + } + + ~PlutoSDRSourceModule() { + spdlog::info("PlutoSDRSourceModule '{0}': Instance deleted!", name); + } + +private: + static void menuSelected(void* ctx) { + PlutoSDRSourceModule* _this = (PlutoSDRSourceModule*)ctx; + core::setInputSampleRate(_this->sampleRate); + spdlog::info("PlutoSDRSourceModule '{0}': Menu Select!", _this->name); + } + + static void menuDeselected(void* ctx) { + PlutoSDRSourceModule* _this = (PlutoSDRSourceModule*)ctx; + spdlog::info("PlutoSDRSourceModule '{0}': Menu Deselect!", _this->name); + } + + static void start(void* ctx) { + PlutoSDRSourceModule* _this = (PlutoSDRSourceModule*)ctx; + if (_this->running) { + return; + } + + // TODO: INIT CONTEXT HERE + _this->ctx = iio_create_context_from_uri("ip:192.168.2.1"); + if (_this->ctx == NULL) { + spdlog::error("Could not open pluto"); + return; + } + _this->phy = iio_context_find_device(_this->ctx, "ad9361-phy"); + if (_this->phy == NULL) { + spdlog::error("Could not connect to pluto phy"); + iio_context_destroy(_this->ctx); + return; + } + _this->dev = iio_context_find_device(_this->ctx, "cf-ad9361-lpc"); + if (_this->dev == NULL) { + spdlog::error("Could not connect to pluto dev"); + iio_context_destroy(_this->ctx); + return; + } + + // Configure pluto + iio_channel_attr_write_longlong(iio_device_find_channel(_this->phy, "altvoltage0", true), "frequency", _this->freq); // Freq + iio_channel_attr_write_longlong(iio_device_find_channel(_this->phy, "voltage0", false), "sampling_frequency", _this->sampleRate); // Sample rate + iio_channel_attr_write(iio_device_find_channel(_this->phy, "voltage0", false), "gain_control_mode", "manual"); // manual gain + iio_channel_attr_write_longlong(iio_device_find_channel(_this->phy, "voltage0", false), "hardwaregain", _this->gain); // gain + + _this->running = true; + _this->workerThread = std::thread(worker, _this); + spdlog::info("PlutoSDRSourceModule '{0}': Start!", _this->name); + } + + static void stop(void* ctx) { + PlutoSDRSourceModule* _this = (PlutoSDRSourceModule*)ctx; + if (!_this->running) { + return; + } + _this->running = false; + _this->stream.stopWriter(); + _this->workerThread.join(); + _this->stream.clearWriteStop(); + + // DESTROY CONTEXT HERE + if (_this->ctx != NULL) { + iio_context_destroy(_this->ctx); + _this->ctx = NULL; + } + + spdlog::info("PlutoSDRSourceModule '{0}': Stop!", _this->name); + } + + static void tune(double freq, void* ctx) { + PlutoSDRSourceModule* _this = (PlutoSDRSourceModule*)ctx; + if (_this->running) { + // SET PLUTO FREQ HERE + iio_channel_attr_write_longlong(iio_device_find_channel(_this->phy, "altvoltage0", true), "frequency", _this->freq); + } + _this->freq = freq; + spdlog::info("PlutoSDRSourceModule '{0}': Tune: {1}!", _this->name, freq); + } + + static void menuHandler(void* ctx) { + PlutoSDRSourceModule* _this = (PlutoSDRSourceModule*)ctx; + float menuWidth = ImGui::GetContentRegionAvailWidth(); + + char test[] = "192.168.2.1"; + ImGui::Text("IP"); + ImGui::SameLine(); + ImGui::SetNextItemWidth(menuWidth - ImGui::GetCursorPosX()); + ImGui::InputText("", test, 16); + + // SELECT PLUTO HERE + ImGui::Text("Gain Mode"); + ImGui::SameLine(); + ImGui::SetNextItemWidth(menuWidth - ImGui::GetCursorPosX()); + if (ImGui::Combo("", &_this->gainMode, gainModesTxt)) { + + } + + ImGui::Text("PGA Gain"); + ImGui::SameLine(); + ImGui::SetNextItemWidth(menuWidth - ImGui::GetCursorPosX()); + if (ImGui::SliderFloat(CONCAT("##_gain_select_", _this->name), &_this->gain, 0, 76)) { + if (_this->running) { + // SET PLUTO GAIN HERE + iio_channel_attr_write_longlong(iio_device_find_channel(_this->phy, "voltage0", false),"hardwaregain", _this->gain); + } + } + } + + static void worker(void* ctx) { + PlutoSDRSourceModule* _this = (PlutoSDRSourceModule*)ctx; + int blockSize = _this->sampleRate / 200.0; + + struct iio_channel *rx0_i, *rx0_q; + struct iio_buffer *rxbuf; + + rx0_i = iio_device_find_channel(_this->dev, "voltage0", 0); + rx0_q = iio_device_find_channel(_this->dev, "voltage1", 0); + + iio_channel_enable(rx0_i); + iio_channel_enable(rx0_q); + + rxbuf = iio_device_create_buffer(_this->dev, blockSize, false); + if (!rxbuf) { + spdlog::error("Could not create RX buffer"); + return; + } + + while (true) { + // Read samples here + // TODO: RECEIVE HERE + iio_buffer_refill(rxbuf); + + int16_t* buf = (int16_t*)iio_buffer_first(rxbuf, rx0_i); + + if (_this->stream.aquire() < 0) { break; } + for (int i = 0; i < blockSize; i++) { + _this->stream.data[i].q = (float)buf[i * 2] / 32768.0f; + _this->stream.data[i].i = (float)buf[(i * 2) + 1] / 32768.0f; + } + _this->stream.write(blockSize); + } + + iio_buffer_destroy(rxbuf); + } + + std::string name; + dsp::stream stream; + double sampleRate; + SourceManager::SourceHandler handler; + std::thread workerThread; + struct iio_context *ctx = NULL; + struct iio_device *phy = NULL; + struct iio_device *dev = NULL; + bool running = false; + double freq; + char ip[1024] = "localhost"; + int port = 1234; + int gainMode = 0; + float gain = 0; +}; + +MOD_EXPORT void _INIT_() { + // Do your one time init here +} + +MOD_EXPORT void* _CREATE_INSTANCE_(std::string name) { + return new PlutoSDRSourceModule(name); +} + +MOD_EXPORT void _DELETE_INSTANCE_(void* instance) { + delete (PlutoSDRSourceModule*)instance; +} + +MOD_EXPORT void _STOP_() { + // Do your one shutdown here +} \ No newline at end of file diff --git a/root_dev/config.json b/root_dev/config.json index de42e5b0..cfabfd72 100644 --- a/root_dev/config.json +++ b/root_dev/config.json @@ -3,7 +3,7 @@ "Radio": { "device": "Speakers (Realtek High Definiti", "sampleRate": 48000.0, - "volume": 0.39259257912635803 + "volume": 0.3777777850627899 }, "Radio 1": { "device": "Speakers (Realtek High Definition Audio)", @@ -20,9 +20,9 @@ "bandPlanEnabled": true, "defaultSink": "Audio", "fftHeight": 296, - "frequency": 7300000, + "frequency": 99000000, "max": 0.0, - "maximized": true, + "maximized": false, "menuOrder": [ "Source", "Radio", @@ -33,7 +33,7 @@ "Display" ], "menuWidth": 300, - "min": -74.26470184326172, + "min": -69.11764526367188, "showWaterfall": true, "source": "", "sourceSettings": {}, diff --git a/root_dev/module_list.json b/root_dev/module_list.json index cfdef7a2..360427f9 100644 --- a/root_dev/module_list.json +++ b/root_dev/module_list.json @@ -5,5 +5,6 @@ "RTLTCPSource": "./rtl_tcp_source/Release/rtl_tcp_source.dll", "FileSource": "./file_source/Release/file_source.dll", "RX888Source": "./rx888_source/Release/rx888_source.dll", + "PlutoSDRSource": "./plutosdr_source/Release/plutosdr_source.dll", "AudioSink": "./audio_sink/Release/audio_sink.dll" } diff --git a/root_dev/soapy_source_config.json b/root_dev/soapy_source_config.json index c0894487..6b133442 100644 --- a/root_dev/soapy_source_config.json +++ b/root_dev/soapy_source_config.json @@ -1,6 +1,12 @@ { - "device": "CABLE Output (VB-Audio Virtual Cable)", + "device": "", "devices": { + "": { + "gains": { + "PGA": 0.0 + }, + "sampleRate": 4000000.0 + }, "AirSpy HF+ [c852435de0224af7]": { "gains": { "LNA": 6.0,