Skip to content

Commit

Permalink
add a new button type to switch pages
Browse files Browse the repository at this point in the history
  • Loading branch information
ftl committed Dec 9, 2023
1 parent a9afcb5 commit 495a02d
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func run(cmd *cobra.Command, args []string) {
device.SetBrightness(rootFlags.brightness)

deck := hamdeck.New(device)
deck.RegisterFactory(hamdeck.NewButtonFactory(deck))
deck.RegisterFactory(pulse.NewButtonFactory())
if rootFlags.hamlibAddress != "" {
deck.RegisterFactory(hamlib.NewButtonFactory(rootFlags.hamlibAddress))
Expand Down
41 changes: 41 additions & 0 deletions pkg/hamdeck/buttons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package hamdeck

import "image"

/*
PageButton
*/

type PageButton struct {
BaseButton
image image.Image

pageSwitcher PageSwitcher
id string
label string
}

func NewPageButton(pageSwitcher PageSwitcher, id string, label string) *PageButton {
return &PageButton{
pageSwitcher: pageSwitcher,
id: id,
label: label,
}
}

func (b *PageButton) Image(gc GraphicContext, redrawImages bool) image.Image {
if b.image == nil || redrawImages {
gc.SetForeground(White)
gc.SetBackground(Black)
b.image = gc.DrawSingleLineTextButton(b.label)
}
return b.image
}

func (b *PageButton) Pressed() {
b.pageSwitcher.AttachPage(b.id)
}

func (b *PageButton) Released() {
// nop
}
2 changes: 1 addition & 1 deletion pkg/hamdeck/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (d *HamDeck) loadButtons(configuration []any) ([]Button, error) {
log.Printf("buttons[%d] has no valid index", i)
continue
}
if buttonIndex <= 0 || buttonIndex >= len(d.buttons) {
if buttonIndex < 0 || buttonIndex >= len(d.buttons) {
log.Printf("%d is not a valid button index in [0, %d])", buttonIndex, len(d.buttons))
}

Expand Down
51 changes: 51 additions & 0 deletions pkg/hamdeck/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package hamdeck

import "log"

const (
ConfigPage = "page"
ConfigLabel = "label"
)

const (
PageButtonType = "hamdeck.Page"
)

type Factory struct {
pageSwitcher PageSwitcher
}

type PageSwitcher interface {
AttachPage(string) error
}

func NewButtonFactory(pageSwitcher PageSwitcher) *Factory {
return &Factory{
pageSwitcher: pageSwitcher,
}
}

func (f *Factory) Close() {
// nop
}

func (f *Factory) CreateButton(config map[string]any) Button {
switch config[ConfigType] {
case PageButtonType:
return f.createPageButton(config)
default:
return nil
}
}

func (f *Factory) createPageButton(config map[string]any) Button {
id, haveID := ToString(config[ConfigPage])
label, haveLabel := ToString(config[ConfigLabel])
if !haveID {
log.Print("A hamdeck.Page button must have a page field.")
}
if !haveLabel {
log.Print("A hamdeck.Page button must have a label field.")
}
return NewPageButton(f.pageSwitcher, id, label)
}
2 changes: 2 additions & 0 deletions pkg/hamdeck/hamdeck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func runWithConfigFile(t *testing.T, filename string, f func(*testing.T, *HamDec
device := newDefaultTestDevice()
deck := New(device)
deck.RegisterFactory(new(testButtonFactory))
deck.RegisterFactory(NewButtonFactory(deck))

reader, err := openTestConfigFile(filename)
require.NoError(t, err)
Expand Down Expand Up @@ -70,6 +71,7 @@ func runWithConfigString(t *testing.T, config string, f func(*testing.T, *HamDec
device := newDefaultTestDevice()
deck := New(device)
deck.RegisterFactory(new(testButtonFactory))
deck.RegisterFactory(NewButtonFactory(deck))

reader, err := openTestConfigString(config)
require.NoError(t, err)
Expand Down
40 changes: 40 additions & 0 deletions pkg/hamdeck/pages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,43 @@ func TestAttachPage(t *testing.T) {
assert.Same(t, deck.noButton, deck.buttons[1])
})
}

func TestPageButton(t *testing.T) {
runWithConfigString(t, `{
"pages": {
"main": {
"buttons": [
{ "type": "hamdeck.Page", "index": 0, "page": "", "label": "Back" }
]
}
},
"buttons": [
{ "type": "hamdeck.Page", "index": 0, "page": "main", "label": "Main" }
]
}`, func(t *testing.T, deck *HamDeck, device *testDevice, _ chan struct{}) {
assert.Equal(t, legacyPageID, deck.startPageID)
assert.Equal(t, 2, len(deck.pages))

mainPage := deck.pages["main"]
require.Equal(t, len(deck.buttons), len(mainPage.buttons))
mainButton, ok := mainPage.buttons[0].(*PageButton)
require.True(t, ok)
assert.Equal(t, "Back", mainButton.label)

legacyPage := deck.pages[legacyPageID]
require.Equal(t, len(deck.buttons), len(legacyPage.buttons))
legacyButton, ok := legacyPage.buttons[0].(*PageButton)
require.True(t, ok)
assert.Equal(t, "Main", legacyButton.label)

assert.Same(t, legacyButton, deck.buttons[0])

device.Press(0)
device.WaitForLastKey()
assert.Same(t, mainButton, deck.buttons[0])

device.Press(0)
device.WaitForLastKey()
assert.Same(t, legacyButton, deck.buttons[0])
})
}
54 changes: 54 additions & 0 deletions pkg/hamdeck/testdata/testEightPages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"buttons": [
{"type": "hamdeck.Page", "index": 0, "page": "1", "label": "1"},
{"type": "hamdeck.Page", "index": 1, "page": "2", "label": "2"},
{"type": "hamdeck.Page", "index": 2, "page": "3", "label": "3"},
{"type": "hamdeck.Page", "index": 3, "page": "4", "label": "4"},
{"type": "hamdeck.Page", "index": 4, "page": "5", "label": "5"},
{"type": "hamdeck.Page", "index": 5, "page": "6", "label": "6"},
{"type": "hamdeck.Page", "index": 6, "page": "7", "label": "7"},
{"type": "hamdeck.Page", "index": 7, "page": "8", "label": "8"}
],
"pages": {
"1": {
"buttons": [
{"type": "hamdeck.Page", "index": 0, "page": "", "label": "Back"}
]
},
"2": {
"buttons": [
{"type": "hamdeck.Page", "index": 1, "page": "", "label": "Back"}
]
},
"3": {
"buttons": [
{"type": "hamdeck.Page", "index": 2, "page": "", "label": "Back"}
]
},
"4": {
"buttons": [
{"type": "hamdeck.Page", "index": 3, "page": "", "label": "Back"}
]
},
"5": {
"buttons": [
{"type": "hamdeck.Page", "index": 4, "page": "", "label": "Back"}
]
},
"6": {
"buttons": [
{"type": "hamdeck.Page", "index": 5, "page": "", "label": "Back"}
]
},
"7": {
"buttons": [
{"type": "hamdeck.Page", "index": 6, "page": "", "label": "Back"}
]
},
"8": {
"buttons": [
{"type": "hamdeck.Page", "index": 7, "page": "", "label": "Back"}
]
}
}
}

0 comments on commit 495a02d

Please sign in to comment.