use bladeRF
This commit is contained in:
parent
7fced717e6
commit
9eb13ee140
5
Makefile
5
Makefile
@ -6,11 +6,10 @@ OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
|
||||
|
||||
CXXFLAGS = -std=c++14 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends -I$(POCKETFFT_DIR)
|
||||
CXXFLAGS += -g -Wall -Wformat -O2
|
||||
LIBS = -L$(TINYALSA_DIR)/lib
|
||||
|
||||
LIBS += -lGL `pkg-config --static --libs glfw3 alsa`
|
||||
LIBS += -lGL `pkg-config --static --libs glfw3 libbladeRF`
|
||||
|
||||
CXXFLAGS += `pkg-config --cflags glfw3 alsa`
|
||||
CXXFLAGS += `pkg-config --cflags glfw3 libbladeRF`
|
||||
CFLAGS = $(CXXFLAGS)
|
||||
|
||||
##---------------------------------------------------------------------
|
||||
|
@ -17,7 +17,7 @@
|
||||
name = "fastsa";
|
||||
src = self;
|
||||
nativeBuildInputs = [ pkgs.pkg-config ];
|
||||
propagatedBuildInputs = [ pkgs.wayland pkgs.glfw-wayland pkgs.libffi pkgs.alsa-lib ];
|
||||
propagatedBuildInputs = [ pkgs.wayland pkgs.glfw-wayland pkgs.libffi pkgs.libbladeRF ];
|
||||
preBuild = ''
|
||||
export IMGUI_DIR=${imgui_dir}
|
||||
export POCKETFFT_DIR=${pocketfft}
|
||||
|
86
main.cpp
86
main.cpp
@ -14,14 +14,14 @@
|
||||
#include <imgui_impl_glfw.h>
|
||||
#include <imgui_impl_opengl3.h>
|
||||
|
||||
#include <alsa/asoundlib.h>
|
||||
#include <libbladeRF.h>
|
||||
|
||||
#include <pocketfft_hdronly.h>
|
||||
|
||||
|
||||
static std::atomic<bool> terminate_dsp;
|
||||
|
||||
static snd_pcm_t* pcm;
|
||||
struct bladerf* bladerf_dev;
|
||||
|
||||
static std::mutex waterfall_data_mutex;
|
||||
static int waterfall_width = 1600;
|
||||
@ -33,10 +33,10 @@ static std::atomic<int> fps;
|
||||
static void dsp_thread() {
|
||||
using namespace std::literals::chrono_literals;
|
||||
|
||||
size_t len = 6400;
|
||||
std::vector<short int> frames(len);
|
||||
size_t len = 8192;
|
||||
std::vector<std::complex<int16_t>> frames(len);
|
||||
|
||||
std::vector<float> frames_f(len);
|
||||
std::vector<std::complex<float>> frames_f(len);
|
||||
pocketfft::shape_t shape{len};
|
||||
pocketfft::stride_t stride(shape.size());
|
||||
size_t tmp=sizeof(float);
|
||||
@ -51,19 +51,19 @@ static void dsp_thread() {
|
||||
int iterations = 0;
|
||||
auto last_second = std::chrono::steady_clock::now();
|
||||
while(!terminate_dsp) {
|
||||
int read_count = snd_pcm_readi(pcm, frames.data(), len);
|
||||
if(read_count < 0) {
|
||||
std::cerr << "read from audio interface failed: " << snd_strerror(read_count) << std::endl;
|
||||
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<len;i++)
|
||||
frames_f[i] = frames[i];
|
||||
pocketfft::r2c(shape, stride, stride, axes, true, frames_f.data(), frames_ft.data(), 1.0f);
|
||||
pocketfft::c2c(shape, stride, stride, axes, true, frames_f.data(), frames_ft.data(), 1.0f);
|
||||
{
|
||||
std::lock_guard<std::mutex> 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<waterfall_width;i++) {
|
||||
waterfall_data[i] = 0xff000000 | 0x010101*std::min(int(abs(frames_ft[i*len/(4*waterfall_width)])/10000.), 255);
|
||||
waterfall_data[i] = 0xff000000 | 0x010101*std::min(int(abs(frames_ft[i*len/(2*waterfall_width)])/300.), 255);
|
||||
}
|
||||
}
|
||||
iterations++;
|
||||
@ -77,7 +77,7 @@ static void dsp_thread() {
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (!glfwInit()) {
|
||||
if(!glfwInit()) {
|
||||
std::cerr << "failed to initialize GLFW" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
@ -85,7 +85,7 @@ int main(int argc, char* argv[])
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
|
||||
static GLFWwindow* window = glfwCreateWindow(1900, 720, "fastsa", nullptr, nullptr);
|
||||
if (window == nullptr) {
|
||||
if(window == nullptr) {
|
||||
std::cerr << "failed to create GLFW window" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
@ -118,55 +118,49 @@ int main(int argc, char* argv[])
|
||||
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 err;
|
||||
if ((err = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_CAPTURE, 0)) < 0) {
|
||||
std::cerr << "cannot open ALSA device: " << snd_strerror(err) << std::endl;
|
||||
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 PCMClose = []() {
|
||||
snd_pcm_close(pcm);
|
||||
static auto BladeRFClose = []() {
|
||||
bladerf_close(bladerf_dev);
|
||||
};
|
||||
std::atexit(PCMClose);
|
||||
snd_pcm_hw_params_t* pcm_hw_params;
|
||||
if((err = snd_pcm_hw_params_malloc(&pcm_hw_params)) < 0) {
|
||||
std::cerr << "cannot allocate ALSA hardware parameter structure: " << snd_strerror(err) << std::endl;
|
||||
std::atexit(BladeRFClose);
|
||||
if((status = bladerf_set_frequency(bladerf_dev, BLADERF_CHANNEL_RX(0), 1'650'000'000)) != 0) {
|
||||
std::cerr << "failed to set bladeRF frequency: " << bladerf_strerror(status) << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if((err = snd_pcm_hw_params_any(pcm, pcm_hw_params)) < 0) {
|
||||
std::cerr << "cannot initialize ALSA hardware parameter structure: " << snd_strerror (err) << std::endl;
|
||||
snd_pcm_hw_params_free(pcm_hw_params);
|
||||
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((err = snd_pcm_hw_params_set_access(pcm, pcm_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
|
||||
std::cerr << "cannot set ALSA access type: " << snd_strerror (err) << std::endl;
|
||||
snd_pcm_hw_params_free(pcm_hw_params);
|
||||
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((err = snd_pcm_hw_params_set_format(pcm, pcm_hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
|
||||
std::cerr << "cannot set sample format: " << snd_strerror (err) << std::endl;
|
||||
snd_pcm_hw_params_free(pcm_hw_params);
|
||||
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((err = snd_pcm_hw_params_set_rate(pcm, pcm_hw_params, 192000, 0)) < 0) {
|
||||
std::cerr << "cannot set ALSA sample rate: " << snd_strerror (err) << std::endl;
|
||||
snd_pcm_hw_params_free(pcm_hw_params);
|
||||
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((err = snd_pcm_hw_params_set_channels(pcm, pcm_hw_params, 1)) < 0) {
|
||||
std::cerr << "cannot set ALSA channel count: " << snd_strerror (err) << std::endl;
|
||||
snd_pcm_hw_params_free(pcm_hw_params);
|
||||
return 1;
|
||||
}
|
||||
if((err = snd_pcm_hw_params(pcm, pcm_hw_params)) < 0) {
|
||||
std::cerr << "cannot set ALSA parameters: " << snd_strerror (err) << std::endl;
|
||||
snd_pcm_hw_params_free(pcm_hw_params);
|
||||
return 1;
|
||||
}
|
||||
snd_pcm_hw_params_free(pcm_hw_params);
|
||||
if((err = snd_pcm_prepare(pcm)) < 0) {
|
||||
std::cerr << "cannot prepare ALSA interface: " << snd_strerror (err) << std::endl;
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user