Cleanup / Reformat

This commit is contained in:
Charles J. Cliffe 2014-11-22 22:33:32 -05:00
parent 58708a720e
commit 39ba38b82a
10 changed files with 353 additions and 395 deletions

View File

@ -61,9 +61,9 @@ AppFrame::AppFrame() :
audioInputQueue = new AudioThreadInputQueue; audioInputQueue = new AudioThreadInputQueue;
audioThread = new AudioThread(audioInputQueue); audioThread = new AudioThread(audioInputQueue);
t1 = new std::thread(&AudioThread::threadMain, audioThread); threadAudio = new std::thread(&AudioThread::threadMain, audioThread);
demodulatorTest = demodMgr.newThread(this); demodulatorTest = demodMgr.newThread();
demodulatorTest->params.audioInputQueue = audioInputQueue; demodulatorTest->params.audioInputQueue = audioInputQueue;
demodulatorTest->init(); demodulatorTest->init();
@ -77,7 +77,7 @@ AppFrame::AppFrame() :
iqVisualQueue = new SDRThreadIQDataQueue; iqVisualQueue = new SDRThreadIQDataQueue;
sdrThread->setIQVisualQueue(iqVisualQueue); sdrThread->setIQVisualQueue(iqVisualQueue);
t_SDR = new std::thread(&SDRThread::threadMain, sdrThread); threadSDR = new std::thread(&SDRThread::threadMain, sdrThread);
// static const int attribs[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0 }; // static const int attribs[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0 };
// wxLogStatus("Double-buffered display %s supported", wxGLCanvas::IsDisplaySupported(attribs) ? "is" : "not"); // wxLogStatus("Double-buffered display %s supported", wxGLCanvas::IsDisplaySupported(attribs) ? "is" : "not");

View File

@ -16,10 +16,9 @@ class AppFrame: public wxFrame {
public: public:
AppFrame(); AppFrame();
~AppFrame(); ~AppFrame();
void OnThread (wxCommandEvent& event); void OnThread(wxCommandEvent& event);
void OnEventInput(wxThreadEvent& event); void OnEventInput(wxThreadEvent& event);
void setFrequency(unsigned int freq); void setFrequency(unsigned int freq);
int getFrequency(); int getFrequency();
@ -47,8 +46,8 @@ private:
SDRThreadIQDataQueue* iqVisualQueue; SDRThreadIQDataQueue* iqVisualQueue;
DemodulatorThreadOutputQueue* audioVisualQueue; DemodulatorThreadOutputQueue* audioVisualQueue;
std::thread *t1; std::thread *threadAudio;
std::thread *t_SDR; std::thread *threadSDR;
// event table // event table
wxDECLARE_EVENT_TABLE(); wxDECLARE_EVENT_TABLE();

View File

@ -44,6 +44,20 @@ static int audioCallback(const void *inputBuffer, void *outputBuffer, unsigned l
return paContinue; return paContinue;
} }
AudioThread::AudioThread(AudioThreadInputQueue *inputQueue) :
inputQueue(inputQueue), stream(NULL), audio_queue_ptr(0) {
}
AudioThread::~AudioThread() {
PaError err;
err = Pa_StopStream(stream);
err = Pa_CloseStream(stream);
Pa_Terminate();
std::cout << std::endl << "Audio Thread Done." << std::endl << std::endl;
}
void AudioThread::threadMain() { void AudioThread::threadMain() {
PaError err; PaError err;
err = Pa_Initialize(); err = Pa_Initialize();

View File

@ -33,19 +33,9 @@ public:
std::queue<std::vector<float> > audio_queue; std::queue<std::vector<float> > audio_queue;
unsigned int audio_queue_ptr; unsigned int audio_queue_ptr;
AudioThread(AudioThreadInputQueue *inputQueue) : AudioThread(AudioThreadInputQueue *inputQueue);
inputQueue(inputQueue), stream(NULL), audio_queue_ptr(0) {
} ~AudioThread();
~AudioThread() {
PaError err;
err = Pa_StopStream(stream);
err = Pa_CloseStream(stream);
Pa_Terminate();
std::cout << std::endl << "Audio Thread Done." << std::endl << std::endl;
}
void threadMain(); void threadMain();

View File

@ -0,0 +1,40 @@
#include <DemodulatorMgr.h>
DemodulatorInstance::DemodulatorInstance() :
t_Demod(NULL), threadQueueDemod(NULL), demodulatorThread(NULL) {
}
void DemodulatorInstance::setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue) {
demodulatorThread->setVisualOutputQueue(tQueue);
}
void DemodulatorInstance::init() {
if (threadQueueDemod) {
delete threadQueueDemod;
}
if (demodulatorThread) {
delete demodulatorThread;
}
threadQueueDemod = new DemodulatorThreadInputQueue;
demodulatorThread = new DemodulatorThread(threadQueueDemod, &params);
t_Demod = new std::thread(&DemodulatorThread::threadMain, demodulatorThread);
}
DemodulatorMgr::DemodulatorMgr() {
}
DemodulatorMgr::~DemodulatorMgr() {
while (demods.size()) {
DemodulatorInstance *d = demods.back();
demods.pop_back();
delete d;
}
}
DemodulatorInstance *DemodulatorMgr::newThread() {
DemodulatorInstance *newDemod = new DemodulatorInstance;
demods.push_back(newDemod);
return newDemod;
}

View File

@ -13,48 +13,18 @@ public:
DemodulatorThreadInputQueue* threadQueueDemod; DemodulatorThreadInputQueue* threadQueueDemod;
DemodulatorThreadParameters params; DemodulatorThreadParameters params;
DemodulatorInstance() : DemodulatorInstance();
t_Demod(NULL), threadQueueDemod(NULL), demodulatorThread(NULL) { void setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue);
} void init();
void setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue) {
demodulatorThread->setVisualOutputQueue(tQueue);
}
void init() {
if (threadQueueDemod) {
delete threadQueueDemod;
}
if (demodulatorThread) {
delete demodulatorThread;
}
threadQueueDemod = new DemodulatorThreadInputQueue;
demodulatorThread = new DemodulatorThread(threadQueueDemod, &params);
t_Demod = new std::thread(&DemodulatorThread::threadMain, demodulatorThread);
}
}; };
class DemodulatorMgr { class DemodulatorMgr {
public: public:
DemodulatorMgr() { DemodulatorMgr();
~DemodulatorMgr();
} DemodulatorInstance *newThread();
~DemodulatorMgr() {
while (demods.size()) {
DemodulatorInstance *d = demods.back();
demods.pop_back();
delete d;
}
}
DemodulatorInstance *newThread(wxEvtHandler* pParent) {
DemodulatorInstance *newDemod = new DemodulatorInstance;
demods.push_back(newDemod);
return newDemod;
}
private:
std::vector<DemodulatorInstance *> demods; std::vector<DemodulatorInstance *> demods;
}; };

View File

@ -3,7 +3,7 @@
#include <vector> #include <vector>
DemodulatorThread::DemodulatorThread(DemodulatorThreadInputQueue* pQueue, DemodulatorThreadParameters *params_in) : DemodulatorThread::DemodulatorThread(DemodulatorThreadInputQueue* pQueue, DemodulatorThreadParameters *params_in) :
m_pQueue(pQueue), visOutQueue(NULL) { inputQueue(pQueue), visOutQueue(NULL) {
DemodulatorThreadParameters defaultParams; DemodulatorThreadParameters defaultParams;
if (!params_in) { if (!params_in) {
@ -57,7 +57,7 @@ void DemodulatorThread::threadMain() {
while (1) { while (1) {
DemodulatorThreadIQData inp; DemodulatorThreadIQData inp;
m_pQueue->pop(inp); inputQueue->pop(inp);
std::vector<signed char> *data = &inp.data; std::vector<signed char> *data = &inp.data;
if (data->size()) { if (data->size()) {
@ -139,22 +139,6 @@ void DemodulatorThread::threadMain() {
if (visOutQueue != NULL) { if (visOutQueue != NULL) {
visOutQueue->push(ati); visOutQueue->push(ati);
} }
/*if (!TestDestroy()) {
DemodulatorThreadAudioData *audioOut = new DemodulatorThreadAudioData(task.data->frequency, params.audioSampleRate, newBuffer);
m_pQueue->sendAudioData(DemodulatorThreadTask::DEMOD_THREAD_AUDIO_DATA, audioOut);
if (params.audioInputQueue != NULL) {
AudioThreadInput ati;
ati.data = newBuffer;
params.audioInputQueue->push(ati);
// AudioThreadTask audio_task = AudioThreadTask(AudioThreadTask::AUDIO_THREAD_DATA);
// audio_task.data = new AudioThreadData(task.data->frequency, params.audioSampleRate, newBuffer);
// params.audioQueue->addTask(audio_task, AudioThreadQueue::AUDIO_PRIORITY_HIGHEST);
}
}*/
} }
} }
} }

View File

@ -102,7 +102,7 @@ public:
} }
protected: protected:
DemodulatorThreadInputQueue* m_pQueue; DemodulatorThreadInputQueue* inputQueue;
DemodulatorThreadOutputQueue* visOutQueue; DemodulatorThreadOutputQueue* visOutQueue;
firfilt_crcf fir_filter; firfilt_crcf fir_filter;

View File

@ -15,42 +15,36 @@
#include <cstdint> #include <cstdint>
#include <condition_variable> #include <condition_variable>
/** A thread-safe asynchronous queue */ /** A thread-safe asynchronous queue */
template <class T, class Container = std::list<T>> template<class T, class Container = std::list<T>>
class ThreadQueue class ThreadQueue {
{
typedef typename Container::value_type value_type; typedef typename Container::value_type value_type;
typedef typename Container::size_type size_type; typedef typename Container::size_type size_type;
typedef Container container_type; typedef Container container_type;
public: public:
/*! Create safe queue. */ /*! Create safe queue. */
ThreadQueue() = default; ThreadQueue() = default;
ThreadQueue (ThreadQueue&& sq) ThreadQueue(ThreadQueue&& sq) {
{ m_queue = std::move(sq.m_queue);
m_queue = std::move (sq.m_queue);
} }
ThreadQueue (const ThreadQueue& sq) ThreadQueue(const ThreadQueue& sq) {
{ std::lock_guard < std::mutex > lock(sq.m_mutex);
std::lock_guard<std::mutex> lock (sq.m_mutex);
m_queue = sq.m_queue; m_queue = sq.m_queue;
} }
/*! Destroy safe queue. */ /*! Destroy safe queue. */
~ThreadQueue() ~ThreadQueue() {
{ std::lock_guard < std::mutex > lock(m_mutex);
std::lock_guard<std::mutex> lock (m_mutex);
} }
/** /**
* Sets the maximum number of items in the queue. Defaults is 0: No limit * Sets the maximum number of items in the queue. Defaults is 0: No limit
* \param[in] item An item. * \param[in] item An item.
*/ */
void set_max_num_items (unsigned int max_num_items) void set_max_num_items(unsigned int max_num_items) {
{
m_max_num_items = max_num_items; m_max_num_items = max_num_items;
} }
@ -59,14 +53,13 @@ class ThreadQueue
* \param[in] item An item. * \param[in] item An item.
* \return true if an item was pushed into the queue * \return true if an item was pushed into the queue
*/ */
bool push (const value_type& item) bool push(const value_type& item) {
{ std::lock_guard < std::mutex > lock(m_mutex);
std::lock_guard<std::mutex> lock (m_mutex);
if (m_max_num_items > 0 && m_queue.size() > m_max_num_items) if (m_max_num_items > 0 && m_queue.size() > m_max_num_items)
return false; return false;
m_queue.push (item); m_queue.push(item);
m_condition.notify_one(); m_condition.notify_one();
return true; return true;
} }
@ -76,14 +69,13 @@ class ThreadQueue
* \param[in] item An item. * \param[in] item An item.
* \return true if an item was pushed into the queue * \return true if an item was pushed into the queue
*/ */
bool push (const value_type&& item) bool push(const value_type&& item) {
{ std::lock_guard < std::mutex > lock(m_mutex);
std::lock_guard<std::mutex> lock (m_mutex);
if (m_max_num_items > 0 && m_queue.size() > m_max_num_items) if (m_max_num_items > 0 && m_queue.size() > m_max_num_items)
return false; return false;
m_queue.push (item); m_queue.push(item);
m_condition.notify_one(); m_condition.notify_one();
return true; return true;
} }
@ -92,10 +84,9 @@ class ThreadQueue
* Pops item from the queue. If queue is empty, this function blocks until item becomes available. * Pops item from the queue. If queue is empty, this function blocks until item becomes available.
* \param[out] item The item. * \param[out] item The item.
*/ */
void pop (value_type& item) void pop(value_type& item) {
{ std::unique_lock < std::mutex > lock(m_mutex);
std::unique_lock<std::mutex> lock (m_mutex); m_condition.wait(lock, [this]() // Lambda funct
m_condition.wait (lock, [this]() // Lambda funct
{ {
return !m_queue.empty(); return !m_queue.empty();
}); });
@ -109,14 +100,13 @@ class ThreadQueue
* If queue is empty, this function blocks until item becomes available. * If queue is empty, this function blocks until item becomes available.
* \param[out] item The item. * \param[out] item The item.
*/ */
void move_pop (value_type& item) void move_pop(value_type& item) {
{ std::unique_lock < std::mutex > lock(m_mutex);
std::unique_lock<std::mutex> lock (m_mutex); m_condition.wait(lock, [this]() // Lambda funct
m_condition.wait (lock, [this]() // Lambda funct
{ {
return !m_queue.empty(); return !m_queue.empty();
}); });
item = std::move (m_queue.front()); item = std::move(m_queue.front());
m_queue.pop(); m_queue.pop();
} }
@ -125,9 +115,8 @@ class ThreadQueue
* \param[out] item The item. * \param[out] item The item.
* \return False is returned if no item is available. * \return False is returned if no item is available.
*/ */
bool try_pop (value_type& item) bool try_pop(value_type& item) {
{ std::unique_lock < std::mutex > lock(m_mutex);
std::unique_lock<std::mutex> lock (m_mutex);
if (m_queue.empty()) if (m_queue.empty())
return false; return false;
@ -143,14 +132,13 @@ class ThreadQueue
* \param[out] item The item. * \param[out] item The item.
* \return False is returned if no item is available. * \return False is returned if no item is available.
*/ */
bool try_move_pop (value_type& item) bool try_move_pop(value_type& item) {
{ std::unique_lock < std::mutex > lock(m_mutex);
std::unique_lock<std::mutex> lock (m_mutex);
if (m_queue.empty()) if (m_queue.empty())
return false; return false;
item = std::move (m_queue.front()); item = std::move(m_queue.front());
m_queue.pop(); m_queue.pop();
return true; return true;
} }
@ -161,16 +149,14 @@ class ThreadQueue
* \param[in] timeout The number of microseconds to wait. * \param[in] timeout The number of microseconds to wait.
* \return true if get an item from the queue, false if no item is received before the timeout. * \return true if get an item from the queue, false if no item is received before the timeout.
*/ */
bool timeout_pop (value_type& item, std::uint64_t timeout) bool timeout_pop(value_type& item, std::uint64_t timeout) {
{ std::unique_lock < std::mutex > lock(m_mutex);
std::unique_lock<std::mutex> lock (m_mutex);
if (m_queue.empty()) if (m_queue.empty()) {
{
if (timeout == 0) if (timeout == 0)
return false; return false;
if (m_condition.wait_for (lock, std::chrono::microseconds (timeout)) == std::cv_status::timeout) if (m_condition.wait_for(lock, std::chrono::microseconds(timeout)) == std::cv_status::timeout)
return false; return false;
} }
@ -187,20 +173,18 @@ class ThreadQueue
* \param[in] timeout The number of microseconds to wait. * \param[in] timeout The number of microseconds to wait.
* \return true if get an item from the queue, false if no item is received before the timeout. * \return true if get an item from the queue, false if no item is received before the timeout.
*/ */
bool timeout_move_pop (value_type& item, std::uint64_t timeout) bool timeout_move_pop(value_type& item, std::uint64_t timeout) {
{ std::unique_lock < std::mutex > lock(m_mutex);
std::unique_lock<std::mutex> lock (m_mutex);
if (m_queue.empty()) if (m_queue.empty()) {
{
if (timeout == 0) if (timeout == 0)
return false; return false;
if (m_condition.wait_for (lock, std::chrono::microseconds (timeout)) == std::cv_status::timeout) if (m_condition.wait_for(lock, std::chrono::microseconds(timeout)) == std::cv_status::timeout)
return false; return false;
} }
item = std::move (m_queue.front()); item = std::move(m_queue.front());
m_queue.pop(); m_queue.pop();
return true; return true;
} }
@ -209,9 +193,8 @@ class ThreadQueue
* Gets the number of items in the queue. * Gets the number of items in the queue.
* \return Number of items in the queue. * \return Number of items in the queue.
*/ */
size_type size() const size_type size() const {
{ std::lock_guard < std::mutex > lock(m_mutex);
std::lock_guard<std::mutex> lock (m_mutex);
return m_queue.size(); return m_queue.size();
} }
@ -219,9 +202,8 @@ class ThreadQueue
* Check if the queue is empty. * Check if the queue is empty.
* \return true if queue is empty. * \return true if queue is empty.
*/ */
bool empty() const bool empty() const {
{ std::lock_guard < std::mutex > lock(m_mutex);
std::lock_guard<std::mutex> lock (m_mutex);
return m_queue.empty(); return m_queue.empty();
} }
@ -229,13 +211,11 @@ class ThreadQueue
* Swaps the contents. * Swaps the contents.
* \param[out] sq The ThreadQueue to swap with 'this'. * \param[out] sq The ThreadQueue to swap with 'this'.
*/ */
void swap (ThreadQueue& sq) void swap(ThreadQueue& sq) {
{ if (this != &sq) {
if (this != &sq) std::lock_guard < std::mutex > lock1(m_mutex);
{ std::lock_guard < std::mutex > lock2(sq.m_mutex);
std::lock_guard<std::mutex> lock1 (m_mutex); m_queue.swap(sq.m_queue);
std::lock_guard<std::mutex> lock2 (sq.m_mutex);
m_queue.swap (sq.m_queue);
if (!m_queue.empty()) if (!m_queue.empty())
m_condition.notify_all(); m_condition.notify_all();
@ -246,14 +226,12 @@ class ThreadQueue
} }
/*! The copy assignment operator */ /*! The copy assignment operator */
ThreadQueue& operator= (const ThreadQueue& sq) ThreadQueue& operator=(const ThreadQueue& sq) {
{ if (this != &sq) {
if (this != &sq) std::lock_guard < std::mutex > lock1(m_mutex);
{ std::lock_guard < std::mutex > lock2(sq.m_mutex);
std::lock_guard<std::mutex> lock1 (m_mutex); std::queue<T, Container> temp { sq.m_queue };
std::lock_guard<std::mutex> lock2 (sq.m_mutex); m_queue.swap(temp);
std::queue<T, Container> temp {sq.m_queue};
m_queue.swap (temp);
if (!m_queue.empty()) if (!m_queue.empty())
m_condition.notify_all(); m_condition.notify_all();
@ -263,18 +241,17 @@ class ThreadQueue
} }
/*! The move assignment operator */ /*! The move assignment operator */
ThreadQueue& operator= (ThreadQueue && sq) ThreadQueue& operator=(ThreadQueue && sq) {
{ std::lock_guard < std::mutex > lock(m_mutex);
std::lock_guard<std::mutex> lock (m_mutex); m_queue = std::move(sq.m_queue);
m_queue = std::move (sq.m_queue);
if (!m_queue.empty()) m_condition.notify_all(); if (!m_queue.empty())
m_condition.notify_all();
return *this; return *this;
} }
private:
private:
std::queue<T, Container> m_queue; std::queue<T, Container> m_queue;
mutable std::mutex m_mutex; mutable std::mutex m_mutex;
@ -283,8 +260,7 @@ class ThreadQueue
}; };
/*! Swaps the contents of two ThreadQueue objects. */ /*! Swaps the contents of two ThreadQueue objects. */
template <class T, class Container> template<class T, class Container>
void swap (ThreadQueue<T, Container>& q1, ThreadQueue<T, Container>& q2) void swap(ThreadQueue<T, Container>& q1, ThreadQueue<T, Container>& q2) {
{ q1.swap(q2);
q1.swap (q2);
} }

View File

@ -1,4 +1,3 @@
#ifndef TIMER_H #ifndef TIMER_H
#define TIMER_H #define TIMER_H
@ -13,8 +12,7 @@
* Class provides high resolution timing and useful time control functions * Class provides high resolution timing and useful time control functions
*/ */
class Timer class Timer {
{
private: private:
unsigned long time_elapsed; unsigned long time_elapsed;
@ -31,7 +29,6 @@ private:
struct timezone time_zone; struct timezone time_zone;
#endif #endif
bool paused_state; bool paused_state;
bool lock_state; bool lock_state;
float lock_rate; float lock_rate;
@ -53,35 +50,30 @@ public:
*/ */
void stop(void); void stop(void);
/// Locks the timer to a specified framerate (for recording / benchmarking purposes typically) /// Locks the timer to a specified framerate (for recording / benchmarking purposes typically)
/** /**
* Locks the timer to a specified framerate (for recording / benchmarking purposes typically) * Locks the timer to a specified framerate (for recording / benchmarking purposes typically)
*/ */
void lockFramerate(float f_rate); void lockFramerate(float f_rate);
/// Unlock any framerate lock that's been applied /// Unlock any framerate lock that's been applied
/** /**
* Unlock any framerate lock that's been applied * Unlock any framerate lock that's been applied
*/ */
void unlock(); void unlock();
/// Check locked state /// Check locked state
/** /**
* Check locked state * Check locked state
*/ */
bool locked(); bool locked();
/// Reset the timer counter /// Reset the timer counter
/** /**
* Resetting the timer will reset the current time to 0 * Resetting the timer will reset the current time to 0
*/ */
void reset(void); void reset(void);
/// Timer update /// Timer update
/** /**
* Calling the update command will bring the timer value up to date, this is meant * Calling the update command will bring the timer value up to date, this is meant
@ -89,7 +81,6 @@ public:
*/ */
void update(void); void update(void);
/// Get the total time elapsed since the timer start, not counting paused time /// Get the total time elapsed since the timer start, not counting paused time
/** /**
* Returns the total time elapsed in since the timer start() to the last update() but * Returns the total time elapsed in since the timer start() to the last update() but
@ -103,7 +94,6 @@ public:
*/ */
double getSeconds(void); double getSeconds(void);
/// Get the total time elapsed since the timer start /// Get the total time elapsed since the timer start
/** /**
* Returns the total time elapsed in since the timer start() to the last update() * Returns the total time elapsed in since the timer start() to the last update()
@ -117,7 +107,6 @@ public:
*/ */
double totalSeconds(void); double totalSeconds(void);
/// Set the amount of time elapsed /// Set the amount of time elapsed
/** /**
* Force the timer duration to a specific value, useful for rolling forward or back in a system * Force the timer duration to a specific value, useful for rolling forward or back in a system
@ -131,7 +120,6 @@ public:
*/ */
void setSeconds(double seconds_in); void setSeconds(double seconds_in);
/// Get the amount of times the update() command has been called /// Get the amount of times the update() command has been called
/** /**
* By using the number of times the update() command has been called you can easily determine * By using the number of times the update() command has been called you can easily determine
@ -140,7 +128,6 @@ public:
*/ */
unsigned long getNumUpdates(void); unsigned long getNumUpdates(void);
/// Get the timer duration during the last update /// Get the timer duration during the last update
/** /**
* Useful for determining the amount of time which elapsed during the last update * Useful for determining the amount of time which elapsed during the last update
@ -154,7 +141,6 @@ public:
*/ */
double lastUpdateSeconds(void); double lastUpdateSeconds(void);
/// Set the timer pause state /// Set the timer pause state
/** /**
* Pause the timer, allowing for continued update() calls without an increment in timing but * Pause the timer, allowing for continued update() calls without an increment in timing but
@ -171,6 +157,5 @@ public:
bool paused(); bool paused();
}; };
#endif #endif