-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhamlibconnector.cpp
627 lines (497 loc) · 18 KB
/
hamlibconnector.cpp
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
#include "hamlibconnector.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QApplication>
HamlibConnector::HamlibConnector(QObject *parent)
: QObject{parent}
{
// For debug only
verbose = RIG_DEBUG_NONE;
// verbose = RIG_DEBUG_TRACE;
lockout_spot = false;
spotDelayWorker_p = nullptr;
current_vfo = RIG_VFO_A;
current_mode = RIG_MODE_NONE;
current_pbwidth = 800; // Good default for CW
modeStr_p = nullptr;
cached_freq_a = 0e0;
cached_freq_b = 0e0;
init_succeeded = false;
split_enabled = false;
// Config object not yet initialized - this will fail
#if 0
// Figure out how we're configured - ie what rig and device
config_obj_p = Ui::MainWindow::getConfigObjPtr();
QString model_str = "Rig Model";
QString rig_model_str = config_obj_p->get_value_from_key(model_str);
qDebug() << "HamlibConnector::HamlibConnector(): Rig Model configured as" << rig_model_str;
#endif
my_model = RIG_MODEL_K3;
rig_set_debug(verbose);
my_rig = rig_init(my_model);
if (!my_rig) {
qDebug() << "Unknown rig num " << my_model << "or initialization error\n";
QApplication::exit(16);
}
strncpy(my_rig->state.rigport.pathname, rig_file, 511);
// qDebug() << "rig_file = " << my_rig->state.rigport.pathname;
// rig_open will take 75 seconds if the server end is not listening
retcode = rig_open(my_rig);
if (retcode != RIG_OK) {
qDebug() << "HamlibConnector::HamlibConnector(): rig_open: error = " << rigerror(retcode) << rig_file << strerror(errno) << "\n";
// Can't quit here - main loop isn't yet running. All we can do is report the failure.
goto bailout;
}
retcode = rig_get_vfo(my_rig, ¤t_vfo);
qDebug() << "HamlibConnector::HamlibConnector(): rig_get_vfo returned" << current_vfo;
strength = -54; // Initialize S-Meter to effectively zero
// Initialize rig mode, etc
get_rig_mode_and_bw();
mrr_getRigFrequency(RIG_VFO_A);
mrr_getRigFrequency(RIG_VFO_B);
init_succeeded = true;
bailout:
return;
}
freq_t HamlibConnector::mrr_getRigFrequency(vfo_t vfo) {
freq_t freq;
retcode = rig_get_freq(my_rig, vfo, &freq);
if (retcode != RIG_OK) {
qDebug() << "rig_get_freq: error = " << rigerror(retcode) << rig_file << strerror(errno) << "\n";
QApplication::exit();
}
if ( vfo == RIG_VFO_A ) {
cached_freq_a = freq;
return freq;
}
else {
cached_freq_b = freq;
return freq;
}
qDebug() << "Current freq (on currently selected VFO) is " << cached_freq_a;
return 0;
}
void HamlibConnector::autoupdate_frequency() {
freq_t f = mrr_getRigFrequency(current_vfo);
QString str_tmp = HamlibConnector::get_display_frequency(f);
ui_pointer->freqDisplay->display(str_tmp);
}
void HamlibConnector::store_ui_pointer(Ui::MainWindow *p) {
ui_pointer = p;
}
void HamlibConnector::autoupdate_smeter() {
// qDebug("HamlibConnector::autoupdate_smeter() called");
// Create a moving average
static int moving_avg_length = 5;
static QList<int> s_readings;
int avg = 0, i;
// Read the rig's S meter value
int s = read_rig_strength();
s_readings.append(s);
if ( s_readings.size() > moving_avg_length ) {
// Can't really do the moving average with less than enough readings
avg = 0;
for ( i=0; i<5; i++) {
avg += s_readings.at(i);
}
s_readings.removeFirst();
avg = avg / 5;
}
int t = get_SMeter_progbar_value(avg);
ui_pointer->smeterProgressBar->setValue(t);
// Get the text equivalent of the s-meter value
ui_pointer->smeterLabel->setText(sMeter_cal[t].s);
}
int HamlibConnector::get_SMeter_progbar_value(int x) {
// Get value appropriate for our S-Meter progress bar, from the raw S-Meter data from the rig
for (int i = 0; i < (int) sizeof(sMeter_cal) / (int) (sizeof(sMeter_cal[0]) - 1); ++i) {
if ( x >= sMeter_cal[i].raw && x < sMeter_cal[i+1].raw ) {
// qDebug() << "HamlibConnector::get_SMeter_progbar_value(): sMeter_cal[].raw" << sMeter_cal[i].raw;
strength = i;
return (strength = i);
}
}
return 0; // Default - shouldn't get here
}
int HamlibConnector::read_rig_strength() {
value_t s;
rig_get_level(my_rig, current_vfo, RIG_LEVEL_STRENGTH, &s);
return (s.i);
}
float HamlibConnector::read_rig_swr() {
value_t s;
int rc = rig_get_level(my_rig, current_vfo, RIG_LEVEL_SWR, &s);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::read_rig_swr(): rig_get_level failed" << rigerror(rc);
return -1e0f;
}
qDebug() << "HamlibConnector::read_rig_swr(): returned" << s.f;
return (s.f);
}
QString HamlibConnector::get_display_frequency(freq_t f) {
// Hamlib hands us a double value equal to the frequency in Hz. We want to display in MHz
// qDebug() << "HamlibConnector::get_display_frequency() entered: (freq_t f) = " << f;
freq_t f_MHz = f / 1000000;
// qDebug() << "HamlibConnector::get_display_frequency() entered: f_MHZ = " << f_MHz;
QString ftmp = QString("%1").arg(f_MHz, 0, 'f', 5);
qsizetype dot_index = ftmp.indexOf(QChar('.'));
if ( dot_index == -1 ) {
qDebug() << "HamlibConnector::get_display_frequency(): No decimal point found in frequency string" << ftmp;
QApplication::quit();
}
// qDebug() << "String ftmp now: " << ftmp << "Dot Index = " << dot_index;
QString fd = ftmp.left(dot_index+6); // Print the decimal point + 5 digits after (dot_index is zero based, left() arg is 1 based.
return fd;
}
int HamlibConnector::mrrSetRigFreqA(freq_t f) {
return rig_set_freq(my_rig, RIG_VFO_A, f);
}
int HamlibConnector::mrrSetRigFreqB(freq_t f) {
return rig_set_freq(my_rig, RIG_VFO_B, f);
}
vfo_t HamlibConnector::mrr_getVFO() {
return current_vfo;
}
freq_t HamlibConnector::mrrGetCachedFreqA() {
return cached_freq_a;
}
void HamlibConnector::mrrSetCachedFreqA(freq_t f) {
cached_freq_a = f;
}
freq_t HamlibConnector::mrrGetCachedFreqB() {
return cached_freq_b;
}
void HamlibConnector::mrrSetCachedFreqB(freq_t f) {
cached_freq_b = f;
}
int HamlibConnector::get_retcode(void) {
return retcode;
}
void HamlibConnector::setSpot(void) {
if ( lockout_spot ) return;
int rc = rig_set_func(my_rig, current_vfo, RIG_FUNC_SPOT, 1);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::setSpot() failed: rc = " << rigerror(rc);
}
lockout_spot = true;
// Start a thread with a 2 second timer. Update freq when done.
spotDelayWorker_p = new SpotDelayWorker();
connect(spotDelayWorker_p, &SpotDelayWorker::spotDelayExpired, this, &HamlibConnector::cleanupSpotDelay);
spotDelayWorker_p->start(); // start() unlike run() detaches and returns immediately
}
void HamlibConnector::cleanupSpotDelay() {
qDebug() << "HamlibConnector::cleanupSpotDelay(): killing worker thread, updating freq";
delete spotDelayWorker_p;
spotDelayWorker_p = nullptr;
autoupdate_frequency();
lockout_spot = false;
emit spotDone();
}
void HamlibConnector::setSwapAB() {
int rc = rig_set_func(my_rig, current_vfo, RIG_FUNC_ABSWAP, 1);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::setSpot() failed: rc = " << rigerror(rc);
}
}
mode_t HamlibConnector::mrrGetMode() {
return current_mode;
}
const char *HamlibConnector::mrr_getModeString(mode_t mode) {
return rig_strrmode(mode);
}
int HamlibConnector::mrr_set_mode(mode_t mode) {
pbwidth_t w = rig_passband_normal(my_rig, mode);
qDebug() << "HamlibConnector::mrr_set_mode(): normal passband reported as" << w;
// int rc = rig_set_mode(my_rig, current_vfo, mode, RIG_PASSBAND_NOCHANGE);
int rc = rig_set_mode(my_rig, current_vfo, mode, w);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrr_set_mode() failed: rc = " << rigerror(rc);
}
current_mode = mode;
// Update bandwidth on change of mode
get_rig_mode_and_bw();
return rc;
}
pbwidth_t HamlibConnector::mrr_get_width() {
return current_pbwidth;
}
int HamlibConnector::bwidthChangeRequest(int bw){
int rc;
// qDebug() << "HamlibConnector::bwidth_change_request() with signal:" << "bw:" << bw;
rc = rig_set_mode(my_rig, current_vfo, current_mode, bw);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::bwidth_change_request(): rig_set_mode failed" << rigerror(rc);
return rc;
}
emit updateWidthSlider(bw);
current_pbwidth = bw;
return rc;
}
int HamlibConnector::get_rig_mode_and_bw() {
rmode_t mode;
pbwidth_t bw;
int rc = rig_get_mode(my_rig, current_vfo, &mode, &bw);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::get_rig_mode(): rig_get_mode failed" << rigerror(rc);
}
current_mode = mode;
current_pbwidth = bw;
return rc;
}
void HamlibConnector::mrrSetTune(bool on) {
if ( on ) {
int rc = rig_set_func(my_rig, current_vfo, RIG_FUNC_TUNE, 1);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrrSetTune() failed: rc = " << rigerror(rc);
}
}
else {
mrrSetRx();
}
}
void HamlibConnector::mrrSetRx() {
int rc = rig_set_func(my_rig, current_vfo, RIG_FUNC_RX, 1);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrrSetRx() failed: rc = " << rigerror(rc);
}
}
int HamlibConnector::txCW_Char(char c) {
char c_tmp = c;
int rc;
// qDebug() << "HamlibConnector::txCW_Char(): Entered with " << c;
rc = rig_set_func(my_rig, current_vfo, RIG_FUNC_SEND_MORSE, c_tmp);
return rc;
}
int HamlibConnector::mrr_set_level(setting_t level, value_t val) {
int rc = rig_set_level(my_rig, current_vfo, level, val);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrr_set_level() failed: rc = " << rigerror(rc);
}
return rc;
}
void HamlibConnector::abortTX() {
qDebug() << "HamlibConnector::abortTX(): entered";
mrrSetRx();
}
int HamlibConnector::getCwSpeed() {
int rc;
value_t val;
rc = rig_get_level(my_rig, current_vfo, RIG_LEVEL_KEYSPD, &val);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::getCwSpeed(): failed" << rigerror(rc);
return -1;
}
// qDebug() << "HamlibConnector::getCwSpeed(): returned" << val.i;
cw_speed = val.i;
return val.i;
}
int HamlibConnector::bumpCwSpeed(bool up) {
value_t val;
// Rig limits: 8 - 50wpm
if ( up == true ) {
cw_speed = (cw_speed >= 50) ? 50 : ++cw_speed;
val.i = cw_speed;
} else {
cw_speed = (cw_speed <= 8) ? 8 : --cw_speed;
val.i = cw_speed;
}
qDebug() << "HamlibConnector::bumpCwSpeed(): new value is" << cw_speed;
int rc = rig_set_level(my_rig, current_vfo, RIG_LEVEL_KEYSPD, val);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::bumpCwSpeed(): failed" << rigerror(rc);
return -1;
}
return cw_speed;
}
void HamlibConnector::setPauseTx(bool checked) {
emit pauseTxSig(checked);
qDebug() << "HamlibConnector::setPauseTx(): paused =" << checked;
}
int HamlibConnector::mrrGetIcConfig(unsigned char *p) {
value_t val;
unsigned char buff[6];
val.s = (char *)buff;
int rc = rig_get_level(my_rig, current_vfo, RIG_LEVEL_ICONSTATUS, &val);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrr_get_ic_config(): failed" << rigerror(rc);
return rc;
}
// Copy the bytes into local storage
strncpy((char *)p, (char *)&buff, 5);
return 0;
}
void HamlibConnector::mrr_set_tx_test() {
// This is a toggle function - needs no "value"
int rc = rig_set_func(my_rig, current_vfo, RIG_FUNC_TXTEST, 0);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrr_set_tx_test(): failed" << rigerror(rc);
}
}
void HamlibConnector::mrr_set_band(int band) {
int rc = rig_set_func(my_rig, current_vfo, RIG_FUNC_BANDNUM, band);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrr_set_band(): failed" << rigerror(rc);
}
}
int HamlibConnector::mrr_get_band() {
int band, rc;
rc = rig_get_func(my_rig, current_vfo, RIG_FUNC_BANDNUM, &band);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrr_get_band(): failed" << rigerror(rc);
}
return band;
}
void HamlibConnector::mrr_a_2_b() {
int rc = rig_set_func(my_rig, current_vfo, RIG_FUNC_VFOA2B, 0);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrr_a_2_b(): failed" << rigerror(rc);
}
}
int HamlibConnector::mrrGetMonLevel() {
value_t val;
int rc = rig_get_level(my_rig, current_vfo, RIG_LEVEL_MONITOR_GAIN, &val);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrrGetMonLevel(): failed" << rigerror(rc);
}
qDebug() << "HamlibConnector::mrrGetMonLevel(): monitor level:" << val.f;
return (int) (val.f * 60.0f);
}
int HamlibConnector::mrrSetMonLevel(int level) {
value_t val;
val.f = (float) (level / 60.0f);
int rc = rig_set_level(my_rig, current_vfo, RIG_LEVEL_MONITOR_GAIN, val);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrrSetMonLevel(): failed" << rigerror(rc);
}
return rc;
}
int HamlibConnector::mrrGetXFILValue() {
value_t status;
qDebug() << "HamlibConnector::mrrGetXFIL(): entered";
int rc = rig_get_level(my_rig, current_vfo, RIG_LEVEL_XFILV, &status);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrrGetXFIL(): failed" << rigerror(rc);
}
xfil_bandwidth = status.i;
return status.i;
}
void HamlibConnector::mrrSetXFIL() {
int rc = rig_set_func(my_rig, current_vfo, RIG_FUNC_XFIL, 0);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrrSetXFIL(): failed" << rigerror(rc);
}
emit updateXFIL_sig();
}
void HamlibConnector::powerOFF() {
rig_set_powerstat(my_rig, RIG_POWER_OFF);
}
const char *HamlibConnector::getRigError(int err_number) {
return rigerror(err_number);
}
const char *HamlibConnector::getXFILString(int number) {
if ( number < 1 || number > 5 ) {
qDebug() << "HamlibConnector::getXFILString()): Invalid filter number" << number;
}
// Array is zero based, XFIL starts at 1
return xtal_filter_values[number - 1].filter_bandwidth;
}
/*
extern HAMLIB_EXPORT(int)
rig_token_foreach HAMLIB_PARAMS((RIG *rig,
int (*cfunc)(const struct confparams *,
rig_ptr_t),
rig_ptr_t data));
rig.h:2802:1: note: candidate function not viable:
no known conversion from 'int (HamlibConnector::*)(const struct confparams *, void *)'
to 'int (*)(const struct confparams *, void *)' for 2nd argument
*/
int HamlibConnector::listTokensCallback(const struct confparams *cp, rig_ptr_t rp) {
#pragma unused (rp)
qDebug() << "HamlibConnector::listTokensCallback(): confparms:";
qDebug() << " token:" << cp->token;
qDebug() << " name:" << cp->name;
qDebug() << " label:" << cp->label;
qDebug() << " type:" << get_rig_conf_type(cp->type);
if ( cp->type == RIG_CONF_STRING )
qDebug() << " data:" << *cp->u.c.combostr;
qDebug() << "";
return 1;
}
const char *HamlibConnector::get_rig_conf_type(enum rig_conf_e type)
{
switch (type)
{
case RIG_CONF_STRING:
return "STRING";
case RIG_CONF_COMBO:
return "COMBO";
case RIG_CONF_NUMERIC:
return "NUMERIC";
case RIG_CONF_CHECKBUTTON:
return "CHECKBUTTON";
case RIG_CONF_BUTTON:
return "BUTTON";
case RIG_CONF_BINARY:
return "BINARY";
}
return "UNKNOWN";
}
int HamlibConnector::mrrRigSetSplitVfo(bool split_on) {
int rc;
if ( split_on ) {
rc = rig_set_split_vfo (my_rig, RIG_VFO_A, RIG_SPLIT_ON, RIG_VFO_B);
}
else {
rc = rig_set_split_vfo (my_rig, RIG_VFO_A, RIG_SPLIT_OFF, RIG_VFO_B);
}
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrrRigSetSplitVfo(): failed" << rigerror(rc);
}
return rc;
}
void HamlibConnector::mrrGetRigIF_XCVR_Info() {
qDebug() << "HamlibConnector::mrrGetRigIF_XCVR_Info(): entered";
value_t value;
unsigned char buff[40];
value.s = (char *)buff;
int rc = rig_get_level(my_rig, current_vfo, RIG_LEVEL_INFO, &value);
if ( rc != RIG_OK ) {
qDebug() << "HamlibConnector::mrrGetRigIF_XCVR_Info(): failed" << rigerror(rc);
}
// Parse rig response
parseRigIF_Response(buff);
}
void HamlibConnector::parseRigIF_Response(unsigned char *r) {
// RSP format: IF[f]*****+yyyyrx*00tmvspbd1* (37 bytes)
// [f] Operating frequency, excluding any RIT/XIT offset (11 digits; see FA command format)
// * represents a space (BLANK, or ASCII 0x20)
// + either "+" or "-" (sign of RIT/XIT offset)
// yyyy RIT/XIT offset in Hz (range is -9999 to +9999 Hz when computer-controlled)
// r 1 if RIT is on, 0 if off
// x 1 if XIT is on, 0 if off
// t 1 if the K3 is in transmit mode, 0 if receive
// m operating mode (see MD command)
// v receive-mode VFO selection, 0 for VFO A, 1 for VFO B
// s 1 if scan is in progress, 0 otherwise
// p 1 if the transceiver is in split mode, 0 otherwise
// b Basic RSP format: always 0; K2 Extended RSP format (K22): 1 if present IF response
// is due to a band change; 0 otherwise
// d Basic RSP format: always 0; K3 Extended RSP format (K31): DATA sub-mode,
// if applicable (0=DATA A, 1=AFSK A, 2= FSK D, 3=PSK D)
std::memcpy(&if_resp, r, sizeof(if_resp));
qDebug() << "HamlibConnector::parseRigIF_Response(): returned these bytes: (sizeof)" << sizeof(if_resp);
char _fa[12];
std::memcpy(_fa, &if_resp.fa, 11);
_fa[11] = '\0';
qDebug() << "HamlibConnector::parseRigIF_Response(): FA =" << _fa;
fprintf(stderr, " %c\n", if_resp.rit_offset_sign);
// Rig offset
std::memcpy(_fa, &if_resp.rit_offset, 4);
_fa[4] = '\0';
fprintf(stderr, " %s\n", _fa);
// for ( int i=0; i<38; i++ ) {
// // qDebug() << " " << Qt::hex << r[i];
// fprintf(stderr, "%c ", (unsigned char) r[i]);
// }
// qDebug() << "";
}