Skip to content

Commit

Permalink
Optimize many of the commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Dec 11, 2024
1 parent 64a40e4 commit 4af9834
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
14 changes: 5 additions & 9 deletions remote/power.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package remote

import "fmt"

const powerCommand = "-p.%s\r"

// SetPower sets the power to either on or off.
func (c *Control) SetPower(on bool) error {
state := "0"
packet := []byte("-p.0\r")
if on {
state = "1"
packet[3] = '1'
}

_, err := fmt.Fprintf(c.conn, powerCommand, state)
_, err := c.conn.Write(packet)
if err != nil {
return err
}
Expand All @@ -21,7 +17,7 @@ func (c *Control) SetPower(on bool) error {

// TogglePower toggles the power on and off.
func (c *Control) TogglePower() error {
_, err := fmt.Fprintf(c.conn, powerCommand, "t")
_, err := c.conn.Write([]byte("-p.t\r"))
if err != nil {
return err
}
Expand All @@ -31,7 +27,7 @@ func (c *Control) TogglePower() error {

// GetPower returns the current power status.
func (c *Control) GetPower() (bool, error) {
_, err := fmt.Fprintf(c.conn, powerCommand, "?")
_, err := c.conn.Write([]byte("-p.?\r"))
if err != nil {
return false, err
}
Expand Down
4 changes: 2 additions & 2 deletions remote/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c *Control) SetResetDelay(delay uint8) error {

// StopResetDelay stops the delayed reset from happening.
func (c *Control) StopResetDelay() error {
_, err := fmt.Fprintf(c.conn, commandFormat, "r", "~")
_, err := c.conn.Write([]byte("-r.~\r"))
if err != nil {
return err
}
Expand All @@ -29,7 +29,7 @@ func (c *Control) StopResetDelay() error {
// GetResetDelay returns the current delay until reset.
// Returns the delay or a bool indicating if it is stopped or not.
func (c *Control) GetResetDelay() (uint8, bool, error) {
_, err := fmt.Fprintf(c.conn, commandFormat, "r", "?")
_, err := c.conn.Write([]byte("-r.?\r"))
if err != nil {
return 0, false, err
}
Expand Down
2 changes: 1 addition & 1 deletion remote/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *Control) GetSourceName(amp device.Device) (string, error) {

// GetSourceNumber returns the currently selected source number.
func (c *Control) GetSourceNumber() (uint, error) {
_, err := fmt.Fprintf(c.conn, commandFormat, "i", "?")
_, err := c.conn.Write([]byte("-i.?\r"))
if err != nil {
return 0, err
}
Expand Down
14 changes: 7 additions & 7 deletions remote/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *Control) SetVolume(percentage uint8) error {

// VolumeUp increases the volume one step.
func (c *Control) VolumeUp() error {
_, err := fmt.Fprintf(c.conn, commandFormat, "v", "u")
_, err := c.conn.Write([]byte("-v.u\r"))
if err != nil {
return err
}
Expand All @@ -35,7 +35,7 @@ func (c *Control) VolumeUp() error {

// VolumeDown decreases the volume one step.
func (c *Control) VolumeDown() error {
_, err := fmt.Fprintf(c.conn, commandFormat, "v", "d")
_, err := c.conn.Write([]byte("-v.d\r"))
if err != nil {
return err
}
Expand All @@ -45,7 +45,7 @@ func (c *Control) VolumeDown() error {

// GetVolume returns the currrently selected volume percentage.
func (c *Control) GetVolume() (uint, error) {
_, err := fmt.Fprintf(c.conn, commandFormat, "v", "?")
_, err := c.conn.Write([]byte("-v.?\r"))
if err != nil {
return 0, err
}
Expand All @@ -68,12 +68,12 @@ func (c *Control) GetVolume() (uint, error) {

// SetVolumeMute allows turning on or off mute.
func (c *Control) SetVolumeMute(mute bool) error {
state := "0"
packet := []byte("-m.0\r")
if mute {
state = "1"
packet[3] = '1'
}

_, err := fmt.Fprintf(c.conn, commandFormat, "m", state)
_, err := c.conn.Write(packet)
if err != nil {
return err
}
Expand All @@ -83,7 +83,7 @@ func (c *Control) SetVolumeMute(mute bool) error {

// GetVolumeMute returns true if the device is muted.
func (c *Control) GetVolumeMute() (bool, error) {
_, err := fmt.Fprintf(c.conn, commandFormat, "m", "?")
_, err := c.conn.Write([]byte("-m.?\r"))
if err != nil {
return false, err
}
Expand Down

0 comments on commit 4af9834

Please sign in to comment.