Skip to content

Commit

Permalink
feat: implement IBasicVolumeControlWithFeedback
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-welker committed May 1, 2024
1 parent dc95381 commit 43e4fff
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
50 changes: 49 additions & 1 deletion src/Rs232Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -49,11 +56,52 @@ public static IQueueMessage GetVolumeQuery(IBasicCommunication coms, Action<eCom
return new Rs232Command(coms, VolumeQuery.WithChecksum(), action, eCommandType.VolumeQuery);
}

public static IQueueMessage GetVolumeUp(IBasicCommunication coms, Action<eCommandType> action)
{
return new Rs232Command(coms, VolumeUp.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetVolumeDown(IBasicCommunication coms, Action<eCommandType> action)
{
return new Rs232Command(coms, VolumeDown.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetVolumeDirect(IBasicCommunication coms, Action<eCommandType> 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<eCommandType> action)
{
return new Rs232Command(coms, MuteQuery.WithChecksum(), action, eCommandType.MuteQuery);
}

public static IQueueMessage GetMuteOn(IBasicCommunication coms, Action<eCommandType> action)
{
return new Rs232Command(coms, MuteOn.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetMuteOff(IBasicCommunication coms, Action<eCommandType> action)
{
return new Rs232Command(coms, MuteOff.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetPowerOn(IBasicCommunication coms, Action<eCommandType> action)
{
return new Rs232Command(coms, PowerOn.WithChecksum(), action, eCommandType.Command);
Expand Down
62 changes: 61 additions & 1 deletion src/SonyBraviaDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +22,8 @@ namespace SonyBraviaEpi
{
public class SonyBraviaDevice : TwoWayDisplayBase, ICommunicationMonitor, IBridgeAdvanced,
IInputHdmi1, IInputHdmi2, IInputHdmi3, IInputHdmi4, IInputVga1,
IOnline
IOnline,
IBasicVolumeWithFeedback
#if SERIES4
, IHasInputs<string, string>
#endif
Expand Down Expand Up @@ -125,6 +127,9 @@ public SonyBraviaDevice(DeviceConfig config, IBasicCommunication comms)

_pollTimer = new CTimer((o) => Poll(new List<IQueueMessage> { powerQuery, inputQuery, muteQuery, volumeQuery }),Timeout.Infinite);

MuteFeedback = new BoolFeedback(() => _muted);
VolumeLevelFeedback = new IntFeedback(() => CrestronEnvironment.ScaleWithLimits(_rawVolume, 255, 0, 65535, 0));

CrestronEnvironment.ProgramStatusEventHandler += type =>
{
try
Expand Down Expand Up @@ -262,6 +267,13 @@ public void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, E
#if SERIES4
public ISelectableItems<string> Inputs { get; private set; }
#endif
private bool _muted;

private int _rawVolume;

public BoolFeedback MuteFeedback { get; private set; }

public IntFeedback VolumeLevelFeedback { get; private set; }

/// <summary>
/// Poll device
Expand Down Expand Up @@ -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);
}


}
}

0 comments on commit 43e4fff

Please sign in to comment.