From 9ade2a684566343ae5c1a8559b3931962b04e4c2 Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Thu, 16 May 2024 22:12:58 -0500 Subject: [PATCH 1/2] fix: use correct command format --- src/Rs232Commands.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rs232Commands.cs b/src/Rs232Commands.cs index a047fc7..8ce519e 100644 --- a/src/Rs232Commands.cs +++ b/src/Rs232Commands.cs @@ -25,7 +25,7 @@ public static class Rs232Commands public static readonly byte[] VolumeUp = { 0x8C, 0x00, 0x05, 0x03, 0x00, 0x00 }; public static readonly byte[] VolumeDown = { 0x8C, 0x00, 0x05, 0x03, 0x00, 0x01 }; - public static readonly byte[] VolumeDirect = { 0x83, 0x00, 0x05, 0x03, 0x01, 0x00 }; //reset byte[5] to actual volume level + public static readonly byte[] VolumeDirect = { 0x8C, 0x00, 0x05, 0x03, 0x01, 0x00 }; //reset byte[5] to actual volume level public static readonly byte[] MuteOn = { 0x8C, 0x00, 0x06, 0x03, 0x01, 0x01}; public static readonly byte[] MuteOff = { 0x8C, 0x00, 0x06, 0x03, 0x01, 0x00 }; From 2482e48021586d43ab8e0f036129d0f30c8cd5d4 Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Thu, 16 May 2024 22:13:14 -0500 Subject: [PATCH 2/2] feat: volume control works --- src/SonyBraviaDevice.cs | 91 +++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 18 deletions(-) diff --git a/src/SonyBraviaDevice.cs b/src/SonyBraviaDevice.cs index df7b942..ff24e87 100644 --- a/src/SonyBraviaDevice.cs +++ b/src/SonyBraviaDevice.cs @@ -1122,6 +1122,32 @@ public void SetVolume(ushort level) _pollTimer.Reset(1000, pollTime); } + private void SetVolume(int level, bool resetPoll = false) + { + Debug.Console(2, this, "Input level: {0}", level); + + var volumeCommand = Rs232Commands.VolumeDirect; + + volumeCommand[5] = (byte)level; + + if (_comsIsRs232) + { + var command = volumeCommand.WithChecksum(); + _lastCommand = command; + _coms.SendBytes(command); + + if (resetPoll) + { + _pollTimer.Reset(1000, pollTime); + } + + return; + } + + CommandQueue.Enqueue(SimpleIpCommands.GetControlCommand(_coms, "VOLM", level)); + _pollTimer.Reset(1000, pollTime); + } + public void VolumeUp(bool pressRelease) { if (!pressRelease) @@ -1133,10 +1159,6 @@ public void VolumeUp(bool pressRelease) _volumeTimer = null; } - var command = Rs232Commands.VolumeQuery.WithChecksum(); - _lastCommand = command; - _coms.SendBytes(command); - _pollTimer.Reset(1000, pollTime); return; } @@ -1148,10 +1170,28 @@ public void VolumeUp(bool pressRelease) _volumeCounter = 0; _volumeTimer = new CTimer(o => { - var command = _volumeCounter % 2 == 0 ? Rs232Commands.VolumeUp.WithChecksum() : Rs232Commands.VolumeQuery.WithChecksum(); - _lastCommand = command; - Debug.LogMessage(Serilog.Events.LogEventLevel.Information, _volumeCounter % 2 == 0 ? "Sending Volume Up command {command}" : "Sending Volume Query command {command}", this, ComTextHelper.GetEscapedText(command)); - _coms.SendBytes(command); + this.LogVerbose("rawVolume: {raw:X2} maxVolume: {max:X2}", _rawVolume, maxVolumeLevel); + + if (_rawVolume > maxVolumeLevel) return; + + int increment = 1; + + if (_volumeCounter > 4) + { + increment = 2; + } + + if (_volumeCounter > 16) + { + increment = 4; + } + + _rawVolume += increment; + + SetVolume(_rawVolume); + + VolumeLevelFeedback.FireUpdate(); + _volumeCounter += 1; }, null, 0, 500); @@ -1168,11 +1208,7 @@ public void VolumeDown(bool pressRelease) _volumeTimer.Stop(); _volumeTimer.Dispose(); _volumeTimer = null; - } - - var command = Rs232Commands.VolumeQuery.WithChecksum(); - _lastCommand = command; - _coms.SendBytes(command); + } _pollTimer.Reset(1000, pollTime); return; @@ -1183,13 +1219,32 @@ public void VolumeDown(bool pressRelease) _pollTimer.Stop(); _volumeCounter = 0; - + _volumeTimer = new CTimer(o => { - var command = _volumeCounter % 2 == 0 ? Rs232Commands.VolumeDown.WithChecksum() : Rs232Commands.VolumeQuery.WithChecksum(); - _lastCommand = command; - Debug.LogMessage(Serilog.Events.LogEventLevel.Information, _volumeCounter % 2 == 0 ? "Sending Volume Down command {command}" : "Sending Volume Query command {command}", this, ComTextHelper.GetEscapedText(command)); - _coms.SendBytes(command); + this.LogVerbose("rawVolume: {raw:X2} maxVolume: {max:X2}", _rawVolume, maxVolumeLevel); + + if (_rawVolume <= 0) return; + + int increment = 1; + + if(_volumeCounter > 4) + { + increment = 2; + } + + if(_volumeCounter > 16) + { + increment = 4; + } + + _rawVolume -= increment; + + SetVolume(_rawVolume); + + VolumeLevelFeedback.FireUpdate(); + _volumeCounter += 1; + }, null, 0, 500); return;