#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define POCKETFFT_CACHE_SIZE (16*1024*1024) #define POCKETFFT_NO_MULTITHREADING // MT doesn't seem to help at small sizes #include static std::atomic terminate_dsp; struct bladerf* bladerf_dev; static std::mutex waterfall_data_mutex; static int waterfall_width = 1000; static int waterfall_height = 2000; static unsigned int waterfall_data[1000*2000]; static std::atomic fps; static void dsp_thread() { using namespace std::literals::chrono_literals; size_t len = 16384; std::vector> frames(len); std::vector> frames_f(len); pocketfft::shape_t shape{len}; pocketfft::stride_t stride(1); stride[0] = 2*sizeof(float); pocketfft::shape_t axes; axes.push_back(0); std::vector> frames_ft(len); int iterations = 0; auto last_second = std::chrono::steady_clock::now(); while(!terminate_dsp) { int status; if((status = bladerf_sync_rx(bladerf_dev, frames.data(), len, NULL, 0)) != 0) { std::cerr << "failed to receive samples from bladeRF: " << bladerf_strerror(status) << std::endl; break; } for(size_t i=0;i guard(waterfall_data_mutex); std::memmove(&waterfall_data[waterfall_width], &waterfall_data[0], sizeof(int)*waterfall_width*(waterfall_height - 1)); for(int i=0;i= 1s) { fps = iterations; iterations = 0; last_second += 1s; } }; } int main(int argc, char* argv[]) { if(!glfwInit()) { std::cerr << "failed to initialize GLFW" << std::endl; return 1; } std::atexit(glfwTerminate); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); static GLFWwindow* window = glfwCreateWindow(1280, 2200, "fastsa", nullptr, nullptr); if(window == nullptr) { std::cerr << "failed to create GLFW window" << std::endl; return 1; } static auto DestroyWindow = []() { glfwDestroyWindow(window); }; std::atexit(DestroyWindow); glfwMakeContextCurrent(window); glfwSwapInterval(1); IMGUI_CHECKVERSION(); ImGui::CreateContext(); static auto ImGuiDestroyContext = []() { ImGui::DestroyContext(); }; std::atexit(ImGuiDestroyContext); ImGuiIO& io = ImGui::GetIO(); io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; ImGui::StyleColorsDark(); ImGui_ImplGlfw_InitForOpenGL(window, true); std::atexit(ImGui_ImplGlfw_Shutdown); ImGui_ImplOpenGL3_Init("#version 130"); std::atexit(ImGui_ImplOpenGL3_Shutdown); GLuint waterfall; glGenTextures(1, &waterfall); glBindTexture(GL_TEXTURE_2D, waterfall); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); int status; struct bladerf_devinfo dev_info; bladerf_init_devinfo(&dev_info); if((status = bladerf_open_with_devinfo(&bladerf_dev, &dev_info)) != 0) { std::cerr << "cannot open bladeRF device: " << bladerf_strerror(status) << std::endl; return 1; } static auto BladeRFClose = []() { bladerf_close(bladerf_dev); }; std::atexit(BladeRFClose); if((status = bladerf_set_frequency(bladerf_dev, BLADERF_CHANNEL_RX(0), 1'664'000'000)) != 0) { std::cerr << "failed to set bladeRF frequency: " << bladerf_strerror(status) << std::endl; return 1; } if((status = bladerf_set_sample_rate(bladerf_dev, BLADERF_CHANNEL_RX(0), 40'000'000, NULL)) != 0) { std::cerr << "failed to set bladeRF sample rate: " << bladerf_strerror(status) << std::endl; return 1; } if((status = bladerf_set_bandwidth(bladerf_dev, BLADERF_CHANNEL_RX(0), 35'000'000, NULL)) != 0) { std::cerr << "failed to set bladeRF bandwidth: " << bladerf_strerror(status) << std::endl; return 1; } if((status = bladerf_set_gain(bladerf_dev, BLADERF_CHANNEL_RX(0), 20)) != 0) { std::cerr << "failed to set bladeRF gain: " << bladerf_strerror(status) << std::endl; return 1; } if((status = bladerf_sync_config(bladerf_dev, BLADERF_RX_X1, BLADERF_FORMAT_SC16_Q11, 16, /* num_buffers */ 8192, /* buffer_size */ 8, /* num_transfers */ 3500 /* timeout_ms */)) != 0) { std::cerr << "failed to set bladeRF sync settings: " << bladerf_strerror(status) << std::endl; return 1; } if((status = bladerf_enable_module(bladerf_dev, BLADERF_RX, true) != 0)) { std::cerr << "failed to enable bladeRF RX: " << bladerf_strerror(status) << std::endl; return 1; } static auto BladeRFDisableRX = []() { bladerf_enable_module(bladerf_dev, BLADERF_RX, false); }; std::atexit(BladeRFDisableRX); terminate_dsp = false; static std::thread dsp_thread_h = std::thread(dsp_thread); static auto TerminateDSP = []() { terminate_dsp = true; dsp_thread_h.join(); }; std::atexit(TerminateDSP); bool exit = false; bool update = true; while(!exit && !glfwWindowShouldClose(window)) { glfwPollEvents(); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, waterfall); #if defined(GL_UNPACK_ROW_LENGTH) && !defined(__EMSCRIPTEN__) glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); #endif if(update) { std::lock_guard guard(waterfall_data_mutex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, waterfall_width, waterfall_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, waterfall_data); } ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f)); ImGui::SetNextWindowSize(io.DisplaySize); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); ImGui::Begin("fastsa", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize); ImGui::BeginTable("fastsa", 2, ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_Resizable); ImGui::TableSetupColumn("", 0, 280.0); ImGui::TableSetupColumn("", 0, 1000.0); ImGui::TableNextColumn(); ImGui::Checkbox("Update", &update); if(ImGui::Button("Exit")) exit = true; ImGui::Text("FPS: %d", (int)fps); ImGui::TableNextColumn(); ImGui::Image((void*)(intptr_t)waterfall, ImVec2(waterfall_width, waterfall_height)); ImGui::EndTable(); ImGui::End(); ImGui::PopStyleVar(1); ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); int display_w, display_h; glfwGetFramebufferSize(window, &display_w, &display_h); glViewport(0, 0, display_w, display_h); glfwSwapBuffers(window); } return 0; }