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.
plutosdroutputthread.cpp
Go to the documentation of this file.
1 // Copyright (C) 2017 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
19 #include "plutosdroutputsettings.h"
20 #include "iio.h"
21 #include "plutosdroutputthread.h"
22 
23 PlutoSDROutputThread::PlutoSDROutputThread(uint32_t blocksizeSamples, DevicePlutoSDRBox* plutoBox, SampleSourceFifo* sampleFifo, QObject* parent) :
24  QThread(parent),
25  m_running(false),
26  m_plutoBox(plutoBox),
27  m_blockSizeSamples(blocksizeSamples),
28  m_sampleFifo(sampleFifo),
29  m_log2Interp(0)
30 {
31  m_buf = new qint16[blocksizeSamples*2];
32 // m_bufConv = new qint16[blocksizeSamples*(sizeof(Sample)/sizeof(qint16))];
33 }
34 
36 {
37  stopWork();
38  delete[] m_buf;
39 }
40 
42 {
43  if (m_running) return; // return if running already
44 
45  m_startWaitMutex.lock();
46  start();
47  while(!m_running)
48  m_startWaiter.wait(&m_startWaitMutex, 100);
49  m_startWaitMutex.unlock();
50 }
51 
53 {
54  if (!m_running) return; // return if not running
55 
56  m_running = false;
57  wait();
58 }
59 
60 void PlutoSDROutputThread::setLog2Interpolation(unsigned int log2_interp)
61 {
62  m_log2Interp = log2_interp;
63 }
64 
66 {
67  std::ptrdiff_t p_inc = m_plutoBox->txBufferStep();
68 
69  qDebug("PlutoSDROutputThread::run: txBufferStep: %ld bytes", p_inc);
70  qDebug("PlutoSDROutputThread::run: Rx sample size is %ld bytes", m_plutoBox->getRxSampleSize());
71  qDebug("PlutoSDROutputThread::run: Tx sample size is %ld bytes", m_plutoBox->getTxSampleSize());
72  qDebug("PlutoSDROutputThread::run: nominal nbytes_tx is %d bytes", m_blockSizeSamples*4);
73 
74  m_running = true;
75  m_startWaiter.wakeAll();
76 
77  while (m_running)
78  {
79  ssize_t nbytes_tx;
80  char *p_dat, *p_end;
81  int ihs; // half sample index (I then Q to make a sample)
82 
83  convert(m_buf, 2*m_blockSizeSamples); // size given in number of int16_t (I and Q interleaved)
84 
85  // WRITE: Get pointers to TX buf and write IQ to TX buf port 0
86  p_end = m_plutoBox->txBufferEnd();
87  ihs = 0;
88 
89  // p_inc is 2 on a char* buffer therefore each iteration processes only the I or Q sample
90  // I and Q samples are processed one after the other
91  // conversion is not needed as samples are little endian
92 
93  for (p_dat = m_plutoBox->txBufferFirst(), ihs = 0; p_dat < p_end; p_dat += p_inc, ihs += 2)
94  {
95  m_plutoBox->txChannelConvert((int16_t*) p_dat, &m_buf[ihs]);
96  }
97 
98  // Schedule TX buffer for sending
99  nbytes_tx = m_plutoBox->txBufferPush();
100 
101  if (nbytes_tx != 4*m_blockSizeSamples)
102  {
103  qDebug("PlutoSDROutputThread::run: error pushing buf %d / %d", (int) nbytes_tx, (int) 4*m_blockSizeSamples);
104  usleep(200000);
105  continue;
106  }
107  }
108 
109  m_running = false;
110 }
111 
112 // Decimate according to specified log2 (ex: log2=4 => decim=16)
113 // len is in half samples (I or Q) thus the size up to which buf is filled
114 // SampleVector contains full (I, Q) samples
115 void PlutoSDROutputThread::convert(qint16* buf, qint32 len)
116 {
117  // pull samples from baseband generator
118  SampleVector::iterator beginRead;
119  m_sampleFifo->readAdvance(beginRead, len/(2*(1<<m_log2Interp)));
120  beginRead -= len/2;
121 
122  if (m_log2Interp == 0)
123  {
124  m_interpolators.interpolate1(&beginRead, buf, len);
125  }
126  else
127  {
128  switch (m_log2Interp)
129  {
130  case 1:
131  m_interpolators.interpolate2_cen(&beginRead, buf, len);
132  break;
133  case 2:
134  m_interpolators.interpolate4_cen(&beginRead, buf, len);
135  break;
136  case 3:
137  m_interpolators.interpolate8_cen(&beginRead, buf, len);
138  break;
139  case 4:
140  m_interpolators.interpolate16_cen(&beginRead, buf, len);
141  break;
142  case 5:
143  m_interpolators.interpolate32_cen(&beginRead, buf, len);
144  break;
145  case 6:
146  m_interpolators.interpolate64_cen(&beginRead, buf, len);
147  break;
148  default:
149  break;
150  }
151  }
152 }
153 
short int16_t
Definition: rtptypes_win.h:43
DevicePlutoSDRBox * m_plutoBox
int16_t * m_buf
holds I+Q values of each sample from devce
PlutoSDROutputThread(uint32_t blocksize, DevicePlutoSDRBox *plutoBox, SampleSourceFifo *sampleFifo, QObject *parent=0)
uint32_t m_blockSizeSamples
buffer sizes in number of (I,Q) samples
std::ptrdiff_t txBufferStep()
void txChannelConvert(int16_t *dst, int16_t *src)
void interpolate64_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void interpolate32_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
unsigned int uint32_t
Definition: rtptypes_win.h:46
void interpolate8_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void interpolate2_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void readAdvance(SampleVector::iterator &readUntil, unsigned int nbSamples)
SampleSourceFifo * m_sampleFifo
DSP sample FIFO (I,Q)
Interpolators< qint16, SDR_TX_SAMP_SZ, 16 > m_interpolators
Pluto is on 12 bit but iio_channel_convert_inverse converts from 16 to 12 bits.
void interpolate1(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void interpolate16_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void interpolate4_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void convert(qint16 *buf, qint32 len)
QWaitCondition m_startWaiter
void setLog2Interpolation(unsigned int log2_interp)