Skip to content

Commit

Permalink
Conform to -Wshadow and m_ prefix standard
Browse files Browse the repository at this point in the history
  • Loading branch information
williamyang98 committed Feb 15, 2024
1 parent e17cf0c commit 493a538
Show file tree
Hide file tree
Showing 84 changed files with 1,799 additions and 1,806 deletions.
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ add_project_target_flags(ofdm_core)
add_project_target_flags(dab_core)
add_project_target_flags(basic_radio)
add_project_target_flags(basic_scraper)
# examples/
# examples/*.cpp
add_project_target_flags(radio_app)
add_project_target_flags(basic_radio_app)
add_project_target_flags(basic_radio_app_cli)
add_project_target_flags(rtl_sdr)
add_project_target_flags(simulate_transmitter)
add_project_target_flags(convert_viterbi)
add_project_target_flags(apply_frequency_shift)
add_project_target_flags(read_wav)
add_project_target_flags(read_wav)
# examples/
add_project_target_flags(audio_lib)
add_project_target_flags(device_lib)
# examples/gui
add_project_target_flags(ofdm_gui)
add_project_target_flags(basic_radio_gui)
add_project_target_flags(audio_gui)
add_project_target_flags(device_gui)
6 changes: 3 additions & 3 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"name": "windows-clang-warnings",
"inherits": ["windows-clang"],
"cacheVariables": {
"PROJECT_TARGET_PRIVATE_COMPILER_FLAGS": "-Wall -Wextra -Werror -Wno-unused-function -Wno-unused-parameter"
"PROJECT_TARGET_PRIVATE_COMPILER_FLAGS": "-Wall -Wextra -Werror -Wno-unused-function -Wno-unused-parameter -Wshadow"
}
},
{
Expand All @@ -62,7 +62,7 @@
"name": "gcc-warnings",
"inherits": ["gcc"],
"cacheVariables": {
"PROJECT_TARGET_PRIVATE_COMPILER_FLAGS": "-Wall -Wextra -Werror -Wno-unused-function -Wno-unused-parameter"
"PROJECT_TARGET_PRIVATE_COMPILER_FLAGS": "-Wall -Wextra -Werror -Wno-unused-function -Wno-unused-parameter -Wshadow"
}
},
{
Expand All @@ -79,7 +79,7 @@
"name": "clang-warnings",
"inherits": ["clang"],
"cacheVariables": {
"PROJECT_TARGET_PRIVATE_COMPILER_FLAGS": "-Wall -Wextra -Werror -Wno-unused-function -Wno-unused-parameter"
"PROJECT_TARGET_PRIVATE_COMPILER_FLAGS": "-Wall -Wextra -Werror -Wno-unused-function -Wno-unused-parameter -Wshadow"
}
},
{
Expand Down
12 changes: 6 additions & 6 deletions examples/audio/audio_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ void AudioPipelineSource::write(tcb::span<const Frame<int16_t>> src, float src_s
if (resample_length == src.size()) {
audio_map_with_callback<int16_t,float>(
src, m_resampling_buffer,
[gain](Frame<float>& dest, const Frame<int16_t>& src) {
dest = static_cast<Frame<float>>(src) * gain;
[gain](Frame<float>& v_dest, const Frame<int16_t>& v_src) {
v_dest = static_cast<Frame<float>>(v_src) * gain;
}
);
} else {
audio_resample_with_callback<int16_t,float>(
src, m_resampling_buffer,
[gain](Frame<float>& dest, const Frame<float>& src) {
dest = src * gain;
[gain](Frame<float>& v_dest, const Frame<float>& v_src) {
v_dest = v_src * gain;
}
);
}
Expand Down Expand Up @@ -136,8 +136,8 @@ void AudioPipeline::mix_sources_to_sink(tcb::span<Frame<float>> dest, float dest

audio_resample_same_type_with_callback<float>(
m_read_buffer, dest,
[](Frame<float>& dest, const Frame<float>& src) {
dest += src;
[](Frame<float>& v_dest, const Frame<float>& v_src) {
v_dest += v_src;
}
);
total_sources_mixed++;
Expand Down
1 change: 1 addition & 0 deletions examples/audio/portaudio_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class PortAudioSink: public AudioPipelineSink
PortAudioSink& operator=(PortAudioSink&&) = delete;
void set_callback(AudioPipelineSink::Callback callback) override { m_callback = callback; }
std::string_view get_name() const override { return m_device_name; }
size_t get_frames_per_buffer() const { return m_frames_per_buffer; }
static PortAudioSinkCreateResult create_from_index(
PaDeviceIndex index,
float sample_rate=DEFAULT_AUDIO_SAMPLE_RATE,
Expand Down
1 change: 1 addition & 0 deletions examples/basic_radio_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "./app_helpers/app_common_gui.h"
#include "./gui/ofdm/render_ofdm_demod.h"
#include "./gui/ofdm/render_profiler.h"
#include "./gui/basic_radio/basic_radio_view_controller.h"
#include "./gui/basic_radio/render_basic_radio.h"
#include "./gui/audio/render_portaudio_controls.h"
#endif
Expand Down
97 changes: 48 additions & 49 deletions examples/device/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,30 @@ extern "C" {
#include <stdio.h>
#include <stdlib.h>

Device::Device(rtlsdr_dev_t* _device, const DeviceDescriptor& _descriptor, const int _block_size)
: device(_device), descriptor(_descriptor),
block_size(_block_size)
Device::Device(rtlsdr_dev_t* device, const DeviceDescriptor& descriptor, const int block_size)
: m_descriptor(descriptor), m_device(device), m_block_size(block_size)
{
is_running = true;
is_gain_manual = true;
selected_gain = 0.0f;
m_is_running = true;
m_is_gain_manual = true;
m_selected_gain = 0.0f;

SearchGains();
SetNearestGain(19.0f);
SetSamplingFrequency(2048000);

int status = 0;
status = rtlsdr_set_bias_tee(device, 0);
if (status < 0) error_list.push_back(fmt::format("Failed to disable bias tee ({})", status));
status = rtlsdr_reset_buffer(device);
if (status < 0) error_list.push_back(fmt::format("Failed to reset buffer ({})", status));

runner_thread = std::make_unique<std::thread>([this]() {
const int status = rtlsdr_read_async(
device,
status = rtlsdr_set_bias_tee(m_device, 0);
if (status < 0) m_error_list.push_back(fmt::format("Failed to disable bias tee ({})", status));
status = rtlsdr_reset_buffer(m_device);
if (status < 0) m_error_list.push_back(fmt::format("Failed to reset buffer ({})", status));

m_runner_thread = std::make_unique<std::thread>([this]() {
const int status_read = rtlsdr_read_async(
m_device,
&Device::rtlsdr_callback, reinterpret_cast<void*>(this),
0, block_size
0, m_block_size
);
fprintf(stderr, "[device] rtlsdr_read_sync exited with %d\n", status);
fprintf(stderr, "[device] rtlsdr_read_sync exited with %d\n", status_read);
});
}

Expand All @@ -43,30 +42,30 @@ Device::~Device() {
// 2. Driver goes into an infinite loop
// 3. runner_thread doesn't exit and destructor is stuck at thread join
// 4. It works but rtlsdr_read_async returns a negative status code
runner_thread->join();
rtlsdr_close(device);
device = nullptr;
m_runner_thread->join();
rtlsdr_close(m_device);
m_device = nullptr;
}

void Device::Close() {
is_running = false;
rtlsdr_cancel_async(device);
m_is_running = false;
rtlsdr_cancel_async(m_device);
}

void Device::SetAutoGain(void) {
const int status = rtlsdr_set_tuner_gain_mode(device, 0);
const int status = rtlsdr_set_tuner_gain_mode(m_device, 0);
if (status < 0) {
error_list.push_back(fmt::format("Failed to set tuner gain mode to automatic ({})", status));
m_error_list.push_back(fmt::format("Failed to set tuner gain mode to automatic ({})", status));
return;
}
is_gain_manual = false;
selected_gain = 0.0f;
m_is_gain_manual = false;
m_selected_gain = 0.0f;
}

void Device::SetNearestGain(const float target_gain) {
float min_err = 10000.0f;
float nearest_gain = 0.0f;
for (auto& gain: gain_list) {
for (auto& gain: m_gain_list) {
const float err = std::abs(gain-target_gain);
if (err < min_err) {
min_err = err;
Expand All @@ -79,24 +78,24 @@ void Device::SetNearestGain(const float target_gain) {
void Device::SetGain(const float gain) {
const int qgain = static_cast<int>(gain*10.0f);
int status = 0;
status = rtlsdr_set_tuner_gain_mode(device, 1);
status = rtlsdr_set_tuner_gain_mode(m_device, 1);
if (status < 0) {
error_list.push_back(fmt::format("Failed to set tuner gain mode to manual ({})", status));
m_error_list.push_back(fmt::format("Failed to set tuner gain mode to manual ({})", status));
return;
}
status = rtlsdr_set_tuner_gain(device, qgain);
status = rtlsdr_set_tuner_gain(m_device, qgain);
if (status < 0) {
error_list.push_back(fmt::format("Failed to set manual gain to {:.1f}dB ({})", gain, status));
m_error_list.push_back(fmt::format("Failed to set manual gain to {:.1f}dB ({})", gain, status));
return;
}
is_gain_manual = true;
selected_gain = gain;
m_is_gain_manual = true;
m_selected_gain = gain;
}

void Device::SetSamplingFrequency(const uint32_t freq) {
const int status = rtlsdr_set_sample_rate(device, freq);
const int status = rtlsdr_set_sample_rate(m_device, freq);
if (status < 0) {
error_list.push_back(fmt::format("Failed to set sampling frequency to {} Hz ({})", freq, status));
m_error_list.push_back(fmt::format("Failed to set sampling frequency to {} Hz ({})", freq, status));
return;
}
}
Expand All @@ -106,41 +105,41 @@ void Device::SetCenterFrequency(const uint32_t freq) {
}

void Device::SetCenterFrequency(const std::string& label, const uint32_t freq) {
if (callback_on_center_frequency != nullptr) {
callback_on_center_frequency(label, freq);
if (m_callback_on_center_frequency != nullptr) {
m_callback_on_center_frequency(label, freq);
}
const int status = rtlsdr_set_center_freq(device, freq);
const int status = rtlsdr_set_center_freq(m_device, freq);
if (status < 0) {
error_list.push_back(fmt::format("Failed to set center frequency to {}@{}Hz ({})", label, freq, status));
m_error_list.push_back(fmt::format("Failed to set center frequency to {}@{}Hz ({})", label, freq, status));
// Resend notification with original frequency
if (callback_on_center_frequency != nullptr) {
callback_on_center_frequency(selected_frequency_label, selected_frequency);
if (m_callback_on_center_frequency != nullptr) {
m_callback_on_center_frequency(m_selected_frequency_label, m_selected_frequency);
}
return;
}
selected_frequency_label = label;
selected_frequency = freq;
m_selected_frequency_label = label;
m_selected_frequency = freq;
}

void Device::SearchGains(void) {
const int total_gains = rtlsdr_get_tuner_gains(device, NULL);
const int total_gains = rtlsdr_get_tuner_gains(m_device, NULL);
if (total_gains <= 0) {
return;
}
auto qgains = std::vector<int>(size_t(total_gains));
gain_list.resize(size_t(total_gains));
rtlsdr_get_tuner_gains(device, qgains.data());
m_gain_list.resize(size_t(total_gains));
rtlsdr_get_tuner_gains(m_device, qgains.data());
for (size_t i = 0; i < size_t(total_gains); i++) {
const int qgain = qgains[i];
const float gain = static_cast<float>(qgain) * 0.1f;
gain_list[i] = gain;
m_gain_list[i] = gain;
}
}

void Device::OnData(tcb::span<const uint8_t> buf) {
if (!is_running) return;
if (callback_on_data == nullptr) return;
const size_t total_bytes = callback_on_data(buf);
if (!m_is_running) return;
if (m_callback_on_data == nullptr) return;
const size_t total_bytes = m_callback_on_data(buf);
if (total_bytes != buf.size()) {
fprintf(stderr, "Short write, samples lost, %zu/%zu, shutting down device!\n", total_bytes, buf.size());
Close();
Expand Down
50 changes: 25 additions & 25 deletions examples/device/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,38 @@ struct DeviceDescriptor {
class Device
{
private:
DeviceDescriptor descriptor;
struct rtlsdr_dev* device;
const int block_size;
bool is_running;
std::unique_ptr<std::thread> runner_thread;
DeviceDescriptor m_descriptor;
struct rtlsdr_dev* m_device;
const int m_block_size;
bool m_is_running;
std::unique_ptr<std::thread> m_runner_thread;

std::vector<float> gain_list;
bool is_gain_manual;
float selected_gain;
uint32_t selected_frequency;
std::string selected_frequency_label;
std::list<std::string> error_list;
std::function<size_t(tcb::span<const uint8_t>)> callback_on_data = nullptr;
std::function<void(const std::string&, const uint32_t)> callback_on_center_frequency = nullptr;
std::vector<float> m_gain_list;
bool m_is_gain_manual;
float m_selected_gain;
uint32_t m_selected_frequency;
std::string m_selected_frequency_label;
std::list<std::string> m_error_list;
std::function<size_t(tcb::span<const uint8_t>)> m_callback_on_data = nullptr;
std::function<void(const std::string&, const uint32_t)> m_callback_on_center_frequency = nullptr;
public:
Device(struct rtlsdr_dev* _device, const DeviceDescriptor& _descriptor, const int _block_size=8192);
explicit Device(struct rtlsdr_dev* device, const DeviceDescriptor& descriptor, const int block_size=8192);
~Device();
// we are holding a pointer to rtlsdr_dev_t, so we cant move/copy this class
Device(Device&) = delete;
Device(Device&&) = delete;
Device& operator=(Device&) = delete;
Device& operator=(Device&&) = delete;
void Close();
bool IsRunning() const { return is_running; }
const auto& GetDescriptor() { return descriptor; }
int GetBlockSize(void) { return block_size; }
const auto& GetGainList(void) { return gain_list; }
bool GetIsGainManual(void) { return is_gain_manual; }
float GetSelectedGain(void) { return selected_gain; }
uint32_t GetSelectedFrequency(void) { return selected_frequency; }
const auto& GetSelectedFrequencyLabel(void) { return selected_frequency_label; }
auto& GetErrorList(void) { return error_list; }
bool IsRunning() const { return m_is_running; }
const auto& GetDescriptor() { return m_descriptor; }
int GetBlockSize(void) { return m_block_size; }
const auto& GetGainList(void) { return m_gain_list; }
bool GetIsGainManual(void) { return m_is_gain_manual; }
float GetSelectedGain(void) { return m_selected_gain; }
uint32_t GetSelectedFrequency(void) { return m_selected_frequency; }
const auto& GetSelectedFrequencyLabel(void) { return m_selected_frequency_label; }
auto& GetErrorList(void) { return m_error_list; }
void SetAutoGain(void);
void SetNearestGain(const float target_gain);
void SetGain(const float gain);
Expand All @@ -59,11 +59,11 @@ class Device
void SetCenterFrequency(const std::string& label, const uint32_t freq);
template <typename F>
void SetDataCallback(F&& func) {
callback_on_data = std::move(func);
m_callback_on_data = std::move(func);
}
template <typename F>
void SetFrequencyChangeCallback(F&& func) {
callback_on_center_frequency = std::move(func);
m_callback_on_center_frequency = std::move(func);
}
private:
void SearchGains(void);
Expand Down
4 changes: 2 additions & 2 deletions examples/device/device_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ std::shared_ptr<Device> DeviceList::get_device(size_t index) {
return nullptr;
}

auto lock = std::unique_lock(m_mutex_descriptors);
auto lock_descriptors = std::unique_lock(m_mutex_descriptors);
const auto descriptor = m_descriptors[index];
lock.unlock();
lock_descriptors.unlock();

rtlsdr_dev_t* device = nullptr;
const auto status = rtlsdr_open(&device, uint32_t(index));
Expand Down
Loading

0 comments on commit 493a538

Please sign in to comment.