SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
rtlsdrthread.cpp
Go to the documentation of this file.
1 // Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
3 // written by Christian Daniel //
4 // //
5 // This program is free software; you can redistribute it and/or modify //
6 // it under the terms of the GNU General Public License as published by //
7 // the Free Software Foundation as version 3 of the License, or //
8 // (at your option) any later version. //
9 // //
10 // This program is distributed in the hope that it will be useful, //
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
13 // GNU General Public License V3 for more details. //
14 // //
15 // You should have received a copy of the GNU General Public License //
16 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
18 
19 #include <stdio.h>
20 #include <errno.h>
21 #include "rtlsdrthread.h"
22 
23 #include "dsp/samplesinkfifo.h"
24 
25 #define FCD_BLOCKSIZE 16384
26 
27 RTLSDRThread::RTLSDRThread(rtlsdr_dev_t* dev, SampleSinkFifo* sampleFifo, QObject* parent) :
28  QThread(parent),
29  m_running(false),
30  m_dev(dev),
31  m_convertBuffer(FCD_BLOCKSIZE),
32  m_sampleFifo(sampleFifo),
33  m_samplerate(288000),
34  m_log2Decim(4),
35  m_fcPos(0)
36 {
37 }
38 
40 {
41  stopWork();
42 }
43 
45 {
46  m_startWaitMutex.lock();
47  start();
48  while(!m_running)
49  m_startWaiter.wait(&m_startWaitMutex, 100);
50  m_startWaitMutex.unlock();
51 }
52 
54 {
55  m_running = false;
56  wait();
57 }
58 
59 void RTLSDRThread::setSamplerate(int samplerate)
60 {
61  m_samplerate = samplerate;
62 }
63 
64 void RTLSDRThread::setLog2Decimation(unsigned int log2_decim)
65 {
66  m_log2Decim = log2_decim;
67 }
68 
69 void RTLSDRThread::setFcPos(int fcPos)
70 {
71  m_fcPos = fcPos;
72 }
73 
75 {
76  int res;
77 
78  m_running = true;
79  m_startWaiter.wakeAll();
80 
81  while(m_running) {
82  if((res = rtlsdr_read_async(m_dev, &RTLSDRThread::callbackHelper, this, 32, FCD_BLOCKSIZE)) < 0) {
83  qCritical("RTLSDRThread: async error: %s", strerror(errno));
84  break;
85  }
86  }
87 
88  m_running = false;
89 }
90 
91 // Decimate according to specified log2 (ex: log2=4 => decim=16)
92 void RTLSDRThread::callback(const quint8* buf, qint32 len)
93 {
94  SampleVector::iterator it = m_convertBuffer.begin();
95 
96  if (m_log2Decim == 0)
97  {
98  m_decimators.decimate1(&it, buf, len);
99  }
100  else
101  {
102  if (m_fcPos == 0) // Infradyne
103  {
104  switch (m_log2Decim)
105  {
106  case 1:
107  m_decimators.decimate2_inf(&it, buf, len);
108  break;
109  case 2:
110  m_decimators.decimate4_inf(&it, buf, len);
111  break;
112  case 3:
113  m_decimators.decimate8_inf(&it, buf, len);
114  break;
115  case 4:
116  m_decimators.decimate16_inf(&it, buf, len);
117  break;
118  case 5:
119  m_decimators.decimate32_inf(&it, buf, len);
120  break;
121  case 6:
122  m_decimators.decimate64_inf(&it, buf, len);
123  break;
124  default:
125  break;
126  }
127  }
128  else if (m_fcPos == 1) // Supradyne
129  {
130  switch (m_log2Decim)
131  {
132  case 1:
133  m_decimators.decimate2_sup(&it, buf, len);
134  break;
135  case 2:
136  m_decimators.decimate4_sup(&it, buf, len);
137  break;
138  case 3:
139  m_decimators.decimate8_sup(&it, buf, len);
140  break;
141  case 4:
142  m_decimators.decimate16_sup(&it, buf, len);
143  break;
144  case 5:
145  m_decimators.decimate32_sup(&it, buf, len);
146  break;
147  case 6:
148  m_decimators.decimate64_sup(&it, buf, len);
149  break;
150  default:
151  break;
152  }
153  }
154  else // Centered
155  {
156  switch (m_log2Decim)
157  {
158  case 1:
159  m_decimators.decimate2_cen(&it, buf, len);
160  break;
161  case 2:
162  m_decimators.decimate4_cen(&it, buf, len);
163  break;
164  case 3:
165  m_decimators.decimate8_cen(&it, buf, len);
166  break;
167  case 4:
168  m_decimators.decimate16_cen(&it, buf, len);
169  break;
170  case 5:
171  m_decimators.decimate32_cen(&it, buf, len);
172  break;
173  case 6:
174  m_decimators.decimate64_cen(&it, buf, len);
175  break;
176  default:
177  break;
178  }
179  }
180  }
181 
182  m_sampleFifo->write(m_convertBuffer.begin(), it);
183 
184  if(!m_running)
185  rtlsdr_cancel_async(m_dev);
186 }
187 
188 void RTLSDRThread::callbackHelper(unsigned char* buf, uint32_t len, void* ctx)
189 {
190  RTLSDRThread* thread = (RTLSDRThread*)ctx;
191  thread->callback(buf, len);
192 }
193 
void decimate4_sup(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:341
void setSamplerate(int samplerate)
void decimate2_sup(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:263
void decimate8_inf(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:391
void decimate16_inf(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:543
uint write(const quint8 *data, uint count)
void startWork()
void decimate16_cen(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:2538
void decimate2_inf(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:235
static void callbackHelper(unsigned char *buf, uint32_t len, void *ctx)
void decimate8_cen(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:2478
void decimate4_cen(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:2435
void decimate4_inf(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:291
#define FCD_BLOCKSIZE
void setFcPos(int fcPos)
unsigned int uint32_t
Definition: rtptypes_win.h:46
void decimate32_inf(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:823
void setLog2Decimation(unsigned int log2_decim)
RTLSDRThread(rtlsdr_dev_t *dev, SampleSinkFifo *sampleFifo, QObject *parent=NULL)
void decimate32_cen(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:2647
void decimate32_sup(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:1091
QWaitCondition m_startWaiter
Definition: rtlsdrthread.h:45
void decimate64_inf(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:1359
void decimate64_cen(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:2853
void decimate16_sup(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:683
rtlsdr_dev_t * m_dev
Definition: rtlsdrthread.h:48
unsigned int m_log2Decim
Definition: rtlsdrthread.h:53
void decimate1(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:220
DecimatorsU< qint32, quint8, SDR_RX_SAMP_SZ, 8, 127 > m_decimators
Definition: rtlsdrthread.h:56
QMutex m_startWaitMutex
Definition: rtlsdrthread.h:44
SampleSinkFifo * m_sampleFifo
Definition: rtlsdrthread.h:50
void decimate2_cen(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:2407
SampleVector m_convertBuffer
Definition: rtlsdrthread.h:49
void decimate64_sup(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:1883
void decimate8_sup(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimatorsu.h:467
void callback(const quint8 *buf, qint32 len)