2023-08-23 18:44:23 +08:00
|
|
|
#include <iostream>
|
2023-08-23 15:06:05 +08:00
|
|
|
#include <cstdlib>
|
2023-08-23 18:44:23 +08:00
|
|
|
#include <cstring>
|
2023-08-23 15:53:50 +08:00
|
|
|
#include <thread>
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
2023-08-23 18:44:23 +08:00
|
|
|
#include <vector>
|
2023-08-23 21:29:42 +08:00
|
|
|
#include <complex>
|
2023-08-24 00:14:27 +08:00
|
|
|
#include <chrono>
|
2023-09-11 19:20:51 +08:00
|
|
|
#include <format>
|
|
|
|
#include <experimental/net>
|
2023-08-23 15:06:05 +08:00
|
|
|
|
2023-08-22 23:41:16 +08:00
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <imgui_impl_glfw.h>
|
|
|
|
#include <imgui_impl_opengl3.h>
|
|
|
|
|
2023-09-07 20:17:02 +08:00
|
|
|
#include <libbladeRF.h>
|
2023-08-23 18:44:23 +08:00
|
|
|
|
2023-09-11 19:20:20 +08:00
|
|
|
#define POCKETFFT_CACHE_SIZE (16*1024*1024)
|
|
|
|
#define POCKETFFT_NO_MULTITHREADING // MT doesn't seem to help at small sizes
|
2023-08-23 19:57:01 +08:00
|
|
|
#include <pocketfft_hdronly.h>
|
|
|
|
|
2023-09-11 19:20:51 +08:00
|
|
|
namespace net = std::experimental::net;
|
2023-08-23 19:57:01 +08:00
|
|
|
|
2023-08-23 15:53:50 +08:00
|
|
|
static std::atomic<bool> terminate_dsp;
|
2023-08-23 18:44:23 +08:00
|
|
|
|
2023-09-07 20:17:02 +08:00
|
|
|
struct bladerf* bladerf_dev;
|
2023-08-23 18:44:23 +08:00
|
|
|
|
2023-08-23 15:53:50 +08:00
|
|
|
static std::mutex waterfall_data_mutex;
|
2023-09-07 21:00:22 +08:00
|
|
|
static int waterfall_width = 1000;
|
|
|
|
static int waterfall_height = 2000;
|
|
|
|
static unsigned int waterfall_data[1000*2000];
|
2023-08-23 15:53:50 +08:00
|
|
|
|
2023-08-24 00:14:27 +08:00
|
|
|
static std::atomic<int> fps;
|
|
|
|
|
2023-08-23 15:53:50 +08:00
|
|
|
static void dsp_thread() {
|
2023-08-24 00:14:27 +08:00
|
|
|
using namespace std::literals::chrono_literals;
|
|
|
|
|
2023-09-11 19:20:51 +08:00
|
|
|
net::io_context io_context;
|
|
|
|
net::ip::tcp::socket tec_socket(io_context);
|
|
|
|
tec_socket.connect(net::ip::tcp::endpoint(net::ip::make_address("192.168.1.91"), 5555));
|
|
|
|
|
2023-09-07 21:00:22 +08:00
|
|
|
size_t len = 16384;
|
2023-09-07 20:17:02 +08:00
|
|
|
std::vector<std::complex<int16_t>> frames(len);
|
|
|
|
std::vector<std::complex<float>> frames_f(len);
|
2023-08-23 21:29:42 +08:00
|
|
|
pocketfft::shape_t shape{len};
|
2023-09-07 21:00:22 +08:00
|
|
|
pocketfft::stride_t stride(1);
|
|
|
|
stride[0] = 2*sizeof(float);
|
2023-08-23 21:29:42 +08:00
|
|
|
pocketfft::shape_t axes;
|
2023-09-07 21:00:22 +08:00
|
|
|
axes.push_back(0);
|
2023-08-23 21:29:42 +08:00
|
|
|
std::vector<std::complex<float>> frames_ft(len);
|
2023-09-11 19:20:51 +08:00
|
|
|
|
2023-08-24 00:14:27 +08:00
|
|
|
int iterations = 0;
|
|
|
|
auto last_second = std::chrono::steady_clock::now();
|
2023-09-11 19:20:51 +08:00
|
|
|
auto last_tec = std::chrono::steady_clock::now();
|
2023-08-23 15:53:50 +08:00
|
|
|
while(!terminate_dsp) {
|
2023-09-07 20:17:02 +08:00
|
|
|
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;
|
2023-08-23 19:46:36 +08:00
|
|
|
break;
|
|
|
|
}
|
2023-09-11 19:20:51 +08:00
|
|
|
|
|
|
|
// FFT
|
2023-08-23 21:29:42 +08:00
|
|
|
for(size_t i=0;i<len;i++)
|
|
|
|
frames_f[i] = frames[i];
|
2023-09-07 21:00:22 +08:00
|
|
|
pocketfft::c2c(shape, stride, stride, axes, pocketfft::FORWARD, frames_f.data(), frames_ft.data(), 1.0f);
|
2023-09-11 19:20:51 +08:00
|
|
|
|
|
|
|
// stabilize laser
|
|
|
|
if((std::chrono::steady_clock::now() - last_tec) >= 100ms) {
|
|
|
|
// FIXME: net::write seems unimplemented as of libstdc++ 13
|
|
|
|
tec_socket.write_some(net::buffer(std::format(":SOUR2:VOLT {:.3f}\n", 0.1)));
|
|
|
|
last_tec += 100ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update waterfall
|
2023-08-23 15:53:50 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(waterfall_data_mutex);
|
2023-08-23 21:29:42 +08:00
|
|
|
std::memmove(&waterfall_data[waterfall_width], &waterfall_data[0], sizeof(int)*waterfall_width*(waterfall_height - 1));
|
2023-08-23 18:44:23 +08:00
|
|
|
for(int i=0;i<waterfall_width;i++) {
|
2023-09-07 21:00:22 +08:00
|
|
|
int j = i*len/waterfall_width;
|
|
|
|
j = (j + len/2) % len; // display negative frequencies first
|
|
|
|
waterfall_data[i] = 0xff000000 | 0x010101*std::min(int(abs(frames_ft[j])/900.), 255);
|
2023-08-23 18:44:23 +08:00
|
|
|
}
|
2023-08-23 15:53:50 +08:00
|
|
|
}
|
2023-09-11 19:20:51 +08:00
|
|
|
|
|
|
|
// FPS counter
|
2023-08-24 00:14:27 +08:00
|
|
|
iterations++;
|
|
|
|
if((std::chrono::steady_clock::now() - last_second) >= 1s) {
|
|
|
|
fps = iterations;
|
|
|
|
iterations = 0;
|
|
|
|
last_second += 1s;
|
|
|
|
}
|
2023-09-11 19:20:51 +08:00
|
|
|
}
|
2023-08-23 15:53:50 +08:00
|
|
|
}
|
|
|
|
|
2023-08-23 19:46:36 +08:00
|
|
|
int main(int argc, char* argv[])
|
2023-08-22 23:41:16 +08:00
|
|
|
{
|
2023-09-07 20:17:02 +08:00
|
|
|
if(!glfwInit()) {
|
2023-08-23 18:44:23 +08:00
|
|
|
std::cerr << "failed to initialize GLFW" << std::endl;
|
2023-08-22 23:41:16 +08:00
|
|
|
return 1;
|
2023-08-23 18:44:23 +08:00
|
|
|
}
|
2023-08-23 15:06:05 +08:00
|
|
|
std::atexit(glfwTerminate);
|
2023-08-22 23:41:16 +08:00
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
|
2023-09-07 21:00:22 +08:00
|
|
|
static GLFWwindow* window = glfwCreateWindow(1280, 2200, "fastsa", nullptr, nullptr);
|
2023-09-07 20:17:02 +08:00
|
|
|
if(window == nullptr) {
|
2023-08-23 18:44:23 +08:00
|
|
|
std::cerr << "failed to create GLFW window" << std::endl;
|
2023-08-22 23:41:16 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2023-08-23 15:06:05 +08:00
|
|
|
static auto DestroyWindow = []() {
|
|
|
|
glfwDestroyWindow(window);
|
|
|
|
};
|
|
|
|
std::atexit(DestroyWindow);
|
2023-08-22 23:41:16 +08:00
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
glfwSwapInterval(1);
|
|
|
|
|
2023-08-23 13:12:28 +08:00
|
|
|
IMGUI_CHECKVERSION();
|
2023-08-22 23:41:16 +08:00
|
|
|
ImGui::CreateContext();
|
2023-08-23 15:06:05 +08:00
|
|
|
static auto ImGuiDestroyContext = []() {
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
};
|
|
|
|
std::atexit(ImGuiDestroyContext);
|
2023-08-23 13:12:28 +08:00
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
2023-08-22 23:41:16 +08:00
|
|
|
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
2023-08-23 15:06:05 +08:00
|
|
|
std::atexit(ImGui_ImplGlfw_Shutdown);
|
2023-08-22 23:41:16 +08:00
|
|
|
ImGui_ImplOpenGL3_Init("#version 130");
|
2023-08-23 15:06:05 +08:00
|
|
|
std::atexit(ImGui_ImplOpenGL3_Shutdown);
|
2023-08-22 23:41:16 +08:00
|
|
|
|
2023-08-23 13:12:28 +08:00
|
|
|
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);
|
|
|
|
|
2023-09-07 20:17:02 +08:00
|
|
|
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;
|
2023-08-23 18:44:23 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2023-09-07 20:17:02 +08:00
|
|
|
static auto BladeRFClose = []() {
|
|
|
|
bladerf_close(bladerf_dev);
|
2023-08-23 18:44:23 +08:00
|
|
|
};
|
2023-09-07 20:17:02 +08:00
|
|
|
std::atexit(BladeRFClose);
|
2023-09-07 21:00:22 +08:00
|
|
|
if((status = bladerf_set_frequency(bladerf_dev, BLADERF_CHANNEL_RX(0), 1'664'000'000)) != 0) {
|
2023-09-07 20:17:02 +08:00
|
|
|
std::cerr << "failed to set bladeRF frequency: " << bladerf_strerror(status) << std::endl;
|
2023-08-23 19:46:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2023-09-07 20:17:02 +08:00
|
|
|
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;
|
2023-08-23 19:46:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2023-09-07 20:17:02 +08:00
|
|
|
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;
|
2023-08-23 19:46:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2023-09-07 20:17:02 +08:00
|
|
|
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;
|
2023-08-23 19:46:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2023-09-07 20:17:02 +08:00
|
|
|
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;
|
2023-08-23 19:46:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2023-09-07 20:17:02 +08:00
|
|
|
if((status = bladerf_enable_module(bladerf_dev, BLADERF_RX, true) != 0)) {
|
|
|
|
std::cerr << "failed to enable bladeRF RX: " << bladerf_strerror(status) << std::endl;
|
2023-08-23 18:44:23 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2023-09-07 20:17:02 +08:00
|
|
|
static auto BladeRFDisableRX = []() {
|
|
|
|
bladerf_enable_module(bladerf_dev, BLADERF_RX, false);
|
|
|
|
};
|
|
|
|
std::atexit(BladeRFDisableRX);
|
2023-08-23 18:44:23 +08:00
|
|
|
|
2023-08-23 15:53:50 +08:00
|
|
|
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);
|
|
|
|
|
2023-08-23 13:12:28 +08:00
|
|
|
bool exit = false;
|
2023-09-07 21:26:26 +08:00
|
|
|
bool update = true;
|
2023-08-23 15:53:50 +08:00
|
|
|
while(!exit && !glfwWindowShouldClose(window)) {
|
2023-08-22 23:41:16 +08:00
|
|
|
glfwPollEvents();
|
2023-08-23 13:12:28 +08:00
|
|
|
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
|
2023-09-07 21:26:26 +08:00
|
|
|
if(update) {
|
2023-08-23 15:53:50 +08:00
|
|
|
std::lock_guard<std::mutex> guard(waterfall_data_mutex);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, waterfall_width, waterfall_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, waterfall_data);
|
|
|
|
}
|
2023-08-22 23:41:16 +08:00
|
|
|
|
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
|
|
|
ImGui_ImplGlfw_NewFrame();
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
2023-08-23 13:12:28 +08:00
|
|
|
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
|
|
|
|
ImGui::SetNextWindowSize(io.DisplaySize);
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
|
2023-09-07 19:15:07 +08:00
|
|
|
ImGui::Begin("fastsa", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize);
|
2023-08-23 13:12:28 +08:00
|
|
|
|
2023-09-07 19:15:07 +08:00
|
|
|
ImGui::BeginTable("fastsa", 2, ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_Resizable);
|
2023-08-23 13:12:28 +08:00
|
|
|
ImGui::TableSetupColumn("", 0, 280.0);
|
2023-09-07 21:00:22 +08:00
|
|
|
ImGui::TableSetupColumn("", 0, 1000.0);
|
2023-08-23 13:12:28 +08:00
|
|
|
ImGui::TableNextColumn();
|
2023-09-07 21:26:26 +08:00
|
|
|
ImGui::Checkbox("Update", &update);
|
2023-08-23 13:12:28 +08:00
|
|
|
if(ImGui::Button("Exit"))
|
|
|
|
exit = true;
|
2023-08-24 00:14:27 +08:00
|
|
|
ImGui::Text("FPS: %d", (int)fps);
|
2023-08-23 13:12:28 +08:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::Image((void*)(intptr_t)waterfall, ImVec2(waterfall_width, waterfall_height));
|
|
|
|
ImGui::EndTable();
|
|
|
|
|
|
|
|
ImGui::End();
|
|
|
|
ImGui::PopStyleVar(1);
|
|
|
|
|
2023-08-22 23:41:16 +08:00
|
|
|
ImGui::Render();
|
2023-08-23 13:12:28 +08:00
|
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
2023-08-22 23:41:16 +08:00
|
|
|
int display_w, display_h;
|
|
|
|
glfwGetFramebufferSize(window, &display_w, &display_h);
|
|
|
|
glViewport(0, 0, display_w, display_h);
|
|
|
|
glfwSwapBuffers(window);
|
|
|
|
}
|
|
|
|
|
2023-08-22 23:24:17 +08:00
|
|
|
return 0;
|
|
|
|
}
|