Skip to content

Commit

Permalink
Merge pull request #7 from PepperDash/hotfix/add-receivequeue
Browse files Browse the repository at this point in the history
Hotfix/add receivequeue
  • Loading branch information
andrew-welker authored Jan 25, 2021
2 parents ce1d287 + 7a8c131 commit 4f40cdf
Showing 1 changed file with 110 additions and 52 deletions.
162 changes: 110 additions & 52 deletions epi-display-lg/LgDisplayDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
using PepperDash.Essentials.Core.Bridges;
using PepperDash.Essentials.Core.Routing;

using PepperDash_Essentials_Core.Queues;

namespace Epi.Display.Lg
{
public class LgDisplayController : TwoWayDisplayBase, IBasicVolumeWithFeedback, ICommunicationMonitor,
IBridgeAdvanced

{

GenericQueue ReceiveQueue;

public const int InputPowerOn = 101;

public const int InputPowerOff = 102;
Expand Down Expand Up @@ -44,6 +49,8 @@ public LgDisplayController(string key, string name, LgDisplayPropertiesConfig co
{
Communication = comms;

ReceiveQueue = new GenericQueue(key + "-queue");

var props = config;
if (props == null)
{
Expand All @@ -59,6 +66,8 @@ public LgDisplayController(string key, string name, LgDisplayPropertiesConfig co
_warmingTimeMs = props.warmingTimeMs > 0 ? props.warmingTimeMs : 8000;
//UdpSocketKey = props.udpSocketKey;

InputNumberFeedback = new IntFeedback(() => _inputNumber);

Init();
}

Expand All @@ -72,8 +81,33 @@ public bool PowerIsOn
get { return _powerIsOn; }
set
{
if (_powerIsOn == value)
{
return;
}

_powerIsOn = value;
PowerIsOnFeedback.FireUpdate();

if (_powerIsOn)
{
IsWarmingUp = true;

WarmupTimer = new CTimer(o =>
{
IsWarmingUp = false;
}, WarmupTime);
}
else
{
IsCoolingDown = true;

CooldownTimer = new CTimer(o =>
{
IsCoolingDown = false;
}, CooldownTime);
}

PowerIsOnFeedback.FireUpdate();
}
}

Expand Down Expand Up @@ -325,6 +359,7 @@ private void Init()
AddRoutingInputPort(
new RoutingInputPort(RoutingPortNames.HdmiIn2, eRoutingSignalType.Audio | eRoutingSignalType.Video,
eRoutingPortConnectionType.Hdmi, new Action(InputHdmi2), this), "91");

AddRoutingInputPort(
new RoutingInputPort(RoutingPortNames.DisplayPortIn, eRoutingSignalType.Audio | eRoutingSignalType.Video,
eRoutingPortConnectionType.DisplayPort, new Action(InputDisplayPort), this), "C0");
Expand All @@ -349,40 +384,51 @@ private void CommunicationMonitor_StatusChange(object sender, MonitorStatusChang

private void PortGather_LineReceived(object sender, GenericCommMethodReceiveTextArgs args)
{
CrestronInvoke.BeginInvoke((o) =>
{
var data = args.Text.Trim().Replace("OK", "").Split(' ');
ReceiveQueue.Enqueue(new ProcessStringMessage(args.Text, ProcessResponse));
}

private void ProcessResponse(string s)
{
var data = s.Trim().Replace("OK", "").Split(' ');

var command = data[0];
var id = data[1];
var responseValue = data[2];
string command;
string id;
string responseValue;

if (!id.Equals(Id))
{
Debug.Console(2, this, "Device ID Mismatch - Discarding Response");
return;
}
if (data.Length < 3)
{
Debug.Console(2, this, "Unable to parse message, not enough data in message: {0}", s);
return;
}
else
{
command = data[0];
id = data[1];
responseValue = data[2];
}

//command = 'ka'
switch (command)
{
case ("a"):
UpdatePowerFb(responseValue);
break;
case ("b"):
UpdateInputFb(responseValue);
break;
case ("f"):
UpdateVolumeFb(responseValue);
break;
case ("e"):
UpdateMuteFb(responseValue);
break;
}
});
if (!id.Equals(Id))
{
Debug.Console(2, this, "Device ID Mismatch - Discarding Response");
return;
}

//command = 'ka'
switch (command)
{
case ("a"):
UpdatePowerFb(responseValue);
break;
case ("b"):
UpdateInputFb(responseValue);
break;
case ("f"):
UpdateVolumeFb(responseValue);
break;
case ("e"):
UpdateMuteFb(responseValue);
break;
}
}

private void AddRoutingInputPort(RoutingInputPort port, string fbMatch)
Expand Down Expand Up @@ -578,7 +624,7 @@ public void UpdateInputFb(string s)
public void UpdatePowerFb(string s)
{
PowerIsOn = s.Contains("1");
PowerIsOnFeedback.FireUpdate();

}

/// <summary>
Expand All @@ -587,26 +633,33 @@ public void UpdatePowerFb(string s)
/// <param name="s">response from device</param>
public void UpdateVolumeFb(string s)
{
ushort newVol;
if (!ScaleVolume)
{
newVol = (ushort) NumericalHelpers.Scale(Convert.ToDouble(s), 0, 100, 0, 65535);
}
else
{
newVol = (ushort) NumericalHelpers.Scale(Convert.ToDouble(s), _lowerLimit, _upperLimit, 0, 65535);
}
if (!_volumeIsRamping)
try
{
_lastVolumeSent = newVol;
}
ushort newVol;
if (!ScaleVolume)
{
newVol = (ushort)NumericalHelpers.Scale(Convert.ToDouble(s), 0, 100, 0, 65535);
}
else
{
newVol = (ushort)NumericalHelpers.Scale(Convert.ToDouble(s), _lowerLimit, _upperLimit, 0, 65535);
}
if (!_volumeIsRamping)
{
_lastVolumeSent = newVol;
}

if (newVol == _volumeLevelForSig)
if (newVol == _volumeLevelForSig)
{
return;
}
_volumeLevelForSig = newVol;
VolumeLevelFeedback.FireUpdate();
}
catch (Exception e)
{
return;
Debug.Console(2, this, "Error updating volumefb for value: {1}: {0}", e, s);
}
_volumeLevelForSig = newVol;
VolumeLevelFeedback.FireUpdate();
}

/// <summary>
Expand Down Expand Up @@ -655,11 +708,16 @@ private void UpdateBooleanFeedback(int data)
public void StatusGet()
{
//SendBytes(new byte[] { Header, StatusControlCmd, 0x00, 0x00, StatusControlGet, 0x00 });

PowerGet();
InputGet();
VolumeGet();
MuteGet();
CrestronInvoke.BeginInvoke((o) =>
{
PowerGet();
CrestronEnvironment.Sleep(100);
InputGet();
CrestronEnvironment.Sleep(100);
VolumeGet();
CrestronEnvironment.Sleep(100);
MuteGet();
});
}


Expand Down

0 comments on commit 4f40cdf

Please sign in to comment.