From 00f71fc3843312e3dbd5e5e4303391de852a5667 Mon Sep 17 00:00:00 2001 From: Florian Thienel Date: Tue, 19 Dec 2023 11:29:36 +0100 Subject: [PATCH] add bandwidth and icon parameters to hamlib.SetMode --- pkg/hamlib/buttons.go | 49 ++++++++++++++++++++++++++++++++++++------- pkg/hamlib/client.go | 20 ++++++++++++++++++ pkg/hamlib/factory.go | 12 ++++++++++- pkg/tci/buttons.go | 7 ++++--- 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/pkg/hamlib/buttons.go b/pkg/hamlib/buttons.go index 990f948..dc028a6 100644 --- a/pkg/hamlib/buttons.go +++ b/pkg/hamlib/buttons.go @@ -4,6 +4,7 @@ import ( "image" "log" + "github.com/ftl/hamradio" "github.com/ftl/hamradio/bandplan" "github.com/ftl/rigproxy/pkg/client" @@ -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) @@ -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 @@ -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 } @@ -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 } @@ -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() { @@ -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) } diff --git a/pkg/hamlib/client.go b/pkg/hamlib/client.go index 47686b3..4fbb4fc 100644 --- a/pkg/hamlib/client.go +++ b/pkg/hamlib/client.go @@ -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) } @@ -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) { diff --git a/pkg/hamlib/factory.go b/pkg/hamlib/factory.go index fe86cc2..790c4a3 100644 --- a/pkg/hamlib/factory.go +++ b/pkg/hamlib/factory.go @@ -19,6 +19,8 @@ const ( ConfigLabel1 = "label1" ConfigMode2 = "mode2" ConfigLabel2 = "label2" + ConfigIcon = "icon" + ConfigBandwidth = "bandwidth" ConfigBand = "band" ConfigValue = "value" ConfigVFO = "vfo" @@ -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) @@ -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 { diff --git a/pkg/tci/buttons.go b/pkg/tci/buttons.go index d1bfc5d..d9a1c81 100644 --- a/pkg/tci/buttons.go +++ b/pkg/tci/buttons.go @@ -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() {