minor cleanup, use const

This commit is contained in:
Sebastien Bourdeauducq 2023-09-13 10:45:02 +08:00
parent 52bbfc0c33
commit 9bee53ab49
1 changed files with 26 additions and 23 deletions

View File

@ -24,14 +24,17 @@ namespace net = std::experimental::net;
void fft_mag(std::complex<float>* in, float* out, size_t len);
static const size_t block_len = 16384;
static const int wf_width = 1000;
static const int wf_height = 2000;
static std::atomic<bool> terminate_dsp;
struct bladerf* bladerf_dev;
static 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::mutex wf_data_mutex;
static unsigned int wf_data[wf_width*wf_height];
static std::atomic<float> freq_setpoint;
static std::atomic<float> freq_peak;
@ -41,35 +44,35 @@ static std::atomic<float> tec_current;
static std::atomic<int> fps;
static void dsp_thread() {
static void dsp_thread()
{
using namespace std::literals::chrono_literals;
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.27"), 23));
size_t len = 16384;
std::vector<std::complex<int16_t>> frames(len);
std::vector<std::complex<float>> frames_f(len);
std::vector<float> frames_mag(len);
std::vector<std::complex<int16_t>> frames(block_len);
std::vector<std::complex<float>> frames_f(block_len);
std::vector<float> frames_mag(block_len);
int iterations = 0;
auto last_second = std::chrono::steady_clock::now();
auto last_tec = std::chrono::steady_clock::now();
while(!terminate_dsp) {
int status;
if((status = bladerf_sync_rx(bladerf_dev, frames.data(), len, NULL, 0)) != 0) {
if((status = bladerf_sync_rx(bladerf_dev, frames.data(), block_len, NULL, 0)) != 0) {
std::cerr << "failed to receive samples from bladeRF: " << bladerf_strerror(status) << std::endl;
break;
}
fft_mag(frames_f.data(), frames_mag.data(), len);
fft_mag(frames_f.data(), frames_mag.data(), block_len);
// stabilize laser
bool tick = false;
if((std::chrono::steady_clock::now() - last_tec) >= 100ms) {
float freq_peak_local;
freq_peak_local = 40.0f*float(distance(frames_mag.begin(), max_element(frames_mag.begin(), frames_mag.end())))/float(len);
freq_peak_local = 40.0f*float(distance(frames_mag.begin(), max_element(frames_mag.begin(), frames_mag.end())))/float(block_len);
freq_peak = freq_peak_local;
float freq_error = freq_peak_local - freq_setpoint;
float tec_current_local = std::max(tec_bias+tec_p*freq_error, 0.0f);
@ -91,16 +94,16 @@ static void dsp_thread() {
// update waterfall
{
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++) {
int j = i*len/waterfall_width;
waterfall_data[i] = 0xff000000 | 0x010101*std::min(int(frames_mag[j]/900.0f), 255);
std::lock_guard<std::mutex> guard(wf_data_mutex);
std::memmove(&wf_data[wf_width], &wf_data[0], sizeof(int)*wf_width*(wf_height - 1));
for(int i=0;i<wf_width;i++) {
int j = i*block_len/wf_width;
wf_data[i] = 0xff000000 | 0x010101*std::min(int(frames_mag[j]/900.0f), 255);
}
waterfall_data[int(freq_setpoint*waterfall_width)/40] = 0xff0000ff;
wf_data[int(freq_setpoint*wf_width)/40] = 0xff0000ff;
if(tick)
for(int i=0;i<100;i++)
waterfall_data[i] = 0xffff0000;
wf_data[i] = 0xffff0000;
}
// FPS counter
@ -223,8 +226,8 @@ int main(int argc, char* argv[])
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif
if(update) {
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);
std::lock_guard<std::mutex> guard(wf_data_mutex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, wf_width, wf_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, wf_data);
}
ImGui_ImplOpenGL3_NewFrame();
@ -253,7 +256,7 @@ int main(int argc, char* argv[])
exit = true;
ImGui::Text("FPS: %d", (int)fps);
ImGui::TableNextColumn();
ImGui::Image((void*)(intptr_t)waterfall, ImVec2(waterfall_width, waterfall_height));
ImGui::Image((void*)(intptr_t)waterfall, ImVec2(wf_width, wf_height));
ImGui::EndTable();
ImGui::End();