freq.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2016 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
  2. // Use of this source code is governed by the MIT-license that can be
  3. // found in the LICENSE file.
  4. package main
  5. import (
  6. "fmt"
  7. "log"
  8. "strconv"
  9. "strings"
  10. "github.com/la5nta/wl2k-go/rigcontrol/hamlib"
  11. )
  12. var bands = map[string]Band{
  13. "160m": {1.8e6, 2.0e6},
  14. "80m": {3.5e6, 4.0e6},
  15. "60m": {5.2e6, 5.5e6},
  16. "40m": {7.0e6, 7.3e6},
  17. "30m": {10.1e6, 10.2e6},
  18. "20m": {14.0e6, 14.4e6},
  19. "17m": {18.0e6, 18.2e6},
  20. "15m": {21.0e6, 21.5e6},
  21. "12m": {24.8e6, 25.0e6},
  22. "10m": {28.0e6, 30.0e6},
  23. "6m": {50.0e6, 54.0e6},
  24. "4m": {70.0e6, 70.5e6},
  25. "2m": {144.0e6, 148.0e6},
  26. "1.25m": {219.0e6, 225.0e6}, // 220, 222 (MHz)
  27. "70cm": {420.0e6, 450.0e6},
  28. }
  29. type Band struct{ lower, upper Frequency }
  30. func (b Band) Contains(f Frequency) bool {
  31. if b.lower == 0 && b.upper == 0 {
  32. return true
  33. }
  34. return f >= b.lower && f <= b.upper
  35. }
  36. type Frequency int // Hz
  37. func (f Frequency) String() string {
  38. m := f / 1e6
  39. k := (float64(f) - float64(m)*1e6) / 1e3
  40. return fmt.Sprintf("%d.%06.2f MHz", m, k)
  41. }
  42. func (f Frequency) KHz() float64 { return float64(f) / 1e3 }
  43. func (f Frequency) Dial(mode string) Frequency {
  44. mode = strings.ToLower(mode)
  45. // Try to detect FM modes
  46. // (ARDOP on FM is reported as `ARDOP 2000 FM`)
  47. if strings.HasSuffix(mode, "fm") {
  48. return f
  49. }
  50. offsets := map[string]Frequency{
  51. "winmor": 1500,
  52. "pactor": 1500,
  53. "ardop": 1500,
  54. }
  55. var shift Frequency
  56. for m, offset := range offsets {
  57. if strings.Contains(mode, m) {
  58. shift = -offset
  59. break
  60. }
  61. }
  62. return f + shift
  63. }
  64. func VFOForTransport(transport string) (vfo hamlib.VFO, ok bool) {
  65. switch transport {
  66. case MethodWinmor:
  67. vfo, ok = rigs[config.Winmor.Rig]
  68. case MethodArdop:
  69. vfo, ok = rigs[config.Ardop.Rig]
  70. case MethodAX25:
  71. vfo, ok = rigs[config.AX25.Rig]
  72. }
  73. return
  74. }
  75. func freq(param string) {
  76. parts := strings.SplitN(param, ":", 2)
  77. if parts[0] == "" {
  78. fmt.Println("Need freq method.")
  79. }
  80. rig, ok := VFOForTransport(parts[0])
  81. if !ok {
  82. log.Printf("Hamlib rig not loaded.")
  83. return
  84. }
  85. if len(parts) < 2 {
  86. freq, err := rig.GetFreq()
  87. if err != nil {
  88. log.Printf("Unable to get frequency: %s", err)
  89. }
  90. fmt.Printf("%.3f\n", float64(freq)/1e3)
  91. return
  92. }
  93. if _, _, err := setFreq(rig, parts[1]); err != nil {
  94. log.Printf("Unable to set frequency: %s", err)
  95. }
  96. }
  97. func setFreq(rig hamlib.VFO, freq string) (newFreq, oldFreq int, err error) {
  98. oldFreq, err = rig.GetFreq()
  99. if err != nil {
  100. return 0, 0, fmt.Errorf("Unable to get rig frequency: %s", err)
  101. }
  102. f, err := strconv.ParseFloat(freq, 64)
  103. if err != nil {
  104. return 0, 0, err
  105. }
  106. newFreq = int(f * 1e3)
  107. err = rig.SetFreq(newFreq)
  108. return
  109. }