Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bandwidth and icon parameters to hamlib.SetMode #6

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions pkg/hamlib/buttons.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"image"
"log"

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

Expand All @@ -14,13 +15,15 @@ import (
SetModeButton
*/

func NewSetModeButton(hamlibClient *HamlibClient, mode client.Mode, label string) *SetModeButton {
func NewSetModeButton(hamlibClient *HamlibClient, mode client.Mode, bandwidth client.Frequency, label string, icon string) *SetModeButton {
result := &SetModeButton{
client: hamlibClient,
enabled: hamlibClient.Connected(),
mode: mode,
bandwidth: bandwidth,
bandplanMode: mode.ToBandplanMode(),
label: label,
icon: icon,
}
result.longpress = hamdeck.NewLongpressHandler(result.OnLongpress)

Expand All @@ -32,6 +35,7 @@ func NewSetModeButton(hamlibClient *HamlibClient, mode client.Mode, label string
type SetModeButton struct {
hamdeck.BaseButton
client *HamlibClient
iconImage image.Image
image image.Image
selectedImage image.Image
inModePortionImage image.Image
Expand All @@ -40,7 +44,11 @@ type SetModeButton struct {
inModePortion bool
mode client.Mode
bandplanMode bandplan.Mode
bandwidth client.Frequency
label string
icon string
currentMode client.Mode
currentBandwidth client.Frequency
currentFrequency client.Frequency
longpress *hamdeck.LongpressHandler
}
Expand All @@ -54,8 +62,23 @@ func (b *SetModeButton) Enable(enabled bool) {
}

func (b *SetModeButton) SetMode(mode client.Mode) {
b.currentMode = mode
wasSelected := b.selected
b.selected = (mode == b.mode)
b.selected = (mode == b.mode) && (b.currentBandwidth == b.bandwidth)
if b.selected == wasSelected {
return
}
b.Invalidate(false)
}

func (b *SetModeButton) SetPassband(passband client.Frequency) {
if b.bandwidth == 0 {
b.currentBandwidth = 0
return
}
b.currentBandwidth = passband
wasSelected := b.selected
b.selected = (passband == b.bandwidth) && (b.currentMode == b.mode)
if b.selected == wasSelected {
return
}
Expand Down Expand Up @@ -95,15 +118,27 @@ func (b *SetModeButton) redrawImages(gc hamdeck.GraphicContext) {
if b.label != "" {
text = b.label
}

b.image = gc.DrawSingleLineTextButton(text)
b.image = b.redrawButton(gc, text)

gc.SwapColors()
b.selectedImage = gc.DrawSingleLineTextButton(text)
b.selectedImage = b.redrawButton(gc, text)

gc.SwapColors()
gc.SetBackground(hamdeck.Blue)
b.inModePortionImage = gc.DrawSingleLineTextButton(text)
b.inModePortionImage = b.redrawButton(gc, text)
}

func (b *SetModeButton) redrawButton(gc hamdeck.GraphicContext, text string) image.Image {
if b.icon == "" {
return gc.DrawSingleLineTextButton(text)
}

gc.SetFontSize(16)
if b.iconImage == nil {
iconFile := b.icon + ".png"
b.iconImage = gc.LoadIconAsset(iconFile)
}
return gc.DrawIconLabelButton(b.iconImage, text)
}

func (b *SetModeButton) Pressed() {
Expand All @@ -112,7 +147,7 @@ func (b *SetModeButton) Pressed() {
return
}
ctx := b.client.WithRequestTimeout()
err := b.client.Conn.SetModeAndPassband(ctx, b.mode, 0)
err := b.client.Conn.SetModeAndPassband(ctx, b.mode, hamradio.Frequency(b.bandwidth))
if err != nil {
log.Printf("cannot set mode: %v", err)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/hamlib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ func NotifyModeListeners(listeners []interface{}, mode client.Mode) {
}
}

type PassbandListener interface {
SetPassband(passband client.Frequency)
}

type PassbandListenerFunc func(client.Frequency)

func (f PassbandListenerFunc) SetPassband(passband client.Frequency) {
f(passband)
}

func NotifyPassbandListeners(listeners []interface{}, passband client.Frequency) {
for _, listener := range listeners {
passbandListener, ok := listener.(PassbandListener)
if ok {
passbandListener.SetPassband(passband)
}
}
}

type PowerLevelListener interface {
SetPowerLevel(powerLevel float64)
}
Expand Down Expand Up @@ -245,6 +264,7 @@ func (c *HamlibClient) setFrequency(frequency client.Frequency) {

func (c *HamlibClient) setModeAndPassband(mode client.Mode, passband client.Frequency) {
NotifyModeListeners(c.listeners, mode)
NotifyPassbandListeners(c.listeners, passband)
}

func (c *HamlibClient) setPowerLevel(powerLevel float64) {
Expand Down
12 changes: 11 additions & 1 deletion pkg/hamlib/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
ConfigLabel1 = "label1"
ConfigMode2 = "mode2"
ConfigLabel2 = "label2"
ConfigIcon = "icon"
ConfigBandwidth = "bandwidth"
ConfigBand = "band"
ConfigValue = "value"
ConfigVFO = "vfo"
Expand Down Expand Up @@ -94,11 +96,19 @@ func (f *Factory) CreateButton(config map[string]interface{}) hamdeck.Button {

func (f *Factory) createSetModeButton(config map[string]interface{}) hamdeck.Button {
mode, haveMode := hamdeck.ToString(config[ConfigMode])
bandwidth, haveBandwidth := hamdeck.ToInt(config[ConfigBandwidth])
label, _ := hamdeck.ToString(config[ConfigLabel])
icon, haveIcon := hamdeck.ToString(config[ConfigIcon])
if !haveMode {
log.Print("A hamlib.SetMode button must have a mode field.")
return nil
}
if !haveBandwidth {
bandwidth = 0
}
if !haveIcon {
icon = ""
}

connection, _ := hamdeck.ToString(config[hamdeck.ConfigConnection])
hamlibClient, err := f.connections.Get(connection)
Expand All @@ -107,7 +117,7 @@ func (f *Factory) createSetModeButton(config map[string]interface{}) hamdeck.But
return nil
}

return NewSetModeButton(hamlibClient, client.Mode(mode), label)
return NewSetModeButton(hamlibClient, client.Mode(mode), client.Frequency(bandwidth), label, icon)
}

func (f *Factory) createToggleModeButton(config map[string]interface{}) hamdeck.Button {
Expand Down
7 changes: 4 additions & 3 deletions pkg/tci/buttons.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,14 +542,15 @@ func (b *SetFilterButton) redrawImages(gc hamdeck.GraphicContext) {
}

iconFile := b.icon + ".png"
b.image = gc.DrawIconLabelButton(gc.LoadIconAsset(iconFile), b.label)
iconAsset := gc.LoadIconAsset(iconFile)
b.image = gc.DrawIconLabelButton(iconAsset, b.label)

gc.SwapColors()
b.selectedImage = gc.DrawIconLabelButton(gc.LoadIconAsset(iconFile), b.label)
b.selectedImage = gc.DrawIconLabelButton(iconAsset, b.label)

gc.SwapColors()
gc.SetBackground(hamdeck.Blue)
b.inModePortionImage = gc.DrawIconLabelButton(gc.LoadIconAsset(iconFile), b.label)
b.inModePortionImage = gc.DrawIconLabelButton(iconAsset, b.label)
}

func (b *SetFilterButton) Pressed() {
Expand Down
Loading