Skip to content

Commit

Permalink
fallback to the frequency when direct band switching is not supported…
Browse files Browse the repository at this point in the history
… by the radio
  • Loading branch information
ftl committed Jun 12, 2023
1 parent 82c0a0c commit 330ab5d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
75 changes: 69 additions & 6 deletions core/hamlib/hamlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"time"

"github.com/ftl/hamradio"
"github.com/ftl/hamradio/bandplan"
"github.com/ftl/rigproxy/pkg/client"

Expand Down Expand Up @@ -183,22 +184,44 @@ func (c *Client) SetBand(band core.Band) {
if band == c.outgoing.band {
return
}
c.outgoing.band = band
if c.conn == nil || c.conn.Closed() {
return
}

outgoingBandName := toBandplanBandName(c.outgoing.band)
outgoingBandName := toBandplanBandName(band)
outgoingBand, ok := c.bandplan[outgoingBandName]
if !ok {
log.Printf("unknown band %v", c.outgoing.band)
log.Printf("unknown band %v", band)
return
}
if c.conn == nil || c.conn.Closed() {
c.outgoing.band = band
log.Printf("outgoing band: %v", band)

err := c.switchToBand(outgoingBand)
if err == nil {
return
}
log.Printf("cannot switch to band %s directly: %v", outgoingBand, err)

err = c.switchToBandByFrequencyAndMode(outgoingBand)
if err != nil {
log.Printf("cannot switch to band %s by frequency: %v", band, err)
return
}
}

func (c *Client) switchToBand(band bandplan.Band) error {
ctx, cancel := c.withRequestTimeout()
defer cancel()
c.conn.SwitchToBand(ctx, outgoingBand)
return c.conn.SwitchToBand(ctx, band)
}

log.Printf("outgoing band: %v", band)
func (c *Client) switchToBandByFrequencyAndMode(band bandplan.Band) error {
frequency := findModePortionCenter(c.bandplan, int(band.Center()), toBandplanMode(c.incoming.mode))

ctx, cancel := c.withRequestTimeout()
defer cancel()
return c.conn.SetFrequency(ctx, client.Frequency(frequency))
}

func (c *Client) SetMode(mode core.Mode) {
Expand Down Expand Up @@ -355,3 +378,43 @@ func toClientMode(mode core.Mode) client.Mode {
return client.ModeNone
}
}

func toBandplanMode(mode core.Mode) bandplan.Mode {
log.Printf("to bandplan mode: %s", mode)
switch mode {
case core.ModeCW:
return bandplan.ModeCW
case core.ModeSSB, core.ModeFM:
return bandplan.ModePhone
case core.ModeDigital, core.ModeRTTY:
return bandplan.ModeDigital
default:
return bandplan.ModeDigital
}
}

func findModePortionCenter(bp bandplan.Bandplan, f int, mode bandplan.Mode) int {
log.Printf("find mode portion center: %d %s", f, mode)
frequency := hamradio.Frequency(f)
band := bp.ByFrequency(frequency)
var modePortion bandplan.Portion
var currentPortion bandplan.Portion
for _, portion := range band.Portions {
if (portion.Mode == mode && portion.From < frequency) || modePortion.Mode != mode {
modePortion = portion
}
if portion.Contains(frequency) {
currentPortion = portion
}
if modePortion.Mode == mode && currentPortion.Mode != "" {
break
}
}
if currentPortion.Mode == mode {
return int(currentPortion.Center())
}
if modePortion.Mode == mode {
return int(modePortion.Center())
}
return int(band.Center())
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/ftl/conval v0.5.0
github.com/ftl/gmtry v0.0.0-20201120192810-fa4a1b99fc04
github.com/ftl/hamradio v0.2.7
github.com/ftl/rigproxy v0.2.1
github.com/ftl/rigproxy v0.2.2
github.com/ftl/tci v0.3.2
github.com/golang/protobuf v1.5.2
github.com/gotk3/gotk3 v0.6.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/ftl/hamradio v0.2.7 h1:AXg5JI6oPaSvO/YfM7kwAweVP49EF/zaEBiY+cxYDMI=
github.com/ftl/hamradio v0.2.7/go.mod h1:BvA+ni3sOKmrIJpLt6f2sYK9vc3VfihZm4x0h8kzOPw=
github.com/ftl/localcopy v0.0.0-20190616142648-8915fb81f0d9 h1:ORI3EUKpLTsfA372C6xpuZFDXw+ckmCzLaCcJvakG24=
github.com/ftl/localcopy v0.0.0-20190616142648-8915fb81f0d9/go.mod h1:4sZLCxjgn++exy5u0muVzlvnahfanPuiHLQo0GJQnPA=
github.com/ftl/rigproxy v0.2.1 h1:NenPHHvWMi6EpZUYOINd8Gsvl+zoSQWc+q5Ha/yUsbE=
github.com/ftl/rigproxy v0.2.1/go.mod h1:PrBUiqLwu/6zL44+uOz4lgmOfnis4FIvJDhxDNXoi60=
github.com/ftl/rigproxy v0.2.2 h1:01+7fhn0xqToFJxsAHO1N/JNs1Q5I38yT7/lF43AExI=
github.com/ftl/rigproxy v0.2.2/go.mod h1:PrBUiqLwu/6zL44+uOz4lgmOfnis4FIvJDhxDNXoi60=
github.com/ftl/tci v0.3.2 h1:1Qdgprldiv7/DQvuK96OHMVqb+SDunqbxTHDcWsE5Tk=
github.com/ftl/tci v0.3.2/go.mod h1:3B8x8FI/kBbUwbWnz725tTiiiNfJpIL9cQ67TnjW3aU=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
Expand Down

0 comments on commit 330ab5d

Please sign in to comment.