File: filetxdlg.cpp

package info (click to toggle)
cutesdr 1.20-4
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,848 kB
  • sloc: cpp: 18,902; makefile: 21; sh: 5
file content (416 lines) | stat: -rw-r--r-- 11,065 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "filetxdlg.h"
#include "ui_filetxdlg.h"
#include <QFileDialog>
#include  "interface/wavefilewriter.h"

CFileTxDlg::CFileTxDlg(QWidget *parent, CSdrInterface* pSdrInterface) :
	QDialog(parent),
	m_pSdrInterface(pSdrInterface),
	ui(new Ui::CFileTxDlg)
{
	ui->setupUi(this);
	ui->frameTxFreqCtrl->Setup(10, 100U, 1700000000U, 1, UNITS_MHZ );
	ui->frameTxFreqCtrl->SetBkColor(Qt::black);
	ui->frameTxFreqCtrl->SetDigitColor(Qt::yellow);
	ui->frameTxFreqCtrl->SetUnitsColor(Qt::lightGray);
	ui->frameTxFreqCtrl->SetHighlightColor(Qt::darkGray);
	m_TxFilePath = "";
	m_TxRepeat = false;
	m_FileTotalSamples = 14400;
	m_FileSamplesSent = 0;
}

CFileTxDlg::~CFileTxDlg()
{
	delete ui;
}

void CFileTxDlg::Init()
{
	m_pTimer = new QTimer(this);
	connect(m_pTimer, SIGNAL(timeout()), this, SLOT(OnTimer()));
	connect(m_pSdrInterface, SIGNAL(NewTxMsg(int)), this, SLOT(NewTxMsgSlot(int)));
	connect(ui->frameTxFreqCtrl, SIGNAL(NewFrequency(qint64)), this, SLOT(OnNewCenterFrequency(qint64)));
	ui->lineEdit->setText(m_TxFilePath);
	ui->frameTxFreqCtrl->SetFrequency(m_TxFrequency);
	ui->checkBoxRepeat->setChecked(m_TxRepeat);
	ui->checkBoxUseFile->setChecked(m_UseTxFile);
	ui->spinBoxAmp->setValue((int)m_TxSignalPower);
	ui->spinBoxNoise->setValue((int)m_TxNoisePower);
	ui->spinBoxStart->setValue(m_TxSweepStartFrequency);
	ui->spinBoxStop->setValue(m_TxSweepStopFrequency);
	ui->spinBoxSweep->setValue(m_TxSweepRate);

	ui->progressBar->setValue(0);
	m_FileReader.open(m_TxFilePath);
	ui->labelFileInfo->setText(m_FileReader.m_FileInfoStr);
	m_FileReader.close();
	m_TransmitOn = false;
	m_TxSignalPower = 0.0;
	m_TxNoisePower = -160.0;
}

void CFileTxDlg::done(int r)	//virtual override
{
	SetTxState(false);
	QDialog::done(r);	//call base class
}

/////////////////////////////////////////////////////////////////////
// Handle change event for center frequency control
/////////////////////////////////////////////////////////////////////
void CFileTxDlg::OnNewCenterFrequency(qint64 freq)
{
	m_TxFrequency = freq;
	SetTxFreq(m_TxFrequency);
}

void CFileTxDlg::on_pushButtonFileSelect_clicked()
{
	QString str = QFileDialog::getOpenFileName(this,tr("Select .wav File Base Name"),m_TxFilePath,tr("wav files (*.wav)"));
	if(str != "")
	{
		m_TxFilePath = str;
		ui->lineEdit->setText(m_TxFilePath);
		m_FileReader.open(m_TxFilePath);
		ui->labelFileInfo->setText(m_FileReader.m_FileInfoStr);
		m_FileReader.close();
	}
}

void CFileTxDlg::on_checkBoxRepeat_clicked(bool checked)
{
	m_TxRepeat = checked;
}

void CFileTxDlg::on_checkBoxUseFile_clicked(bool checked)
{
	m_UseTxFile = checked;
}

void CFileTxDlg::on_spinBoxAmp_valueChanged(int pwr)
{
	m_TxSignalPower = (TYPEREAL)pwr;
	m_DataModifier.SetSignalPower(m_TxSignalPower);
}

void CFileTxDlg::on_spinBoxNoise_valueChanged(int pwr)
{
	m_TxNoisePower = (TYPEREAL)pwr;
	m_DataModifier.SetNoisePower(m_TxNoisePower);
}

void CFileTxDlg::on_spinBoxStart_valueChanged(int start)
{
	m_TxSweepStartFrequency = start;
	m_DataModifier.SetSweepStart(m_TxSweepStartFrequency);
}

void CFileTxDlg::on_spinBoxStop_valueChanged(int stop)
{
	m_TxSweepStopFrequency = stop;
	m_DataModifier.SetSweepStop(m_TxSweepStopFrequency);
}

void CFileTxDlg::on_spinBoxSweep_valueChanged(int rate)
{
	m_TxSweepRate = rate;
	m_DataModifier.SetSweepRate(m_TxSweepRate);
}


void CFileTxDlg::on_pushButtonStart_clicked()
{

	SetTxFreq(m_TxFrequency);
	m_DataModifier.Init(64000);
	m_DataModifier.SetSweepRate(m_TxSweepRate);
	m_DataModifier.SetSweepStart(m_TxSweepStartFrequency);
	m_DataModifier.SetSweepStop(m_TxSweepStopFrequency);
	m_DataModifier.SetSignalPower(m_TxSignalPower);
	m_DataModifier.SetNoisePower(m_TxNoisePower);
	if(m_UseTxFile)
	{
		if(m_FileReader.open(m_TxFilePath) )
		{
			m_FileTotalSamples = m_FileReader.GetNumberSamples();
			m_FileSamplesSent = 0;
			ui->progressBar->setValue(0);
			m_pTimer->start(200);		//start up status timer
		}
		else
		{
			ui->labelFileInfo->setText("File Open Failed");
			return;
		}
	}
	else
	{
	}
	SetTxState(true);
	m_TransmitOn = true;
}

void CFileTxDlg::on_pushButtonStop_clicked()
{
	m_pTimer->stop();		//stop status timer
	m_TransmitOn = false;
	SetTxState(false);
	if(m_FileReader.isOpen())
		m_FileReader.close();
}

/////////////////////////////////////////////////////////////////////
// Status Timer event handler
/////////////////////////////////////////////////////////////////////
void CFileTxDlg::OnTimer()
{
	if(m_FileTotalSamples>0)
		ui->progressBar->setValue((100*m_FileSamplesSent)/m_FileTotalSamples);
}

void CFileTxDlg::NewTxMsgSlot(int FifoPtr)
{
	quint8 chan;
	tBtoS tmp16;
	if( NULL==m_pSdrInterface)
		return;
	CAscpRxMsg* pMsg = &(m_pSdrInterface->m_TxAscpMsg[FifoPtr]);
	pMsg->InitRxMsg();

	if( pMsg->GetType() == TYPE_TARG_RESP_CITEM )
	{	// Is a message from SDR in response to a request
		switch(pMsg->GetCItem())
		{
			case CI_TX_STATE:
				chan = pMsg->GetParm8();
				qDebug()<<"Tx State is = "<<pMsg->GetParm8();
				break;
			case CI_TX_FREQUENCY:	//set transmit center frequency
				chan = pMsg->GetParm8();
				qDebug()<<"Tx Frequency is = "<<pMsg->GetParm32();
				break;
		}
	}
	else if( pMsg->GetType() == TYPE_TARG_DATA_ITEM0 )
	{
	}
	else if( pMsg->GetType() == TYPE_TARG_DATA_ITEM1 )
	{
	}
	else if( pMsg->GetType() == TYPE_TARG_DATA_ITEM2 )
	{
	}
	else if( pMsg->GetType() == TYPE_TARG_DATA_ITEM3 )
	{
	}
	else if(pMsg->GetType() == TYPE_DATA_ITEM_ACK)
	{	//is TX fifo status message from SDR
		if(!m_TransmitOn)
			return;
		tmp16.bytes.b0 = pMsg->Buf8[3];
		tmp16.bytes.b1 = pMsg->Buf8[4];
		int BytesAvail = tmp16.all;
//		qDebug()<<"Tx FIFO = "<<BytesAvail;
		if(m_UseTxFile)
		{
			while( BytesAvail >= (240*6) )
			{
				int samplesread = m_FileReader.GetNextDataBlock( m_TxDataBuf, 240 );
				if( samplesread > 0 )
				{
					m_FileSamplesSent += samplesread;
					m_DataModifier.ProcessBlock(m_TxDataBuf, 240);
					int sent = SendIQDataBlk( m_TxDataBuf, 240 );
					BytesAvail -= sent;
				}
				else if( samplesread < 0 )
				{
					qDebug()<<"File read error";
					m_TransmitOn = false;
					SetTxState(false);
					m_FileReader.close();
					break;
				}
				else
				{
					if(m_TxRepeat)
					{
						m_FileSamplesSent = 0;
						m_FileReader.ResetToBeginning();
					}
					else
					{
						m_TransmitOn = false;
						SetTxState(false);
						m_FileReader.close();
						qDebug()<<"File operation complete";
						break;
					}
				}
			}
		}
		else
		{
			while( BytesAvail >= (240*6) )
			{
				GenerateTestData(m_TestBuf, 240);
				int sent = SendIQDataBlk( m_TestBuf, 240);
				BytesAvail -= sent;
			}
		}
//		qDebug()<<"BA "<<BytesAvail;
	}
}

void CFileTxDlg::GenerateTestData(tICpx32* pBuf, int NumSamples)
{
	for(int i=0; i<NumSamples; i++)
	{
		m_TxDataBuf[i].re = 0.707;
		m_TxDataBuf[i].im = 0.707;
	}
	m_DataModifier.ProcessBlock(m_TxDataBuf, NumSamples);

	for(int i=0; i<NumSamples; i++)
	{
			pBuf[i].re = (qint32)(m_TxDataBuf[i].re * 2.147483648e9);
			pBuf[i].im = (qint32)(m_TxDataBuf[i].im * 2.147483648e9);
	}
}

///////////////////////////////////////////////////////////////////////////////
// Sends TX Center frequency command to SDR
///////////////////////////////////////////////////////////////////////////////
void CFileTxDlg::SetTxFreq(quint32 freq)
{
CAscpTxMsg TxMsg;
	TxMsg.InitTxMsg(TYPE_HOST_SET_CITEM);
	TxMsg.AddCItem(CI_TX_FREQUENCY);
	TxMsg.AddParm8(0);
	TxMsg.AddParm32( freq );
	TxMsg.AddParm8(0);		//5th byte of freq
	if(m_pSdrInterface)
		m_pSdrInterface->SendUdpMsg(TxMsg.Buf8,TxMsg.GetLength() );
}

/////////////////////////////////////////////////////////////////////
// Called to start the Tx
/////////////////////////////////////////////////////////////////////
void CFileTxDlg::SetTxState(bool On)
{
CAscpTxMsg TxMsg;
	m_SeqNumber = 0;
	TxMsg.InitTxMsg(TYPE_HOST_SET_CITEM);
	TxMsg.AddCItem(CI_TX_STATE);
	TxMsg.AddParm8(0);
	if(On)
		TxMsg.AddParm8(TX_STATE_ON);
	else
		TxMsg.AddParm8(TX_STATE_OFF);
	if(m_pSdrInterface)
		m_pSdrInterface->SendUdpMsg(TxMsg.Buf8,TxMsg.GetLength() );
}

int CFileTxDlg::SendIQDataBlk(tICpx32* pData, int NumSamples)
{
	tASCPDataMsg TxMsg;
	int len=0;
	for(int j=0; j<NumSamples; j++)
	{
		TxMsg.fld.DataBuf[len++] = (pData[j].re>>24) & 0xFF;	//I2
		TxMsg.fld.DataBuf[len++] = (pData[j].re>>16) & 0xFF;	//I1
		TxMsg.fld.DataBuf[len++] = (pData[j].re>>8) & 0xFF;		//I0

		TxMsg.fld.DataBuf[len++] = (pData[j].im>>24) & 0xFF;	//Q2
		TxMsg.fld.DataBuf[len++] = (pData[j].im>>16) & 0xFF;	//Q1
		TxMsg.fld.DataBuf[len++] = (pData[j].im>>8) & 0xFF;		//Q0
	}
	TxMsg.fld.Hdr = TYPE_TARG_DATA_ITEM0<<8;
	TxMsg.fld.Hdr += (len + 4);
	TxMsg.fld.Sequence = m_SeqNumber++;
	if(m_pSdrInterface)
		m_pSdrInterface->SendUdpMsg(TxMsg.Buf8, len+4);
	return len;
}

int CFileTxDlg::SendIQDataBlk(TYPECPX* pData, int NumSamples)
{
	tASCPDataMsg TxMsg;
	int len=0;
	for(int j=0; j<NumSamples; j++)
	{
		qint32 tmp;
		tmp = (qint32)(pData[j].re * 2.147483648e9);
		TxMsg.fld.DataBuf[len++] = (tmp>>24) & 0xFF;	//I2
		TxMsg.fld.DataBuf[len++] = (tmp>>16) & 0xFF;	//I1
		TxMsg.fld.DataBuf[len++] = (tmp>>8) & 0xFF;		//I0

		tmp = (qint32)(pData[j].im * 2.147483648e9);
		TxMsg.fld.DataBuf[len++] = (tmp>>24) & 0xFF;	//Q2
		TxMsg.fld.DataBuf[len++] = (tmp>>16) & 0xFF;	//Q1
		TxMsg.fld.DataBuf[len++] = (tmp>>8) & 0xFF;		//Q0
	}
	TxMsg.fld.Hdr = TYPE_TARG_DATA_ITEM0<<8;
	TxMsg.fld.Hdr += (len + 4);
	TxMsg.fld.Sequence = m_SeqNumber++;
	if(m_pSdrInterface)
		m_pSdrInterface->SendUdpMsg(TxMsg.Buf8, len+4);
	return len;
}


#if 0
CWaveFileWriter FileWriter;
CDataModifier DataModifier;
if(m_FileReader.open(m_TxFilePath) )
{
	if( !FileWriter.open( "d:\\testwr.wav",true, m_FileReader.GetSampleRate(), true, 0) )
	{
		m_FileReader.close();
		qDebug()<<"File write open error";
		SetTxState(false);
		return;
	}
	int sampleswritten = 0;
	int samplesread = 0;
	DataModifier.Init(m_FileReader.GetSampleRate());
	DataModifier.SetSweepRate(1.0);
	DataModifier.SetSweepStart(-100.0);
	DataModifier.SetSweepStop(100.0);
	while(sampleswritten < m_FileReader.GetNumberSamples())
	{
		//copy in blocks of 512 samples
		samplesread = m_FileReader.GetNextDataBlock( m_TxDataBuf, 512);
		if( samplesread > 0 )
		{
			DataModifier.ProcessBlock(m_TxDataBuf, samplesread);
			if( !FileWriter.Write(m_TxDataBuf, samplesread) )
			{
				m_FileReader.close();
				FileWriter.close();
				qDebug()<<"File copy error";
				m_FileReader.close();
				FileWriter.close();
				return;
			}
			sampleswritten += samplesread;
		}
		else
		{
			if(samplesread < 0 )
				qDebug()<<"File read error";
			else
				qDebug()<<"File operation complete";
			break;
		}
	}
	m_FileReader.close();
	FileWriter.close();
}
else
{
	qDebug()<<"File read open Fail";
}
#endif