diff --git a/src/AppFrame.cpp b/src/AppFrame.cpp index 075d2dd..86547d8 100644 --- a/src/AppFrame.cpp +++ b/src/AppFrame.cpp @@ -19,9 +19,9 @@ wxBEGIN_EVENT_TABLE(AppFrame, wxFrame) //EVT_MENU(wxID_NEW, AppFrame::OnNewWindow) EVT_MENU(wxID_CLOSE, AppFrame::OnClose) -EVT_THREAD(EVENT_SDR_INPUT, AppFrame::OnEventInput) EVT_THREAD(EVENT_DEMOD_INPUT, AppFrame::OnDemodInput) EVT_THREAD(EVENT_AUDIO_INPUT, AppFrame::OnAudioInput) +EVT_COMMAND(wxID_ANY, wxEVT_THREAD, AppFrame::OnThread) EVT_IDLE(AppFrame::OnIdle) wxEND_EVENT_TABLE() @@ -138,25 +138,39 @@ void AppFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event)) { new AppFrame(); } -// SDR IQ -> Demodulator -void AppFrame::OnEventInput(wxThreadEvent& event) { - std::vector *new_buffer = event.GetPayload *>(); -// std::cout << new_buffer->size() << std::endl; - if (new_buffer->size()) { - DemodulatorThreadTask task = DemodulatorThreadTask(DemodulatorThreadTask::DEMOD_THREAD_DATA); - task.setData(*new_buffer); - threadQueueDemod->addTask(task, DemodulatorThreadQueue::DEMOD_PRIORITY_HIGHEST); -// test_demod.writeBuffer(new_buffer); -// scopeCanvas->setWaveformPoints(test_demod.waveform_points); - spectrumCanvas->setData(new_buffer); - waterfallCanvas->setData(new_buffer); - } else { - std::cout << "Incoming IQ data empty?" << std::endl; +void AppFrame::OnThread(wxCommandEvent& event) { + SDRThreadIQData *iqData; + std::vector *new_buffer; + + switch (event.GetId()) { + + // SDR IQ -> Demodulator + case SDRThreadTask::SDR_THREAD_DATA: + iqData = (SDRThreadIQData *) event.GetClientData(); + new_buffer = &(iqData->data); + if (new_buffer->size()) { + DemodulatorThreadTask task = DemodulatorThreadTask(DemodulatorThreadTask::DEMOD_THREAD_DATA); + task.setData(*new_buffer); + + threadQueueDemod->addTask(task, DemodulatorThreadQueue::DEMOD_PRIORITY_HIGHEST); + + spectrumCanvas->setData(new_buffer); + waterfallCanvas->setData(new_buffer); + } else { + std::cout << "Incoming IQ data empty?" << std::endl; + } + delete iqData; + + break; // thread wants to exit: disable controls and destroy main window + default: + event.Skip(); } - delete new_buffer; } + + + // Demodulator -> Audio void AppFrame::OnDemodInput(wxThreadEvent& event) { std::vector *new_buffer = event.GetPayload *>(); diff --git a/src/AppFrame.h b/src/AppFrame.h index a460349..730b096 100644 --- a/src/AppFrame.h +++ b/src/AppFrame.h @@ -15,6 +15,7 @@ class AppFrame: public wxFrame { public: AppFrame(); ~AppFrame(); + void OnThread (wxCommandEvent& event); void OnEventInput(wxThreadEvent& event); void OnDemodInput(wxThreadEvent& event); void OnAudioInput(wxThreadEvent& event); diff --git a/src/SDRThread.cpp b/src/SDRThread.cpp index d5f0824..8fd5a50 100644 --- a/src/SDRThread.cpp +++ b/src/SDRThread.cpp @@ -103,9 +103,12 @@ wxThread::ExitCode SDRThread::Entry() { signed char *buf = (signed char *) malloc(BUF_SIZE); + unsigned int frequency = DEFAULT_FREQ; + unsigned int bandwidth = SRATE; + rtlsdr_open(&dev, first_available); - rtlsdr_set_sample_rate(dev, SRATE); - rtlsdr_set_center_freq(dev, DEFAULT_FREQ); + rtlsdr_set_sample_rate(dev, bandwidth); + rtlsdr_set_center_freq(dev, frequency); rtlsdr_set_agc_mode(dev, 1); rtlsdr_set_offset_tuning(dev, 1); rtlsdr_reset_buffer(dev); @@ -136,17 +139,18 @@ wxThread::ExitCode SDRThread::Entry() { } if (freq_changed) { - rtlsdr_set_center_freq(dev, new_freq); + frequency = new_freq; + rtlsdr_set_center_freq(dev, frequency); } } rtlsdr_read_sync(dev, buf, BUF_SIZE, &n_read); if (!TestDestroy()) { - std::vector *new_buffer = new std::vector(); + std::vector new_buffer; for (int i = 0; i < n_read; i++) { - new_buffer->push_back(buf[i] - 127); + new_buffer.push_back(buf[i] - 127); } double time_slice = (double) n_read / (double) sample_rate; @@ -154,11 +158,8 @@ wxThread::ExitCode SDRThread::Entry() { // std::cout << "Time Slice: " << time_slice << std::endl; if (!TestDestroy()) { - wxThreadEvent event(wxEVT_THREAD, EVENT_SDR_INPUT); - event.SetPayload(new_buffer); - wxQueueEvent(m_pQueue->getHandler(), event.Clone()); - } else { - delete new_buffer; + SDRThreadIQData *iqData = new SDRThreadIQData(bandwidth,frequency,new_buffer); + m_pQueue->sendIQData(SDRThreadTask::SDR_THREAD_DATA, iqData); } } else { this->Yield(); diff --git a/src/SDRThreadQueue.cpp b/src/SDRThreadQueue.cpp index 6f75c42..ecdd4ce 100644 --- a/src/SDRThreadQueue.cpp +++ b/src/SDRThreadQueue.cpp @@ -26,10 +26,11 @@ SDRThreadTask SDRThreadQueue::pop() { return element; } -void SDRThreadQueue::report(const SDRThreadTask::SDR_COMMAND& cmd, const wxString& sArg, int iArg) { +void SDRThreadQueue::sendIQData(const SDRThreadTask::SDR_COMMAND& cmd, SDRThreadIQData *data) { wxCommandEvent evt(wxEVT_THREAD, cmd); - evt.SetString(sArg); - evt.SetInt(iArg); + + evt.SetClientData(data); + m_pParent->AddPendingEvent(evt); } diff --git a/src/SDRThreadQueue.h b/src/SDRThreadQueue.h index dc9cc3e..ada1d84 100644 --- a/src/SDRThreadQueue.h +++ b/src/SDRThreadQueue.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "SDRThreadTask.h" #include "wx/event.h" @@ -13,7 +14,7 @@ public: SDRThreadQueue(wxEvtHandler* pParent); void addTask(const SDRThreadTask& task, const SDR_PRIORITY& priority = SDR_PRIORITY_NORMAL); - void report(const SDRThreadTask::SDR_COMMAND& cmd, const wxString& sArg = wxEmptyString, int iArg = 0); + void sendIQData(const SDRThreadTask::SDR_COMMAND& cmd, SDRThreadIQData *data); SDRThreadTask pop(); size_t stackSize(); diff --git a/src/SDRThreadTask.h b/src/SDRThreadTask.h index ea6b72b..8f17f50 100644 --- a/src/SDRThreadTask.h +++ b/src/SDRThreadTask.h @@ -1,12 +1,36 @@ #pragma once +#include #include "wx/defs.h" #include "wx/string.h" +#include "wx/object.h" + +class SDRThreadIQData: public wxObject { +public: + unsigned int frequency; + unsigned int bandwidth; + std::vector data; + + SDRThreadIQData(unsigned int bandwidth, unsigned int frequency, std::vector data) : + data(data), frequency(frequency), bandwidth(bandwidth) { + + } + + ~SDRThreadIQData() { + + } +}; class SDRThreadTask { public: enum SDR_COMMAND { - SDR_THREAD_EXIT = wxID_EXIT, SDR_THREAD_NULL = wxID_HIGHEST + 1, SDR_THREAD_STARTED, SDR_THREAD_PROCESS, SDR_THREAD_ERROR, SDR_THREAD_TUNING + SDR_THREAD_EXIT = wxID_EXIT, + SDR_THREAD_NULL = wxID_HIGHEST + 1, + SDR_THREAD_STARTED, + SDR_THREAD_PROCESS, + SDR_THREAD_ERROR, + SDR_THREAD_TUNING, + SDR_THREAD_DATA }; SDRThreadTask() :