From 4af9834b69db14e89e5129151bfef24c687d91ea Mon Sep 17 00:00:00 2001 From: Jacalz Date: Wed, 11 Dec 2024 16:18:48 +0100 Subject: [PATCH] Optimize many of the commands --- remote/power.go | 14 +++++--------- remote/reset.go | 4 ++-- remote/source.go | 2 +- remote/volume.go | 14 +++++++------- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/remote/power.go b/remote/power.go index 9eb1892..73f232f 100644 --- a/remote/power.go +++ b/remote/power.go @@ -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 } @@ -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 } @@ -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 } diff --git a/remote/reset.go b/remote/reset.go index d3cf978..0206b35 100644 --- a/remote/reset.go +++ b/remote/reset.go @@ -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 } @@ -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 } diff --git a/remote/source.go b/remote/source.go index 1fe73a8..92c746b 100644 --- a/remote/source.go +++ b/remote/source.go @@ -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 } diff --git a/remote/volume.go b/remote/volume.go index 94b3639..1f710e9 100644 --- a/remote/volume.go +++ b/remote/volume.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 }