mirror of
https://github.com/onyx-and-iris/xair-cli.git
synced 2026-04-09 02:13:35 +00:00
DRY up the factory methods
use optional functions to set address functions
This commit is contained in:
@@ -2,22 +2,31 @@ package xair
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Gate represents the gate parameters.
|
||||
type Gate struct {
|
||||
client *Client
|
||||
baseAddress string
|
||||
AddressFunc func(fmtString string, args ...any) string
|
||||
}
|
||||
|
||||
// Factory function to create Gate instance for Strip
|
||||
func newGateForStrip(c *Client, baseAddress string) *Gate {
|
||||
return &Gate{
|
||||
// Factory function to create Gate instance with optional configuration
|
||||
func newGate(c *Client, baseAddress string, opts ...GateOption) *Gate {
|
||||
gate := &Gate{
|
||||
client: c,
|
||||
baseAddress: fmt.Sprintf("%s/gate", baseAddress),
|
||||
AddressFunc: fmt.Sprintf,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(gate)
|
||||
}
|
||||
|
||||
return gate
|
||||
}
|
||||
|
||||
// On retrieves the on/off status of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) On(index int) (bool, error) {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/on"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/on"
|
||||
err := g.client.SendMessage(address)
|
||||
if err != nil {
|
||||
return false, err
|
||||
@@ -36,7 +45,7 @@ func (g *Gate) On(index int) (bool, error) {
|
||||
|
||||
// SetOn sets the on/off status of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) SetOn(index int, on bool) error {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/on"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/on"
|
||||
var value int32
|
||||
if on {
|
||||
value = 1
|
||||
@@ -46,7 +55,7 @@ func (g *Gate) SetOn(index int, on bool) error {
|
||||
|
||||
// Mode retrieves the current mode of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) Mode(index int) (string, error) {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/mode"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/mode"
|
||||
err := g.client.SendMessage(address)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -67,7 +76,7 @@ func (g *Gate) Mode(index int) (string, error) {
|
||||
|
||||
// SetMode sets the mode of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) SetMode(index int, mode string) error {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/mode"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/mode"
|
||||
possibleModes := []string{"exp2", "exp3", "exp4", "gate", "duck"}
|
||||
|
||||
return g.client.SendMessage(address, int32(indexOf(possibleModes, mode)))
|
||||
@@ -75,7 +84,7 @@ func (g *Gate) SetMode(index int, mode string) error {
|
||||
|
||||
// Threshold retrieves the threshold value of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) Threshold(index int) (float64, error) {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/thr"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/thr"
|
||||
err := g.client.SendMessage(address)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -94,13 +103,13 @@ func (g *Gate) Threshold(index int) (float64, error) {
|
||||
|
||||
// SetThreshold sets the threshold value of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) SetThreshold(index int, threshold float64) error {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/thr"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/thr"
|
||||
return g.client.SendMessage(address, float32(linSet(-80, 0, threshold)))
|
||||
}
|
||||
|
||||
// Range retrieves the range value of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) Range(index int) (float64, error) {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/range"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/range"
|
||||
err := g.client.SendMessage(address)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -119,13 +128,13 @@ func (g *Gate) Range(index int) (float64, error) {
|
||||
|
||||
// SetRange sets the range value of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) SetRange(index int, rangeVal float64) error {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/range"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/range"
|
||||
return g.client.SendMessage(address, float32(linSet(3, 60, rangeVal)))
|
||||
}
|
||||
|
||||
// Attack retrieves the attack time of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) Attack(index int) (float64, error) {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/attack"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/attack"
|
||||
err := g.client.SendMessage(address)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -144,13 +153,13 @@ func (g *Gate) Attack(index int) (float64, error) {
|
||||
|
||||
// SetAttack sets the attack time of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) SetAttack(index int, attack float64) error {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/attack"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/attack"
|
||||
return g.client.SendMessage(address, float32(linSet(0, 120, attack)))
|
||||
}
|
||||
|
||||
// Hold retrieves the hold time of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) Hold(index int) (float64, error) {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/hold"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/hold"
|
||||
err := g.client.SendMessage(address)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -169,13 +178,13 @@ func (g *Gate) Hold(index int) (float64, error) {
|
||||
|
||||
// SetHold sets the hold time of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) SetHold(index int, hold float64) error {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/hold"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/hold"
|
||||
return g.client.SendMessage(address, float32(logSet(0.02, 2000, hold)))
|
||||
}
|
||||
|
||||
// Release retrieves the release time of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) Release(index int) (float64, error) {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/release"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/release"
|
||||
err := g.client.SendMessage(address)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -194,6 +203,6 @@ func (g *Gate) Release(index int) (float64, error) {
|
||||
|
||||
// SetRelease sets the release time of the Gate for a specific strip (1-based indexing).
|
||||
func (g *Gate) SetRelease(index int, release float64) error {
|
||||
address := fmt.Sprintf(g.baseAddress, index) + "/release"
|
||||
address := g.AddressFunc(g.baseAddress, index) + "/release"
|
||||
return g.client.SendMessage(address, float32(logSet(5, 4000, release)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user