-
Notifications
You must be signed in to change notification settings - Fork 3
/
radio.go
545 lines (453 loc) · 11 KB
/
radio.go
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
package localradio
import (
"errors"
"log"
hl "github.com/dh1tw/goHamlib"
sbRadio "github.com/dh1tw/gorigctl/sb_radio"
"github.com/dh1tw/gorigctl/utils"
)
func (r *LocalRadio) GetCaps() (sbRadio.Capabilities, error) {
caps := sbRadio.Capabilities{}
caps.Vfos = r.rig.Caps.Vfos
caps.Modes = r.rig.Caps.Modes
caps.VfoOps = r.rig.Caps.Operations
caps.GetFunctions = r.rig.Caps.GetFunctions
caps.SetFunctions = r.rig.Caps.SetFunctions
caps.GetLevels = utils.HlValuesToPbValues(r.rig.Caps.GetLevels)
caps.SetLevels = utils.HlValuesToPbValues(r.rig.Caps.SetLevels)
caps.GetParameters = utils.HlValuesToPbValues(r.rig.Caps.GetParameters)
caps.SetParameters = utils.HlValuesToPbValues(r.rig.Caps.SetParameters)
caps.MaxRit = int32(r.rig.Caps.MaxRit)
caps.MaxXit = int32(r.rig.Caps.MaxXit)
caps.MaxIfShift = int32(r.rig.Caps.MaxIfShift)
caps.Filters = utils.HlMapToPbMap(r.rig.Caps.Filters)
caps.TuningSteps = utils.HlMapToPbMap(r.rig.Caps.TuningSteps)
caps.Preamps = utils.IntListToint32List(r.rig.Caps.Preamps)
caps.Attenuators = utils.IntListToint32List(r.rig.Caps.Attenuators)
caps.RigModel = int32(r.rig.Caps.RigModel)
caps.ModelName = r.rig.Caps.ModelName
caps.Version = r.rig.Caps.Version
caps.MfgName = r.rig.Caps.MfgName
caps.HasPowerstat = r.rig.Caps.HasGetPowerStat
caps.HasPtt = r.rig.Caps.HasGetPtt
caps.HasRit = r.rig.Caps.HasGetRit
caps.HasXit = r.rig.Caps.HasGetXit
caps.HasSplit = r.rig.Caps.HasGetSplitVfo
caps.HasTs = r.rig.Caps.HasGetTs
caps.HasAnt = r.rig.Caps.HasGetAnt
return caps, nil
}
func (r *LocalRadio) GetState() (sbRadio.State, error) {
return r.queryVfo()
}
func (r *LocalRadio) GetFrequency() (float64, error) {
freq, err := r.rig.GetFreq(r.vfo)
if err != nil {
return 0, err
}
return freq, nil
}
func (r *LocalRadio) SetFrequency(freq float64) error {
return r.rig.SetFreq(r.vfo, freq)
}
func (r *LocalRadio) GetMode() (string, int, error) {
m, pbWidth, err := r.rig.GetMode(r.vfo)
if err != nil {
return "", 0, err
}
mode, ok := hl.ModeName[m]
if !ok {
return "", 0, err
}
return mode, pbWidth, nil
}
func (r *LocalRadio) SetMode(mode string, pbWidth int) error {
m, ok := hl.ModeValue[mode]
if !ok {
return errors.New("unkown mode")
}
err := r.rig.SetMode(r.vfo, m, pbWidth)
if err != nil {
return err
}
return nil
}
func (r *LocalRadio) GetVfo() (string, error) {
v, err := r.rig.GetVfo()
if err != nil {
return "", err
}
vfo, ok := hl.VfoName[v]
if !ok {
return "", errors.New("unknown vfo")
}
return vfo, nil
}
func (r *LocalRadio) SetVfo(vfo string) error {
v, ok := hl.VfoValue[vfo]
if !ok {
return errors.New("unknown vfo")
}
err := r.rig.SetVfo(v)
if err != nil {
return err
}
r.vfo = v
return nil
}
func (r *LocalRadio) GetRit() (int, error) {
rit, err := r.rig.GetRit(r.vfo)
if err != nil {
return 0, err
}
return rit, err
}
func (r *LocalRadio) SetRit(rit int) error {
return r.rig.SetRit(r.vfo, rit)
}
func (r *LocalRadio) GetXit() (int, error) {
xit, err := r.rig.GetXit(r.vfo)
if err != nil {
log.Println(err)
return 0, err
}
return xit, nil
}
func (r *LocalRadio) SetXit(xit int) error {
return r.rig.SetXit(r.vfo, xit)
}
func (r *LocalRadio) GetAntenna() (int, error) {
ant, err := r.rig.GetAnt(r.vfo)
if err != nil {
return 0, err
}
return ant, nil
}
func (r *LocalRadio) SetAntenna(ant int) error {
return r.rig.SetAnt(r.vfo, ant)
}
func (r *LocalRadio) GetPtt() (bool, error) {
ptt, err := r.rig.GetPtt(r.vfo)
if err != nil {
return false, err
}
if ptt == hl.RIG_PTT_ON {
return true, nil
}
return false, nil
}
func (r *LocalRadio) SetPtt(ptt bool) error {
p := hl.RIG_PTT_OFF
if ptt {
p = hl.RIG_PTT_ON
}
return r.rig.SetPtt(r.vfo, p)
}
func (r *LocalRadio) GetTuningStep() (int, error) {
ts, err := r.rig.GetTs(r.vfo)
if err != nil {
return 0, err
}
return ts, nil
}
func (r *LocalRadio) SetTuningStep(ts int) error {
return r.rig.SetTs(r.vfo, ts)
}
func (r *LocalRadio) GetPowerstat() (bool, error) {
ps, err := r.rig.GetPowerStat()
if err != nil {
return false, err
}
if ps == hl.RIG_POWER_ON {
return true, nil
}
return false, nil
}
func (r *LocalRadio) SetPowerstat(ps bool) error {
p := hl.RIG_POWER_OFF
if ps {
p = hl.RIG_POWER_ON
}
return r.rig.SetPowerStat(p)
}
func (r *LocalRadio) ExecVfoOps(ops []string) error {
for op := range ops {
err := r.rig.VfoOp(r.vfo, op)
if err != nil {
return err
}
}
return nil
}
func (r *LocalRadio) GetSplitVfo() (string, bool, error) {
e, v, err := r.rig.GetSplitVfo(r.vfo)
if err != nil {
return "", false, err
}
enabled := false
if e == 1 {
enabled = true
}
vfo, ok := hl.VfoName[v]
if !ok {
return "", false, errors.New("unknown vfo")
}
return vfo, enabled, nil
}
func (r *LocalRadio) SetSplitVfo(vfo string, enabled bool) error {
v, ok := hl.VfoValue[vfo]
if !ok {
return errors.New("unknown vfo")
}
return r.rig.SetSplitVfo(r.vfo, utils.Btoi(enabled), v)
}
func (r *LocalRadio) GetSplitFrequency() (float64, error) {
_, v, err := r.rig.GetSplitVfo(r.vfo)
if err != nil {
return 0, err
}
freq, err := r.rig.GetSplitFreq(v)
if err != nil {
return 0, err
}
return freq, nil
}
func (r *LocalRadio) SetSplitFrequency(freq float64) error {
_, v, err := r.rig.GetSplitVfo(r.vfo)
if err != nil {
return err
}
return r.rig.SetSplitFreq(v, freq*1000) // Hz
}
func (r *LocalRadio) GetSplitMode() (string, int, error) {
_, v, err := r.rig.GetSplitVfo(r.vfo)
if err != nil {
return "", 0, err
}
m, pbWidth, err := r.rig.GetSplitMode(v)
if err != nil {
return "", 0, err
}
mode, ok := hl.ModeName[m]
if !ok {
return "", 0, errors.New("unknown mode")
}
return mode, pbWidth, nil
}
func (r *LocalRadio) SetSplitMode(mode string, pbWidth int) error {
_, v, err := r.rig.GetSplitVfo(r.vfo)
if err != nil {
return err
}
m, ok := hl.ModeValue[mode]
if !ok {
return errors.New("unknown mode")
}
return r.rig.SetSplitMode(v, m, pbWidth)
}
func (r *LocalRadio) GetSplitPbWidth() (int, error) {
_, v, err := r.rig.GetSplitVfo(r.vfo)
if err != nil {
return 0, err
}
_, pbWidth, err := r.rig.GetSplitMode(v)
if err != nil {
return 0, err
}
return pbWidth, nil
}
func (r *LocalRadio) SetSplitPbWidth(pbWidth int) error {
_, v, err := r.rig.GetSplitVfo(r.vfo)
if err != nil {
return err
}
mode, _, err := r.rig.GetSplitMode(v)
if err != nil {
return err
}
return r.rig.SetSplitMode(r.vfo, mode, pbWidth)
}
func (r *LocalRadio) GetSplitFrequencyMode() (float64, string, int, error) {
freq, err := r.GetSplitFrequency()
if err != nil {
return 0, "", 0, err
}
mode, pbWidth, err := r.GetSplitMode()
if err != nil {
return 0, "", 0, err
}
return freq, mode, pbWidth, nil
}
func (r *LocalRadio) SetSplitFrequencyMode(freq float64, mode string, pbWidth int) error {
if err := r.SetSplitFrequency(freq); err != nil {
return err
}
if err := r.SetSplitMode(mode, pbWidth); err != nil {
return err
}
return nil
}
func (r *LocalRadio) GetFunction(function string) (bool, error) {
f, ok := hl.FuncValue[function]
if !ok {
return false, errors.New("unknown function")
}
return r.rig.GetFunc(r.vfo, f)
}
func (r *LocalRadio) SetFunction(function string, value bool) error {
f, ok := hl.FuncValue[function]
if !ok {
return errors.New("unknown function")
}
return r.rig.SetFunc(r.vfo, f, value)
}
func (r *LocalRadio) GetLevel(level string) (float32, error) {
l, ok := hl.LevelValue[level]
if !ok {
return 0, errors.New("unknown level")
}
return r.rig.GetLevel(r.vfo, l)
}
func (r *LocalRadio) SetLevel(level string, value float32) error {
l, ok := hl.LevelValue[level]
if !ok {
return errors.New("unknown level")
}
return r.rig.SetLevel(r.vfo, l, value)
}
func (r *LocalRadio) GetParameter(parm string) (float32, error) {
p, ok := hl.LevelValue[parm]
if !ok {
return 0, errors.New("unknown parameter")
}
return r.rig.GetParm(r.vfo, p)
}
func (r *LocalRadio) SetParameter(parm string, value float32) error {
p, ok := hl.LevelValue[parm]
if !ok {
return errors.New("unknown parameter")
}
return r.rig.SetParm(r.vfo, p, value)
}
func (r *LocalRadio) queryVfo() (sbRadio.State, error) {
state := sbRadio.State{}
state.Vfo = &sbRadio.Vfo{}
state.Vfo.Levels = make(map[string]float32)
state.Vfo.Parameters = make(map[string]float32)
state.Vfo.Functions = make(map[string]bool)
state.Vfo.Split = &sbRadio.Split{}
state.Channel = &sbRadio.Channel{}
if r.rig.Caps.HasGetPowerStat {
pwrOn, err := r.rig.GetPowerStat()
if err != nil {
return state, err
}
if pwrOn == hl.RIG_POWER_ON {
state.RadioOn = true
} else {
state.RadioOn = false
}
}
// Only query radio if Power is On or if Radio has now PowerStat function
// in this case we will assume that the radio is turned on
if (r.rig.Caps.HasGetPowerStat && state.RadioOn) || !r.rig.Caps.HasGetPowerStat {
vfo := hl.VfoValue["CURR"]
if r.rig.Caps.HasGetVfo {
vfo, err := r.GetVfo()
if err != nil {
return state, err
}
state.CurrentVfo = vfo
} else {
state.CurrentVfo = "CURR"
}
if r.rig.Caps.HasGetFreq {
freq, err := r.GetFrequency()
if err != nil {
return state, err
}
state.Vfo.Frequency = freq
}
if r.rig.Caps.HasGetMode {
mode, pbWidth, err := r.GetMode()
if err != nil {
return state, err
}
state.Vfo.Mode = mode
state.Vfo.PbWidth = int32(pbWidth)
}
if r.rig.Caps.HasGetAnt {
ant, err := r.GetAntenna()
if err != nil {
return state, err
}
state.Vfo.Ant = int32(ant)
}
if r.rig.Caps.HasGetRit {
rit, err := r.rig.GetRit(vfo)
if err != nil {
return state, err
} else {
state.Vfo.Rit = int32(rit)
}
}
if r.rig.Caps.HasGetRit {
xit, err := r.rig.GetXit(vfo)
if err != nil {
return state, err
}
state.Vfo.Xit = int32(xit)
}
if r.rig.Caps.HasGetSplitVfo {
txVfo, splitOn, err := r.GetSplitVfo()
if err != nil {
return state, err
}
state.Vfo.Split.Enabled = splitOn
state.Vfo.Split.Vfo = txVfo
if splitOn {
// these checks should be enabled, but most of the
// backends don't have these functions implemented
// therefore they use the emulated functions which
// unfortunately don't work everywhere well (e.g. TS-480)
// if r.rig.Caps.HasGetSplitFreq {
txFreq, txMode, txPbWidth, err := r.GetSplitFrequencyMode()
if err != nil {
return state, err
}
state.Vfo.Split.Frequency = txFreq
state.Vfo.Split.Mode = txMode
state.Vfo.Split.PbWidth = int32(txPbWidth)
}
}
if r.rig.Caps.HasGetTs {
tStep, err := r.rig.GetTs(vfo)
if err != nil {
return state, err
}
state.Vfo.TuningStep = int32(tStep)
}
for _, f := range r.rig.Caps.GetFunctions {
fValue, err := r.GetFunction(f)
if err != nil {
return state, err
}
state.Vfo.Functions[f] = fValue
}
for _, level := range r.rig.Caps.GetLevels {
lValue, err := r.GetLevel(level.Name)
if err != nil {
return state, err
}
state.Vfo.Levels[level.Name] = lValue
}
for _, param := range r.rig.Caps.GetParameters {
pValue, err := r.GetParameter(param.Name)
if err != nil {
return state, err
}
state.Vfo.Parameters[param.Name] = pValue
}
}
return state, nil
}