Skip to content

Commit

Permalink
Merge pull request #4 from ftl/3-add-support-for-multiple-connections
Browse files Browse the repository at this point in the history
3 add support for multiple connections
  • Loading branch information
ftl authored Dec 10, 2023
2 parents 4a82b24 + 6acc4e1 commit 94e5070
Show file tree
Hide file tree
Showing 10 changed files with 551 additions and 87 deletions.
12 changes: 3 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,9 @@ func run(cmd *cobra.Command, args []string) {
deck := hamdeck.New(device)
deck.RegisterFactory(hamdeck.NewButtonFactory(deck))
deck.RegisterFactory(pulse.NewButtonFactory())
if rootFlags.hamlibAddress != "" {
deck.RegisterFactory(hamlib.NewButtonFactory(rootFlags.hamlibAddress))
}
if rootFlags.tciAddress != "" {
deck.RegisterFactory(tci.NewButtonFactory(rootFlags.tciAddress))
}
if rootFlags.mqttAddress != "" {
deck.RegisterFactory(mqtt.NewButtonFactory(rootFlags.mqttAddress, rootFlags.mqttUsername, rootFlags.mqttPassword))
}
deck.RegisterFactory(hamlib.NewButtonFactory(deck, rootFlags.hamlibAddress))
deck.RegisterFactory(tci.NewButtonFactory(deck, rootFlags.tciAddress))
deck.RegisterFactory(mqtt.NewButtonFactory(deck, rootFlags.mqttAddress, rootFlags.mqttUsername, rootFlags.mqttPassword))

err = configureHamDeck(deck, rootFlags.configFile)
if err != nil {
Expand Down
140 changes: 109 additions & 31 deletions example_conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,114 +34,182 @@
{
"type": "hamlib.SwitchToBand",
"index": 0,
"band": "80m"
"band": "80m",
"connection": "hamlib1"
},
{
"type": "hamlib.SwitchToBand",
"index": 1,
"band": "40m"
"band": "40m",
"connection": "hamlib1"
},
{
"type": "hamlib.SwitchToBand",
"index": 2,
"band": "30m"
"band": "30m",
"connection": "hamlib1"
},
{
"type": "hamlib.SwitchToBand",
"index": 3,
"band": "20m"
"band": "20m",
"connection": "hamlib1"
},
{
"type": "hamlib.SwitchToBand",
"index": 4,
"band": "17m"
"band": "17m",
"connection": "hamlib1"
},
{
"type": "hamlib.SwitchToBand",
"index": 5,
"band": "15m"
"band": "15m",
"connection": "hamlib1"
},
{
"type": "hamlib.SwitchToBand",
"index": 6,
"band": "12m"
"band": "12m",
"connection": "hamlib1"
},
{
"type": "hamlib.SwitchToBand",
"index": 7,
"band": "10m"
"band": "10m",
"connection": "hamlib1"
},
{
"type": "hamlib.SetMode",
"type": "hamlib.SwitchToBand",
"index": 8,
"mode": "CW"
"band": "80m",
"connection": "hamlib2"
},
{
"type": "hamlib.SetMode",
"type": "hamlib.SwitchToBand",
"index": 9,
"band": "40m",
"connection": "hamlib2"
},
{
"type": "hamlib.SwitchToBand",
"index": 10,
"band": "30m",
"connection": "hamlib2"
},
{
"type": "hamlib.SwitchToBand",
"index": 11,
"band": "20m",
"connection": "hamlib2"
},
{
"type": "hamlib.SwitchToBand",
"index": 12,
"band": "17m",
"connection": "hamlib2"
},
{
"type": "hamlib.SwitchToBand",
"index": 13,
"band": "15m",
"connection": "hamlib2"
},
{
"type": "hamlib.SwitchToBand",
"index": 14,
"band": "12m",
"connection": "hamlib2"
},
{
"type": "hamlib.SwitchToBand",
"index": 15,
"band": "10m",
"connection": "hamlib2"
},
{
"type": "hamlib.SetMode",
"index": 16,
"mode": "CW",
"connection": "hamlib1"
},
{
"type": "hamlib.SetMode",
"index": 17,
"mode": "PKTUSB",
"label": "Data"
"label": "Data",
"connection": "hamlib1"
},
{
"type": "hamlib.ToggleMode",
"index": 10,
"index": 18,
"mode1": "LSB",
"mode2": "USB"
"mode2": "USB",
"connection": "hamlib1"
},
{
"type": "hamlib.Set",
"index": 11,
"index": 19,
"label": "Band▾",
"command": "vfo_op",
"args": ["BAND_DOWN"]
"args": ["BAND_DOWN"],
"connection": "hamlib1"
},
{
"type": "hamlib.Set",
"index": 12,
"index": 20,
"label": "Band▴",
"command": "vfo_op",
"args": ["BAND_UP"]
"args": ["BAND_UP"],
"connection": "hamlib1"
},
{
"type": "hamlib.SetPowerLevel",
"index": 13,
"index": 21,
"label": "10W",
"value": 0.1
"value": 0.1,
"connection": "hamlib1"
},
{
"type": "hamlib.SetPowerLevel",
"index": 14,
"index": 22,
"label": "50W",
"value": 0.5
"value": 0.5,
"connection": "hamlib1"
},
{
"type": "hamlib.SetPowerLevel",
"index": 15,
"index": 23,
"label": "100W",
"value": 1.0
"value": 1.0,
"connection": "hamlib1"
},
{
"type": "hamlib.SetVFO",
"index": 16,
"index": 24,
"label": "VFO A",
"vfo": "VFOA"
"vfo": "VFOA",
"connection": "hamlib1"
},
{
"type": "hamlib.SetVFO",
"index": 17,
"index": 25,
"label": "VFO B",
"vfo": "VFOB"
"vfo": "VFOB",
"connection": "hamlib1"
},
{
"type": "hamlib.Set",
"index": 18,
"index": 26,
"label": "A=B",
"command": "vfo_op",
"args": ["CPY"]
"args": ["CPY"],
"connection": "hamlib1"
},
{
"type": "hamlib.MOX",
"index": 20
"index": 27,
"connection": "hamlib1"
},
{
"type": "hamdeck.Page",
Expand Down Expand Up @@ -271,5 +339,15 @@
}
]
}
},
"connections": {
"hamlib1": {
"type": "hamlib",
"address": "localhost:4533"
},
"hamlib2": {
"type": "hamlib",
"address": "localhost:4534"
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/fogleman/gg v1.3.0
github.com/ftl/hamradio v0.2.7
github.com/ftl/rigproxy v0.2.4
github.com/ftl/rigproxy v0.2.5
github.com/ftl/tci v0.3.2
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/jfreymuth/pulse v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
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/rigproxy v0.2.4 h1:8g9zfYX049Wd0fa1J6WujQ4wMPiAquXO286SVKNf57k=
github.com/ftl/rigproxy v0.2.4/go.mod h1:PrBUiqLwu/6zL44+uOz4lgmOfnis4FIvJDhxDNXoi60=
github.com/ftl/rigproxy v0.2.5 h1:q5b734nvJia/yNzrVOudsMkPJ58QwNwW2FD9r2QxepA=
github.com/ftl/rigproxy v0.2.5/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/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
Expand Down
38 changes: 37 additions & 1 deletion pkg/hamdeck/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import (
const (
ConfigDefaultFilename = "hamdeck.json"
ConfigMainKey = "hamdeck"
ConfigConnections = "connections"
ConfigStartPageID = "start_page"
ConfigPages = "pages"
ConfigButtons = "buttons"
ConfigType = "type"
ConfigIndex = "index"
ConfigConnection = "connection"
)

func (d *HamDeck) ReadConfig(r io.Reader) error {
Expand All @@ -40,12 +42,21 @@ func (d *HamDeck) ReadConfig(r io.Reader) error {
effectiveConfiguration := findEffectiveConfiguration(configuration)

d.buttonsPerFactory = make([]int, len(d.factories))
d.connections = make(map[connectionKey]ConnectionConfig)
d.pages = make(map[string]Page)

connections, ok := (effectiveConfiguration[ConfigConnections]).(map[string]any)
if ok {
err = d.loadConnections(connections)
}
if err != nil {
return err
}

d.startPageID, ok = effectiveConfiguration[ConfigStartPageID].(string)
if !ok {
d.startPageID = legacyPageID
}

pages, ok := effectiveConfiguration[ConfigPages].(map[string]any)
if ok {
err = d.loadPages(pages)
Expand All @@ -57,6 +68,8 @@ func (d *HamDeck) ReadConfig(r io.Reader) error {
buttons, ok := effectiveConfiguration[ConfigButtons].([]any)
if ok {
err = d.loadLegacyPage(buttons)
} else if len(d.pages) == 0 {
d.loadEmptyLegacyPage()
}
if err != nil {
return err
Expand All @@ -78,6 +91,23 @@ func findEffectiveConfiguration(configuration map[string]any) map[string]any {
return subconfiguration
}

func (d *HamDeck) loadConnections(configuration map[string]any) error {
for name, config := range configuration {
connection, ok := config.(map[string]any)
if !ok {
log.Printf("%s is not a valid connection configuration", name)
continue
}
connectionType, ok := ToString(connection[ConfigType])
if !ok {
log.Printf("connection %s needs a type", name)
continue
}
d.connections[connectionKey{name, connectionType}] = ConnectionConfig(connection)
}
return nil
}

func (d *HamDeck) loadPages(configuration map[string]any) error {
for id, rawPage := range configuration {
pageConfiguration, ok := rawPage.(map[string]any)
Expand Down Expand Up @@ -123,6 +153,12 @@ func (d *HamDeck) loadLegacyPage(configuration []any) error {
return nil
}

func (d *HamDeck) loadEmptyLegacyPage() {
d.pages[legacyPageID] = Page{
buttons: make([]Button, len(d.buttons)),
}
}

func (d *HamDeck) loadButtons(configuration []any) ([]Button, error) {
result := make([]Button, len(d.buttons))
for i, rawButtonConfig := range configuration {
Expand Down
Loading

0 comments on commit 94e5070

Please sign in to comment.