From 55fd0c986fa08f3736d2a11d452dd169badc952b Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Mon, 13 Apr 2015 21:18:45 -0400 Subject: [PATCH] PPM correction now editable + saved and loaded/applied per-device by serial and name --- src/AppConfig.cpp | 10 ++++---- src/AppConfig.h | 4 ++-- src/AppFrame.cpp | 5 ++++ src/AppFrame.h | 1 + src/CubicSDR.cpp | 53 +++++++++++++++++++++++++++++++++---------- src/CubicSDR.h | 4 ++++ src/sdr/SDRThread.cpp | 25 ++++++++++++++++---- src/sdr/SDRThread.h | 12 +++++++++- 8 files changed, 89 insertions(+), 25 deletions(-) diff --git a/src/AppConfig.cpp b/src/AppConfig.cpp index f676393..41e92a5 100644 --- a/src/AppConfig.cpp +++ b/src/AppConfig.cpp @@ -19,15 +19,15 @@ std::string AppConfig::getConfigDir() { return dataDir; } -void AppConfig::setPPM(std::string device_serial, int ppm) { - device_ppm[device_serial] = ppm; +void AppConfig::setPPM(std::string deviceId, int ppm) { + device_ppm[deviceId] = ppm; } -int AppConfig::getPPM(std::string device_serial) { - if (device_ppm.find(device_serial) == device_ppm.end()) { +int AppConfig::getPPM(std::string deviceId) { + if (device_ppm.find(deviceId) == device_ppm.end()) { return 0; } - return device_ppm[device_serial]; + return device_ppm[deviceId]; } bool AppConfig::save() { diff --git a/src/AppConfig.h b/src/AppConfig.h index d2cba42..d613e0e 100644 --- a/src/AppConfig.h +++ b/src/AppConfig.h @@ -11,8 +11,8 @@ public: std::string getConfigDir(); - void setPPM(std::string device_serial, int ppm); - int getPPM(std::string device_serial); + void setPPM(std::string deviceId, int ppm); + int getPPM(std::string deviceId); bool save(); bool load(); bool reset(); diff --git a/src/AppFrame.cpp b/src/AppFrame.cpp index 26ce8ff..f2b0fad 100644 --- a/src/AppFrame.cpp +++ b/src/AppFrame.cpp @@ -132,6 +132,7 @@ AppFrame::AppFrame() : wxMenu *menu = new wxMenu; // menu->Append(wxID_NEW); menu->Append(wxID_SET_FREQ_OFFSET, "Set Frequency Offset"); + menu->Append(wxID_SET_PPM, "Set Device PPM"); menu->Append(wxID_OPEN, "&Open Session"); menu->Append(wxID_SAVE, "&Save Session"); menu->Append(wxID_SAVEAS, "Save Session &As.."); @@ -318,6 +319,10 @@ void AppFrame::OnMenu(wxCommandEvent& event) { if (ofs != -1) { wxGetApp().setOffset(ofs); } + } else if (event.GetId() == wxID_SET_PPM) { + long ofs = wxGetNumberFromUser("Frequency correction for device in PPM.\ni.e. -51 for -51 PPM", "Parts per million (PPM)", + "Frequency Correction", wxGetApp().getPPM(), -1000, 1000, this); + wxGetApp().setPPM(ofs); } else if (event.GetId() == wxID_SAVE) { if (!currentSessionFile.empty()) { saveSession(currentSessionFile); diff --git a/src/AppFrame.h b/src/AppFrame.h index 1863dc2..5a7967f 100644 --- a/src/AppFrame.h +++ b/src/AppFrame.h @@ -15,6 +15,7 @@ #define wxID_RT_AUDIO_DEVICE 1000 #define wxID_SET_FREQ_OFFSET 2001 #define wxID_RESET 2002 +#define wxID_SET_PPM 2003 #define wxID_THEME_DEFAULT 2100 #define wxID_THEME_SHARP 2101 diff --git a/src/CubicSDR.cpp b/src/CubicSDR.cpp index 4ed2979..660cbec 100644 --- a/src/CubicSDR.cpp +++ b/src/CubicSDR.cpp @@ -33,24 +33,18 @@ bool CubicSDR::OnInit() { CFRelease(resourcesURL); chdir(path); #endif - + if (!wxApp::OnInit()) { return false; } - wxApp:SetAppName("cubicsdr"); + wxApp: SetAppName("cubicsdr"); - config.setPPM("RTLBlah :: 00000001",11); - config.setPPM("RTLBlah :: 00000002",12); - config.save(); config.load(); - std::cout << "test1: " << config.getPPM("RTLBlah :: 00000001") << std::endl; - std::cout << "test2: " << config.getPPM("RTLBlah :: 00000002") << std::endl; - std::cout << "test3: " << config.getPPM("foo") << std::endl; - frequency = DEFAULT_FREQ; offset = 0; + ppm = 0; audioVisualQueue = new DemodulatorThreadOutputQueue(); audioVisualQueue->set_max_num_items(1); @@ -59,7 +53,7 @@ bool CubicSDR::OnInit() { sdrThread = new SDRThread(threadCmdQueueSDR); sdrPostThread = new SDRPostThread(); - sdrPostThread->setNumVisSamples(16384*2); + sdrPostThread->setNumVisSamples(16384 * 2); iqPostDataQueue = new SDRThreadIQDataQueue; iqVisualQueue = new DemodulatorThreadInputQueue; @@ -89,7 +83,7 @@ bool CubicSDR::OnInit() { choices.Add(devName); } - int devId = wxGetSingleChoiceIndex(wxT("Devices"),wxT("Choose Input Device"),choices); + int devId = wxGetSingleChoiceIndex(wxT("Devices"), wxT("Choose Input Device"), choices); std::cout << "Chosen: " << devId << std::endl; sdrThread->setDeviceId(devId); @@ -228,13 +222,48 @@ void CubicSDR::setDevice(int deviceId) { SDRThreadCommand command(SDRThreadCommand::SDR_THREAD_CMD_SET_DEVICE); command.llong_value = deviceId; threadCmdQueueSDR->push(command); + + SDRDeviceInfo *dev = (*getDevices())[deviceId]; + + SDRThreadCommand command_ppm(SDRThreadCommand::SDR_THREAD_CMD_SET_PPM); + ppm = config.getPPM(dev->getDeviceId()); + command_ppm.llong_value = ppm; + threadCmdQueueSDR->push(command_ppm); } int CubicSDR::getDevice() { return sdrThread->getDeviceId(); } - AppConfig *CubicSDR::getConfig() { return &config; } + +void CubicSDR::setPPM(int ppm_in) { + if (sdrThread->getDeviceId() < 0) { + return; + } + ppm = ppm_in; + + SDRThreadCommand command(SDRThreadCommand::SDR_THREAD_CMD_SET_PPM); + command.llong_value = ppm; + threadCmdQueueSDR->push(command); + + SDRDeviceInfo *dev = (*getDevices())[getDevice()]; + + config.setPPM(dev->getDeviceId(), ppm_in); + config.save(); +} + +int CubicSDR::getPPM() { + if (sdrThread->getDeviceId() < 0) { + return 0; + } + SDRDeviceInfo *dev = (*getDevices())[getDevice()]; + + SDRThreadCommand command_ppm(SDRThreadCommand::SDR_THREAD_CMD_SET_PPM); + ppm = config.getPPM(dev->getDeviceId()); + + return ppm; +} + diff --git a/src/CubicSDR.h b/src/CubicSDR.h index aead935..1c6da6a 100644 --- a/src/CubicSDR.h +++ b/src/CubicSDR.h @@ -52,6 +52,9 @@ public: AppConfig *getConfig(); + void setPPM(int ppm_in); + int getPPM(); + private: AppConfig config; PrimaryGLContext *m_glContext; @@ -61,6 +64,7 @@ private: long long frequency; long long offset; + int ppm; long long sampleRate; SDRThread *sdrThread; diff --git a/src/sdr/SDRThread.cpp b/src/sdr/SDRThread.cpp index 405ed0f..22ae5e7 100644 --- a/src/sdr/SDRThread.cpp +++ b/src/sdr/SDRThread.cpp @@ -122,8 +122,9 @@ void SDRThread::threadMain() { std::cout << "SDR thread initializing.." << std::endl; int devCount = rtlsdr_get_device_count(); + std::vector devs; if (deviceId == -1) { - deviceId = enumerate_rtl(NULL); + deviceId = enumerate_rtl(&devs); } if (deviceId == -1) { @@ -136,10 +137,12 @@ void SDRThread::threadMain() { signed char buf[BUF_SIZE]; long long frequency = DEFAULT_FREQ; + int ppm = wxGetApp().getConfig()->getPPM(devs[deviceId]->getDeviceId()); rtlsdr_open(&dev, deviceId); rtlsdr_set_sample_rate(dev, sampleRate); rtlsdr_set_center_freq(dev, frequency - offset); + rtlsdr_set_freq_correction(dev, ppm); rtlsdr_set_agc_mode(dev, 1); rtlsdr_set_offset_tuning(dev, 0); rtlsdr_reset_buffer(dev); @@ -164,10 +167,12 @@ void SDRThread::threadMain() { bool offset_changed = false; bool rate_changed = false; bool device_changed = false; - long long new_freq; - long long new_offset; - long long new_rate; - int new_device; + bool ppm_changed = false; + long long new_freq = frequency; + long long new_offset = offset; + long long new_rate = sampleRate; + int new_device = deviceId; + int new_ppm = ppm; while (!cmdQueue->empty()) { SDRThreadCommand command; @@ -197,6 +202,11 @@ void SDRThread::threadMain() { new_device = (int) command.llong_value; std::cout << "Set device: " << new_device << std::endl; break; + case SDRThreadCommand::SDR_THREAD_CMD_SET_PPM: + ppm_changed = true; + new_ppm = (int) command.llong_value; + std::cout << "Set PPM: " << new_ppm << std::endl; + break; default: break; } @@ -207,6 +217,7 @@ void SDRThread::threadMain() { rtlsdr_open(&dev, new_device); rtlsdr_set_sample_rate(dev, sampleRate); rtlsdr_set_center_freq(dev, frequency - offset); + rtlsdr_set_freq_correction(dev, ppm); rtlsdr_set_agc_mode(dev, 1); rtlsdr_set_offset_tuning(dev, 0); rtlsdr_reset_buffer(dev); @@ -224,6 +235,10 @@ void SDRThread::threadMain() { frequency = new_freq; rtlsdr_set_center_freq(dev, frequency - offset); } + if (ppm_changed) { + ppm = new_ppm; + rtlsdr_set_freq_correction(dev, ppm); + } } rtlsdr_read_sync(dev, buf, BUF_SIZE, &n_read); diff --git a/src/sdr/SDRThread.h b/src/sdr/SDRThread.h index 47c0062..57e8523 100644 --- a/src/sdr/SDRThread.h +++ b/src/sdr/SDRThread.h @@ -11,6 +11,16 @@ class SDRDeviceInfo { public: SDRDeviceInfo() : name(""), serial(""), available(false) { } + std::string getDeviceId() { + std::string deviceId; + + deviceId.append(getName()); + deviceId.append(" :: "); + deviceId.append(getSerial()); + + return deviceId; + } + bool isAvailable() const { return available; } @@ -71,7 +81,7 @@ private: class SDRThreadCommand { public: enum SDRThreadCommandEnum { - SDR_THREAD_CMD_NULL, SDR_THREAD_CMD_TUNE, SDR_THREAD_CMD_SET_OFFSET, SDR_THREAD_CMD_SET_SAMPLERATE, SDR_THREAD_CMD_SET_DEVICE + SDR_THREAD_CMD_NULL, SDR_THREAD_CMD_TUNE, SDR_THREAD_CMD_SET_OFFSET, SDR_THREAD_CMD_SET_SAMPLERATE, SDR_THREAD_CMD_SET_PPM, SDR_THREAD_CMD_SET_DEVICE }; SDRThreadCommand() :