Skip to content

Commit

Permalink
fix: modify rs-232 parsing routine
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-welker committed May 1, 2024
1 parent f4c868d commit dc95381
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 188 deletions.
152 changes: 116 additions & 36 deletions src/Rs232Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace SonyBraviaEpi
public static class Rs232Commands
{
public static readonly byte[] PowerOn = {0x8c, 0x00, 0x00, 0x02, 0x01};
public static readonly byte[] PowerOff = {0x8c, 0x00, 0x00, 0x02, 0x00};
public static readonly byte[] PowerQuery = {0x83, 0x00, 0x00, 0xFF, 0xFF};
public static readonly byte[] PowerOff = {0x8c, 0x00, 0x00, 0x02, 0x00};
public static readonly byte[] InputVideo1 = {0x8C, 0x00, 0x02, 0x03, 0x02, 0x01};
public static readonly byte[] InputVideo2 = {0x8C, 0x00, 0x02, 0x03, 0x02, 0x02};
public static readonly byte[] InputVideo3 = {0x8C, 0x00, 0x02, 0x03, 0x02, 0x03};
Expand All @@ -21,8 +20,12 @@ 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[] InputQuery = {0x83, 0x00, 0x02, 0xFF, 0xFF};
public static readonly byte[] InputPc1 = {0x8C, 0x00, 0x02, 0x03, 0x05, 0x01};

public static readonly byte[] PowerQuery = { 0x83, 0x00, 0x00, 0xFF, 0xFF };
public static readonly byte[] InputQuery = { 0x83, 0x00, 0x02, 0xFF, 0xFF };
public static readonly byte[] VolumeQuery = { 0x83, 0x00, 0x05, 0xFF, 0xFF };
public static readonly byte[] MuteQuery = { 0x83, 0x00, 0x06, 0xFF, 0xFF };

public static byte CalculateChecksum(this byte[] data)
{
Expand All @@ -41,84 +44,94 @@ public static byte[] WithChecksum(this byte[] data)
return newArray.ToArray();
}

public static IQueueMessage GetPowerOn(IBasicCommunication coms)
public static IQueueMessage GetVolumeQuery(IBasicCommunication coms, Action<eCommandType> action)
{
return new Rs232Command(coms, VolumeQuery.WithChecksum(), action, eCommandType.VolumeQuery);
}

public static IQueueMessage GetMuteQuery(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, PowerOn.WithChecksum());
return new Rs232Command(coms, MuteQuery.WithChecksum(), action, eCommandType.MuteQuery);
}

public static IQueueMessage GetPowerOff(IBasicCommunication coms)
public static IQueueMessage GetPowerOn(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, PowerOff.WithChecksum());
return new Rs232Command(coms, PowerOn.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetPowerQuery(IBasicCommunication coms)
public static IQueueMessage GetPowerOff(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, PowerQuery.WithChecksum());
return new Rs232Command(coms, PowerOff.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetHdmi1(IBasicCommunication coms)
public static IQueueMessage GetPowerQuery(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputHdmi1.WithChecksum());
return new Rs232Command(coms, PowerQuery.WithChecksum(), action, eCommandType.PowerQuery);
}

public static IQueueMessage GetHdmi2(IBasicCommunication coms)
public static IQueueMessage GetHdmi1(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputHdmi2.WithChecksum());
return new Rs232Command(coms, InputHdmi1.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetHdmi3(IBasicCommunication coms)
public static IQueueMessage GetHdmi2(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputHdmi3.WithChecksum());
return new Rs232Command(coms, InputHdmi2.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetHdmi4(IBasicCommunication coms)
public static IQueueMessage GetHdmi3(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputHdmi4.WithChecksum());
return new Rs232Command(coms, InputHdmi3.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetHdmi5(IBasicCommunication coms)
public static IQueueMessage GetHdmi4(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputHdmi5.WithChecksum());
return new Rs232Command(coms, InputHdmi4.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetVideo1(IBasicCommunication coms)
public static IQueueMessage GetHdmi5(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputVideo1.WithChecksum());
return new Rs232Command(coms, InputHdmi5.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetVideo2(IBasicCommunication coms)
public static IQueueMessage GetVideo1(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputVideo2.WithChecksum());
return new Rs232Command(coms, InputVideo1.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetVideo3(IBasicCommunication coms)
public static IQueueMessage GetVideo2(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputVideo3.WithChecksum());
return new Rs232Command(coms, InputVideo2.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetComponent1(IBasicCommunication coms)
public static IQueueMessage GetVideo3(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputComponent1.WithChecksum());
return new Rs232Command(coms, InputVideo3.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetComponent2(IBasicCommunication coms)
public static IQueueMessage GetComponent1(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputComponent2.WithChecksum());
return new Rs232Command(coms, InputComponent1.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetComponent3(IBasicCommunication coms)
public static IQueueMessage GetComponent2(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputComponent3.WithChecksum());
return new Rs232Command(coms, InputComponent2.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetPc(IBasicCommunication coms)
public static IQueueMessage GetComponent3(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputPc1.WithChecksum());
return new Rs232Command(coms, InputComponent3.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetInputQuery(IBasicCommunication coms)
public static IQueueMessage GetPc(IBasicCommunication coms, Action<eCommandType> action)
{
return new ComsMessage(coms, InputQuery.WithChecksum());
return new Rs232Command(coms, InputPc1.WithChecksum(), action, eCommandType.Command);
}

public static IQueueMessage GetInputQuery(IBasicCommunication coms, Action<eCommandType> action)
{
return new Rs232Command(coms, InputQuery.WithChecksum(), action, eCommandType.InputQuery);
}
/*const commands = {
"power_on": [0x8C, 0x00, 0x00, 0x02, 0x01],
Expand Down Expand Up @@ -249,4 +262,71 @@ public static IQueueMessage GetInputQuery(IBasicCommunication coms)
"signage_productinfo3": [0x83, 0x00, 0x6C, 0xFF, 0xFF]
}*/
}

public class Rs232Command:IQueueMessage
{
private readonly Action<eCommandType> _action;

private readonly byte[] _message;

private readonly IBasicCommunication _comm;

private readonly eCommandType _commandType;

public Rs232Command(IBasicCommunication coms, byte[] message, Action<eCommandType> updateCommandAction, eCommandType commandType)
{
if(coms == null)
{
#if SERIES4
throw new ArgumentNullException(nameof(coms));
#else
throw new ArgumentNullException("coms");
#endif
}

if (message == null)
{
#if SERIES4
throw new ArgumentNullException(nameof(message));
#else
throw new ArgumentNullException("message");
#endif
}

if (updateCommandAction == null)
{
#if SERIES4
throw new ArgumentNullException(nameof(updateCommandAction));
#else
throw new ArgumentNullException("updateCommandAction");
#endif
}

_action = updateCommandAction;
_message = message;
_comm = coms;
_commandType = commandType;
}

public void Dispatch()
{
_action(_commandType);

_comm.SendBytes(_message);
}

public override string ToString()
{
return _message.ToString();
}
}

public enum eCommandType
{
Command,
PowerQuery,
InputQuery,
VolumeQuery,
MuteQuery
}
}
69 changes: 55 additions & 14 deletions src/Rs232ParsingUtils.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,88 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using PepperDash.Core;

namespace SonyBraviaEpi
{
public static class Rs232ParsingUtils
{
private static Dictionary<int, string> _inputMap = new Dictionary<int, string>
{
{0x0201,"video1" },
{0x0202,"video2" },
{0x0203,"video3" },
{0x0301, "component1" },
{0x0302,"component2" },
{0x0303,"component3" },
{0x0401,"hdmi1" },
{0x0402,"hdmi2" },
{0x0403,"hdmi3" },
{0x0404,"hdmi4" },
{0x0405,"hdmi5" },
{0x0501,"vga1" }
};
private const byte Header = 0x70;

public static bool ParsePowerResponse(this byte[] response, out bool power)
{
// TODO [ ] actually add in parsing
public static bool ParsePowerResponse(this byte[] response)
{
Debug.Console(DebugLevels.DebugLevel, "ParsePowerResponse response: {0}", response.ToReadableString());

if (response[2] == 0x00)
{
power = response[3] == 0x01;
return true;
return response[3] == 0x01;
}

if (response[2] == 0x02)
{
power = response[3] != 0x00;
return true;
return response[3] != 0x00;
}

power = false;
return false;
}

public static bool ParseInputResponse(this byte[] response, out string input)
public static string ParseInputResponse(this byte[] response)
{
// TODO [ ] actually add in parsing
Debug.Console(DebugLevels.DebugLevel, "ParseInputResponse response: {0}", response.ToReadableString());

if (response[2] == 0x02)
//add together the input type byte & the input number byte
var inputNumber = response[3] + response[4];

string input;

if(_inputMap.TryGetValue(inputNumber,out input))
{
input = "";
//return true;
Debug.Console(DebugLevels.DebugLevel, "Got input {0}", input);
return input;
}

input = "";
return false;
return input;
}

public static int ParseVolumeResponse(this byte[] response)
{
//not a direct volume response
if (response[3] != 0x01)
{
return 0;
}

return response[4];
}

/// <summary>
/// True = isMuted
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
public static bool ParseMuteResponse(this byte[] response)
{
//not a direct mute response
if (response[3] != 0x01) { return false; }

return response[4] == 0x01;
}

public static bool IsComplete(this byte[] message)
Expand Down
2 changes: 1 addition & 1 deletion src/SimpleIpParsingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace SonyBraviaEpi
{
public static class SimpleIpParsingUtils
{
public static IEnumerable<string> SplitInParts(this String s, Int32 partLength)
public static IEnumerable<string> SplitInParts(this string s, int partLength)
{
if ((s == null) || (partLength <= 0)) yield break;
for (var i = 0; i < s.Length; i += partLength)
Expand Down
Loading

0 comments on commit dc95381

Please sign in to comment.