-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradio.cpp
More file actions
639 lines (585 loc) · 21.3 KB
/
Copy pathradio.cpp
File metadata and controls
639 lines (585 loc) · 21.3 KB
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#
/*
* Copyright (C) 2026
* Jan van Katwijk (J.vanKatwijk@gmail.com)
* Lazy Chair Computing
*
* This file is part of the wspr module
*
* wspr module 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.
*
* wspr module 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 wspr module; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <QSettings>
#include <QString>
#include <QDebug>
#include <QDateTime>
#include <QObject>
#include <QDir>
#include <QColor>
#include <QMessageBox>
#include <QFileDialog>
#include <QHeaderView>
#include <QDesktopServices>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "identity-dialog.h"
#include "constants.h"
#include "radio.h"
#include "psk-reporter.h"
#include "message-handler.h"
#include "decoder.h"
#include "popup-keypad.h"
#define browserAddress "https://pskreporter.info/pskmap.html"
//
static
std::vector<pskMessage> messageStack;
struct {
QString key;
int freq;
} freqTable [] = {
{"LF" , 136000}, {"jan", 144100000}, {"MF", 474200},
{"160m", 1836600}, {"80m", 3568600}, {"60m", 5287200},
{"40m", 7038600}, {"30m", 10138700}, {"20m", 14095600},
{"17m", 18104600}, {"15m", 21094600}, {"12m", 24924600},
{"10m", 28124600}, {"6m", 50293000}, {"4m", 70091000},
{"2m", 144489000}, {"70cm", 432300000},
{"", -1}
};
RadioInterface::RadioInterface (QSettings *sI,
const QString &presetFile,
QWidget *parent):
QMainWindow (parent),
inputData (1024 * 1024),
my_bandPlan (":res/sw-bandplan.xml"),
thePresets (this, presetFile),
decimator_0 (17, -1000, 1000,
48000, 4),
decimator_1 (11, -1000, 1000,
12000, 4),
decimator_2 (9, -150, 150,
3000, 8),
passBuffer (16 * 32768) {
struct timeval lTime;
this -> settings = sI;
setupUi (this);
settings -> beginGroup ("wsprSettings");
theFrequency = settings -> value ("freq", 10136000). toInt ();
settings -> endGroup ();
frequencyDisplay -> display (theFrequency);
fprintf (stderr, "theFrequency %d\n", theFrequency);
tableWidget = new QTableWidget (0, 8);
tableWidget -> setColumnWidth (0, 160); // date
tableWidget -> setColumnWidth (1, 150); // call
tableWidget -> setColumnWidth (2, 100); // loc
tableWidget -> setColumnWidth (3, 40); // power
tableWidget -> setColumnWidth (4, 40); // snr
tableWidget -> setColumnWidth (5, 40); // dt
tableWidget -> setColumnWidth (6, 100); // freq
tableWidget -> setColumnWidth (7, 40); // drift
QHeaderView * headerView = tableWidget -> horizontalHeader ();
headerView -> setSectionResizeMode (1, QHeaderView::Stretch);
tableWidget -> setHorizontalHeaderLabels (
QStringList () << tr ("date") << tr ("call") << tr ("loc") <<
tr ("power") << tr ("snr") << tr ("dt") <<
tr ("freq") << tr ("drift"));
displayArea -> setWidget (tableWidget);
savingSamples. store (false);
statusLabel -> setStyleSheet ("QLabel {background-color:green}");
statusLabel -> setText ("waiting");
sampleCount = 0;
theReporter = nullptr;
filePointer. store (nullptr);
changeRequest. state = false;
changeRequest. freqChange = false;
connect (presetButton, &QPushButton::clicked,
this, &RadioInterface::handle_presetButton);
connect (freqButton, &QPushButton::clicked,
this, &RadioInterface::handle_freqButton);
connect (freqSave, &QPushButton::clicked,
this, &RadioInterface::set_freqSave);
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
connect (subtractionSelector, &QCheckBox::checkStateChanged,
#else
connect (subtractionSelector, &QCheckBox::stateChanged,
#endif
this, &RadioInterface::handle_subtractionSelector);
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
connect (quickModeSelector, &QCheckBox::checkStateChanged,
#else
connect (quickModeSelector, &QCheckBox::stateChanged,
#endif
this, &RadioInterface::handle_quickModeSelector);
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
connect (hashtableSelector, &QCheckBox::checkStateChanged,
#else
connect (hashtableSelector, &QCheckBox::stateChanged,
#endif
this, &RadioInterface::handle_hashtableSelector);
connect (report_Button, &QPushButton::clicked,
this, &RadioInterface::handle_report_Button);
connect (identity_Button, &QPushButton::clicked,
this, &RadioInterface::handle_identity_Button);
connect (npasses_Selector, &QSpinBox::valueChanged,
this, &RadioInterface::handle_npasses_Selector);
connect (dumpButton, &QPushButton::clicked,
this, &RadioInterface::handle_dumpButton);
myDecoder = new decoder (this, &passBuffer);
settings -> beginGroup ("wsprSettings");
QString homeCall =
settings -> value ("homeCall", "your call"). toString ();
homecall_label -> setText (homeCall);
settings -> endGroup ();
mykeyPad = new keyPad (this);
displayTimer. setInterval (1000);
connect (&displayTimer, &QTimer::timeout,
this, &RadioInterface::updateTime);
displayTimer. start (1000);
//
// Initialize decoder and device settings
decOptions. usehashtable = false;
decOptions. npasses = 2;
npasses_Selector -> setValue (2);
decOptions. subtraction = true;
decOptions. quickmode = false;
decOptions. report = false;
//
show_pskStatus (false);
inputHandler = nullptr;
connect (&secondsTimer, &QTimer::timeout,
this, &RadioInterface::twoMinutes);
int theFreq = 10360000;
decOptions. freq = theFreq;
decOptions. dialfreq = theFreq;
settings -> beginGroup ("wsprSettings");
QString serverAddress
= settings -> value ("serverAddress", "127.0.0.1"). toString ();
settings -> endGroup ();
hostNameLabel -> setInputMask ("000.000.000.000");
hostNameLabel -> setText ("serverAddress");
connect (hostNameLabel, &QLineEdit::returnPressed,
this, &RadioInterface::handle_hostName);
}
// The end of all
RadioInterface::~RadioInterface () {
}
// handle hostname is called whenever the user acknowledges the
// hostname, no guarantee tht there is a connection
void RadioInterface::handle_hostName () {
QString hostName = hostNameLabel -> text ();
int portNumber = portLabel -> value ();
if (inputHandler != nullptr)
return;
inputHandler = new messageHandler (hostName, portNumber,
48000, theFrequency,
&inputData);
settings -> beginGroup ("wsprSettings");
settings -> setValue ("serverAddress", hostName);
settings -> endGroup ();
fprintf (stderr, "we have an input handler\n");
connect (inputHandler, &messageHandler::connection_success,
this, &RadioInterface::handle_connect);
connect (inputHandler, &messageHandler::connection_failed,
this, &RadioInterface::handle_disconnect);
}
//
// at this point, the underlying handler has set everything
// ready for processing
// the function is called 10 times a second, i.e.
// with data segments of 960
void RadioInterface::handle_connect () {
disconnect (inputHandler, &messageHandler::connection_success,
this, &RadioInterface::handle_connect);
connect (inputHandler, &messageHandler::dataAvailable,
this, &RadioInterface::sampleHandler);
connect (inputHandler, &messageHandler::signalPower,
this, &RadioInterface::newSNR);
connect (inputHandler, &messageHandler::connection_failed,
this, &RadioInterface::reset);
connectionLabel -> setText ("connected");
connect (inputHandler, &messageHandler::frequency_changed,
this, &RadioInterface::displayFrequency);
}
//
// disconnect is called when - at start up - no connection
// could be made
void RadioInterface::handle_disconnect () {
disconnect (inputHandler, &messageHandler::connection_success,
this, &RadioInterface::handle_connect);
delete inputHandler;
inputHandler = nullptr;
QMessageBox::warning (this, tr ("Warning"),
tr ("Connection lost"));
}
//
// reset is called when - during processing - the connection fails
// for whatever reason, so we completely reset
void RadioInterface::reset () {
delete inputHandler;
inputHandler = nullptr;
hostNameLabel -> setInputMask ("000.000.000.000");
hostNameLabel -> setText ("127.0.0.1");
connect (hostNameLabel, &QLineEdit::returnPressed,
this, &RadioInterface::handle_hostName);
}
//
//
void RadioInterface::updateTime () {
QDateTime currentTime = QDateTime::currentDateTime ();
static int secondsCounter = 0;
if (savingSamples. load ()) {
secondsCounter ++;
if (changeRequest. freqChange)
statusLabel -> setText ("reading " + QString::number (secondsCounter) + " (change pending)");
else
statusLabel -> setText ("reading " + QString::number (secondsCounter));
if (secondsCounter > 120) // something went wrong
savingSamples. store (false);
}
else
secondsCounter = 0;
timeDisplay -> setText (currentTime.
toString (QString ("dd.MM.yy:hh:mm:ss")));
}
#include <QCloseEvent>
void RadioInterface::closeEvent (QCloseEvent *event) {
QMessageBox::StandardButton resultButton =
QMessageBox::question (this, "wspr module",
tr("Are you sure?\n"),
QMessageBox::No | QMessageBox::Yes,
QMessageBox::Yes);
if (resultButton != QMessageBox::Yes) {
event -> ignore();
} else {
secondsTimer. stop ();
thePresets. hide ();
if (inputHandler != nullptr)
delete inputHandler;
delete myDecoder;
mykeyPad -> hidePad ();
delete mykeyPad;
if (filePointer. load () != nullptr)
fclose (filePointer. load ());
hide ();
event -> accept ();
}
}
void RadioInterface::set_band (const QString &s) {
bool b;
int freq = s. toInt (&b);
if (!b)
return;
settings -> beginGroup ("wsprSettings");
settings -> setValue ("selected band", s);
settings -> endGroup ();
set_band (freq);
}
void RadioInterface::set_band (int freq) {
changeRequest. freqChange = true;
changeRequest. freqValue = freq;
}
void RadioInterface::honor_freqRequest () {
if (changeRequest. freqChange) {
decOptions. dialfreq = changeRequest. freqValue;
decOptions. freq = changeRequest. freqValue;
setFrequency (decOptions. dialfreq + 1500);
frequencyDisplay -> display (inputHandler -> getFrequency ());
QString theBand =
my_bandPlan. getFrequencyLabel (decOptions. dialfreq);
bandLabel -> setText (theBand);
changeRequest. freqChange = false;;
}
}
//
// The message handler handles decimation - regardless
// of what the SDRconnect rate is set to - to a decent 48000 samples/sec
// and passes slices of 960 samples on.
// delay will be somewhere around 10 msec
void RadioInterface::sampleHandler (int n) {
if (!savingSamples. load ()) {
struct timeval lTime;
gettimeofday (&lTime, nullptr);
int32_t sec = lTime. tv_sec % 120;
uint32_t msec = (sec * 1000000 + lTime. tv_usec) / 1000;
if (sec != 0) {
statusLabel -> setText ("waiting " + QString::number(120 - sec));
}
// if (changeRequest. freqChange)
// fprintf (stderr, "msec = %d\n", msec);
if (msec > 1500) { // this is 1.5 second, should be enough
honor_freqRequest(); // if any
}
if (msec <= 50) {
statusLabel -> setText ("end of waiting ");
inputData. FlushRingBuffer ();
passBuffer. FlushRingBuffer ();
savingSamples. store (true);
sampleCount = 0;
}
}
n = inputData. GetRingBufferReadAvailable ();
for (int i = 0; i < n; i ++) {
Complex sample;
inputData. getDataFromBuffer (&sample, 1);
if (decimator_0. Pass (sample, &sample) &&
decimator_1. Pass (sample, &sample)) {
if (decimator_2. Pass (sample * 10.0f, &sample)) {
if (savingSamples. load ()) {
passBuffer. putDataIntoBuffer (&sample, 1);
sampleCount ++;
if (sampleCount >= SIGNAL_LENGTH * SIGNAL_SAMPLE_RATE) {
sampleCount = 0;
myDecoder -> goDecoding (decOptions);
savingSamples. store (false);
}
}
}
}
}
}
void RadioInterface::handle_presetButton () {
if (thePresets. isVisible ())
thePresets. hide ();
else
thePresets. show ();
}
void RadioInterface::handle_preset (const QString &freqText) {
set_band (freqText);
}
void RadioInterface::handle_userFreq (int32_t freq) {
set_band (freq);
}
void RadioInterface::handle_freqButton () {
if (mykeyPad -> isVisible ())
mykeyPad -> hidePad ();
else
mykeyPad -> showPad ();
}
void RadioInterface::set_freqSave () {
int frequency = inputHandler -> getFrequency ();
thePresets. addElement (frequency);
}
void RadioInterface::handle_subtractionSelector (int v) {
(void)v;
decOptions. subtraction = subtractionSelector -> isChecked ();
}
void RadioInterface::handle_quickModeSelector (int v) {
(void)v;
decOptions. quickmode = quickModeSelector -> isChecked ();
}
void RadioInterface::handle_hashtableSelector (int v) {
(void)v;
if (!savingSamples. load ()) {
hashtableSelector -> setChecked (false);
return;
}
decOptions. usehashtable = hashtableSelector -> isChecked ();
}
void RadioInterface::handle_report_Button () {
decOptions. report = !decOptions. report;
if (decOptions. report) {
try {
printLock. lock ();
theReporter = new pskReporter (this, settings);
printLock. unlock ();
show_pskStatus (true);
} catch (...) {
printLock. unlock ();
show_pskStatus (false);
decOptions. report = false;
}
}
else {
printLock. lock ();
delete theReporter;
show_pskStatus (false);
theReporter = nullptr;
printLock. unlock ();
}
if (decOptions. report) {
if (!QDesktopServices::openUrl(QUrl (browserAddress))) {
QMessageBox::warning (this, tr ("Warning"),
tr ("cannot show server"));
return;
}
}
}
void RadioInterface::handle_identity_Button () {
identityDialog Identity (settings);
Identity. QDialog::exec ();
settings -> beginGroup ("wsprSettings");
QString homeCall =
settings -> value ("homeCall", ""). toString ();
homecall_label -> setText (homeCall);
settings -> endGroup ();
}
void RadioInterface::handle_npasses_Selector (int n) {
decOptions. npasses = n;
}
void RadioInterface::show_status (const QString &s) {
// pskLabel -> setText (s);
}
void RadioInterface::sendMessage (const QString &call,
const QString &grid,
int freq,
int snr,
int32_t seconds) {
pskMessage m;
if (theReporter == nullptr) {
messageStack. resize (0);
return;
}
m. call = call;
m. grid = grid;
m. freq = freq;
m. snr = snr;
m. seconds = seconds;
messageStack. push_back (m);
}
void RadioInterface::transmitMessages () {
if (theReporter == nullptr)
return;
fprintf (stderr, "x");
printLock. lock ();
theReporter -> sendMessages (messageStack);
printLock. unlock ();
}
void RadioInterface::handle_dumpButton () {
if (filePointer. load () != nullptr) {
fclose (filePointer. load ());
filePointer. store (nullptr);
dumpButton -> setText ("dump");
return;
}
QString currentTime = QDateTime::currentDateTime (). toString ();
QString homeDir = QDir::homePath ();
QString suggestedName =
homeDir + "/-wspr-" + currentTime + ".csv";
suggestedName = QDir::toNativeSeparators (suggestedName);
QString file =
QFileDialog::getSaveFileName (this,
tr ("Save file ..."),
suggestedName,
tr ("csv (*.csv)"),
Q_NULLPTR,
QFileDialog::DontUseNativeDialog);
if (file == QString (""))
return;
file = QDir::toNativeSeparators (file);
if (!file.endsWith (".csv", Qt::CaseInsensitive))
file.append (".csv");
filePointer. store (fopen (file. toUtf8 (). data (), "w"));
if (filePointer. load () == nullptr) {
qDebug () << "Cannot open " << file. toUtf8 (). data ();
return;
}
QString topLine = "; snr; dr; freq; drift; call; loc; power;" ;
fprintf (filePointer. load (), "\n%s ; %s\n",
topLine. toLatin1 (). data (),
currentTime. toLatin1 (). data ());
dumpButton -> setText ("WRITING");
}
void RadioInterface::sendString (const QString &s) {
if (filePointer. load () != nullptr)
fprintf (filePointer. load (), "%s\n", s. toLatin1 (). data ());
}
void RadioInterface::twoMinutes () {
}
void RadioInterface::displayFrequency (int frequency) {
frequencyDisplay -> display (frequency);
}
void RadioInterface::newSNR (double d) {
(void)d;
}
// setFrequency is called from the frequency panel,
// from inside to change VFO and offset,
// and from the decoder
void RadioInterface::setFrequency (int32_t frequency) {
theFrequency = frequency;
settings -> beginGroup ("wsprSettings");
settings -> setValue ("freq", frequency);
settings -> endGroup ();
inputHandler -> setFrequency (frequency);
displayFrequency (frequency);
}
void RadioInterface::show_pskStatus (bool b) {
if (b) {
psk_statusLabel -> setStyleSheet ("QLabel {background-color : green}");
psk_statusLabel -> setText ("sending to pskServer");
}
else {
psk_statusLabel -> setStyleSheet ("QLabel {background-color : red}");
psk_statusLabel -> setText ("");
}
}
void RadioInterface::toOutput (bool valid,
const QString & theTime,
const QString & call,
const QString & loc,
const QString & power,
float snr,
float dt,
double freq,
int drift) {
int row = tableWidget -> rowCount ();
tableWidget -> insertRow (row);
QTableWidgetItem *item0 = new QTableWidgetItem;
item0 -> setTextAlignment (Qt::AlignLeft);
tableWidget -> setItem (row, 0, item0);
QTableWidgetItem *item1 = new QTableWidgetItem;
item1 -> setTextAlignment (Qt::AlignLeft);
tableWidget -> setItem (row, 1, item1);
QTableWidgetItem *item2 = new QTableWidgetItem;
item2 -> setTextAlignment (Qt::AlignLeft);
tableWidget -> setItem (row, 2, item2);
QTableWidgetItem *item3 = new QTableWidgetItem;
item3 -> setTextAlignment (Qt::AlignLeft);
tableWidget -> setItem (row, 3, item3);
QTableWidgetItem *item4 = new QTableWidgetItem;
item4 -> setTextAlignment (Qt::AlignRight);
tableWidget -> setItem (row, 4, item4)
;
QTableWidgetItem *item5 = new QTableWidgetItem;
item5 -> setTextAlignment (Qt::AlignRight);
tableWidget -> setItem (row, 5, item5);
QTableWidgetItem *item6 = new QTableWidgetItem;
item6 -> setTextAlignment (Qt::AlignRight);
tableWidget -> setItem (row, 6, item6);
QTableWidgetItem *item7 = new QTableWidgetItem;
item7 -> setTextAlignment (Qt::AlignRight);
tableWidget -> setItem (row, 7, item7);
tableWidget -> item (row, 0) -> setText (theTime);
tableWidget -> item (row, 1) -> setText (call);
tableWidget -> item (row, 2) -> setText (loc);
tableWidget -> item (row, 3) -> setText (power);
tableWidget -> item (row, 4) -> setText (QString::number (snr));
tableWidget -> item (row, 5) -> setText (QString::number (dt));
tableWidget -> item (row, 6) -> setText (QString::number (freq, 'f', 2));
tableWidget -> item (row, 7) -> setText (QString::number (drift));
if (!valid)
return;
if (filePointer. load () != nullptr) {
QString line;
line = theTime + "\t" + call + "\t" + loc + " ->\t" +
power +
"\t" + QString::number (snr, 'f', 2) +
"\t" + QString::number (dt, 'f', 2) +
"\t" + QString::number (freq, 'f', 2) +
"\t" + QString::number (drift);
sendString (line);
}
sendMessage (call, loc, (int)(freq), (int)(snr), time (NULL));
}