2022-10-24 13:44:46 +02:00
|
|
|
#include "rtltcp.h"
|
|
|
|
|
#include "imgui/imgui_stdlib.h"
|
2023-11-27 21:53:19 -05:00
|
|
|
#include "common/widgets/stepped_slider.h"
|
2022-10-24 13:44:46 +02:00
|
|
|
|
|
|
|
|
void RTLTCPSource::set_gains()
|
|
|
|
|
{
|
|
|
|
|
if (!is_started)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-12-03 23:42:43 +01:00
|
|
|
client.setAGCMode(lna_agc_enabled);
|
2025-01-06 10:38:42 -05:00
|
|
|
logger->debug("Set RTL-TCP LNA AGC to %d", (int)lna_agc_enabled);
|
2022-10-24 13:44:46 +02:00
|
|
|
|
2025-01-06 10:38:42 -05:00
|
|
|
if (tuner_agc_enabled)
|
|
|
|
|
{
|
|
|
|
|
client.setGainMode(0);
|
|
|
|
|
logger->debug("Set RTL-TCP Tuner AGC to %d", 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
client.setGainMode(1);
|
|
|
|
|
logger->debug("Set RTL-TCP Tuner AGC to %d", 0);
|
|
|
|
|
client.setGain(gain * 10);
|
|
|
|
|
logger->debug("Set RTL-TCP Gain to %d", gain * 10);
|
|
|
|
|
}
|
2022-10-24 13:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTLTCPSource::set_bias()
|
|
|
|
|
{
|
|
|
|
|
if (!is_started)
|
|
|
|
|
return;
|
|
|
|
|
client.setBiasTee(bias);
|
2023-05-08 23:08:34 +02:00
|
|
|
logger->debug("Set RTL-TCP Bias to %d", (int)bias);
|
2022-10-24 13:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-25 22:30:54 -04:00
|
|
|
void RTLTCPSource::set_ppm()
|
|
|
|
|
{
|
|
|
|
|
if (!is_started)
|
|
|
|
|
return;
|
|
|
|
|
int ppm = ppm_widget.get();
|
|
|
|
|
client.setPPM(ppm);
|
|
|
|
|
logger->debug("Set RTL-TCP PPM Correction to %d", ppm);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-24 13:44:46 +02:00
|
|
|
void RTLTCPSource::set_settings(nlohmann::json settings)
|
|
|
|
|
{
|
|
|
|
|
d_settings = settings;
|
|
|
|
|
|
2025-01-06 10:38:42 -05:00
|
|
|
// Convert legacy AGC setting to new (was not used for RTL-TCP before, but it was for USB)
|
|
|
|
|
if (d_settings.contains("agc"))
|
|
|
|
|
{
|
|
|
|
|
lna_agc_enabled = tuner_agc_enabled = getValueOrDefault(d_settings["agc"], false);
|
|
|
|
|
d_settings.erase("agc");
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-24 13:44:46 +02:00
|
|
|
ip_address = getValueOrDefault(d_settings["ip_address"], ip_address);
|
|
|
|
|
port = getValueOrDefault(d_settings["port"], port);
|
|
|
|
|
gain = getValueOrDefault(d_settings["gain"], gain);
|
|
|
|
|
lna_agc_enabled = getValueOrDefault(d_settings["lna_agc"], lna_agc_enabled);
|
2025-01-06 10:38:42 -05:00
|
|
|
tuner_agc_enabled = getValueOrDefault(d_settings["tuner_agc"], lna_agc_enabled);
|
2022-10-24 13:44:46 +02:00
|
|
|
bias = getValueOrDefault(d_settings["bias"], bias);
|
2023-09-25 22:30:54 -04:00
|
|
|
ppm_widget.set(getValueOrDefault(d_settings["ppm_correction"], ppm_widget.get()));
|
2022-10-24 13:44:46 +02:00
|
|
|
|
|
|
|
|
if (is_open && is_started)
|
|
|
|
|
{
|
|
|
|
|
set_gains();
|
2023-03-08 18:58:37 +01:00
|
|
|
set_bias();
|
2023-09-25 22:30:54 -04:00
|
|
|
set_ppm();
|
2022-10-24 13:44:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nlohmann::json RTLTCPSource::get_settings()
|
|
|
|
|
{
|
|
|
|
|
d_settings["ip_address"] = ip_address;
|
|
|
|
|
d_settings["port"] = port;
|
|
|
|
|
d_settings["gain"] = gain;
|
|
|
|
|
d_settings["lna_agc"] = lna_agc_enabled;
|
2025-01-06 10:38:42 -05:00
|
|
|
d_settings["tuner_agc"] = tuner_agc_enabled;
|
2023-03-08 18:58:37 +01:00
|
|
|
d_settings["bias"] = bias;
|
2023-09-25 22:30:54 -04:00
|
|
|
d_settings["ppm_correction"] = ppm_widget.get();
|
2022-10-24 13:44:46 +02:00
|
|
|
|
|
|
|
|
return d_settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTLTCPSource::open()
|
|
|
|
|
{
|
|
|
|
|
// Nothing to do
|
|
|
|
|
is_open = true;
|
|
|
|
|
|
|
|
|
|
// Set available samplerate
|
2023-07-14 23:55:02 +02:00
|
|
|
std::vector<double> available_samplerates;
|
2022-10-24 13:44:46 +02:00
|
|
|
available_samplerates.push_back(250000);
|
|
|
|
|
available_samplerates.push_back(1024000);
|
|
|
|
|
available_samplerates.push_back(1536000);
|
|
|
|
|
available_samplerates.push_back(1792000);
|
|
|
|
|
available_samplerates.push_back(1920000);
|
|
|
|
|
available_samplerates.push_back(2048000);
|
|
|
|
|
available_samplerates.push_back(2160000);
|
|
|
|
|
available_samplerates.push_back(2400000);
|
|
|
|
|
available_samplerates.push_back(2560000);
|
|
|
|
|
available_samplerates.push_back(2880000);
|
|
|
|
|
available_samplerates.push_back(3200000);
|
|
|
|
|
|
2023-09-07 21:49:55 -04:00
|
|
|
samplerate_widget.set_list(available_samplerates, true);
|
2022-10-24 13:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTLTCPSource::start()
|
|
|
|
|
{
|
|
|
|
|
if (client.connectToRTL((char *)ip_address.c_str(), port) != 1)
|
2024-03-14 12:12:34 +01:00
|
|
|
throw satdump_exception("Could not connect to RTL-TCP");
|
2022-10-24 13:44:46 +02:00
|
|
|
|
|
|
|
|
DSPSampleSource::start(); // Do NOT reset the stream
|
|
|
|
|
|
2023-07-14 23:55:02 +02:00
|
|
|
uint64_t current_samplerate = samplerate_widget.get_value();
|
|
|
|
|
|
2022-10-24 13:44:46 +02:00
|
|
|
client.setSampleRate(current_samplerate);
|
|
|
|
|
|
|
|
|
|
is_started = true;
|
|
|
|
|
|
|
|
|
|
set_frequency(d_frequency);
|
|
|
|
|
|
|
|
|
|
set_gains();
|
|
|
|
|
set_bias();
|
2023-09-25 22:30:54 -04:00
|
|
|
set_ppm();
|
2022-10-24 13:44:46 +02:00
|
|
|
|
2022-11-16 20:01:56 +01:00
|
|
|
thread_should_run = true;
|
|
|
|
|
work_thread = std::thread(&RTLTCPSource::mainThread, this);
|
2022-10-24 13:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTLTCPSource::stop()
|
|
|
|
|
{
|
|
|
|
|
if (is_started)
|
|
|
|
|
{
|
2022-11-16 20:01:56 +01:00
|
|
|
thread_should_run = false;
|
|
|
|
|
logger->info("Waiting for the thread...");
|
|
|
|
|
if (is_started)
|
|
|
|
|
output_stream->stopWriter();
|
|
|
|
|
if (work_thread.joinable())
|
|
|
|
|
work_thread.join();
|
|
|
|
|
logger->info("Thread stopped");
|
|
|
|
|
|
2023-11-24 21:12:56 +01:00
|
|
|
client.setBiasTee(false);
|
2022-10-24 13:44:46 +02:00
|
|
|
client.disconnect();
|
|
|
|
|
}
|
|
|
|
|
is_started = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTLTCPSource::close()
|
|
|
|
|
{
|
|
|
|
|
is_open = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTLTCPSource::set_frequency(uint64_t frequency)
|
|
|
|
|
{
|
|
|
|
|
if (is_open && is_started)
|
|
|
|
|
{
|
|
|
|
|
client.setFrequency(frequency);
|
2023-05-08 23:08:34 +02:00
|
|
|
logger->debug("Set RTL-TCP frequency to %d", frequency);
|
2022-10-24 13:44:46 +02:00
|
|
|
}
|
|
|
|
|
DSPSampleSource::set_frequency(frequency);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTLTCPSource::drawControlUI()
|
|
|
|
|
{
|
|
|
|
|
if (is_started)
|
|
|
|
|
style::beginDisabled();
|
2023-07-14 23:55:02 +02:00
|
|
|
|
|
|
|
|
samplerate_widget.render();
|
|
|
|
|
|
2022-10-24 13:44:46 +02:00
|
|
|
if (is_started)
|
|
|
|
|
style::endDisabled();
|
|
|
|
|
|
|
|
|
|
if (is_started)
|
|
|
|
|
style::beginDisabled();
|
2023-02-01 11:40:26 -08:00
|
|
|
ImGui::InputText("Address", &ip_address);
|
2022-10-24 13:44:46 +02:00
|
|
|
ImGui::InputInt("Port", &port);
|
|
|
|
|
if (is_started)
|
|
|
|
|
style::endDisabled();
|
|
|
|
|
|
2023-09-26 11:35:21 -04:00
|
|
|
if (ppm_widget.draw())
|
|
|
|
|
set_ppm();
|
|
|
|
|
|
2022-10-24 13:44:46 +02:00
|
|
|
bool gain_changed = false;
|
2025-01-06 10:38:42 -05:00
|
|
|
|
|
|
|
|
if (tuner_agc_enabled)
|
|
|
|
|
style::beginDisabled();
|
2026-04-03 12:53:14 +02:00
|
|
|
gain_changed |= satdump::widgets::SteppedSliderInt("Tuner Gain", &gain, 0, 49);
|
2025-01-06 10:38:42 -05:00
|
|
|
if (tuner_agc_enabled)
|
|
|
|
|
style::endDisabled();
|
|
|
|
|
|
|
|
|
|
gain_changed |= ImGui::Checkbox("LNA AGC", &lna_agc_enabled);
|
|
|
|
|
gain_changed |= ImGui::Checkbox("Tuner AGC", &tuner_agc_enabled);
|
2022-10-24 13:44:46 +02:00
|
|
|
if (gain_changed)
|
|
|
|
|
set_gains();
|
2023-03-08 18:58:37 +01:00
|
|
|
|
|
|
|
|
if (ImGui::Checkbox("Bias-Tee", &bias))
|
|
|
|
|
set_bias();
|
2022-10-24 13:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTLTCPSource::set_samplerate(uint64_t samplerate)
|
|
|
|
|
{
|
2023-07-14 23:55:02 +02:00
|
|
|
if (!samplerate_widget.set_value(samplerate, 3.2e6))
|
2024-03-15 23:59:48 +02:00
|
|
|
throw satdump_exception("Unsupported samplerate : " + std::to_string(samplerate) + "!");
|
2022-10-24 13:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t RTLTCPSource::get_samplerate()
|
|
|
|
|
{
|
2023-07-14 23:55:02 +02:00
|
|
|
return samplerate_widget.get_value();
|
2022-10-24 13:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<dsp::SourceDescriptor> RTLTCPSource::getAvailableSources()
|
|
|
|
|
{
|
|
|
|
|
std::vector<dsp::SourceDescriptor> results;
|
|
|
|
|
|
2024-05-01 16:26:07 +02:00
|
|
|
results.push_back({"rtltcp", "RTL-TCP", "0", false});
|
2022-10-24 13:44:46 +02:00
|
|
|
|
|
|
|
|
return results;
|
2023-09-26 11:35:21 -04:00
|
|
|
}
|