diff --git a/src/Rs232Commands.cs b/src/Rs232Commands.cs index 8c201ef..788c372 100644 --- a/src/Rs232Commands.cs +++ b/src/Rs232Commands.cs @@ -20,7 +20,14 @@ public static class Rs232Commands public static readonly byte[] InputHdmi3 = {0x8C, 0x00, 0x02, 0x03, 0x04, 0x03}; public static readonly byte[] InputHdmi4 = {0x8C, 0x00, 0x02, 0x03, 0x04, 0x04}; public static readonly byte[] InputHdmi5 = {0x8C, 0x00, 0x02, 0x03, 0x04, 0x05}; - public static readonly byte[] InputPc1 = {0x8C, 0x00, 0x02, 0x03, 0x05, 0x01}; + public static readonly byte[] InputPc1 = {0x8C, 0x00, 0x02, 0x03, 0x05, 0x01}; + + 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[] MuteOn = { 0x8C, 0x00, 0x06, 0x03, 0x01, 0x01}; + public static readonly byte[] MuteOff = { 0x8C, 0x00, 0x06, 0x03, 0x01, 0x00 }; public static readonly byte[] PowerQuery = { 0x83, 0x00, 0x00, 0xFF, 0xFF }; public static readonly byte[] InputQuery = { 0x83, 0x00, 0x02, 0xFF, 0xFF }; @@ -49,11 +56,52 @@ public static IQueueMessage GetVolumeQuery(IBasicCommunication coms, Action action) + { + return new Rs232Command(coms, VolumeUp.WithChecksum(), action, eCommandType.Command); + } + + public static IQueueMessage GetVolumeDown(IBasicCommunication coms, Action action) + { + return new Rs232Command(coms, VolumeDown.WithChecksum(), action, eCommandType.Command); + } + + public static IQueueMessage GetVolumeDirect(IBasicCommunication coms, Action action, int volume) + { + if(volume < 0 ) + { + VolumeDirect[5] = 0x00; + + return new Rs232Command(coms, VolumeDirect.WithChecksum(), action, eCommandType.Command); + } + + if (volume > 255) + { + VolumeDirect[5] = 0xFF; + + return new Rs232Command(coms, VolumeDirect.WithChecksum(), action, eCommandType.Command); + } + + VolumeDirect[5] = (byte) volume; + + return new Rs232Command(coms, VolumeDirect.WithChecksum(), action, eCommandType.Command); + } + public static IQueueMessage GetMuteQuery(IBasicCommunication coms, Action action) { return new Rs232Command(coms, MuteQuery.WithChecksum(), action, eCommandType.MuteQuery); } + public static IQueueMessage GetMuteOn(IBasicCommunication coms, Action action) + { + return new Rs232Command(coms, MuteOn.WithChecksum(), action, eCommandType.Command); + } + + public static IQueueMessage GetMuteOff(IBasicCommunication coms, Action action) + { + return new Rs232Command(coms, MuteOff.WithChecksum(), action, eCommandType.Command); + } + public static IQueueMessage GetPowerOn(IBasicCommunication coms, Action action) { return new Rs232Command(coms, PowerOn.WithChecksum(), action, eCommandType.Command); diff --git a/src/SonyBraviaDevice.cs b/src/SonyBraviaDevice.cs index 8d976a8..aa4f04e 100644 --- a/src/SonyBraviaDevice.cs +++ b/src/SonyBraviaDevice.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text.RegularExpressions; using Crestron.SimplSharp; +using Crestron.SimplSharpPro.CrestronConnected; using Crestron.SimplSharpPro.CrestronThread; using Crestron.SimplSharpPro.DeviceSupport; using PepperDash.Core; @@ -21,7 +22,8 @@ namespace SonyBraviaEpi { public class SonyBraviaDevice : TwoWayDisplayBase, ICommunicationMonitor, IBridgeAdvanced, IInputHdmi1, IInputHdmi2, IInputHdmi3, IInputHdmi4, IInputVga1, - IOnline + IOnline, + IBasicVolumeWithFeedback #if SERIES4 , IHasInputs #endif @@ -125,6 +127,9 @@ public SonyBraviaDevice(DeviceConfig config, IBasicCommunication comms) _pollTimer = new CTimer((o) => Poll(new List { powerQuery, inputQuery, muteQuery, volumeQuery }),Timeout.Infinite); + MuteFeedback = new BoolFeedback(() => _muted); + VolumeLevelFeedback = new IntFeedback(() => CrestronEnvironment.ScaleWithLimits(_rawVolume, 255, 0, 65535, 0)); + CrestronEnvironment.ProgramStatusEventHandler += type => { try @@ -262,6 +267,13 @@ public void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, E #if SERIES4 public ISelectableItems Inputs { get; private set; } #endif + private bool _muted; + + private int _rawVolume; + + public BoolFeedback MuteFeedback { get; private set; } + + public IntFeedback VolumeLevelFeedback { get; private set; } /// /// Poll device @@ -939,5 +951,53 @@ public void SetInput(string selector) } } #endif + public void MuteOn() + { + CommandQueue.Enqueue(_comsIsRs232 + ? Rs232Commands.GetMuteOn(_coms, UpdateLastSentCommandType) + : null); + } + + public void MuteOff() + { + CommandQueue.Enqueue(_comsIsRs232 + ? Rs232Commands.GetMuteOff(_coms, UpdateLastSentCommandType) + : null); + } + public void MuteToggle() + { + if (_muted) + { + MuteOff(); + return; + } + + MuteOn(); + } + + public void SetVolume(ushort level) + { + var scaledVolume = CrestronEnvironment.ScaleWithLimits(level, 65535, 0, 255, 0); + + CommandQueue.Enqueue(_comsIsRs232 + ? Rs232Commands.GetVolumeDirect(_coms, UpdateLastSentCommandType, scaledVolume) + : null); + } + + public void VolumeUp(bool pressRelease) + { + CommandQueue.Enqueue(_comsIsRs232 + ? Rs232Commands.GetVolumeUp(_coms, UpdateLastSentCommandType) + : null); + } + + public void VolumeDown(bool pressRelease) + { + CommandQueue.Enqueue(_comsIsRs232 + ? Rs232Commands.GetVolumeDown(_coms, UpdateLastSentCommandType) + : null); + } + + } } \ No newline at end of file