File: txfunctions.cpp

package info (click to toggle)
qsstv 9.5.8-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,928 kB
  • sloc: cpp: 47,579; makefile: 4
file content (618 lines) | stat: -rw-r--r-- 14,387 bytes parent folder | download | duplicates (3)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/***************************************************************************
 *   Copyright (C) 2000-2014 by Johan Maes                                 *
 *   on4qz@telenet.be                                                      *
 *   http://users.telenet.be/on4qz                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "txfunctions.h"
#include "appglobal.h"
#include "sstv/sstvtx.h"
#include "cw.h"


#include "configparams.h"
#include <qmutex.h>
#include "synthes.h"
#include "modes/modes.h"
#include "dispatcher.h"
#include "cw.h"
#include <QApplication>
#include <assert.h>
#include "txwidget.h"
#include "soundbase.h"
#include "waterfalltext.h"
#include "hybridcrypt.h"
#include "rigcontrol.h"
#include "drm.h"
#include "drmtx.h"
#include "fixform.h"
#include "mainwindow.h"



QMutex txMutex;

int templateIndex;
bool useTemplate;
bool useCW;
bool useVOX;

const QString txStateStr[txFunctions::TXTEST+1]=
{
  "IDLE",
  "ACTIVE",
  "SENDTONE",
  "SENDWFID",
  "SENDCWID",
  "SENDDRM",
  "SENDDRMPIC",
  "SENDDRMBINARY",
  "SENDDRMBSR",
  "SENDDRMFIX",
  "SENDDRMTXT",
  "SSTVIMAGE",
  "SSTVPOST",
  "RESTART",
  "PREPARESSTV",
  "PREPAREDRMPIC",
  "PREPAREDRMBINARY",
  "TXTEST"
};


txFunctions::txFunctions(QObject *parent) : QThread(parent)
{

  txState=TXIDLE;
  started=false;
  sstvTxPtr=new sstvTx;
  drmTxPtr=new drmTx;
}

txFunctions::~txFunctions()
{
  delete synthesPtr;
  delete sstvTxPtr;
  delete drmTxPtr;
}



void txFunctions::init()
{
  synthesPtr=new synthesizer(txClock);
  switchTxState(TXIDLE);
  addToLog("txFunc: Init",LOGTXFUNC);
  drmTxPtr->init();

}

void txFunctions::run()
{
  double waterfallTime;
  QString startWFTxt,endWFTxt;


  abort=false;
  init();
  while(!abort)
    {

      started=true;
      switch (txState)
        {
        case TXIDLE:
          msleep(10);
          break;
        case TXACTIVE:
          msleep(1);
          break;
        case TXSENDTONE:
          waitTxOn();
          addToLog("txFunc: entered TXSENDTONE",LOGTXFUNC);
          startProgress(toneDuration);
          synthesPtr->sendTone(toneDuration,toneLowerFrequency,toneUpperFrequency,false);
          addToLog("txFunc: TXSENDTONE waiting for end",LOGTXFUNC);
          waitEnd();
          switchTxState(TXIDLE);
          break;

        case TXPREPAREDRMBINARY:
          {
            bool ok=true;
            if (useHybrid)
              ok=drmTxPtr->ftpDRMHybrid(binaryFilename, drmTxPtr->getTxFileName(binaryFilename));
            switchTxState(TXIDLE);
            prepareTXComplete(ok);
          }
          break;
        case TXPREPAREDRMPIC:
          {
            bool ok=true;
            if (useHybrid)
              ok=drmTxPtr->ftpDRMHybrid("", drmTxPtr->getTxFileName(""));
            switchTxState(TXIDLE);
            prepareTXComplete(ok);
          }
          break;

        case TXSENDDRMPIC:
          if(repeaterEnabled)
            {
              startWFTxt = startRepeaterWF;
              endWFTxt   = endRepeaterWF;
            }
          else
            {
              startWFTxt = startPicWF;
              endWFTxt   = endPicWF;
            }

          if (drmTxPtr->initDRMImage(false,""))
            {
              drmTxPtr->updateTxList();
              switchTxState(TXSENDDRM);
            }
          else
            switchTxState(TXIDLE);
          break;

        case TXSENDDRMBINARY:
          startWFTxt = startBinWF;
          endWFTxt   = endBinWF;
          if (drmTxPtr->initDRMImage(true,binaryFilename))
            {
              drmTxPtr->updateTxList();
              switchTxState(TXSENDDRM);
            }
          else
            switchTxState(TXIDLE);
          break;

        case TXSENDDRMBSR:
          startWFTxt = bsrWF;
          endWFTxt   = "";
          switchTxState(TXSENDDRM);
          break;

        case TXSENDDRMFIX:
          startWFTxt = fixWF;
          endWFTxt   = "";
          switchTxState(TXSENDDRM);
          break;

        case TXSENDDRM:
          waitTxOn();
          waterfallTime=waterfallPtr->getDuration(endWFTxt);
          waterfallTime+=waterfallPtr->getDuration(startWFTxt);


          startProgress(drmTxPtr->calcTxTime(waterfallTime));
          addToLog("start of wf",LOGTXFUNC);
          if(useVOX) synthesPtr->sendTone(1.,1700.,0,false);
          waterfallPtr->setText(startWFTxt);
          synthesPtr->sendWFText();
          addToLog("start of txDrm",LOGTXFUNC);
          drmTxPtr->start();

          addToLog("end of txDrm",LOGTXFUNC);
          if(txState==TXSENDDRM) // abort if txState is idle
            {
              waterfallPtr->setText(endWFTxt);
              synthesPtr->sendWFText();
              addToLog("end of wf",LOGTXFUNC);
              addToLog("txFunc: TXSENDDRM waiting for end",LOGTXFUNC);
              waitEnd();
            }
          switchTxState(TXIDLE);
          break;

        case TXSENDDRMTXT:
          waitTxOn();
          break;

        case TXSENDWFID:
          addToLog("Entered TXSENDID",LOGTXFUNC);
          waitTxOn();
          addToLog("after txon TXSENDID",LOGTXFUNC);
          startProgress(waterfallPtr->getDuration());
          if(useVOX) synthesPtr->sendTone(1.,1700.,0,false);
          synthesPtr->sendWFText();
          addToLog("TXSENDID waiting for end",LOGTXFUNC);
          waitEnd();
          addToLog("TXSENDID  end",LOGTXFUNC);
          switchTxState(TXIDLE);
          break;

        case TXSENDCWID:
          waitTxOn();
          if(useVOX) synthesPtr->sendTone(1.,1700.,0,false);
          sendCW();
          waitEnd();
          switchTxState(TXIDLE);
          break;

        case TXPREPARESSTV:
          switchTxState(TXIDLE);
          prepareTXComplete(true);
          break;

        case TXSSTVIMAGE:
          waitTxOn();
          addToLog("Entered SSTVTXIMAGE",LOGTXFUNC);
          startProgress(sstvTxPtr->calcTxTime(0));
          addToLog("Entered before SSTVTXIMAGE",LOGTXFUNC);
          if(sstvTxPtr->sendImage(txWidgetPtr->getImageViewerPtr()))
            {
              switchTxState(TXSSTVPOST);
            }
          else
            {
              switchTxState(TXIDLE);
            }
          break;
        case TXSSTVPOST:
          addToLog("Entered TXSSTVPOST ",LOGTXFUNC);
          if (useCW)
            {
              sendCW();
            }
          else
            {
              sendFSKID();
            }
          waitEnd();
          switchTxState(TXRESTART);
          break;
        case TXRESTART:
          switchTxState(TXIDLE);
          break;
        case TXTEST:
          sendTestPattern();
          break;

        }
    }
  started=false;
  addToLog("txFunc stopped",LOGTXFUNC);
  abort=false;
  txState=TXIDLE;
}

int txFunctions::calcTxTime(bool binary, int overhead)
{
  bool ok=false;
  int txTime=0;
  if(transmissionModeIndex==TRXSSTV)
    {
      txTime=sstvTxPtr->calcTxTime(0);
    }
  else
    {
  if (binary)
    {
      ok=drmTxPtr->initDRMImage(true, binaryFilename);
    }
  else
    {
      ok=drmTxPtr->initDRMImage(false, "");
    }
  if (ok) txTime = drmTxPtr->calcTxTime(overhead);
  addToLog(QString("ok=%1, time=%2").arg(ok).arg(txTime), LOGTXFUNC);
}
  return txTime;



}

void txFunctions::setDRMTxParams(drmTxParams params)
{
  drmTxPtr->setTxParams(params);
}


void txFunctions::startProgress(double duration)
{
  progressTXEvent *ce;
  ce=new progressTXEvent(duration+rigControllerPtr->getTxDelay()+SILENCEDELAY);
  QApplication::postEvent( dispatcherPtr, ce );  // Qt will delete it when done
}

void txFunctions::stopThread()
{
  abort=true;
  if(!isRunning()) return;
  while(abort && isRunning())
    {
      qApp->processEvents();
    }
}

bool txFunctions::txBusy()
{
  if(isRunning() && txState!=TXIDLE)
    {
      return true;
    }
  return false;
}

void txFunctions::startTX(etxState state)
{
  switchTxState(state);
}

void txFunctions::prepareTXComplete(bool ok)
{
  txPrepareCompleteEvent *ce;
  ce=new txPrepareCompleteEvent(ok);
  QApplication::postEvent( dispatcherPtr, ce );  // Qt will delete it when done
}

void txFunctions::prepareTX(etxState state)
{
  if (txState != TXIDLE)
    {
      addToLog("txState is not TXIDLE",LOGTXFUNC);
      return;
    }

  switch (state)
    {
    case TXPREPARESSTV:
    case TXSSTVIMAGE:
      switchTxState(TXPREPARESSTV);
      break;

    case TXPREPAREDRMPIC:
    case TXSENDDRMPIC:
      switchTxState(TXPREPAREDRMPIC);
      break;

    case TXPREPAREDRMBINARY:
    case TXSENDDRMBINARY:
      switchTxState(TXPREPAREDRMBINARY);
      break;

    default:
      addToLog(QString("Invalid prepareTX state:%1").arg(state),LOGTXFUNC);
    }
}

void txFunctions::waitTxOn()
{
  double txDelay;
  txDelay=rigControllerPtr->getTxDelay();
  if(txDelay!=0)
    {
      synthesPtr->sendSilence(txDelay);
    }
}

void txFunctions::waitEnd()
{
  synthesPtr->sendTone(SILENCEDELAY,00,0,true); // send silence
  addToLog("waitEnd() posting endTXImage",LOGTXFUNC);
  endImageTXEvent *ce=new endImageTXEvent;
  QApplication::postEvent(dispatcherPtr, ce );  // Qt will delete it when done
}

void txFunctions::stopAndWait()
{
  stopDRM=true;
  if(transmissionModeIndex==TRXSSTV)
    {
      sstvTxPtr->abort();
      switchTxState(TXRESTART);
      while(txState!=TXIDLE)
        {
          qApp->processEvents();
        }

    }
  addToLog("txFunc: stop initiated",LOGTXFUNC);
  switchTxState(TXIDLE);
  while(txState!=TXIDLE)
    {
      qApp->processEvents();
    }
  if(soundIOPtr) soundIOPtr->idleTX();
  addToLog("txFunc: stop executed",LOGTXFUNC);
}


void txFunctions::sendTestPattern()
{
  syncBurst();
}


/** send a burst of syncs */
void txFunctions::syncBurst()
{
  // send 5msec 1200Hz followed by 50 msec silence
  //	synthesPtr->sendTone(0.005,1200.);
  for (int i=0;i<50;i++)
    {
      synthesPtr->sendTone(0.005,1200,0,true);
      synthesPtr->sendTone(0.020,1700,0,true);
    }
  synthesPtr->sendSilence(0.100);;
}


void txFunctions::sendCW()
{
  addToLog("txFunc:sendCW",LOGTXFUNC);
  float tone;
  float duration;
  initCW(cwText);
  synthesPtr->sendSilence(0.5);
  while(sendTextCW(tone,duration))
    {
      synthesPtr->sendTone(duration,tone,0,true);
    }
}


void txFunctions:: sendFSKChar(int IDChar)
{
  int TxBit;

  for (int i=0;i<6;i++)
    {
      TxBit = IDChar & 0x01;
      IDChar = IDChar >> 1;
      if (TxBit == 0x01)
        synthesPtr->sendTone(0.022,1900.,0,true);
      else
        synthesPtr->sendTone(0.022,2100.,0,true);
    }
}

// sendFSKID by DL3YAP






void txFunctions:: sendFSKID()
{
  int idx;
  int l;
  int IDChar;
  int Checksum;


  if (myCallsign.isEmpty()) return;
  // addToLog("txFunc:sendFSKID",LOGFSKID);

  l=myCallsign.size();
  idx=0;

  // synthesPtr->sendTone(2.0,00,0,false);
  synthesPtr->sendTone(0.3,1500.,0,false);
  synthesPtr->sendTone(0.1,2100.,0,true);
  synthesPtr->sendTone(0.022,1900.,0,true);

  IDChar = 0x2A;
  sendFSKChar(IDChar);

  QChar IDText=	QChar(myCallsign[idx]);
  IDChar=int(IDText.toLatin1());

  IDChar = (IDChar - 0x20);
  Checksum = IDChar;

  while (idx < l)
    {
      sendFSKChar(IDChar);
      idx++;
      QChar IDText=QChar(myCallsign[idx].toUpper());
      IDChar=int(IDText.toLatin1());
      IDChar = (IDChar - 0x20);

      if (idx < l)
        {
          Checksum = Checksum ^ IDChar;
        }
    }

  IDChar = 0x01;

  sendFSKChar(IDChar);

  IDChar = Checksum & 0x3F ;

  sendFSKChar(IDChar);
  synthesPtr->sendTone(0.1,1900.,0,true);
}

void txFunctions::sendBSR(QByteArray *p,drmTxParams dp)
{
  if(p==NULL) return;
  drmTxPtr->sendBSR(p,dp);
}


//void txFunctions::sendHybrid(QString fn)
//{
//  drmTxPtr->sendHybrid(fn);
//}

bool txFunctions::prepareFIX(QByteArray bsrByteArray)
{
  int trID;
  displayMBoxEvent *stce;
  txSession *sessionPtr;
  fixForm fx(mainWindowPtr);
  trID=drmTxPtr->processFIX(bsrByteArray);
  if(trID<0)  return false;
  sessionPtr=drmTxPtr->getSessionPtr((uint)trID);
  if (sessionPtr==NULL)
    {
      stce= new displayMBoxEvent("BSR Received","This BSR is not for you");
      QApplication::postEvent( dispatcherPtr, stce );  // Qt will delete it when done
      return false;
    }
  else
    {
      // take it from the transmitlist
      fx.setInfoInternal(paramsToMode(sessionPtr->drmParams),sessionPtr->filename,fixBlockList.count(),&sessionPtr->ba);
      if(fx.exec()==QDialog::Rejected) return false;
      drmTxPtr->initDRMFIX(sessionPtr);
    }
  return true;
}

bool txFunctions::prepareBinary(QString fileName)
{
  binaryFilename=fileName;
  return true;
}


void txFunctions::applyTemplate(imageViewer *ivPtr,QString templateFilename)
{
  if(transmissionModeIndex==TRXSSTV)
    {
      sstvTxPtr->applyTemplate(templateFilename,useTemplate,ivPtr);
    }
  else
    {
      drmTxPtr->applyTemplate(templateFilename,useTemplate,ivPtr);
    }
}

void txFunctions::forgetTxFileName()
{
  drmTxPtr->forgetTxFileName();
}

void txFunctions::switchTxState(etxState newState)
{
  addToLog(QString("%1 to %2").arg(txStateStr[txState]).arg(txStateStr[newState]),LOGTXFUNC);
  txState=newState;
}

void txFunctions::txTestPattern(imageViewer *ivPtr,etpSelect sel)
{
  sstvTxPtr->createTestPattern(ivPtr,sel);
}