Skip to content

Commit

Permalink
feat: Implements IHasInputs
Browse files Browse the repository at this point in the history
  • Loading branch information
ndorin committed Apr 30, 2024
1 parent 51f492d commit 0794030
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
*.sln.docstates
*.projectinfo

[Oo]utput/
*._
*.cplz

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

Expand Down Expand Up @@ -353,3 +357,5 @@ MigrationBackup/
.ionide/
*.projectinfo
*.projectinfo~Stashed changes
/output/epi-sony-bravia.4Series.1.0.0-local.cplz
/._.gitignore
Binary file removed output/epi-sony-bravia.4Series.1.0.0-local.cplz
Binary file not shown.
134 changes: 130 additions & 4 deletions src/SonyBraviaDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Core.Bridges;
using PepperDash.Essentials.Core.Config;
using PepperDash.Essentials.Core.DeviceTypeInterfaces;
using PepperDash.Essentials.Core.Queues;
using PepperDash.Essentials.Core.Routing;
using PepperDash.Essentials.Devices.Displays;
using static Crestron.SimplSharpPro.DM.Audio;

namespace SonyBraviaEpi
{
public class SonyBraviaDevice : TwoWayDisplayBase, ICommunicationMonitor, IBridgeAdvanced,
IInputHdmi1, IInputHdmi2, IInputHdmi3, IInputHdmi4, IInputVga1,
IOnline
#if SERIES4
, IHasInputs<string, string>
#endif
{
private readonly IBasicCommunication _coms;
private readonly bool _comsIsRs232;
Expand Down Expand Up @@ -96,6 +101,9 @@ public SonyBraviaDevice(DeviceConfig config, IBasicCommunication comms)

BuildInputRoutingPorts();

SetupInputs();


var worker = _comsIsRs232
? new Thread(ProcessRs232Response, null)
: new Thread(ProcessSimpleIpResponse, null);
Expand Down Expand Up @@ -229,6 +237,8 @@ public void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, E

public BoolFeedback IsOnline { get { return CommunicationMonitor.IsOnlineFeedback; } }

public ISelectableItems<string> Inputs { get; private set; }

/// <summary>
/// Poll device
/// </summary>
Expand All @@ -242,10 +252,10 @@ public static void Poll(object o)
commands
.ToList()
.ForEach(command =>
{
CommandQueue.Enqueue(command);
Thread.Sleep(100);
});
{
CommandQueue.Enqueue(command);
Thread.Sleep(100);
});
}

/// <summary>
Expand Down Expand Up @@ -367,6 +377,78 @@ public void BuildInputRoutingPorts()
new Action(InputComponent3), this), 12);
}

private void SetupInputs()
{
#if SERIES4
Inputs = new SonyBraviaInputs
{
Items = new Dictionary<string, ISelectableItem>
{
{
"hdmi1", new SonyBraviaInput("Hdmi1", "HDMI 1", this,
_comsIsRs232 ? Rs232Commands.GetHdmi1(_coms)
: SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 1))},
{

"hdmi2", new SonyBraviaInput("hdmi2", "HDMI 2", this,
_comsIsRs232 ? Rs232Commands.GetHdmi2(_coms)
: SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 2))
},
{
"hdmi3", new SonyBraviaInput("hdmi3", "HDMI 3", this,
_comsIsRs232 ? Rs232Commands.GetHdmi3(_coms)
: SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 3))
},
{
"hdmi4", new SonyBraviaInput("hdmi4", "HDMI 4", this,
_comsIsRs232 ? Rs232Commands.GetHdmi4(_coms)
: SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 4))
},
{
"hdmi5", new SonyBraviaInput("hdmi5", "HDMI 5", this,
_comsIsRs232 ? Rs232Commands.GetHdmi5(_coms)
: SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 5))
},
{
"video1", new SonyBraviaInput("video1", "Video 1", this,
_comsIsRs232 ? Rs232Commands.GetVideo1(_coms)
: SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 1))
},
{
"video2", new SonyBraviaInput("video2", "Video 2", this,
_comsIsRs232 ? Rs232Commands.GetVideo2(_coms)
: SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 2))
},
{
"video3", new SonyBraviaInput("video3", "Video 3", this,
_comsIsRs232 ? Rs232Commands.GetVideo3(_coms)
: SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 3))
},
{
"component1", new SonyBraviaInput("component1", "Component 1", this,
_comsIsRs232 ? Rs232Commands.GetComponent1(_coms)
: SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 1))
},
{
"component2", new SonyBraviaInput("component2", "Component 2", this,
_comsIsRs232 ? Rs232Commands.GetComponent2(_coms)
: SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 2))
},
{
"component3", new SonyBraviaInput("component3", "Component 3", this,
_comsIsRs232 ? Rs232Commands.GetComponent3(_coms)
: SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 3))
},
{
"vga1", new SonyBraviaInput("vga1", "VGA 1", this,
_comsIsRs232 ? Rs232Commands.GetComponent1(_coms)
: null)
}
}
};
#endif
}

/// <summary>
/// Select HDMI 1 input
/// </summary>
Expand Down Expand Up @@ -674,8 +756,22 @@ private object ProcessRs232Response(object _)
{
_currentInput = input;
CurrentInputFeedback.FireUpdate();

#if SERIES4
if (Inputs.Items.ContainsKey(input))
{
foreach (var item in Inputs.Items)
{
item.Value.IsSelected = item.Key.Equals(input);
}
}

Inputs.CurrentItem = input;
#endif
}



buffer = buffer.NumberOfHeaders() > 1 ? buffer.CleanOutFirstMessage() : new byte[0];

Debug.Console(DebugLevels.DebugLevel, this, seperator);
Expand Down Expand Up @@ -785,6 +881,19 @@ private object ProcessSimpleIpResponse(object _)
}
}


#if SERIES4
// No idea if this will work with _currentInput. It's not clear how the input is determined for IP communication
if (Inputs.Items.ContainsKey(_currentInput))
{
foreach (var item in Inputs.Items)
{
item.Value.IsSelected = item.Key.Equals(_currentInput);
}
}

Inputs.CurrentItem = _currentInput;
#endif
Debug.Console(DebugLevels.ErrorLevel, this, "ProcessSimpleIpResponse: _currentInput == '{0}'", _currentInput);

break;
Expand Down Expand Up @@ -813,5 +922,22 @@ private object ProcessSimpleIpResponse(object _)
}
}
}

public void EnqueueCommand(IQueueMessage command)
{
CommandQueue.Enqueue(command);
}

#if SERIES4
public void SetInput(string selector)
{
var input = Inputs.Items[selector];

if (input != null)
{
input.Select();
}
}
#endif
}
}
100 changes: 100 additions & 0 deletions src/SonyBraviaInputs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

using PepperDash.Essentials.Core.DeviceTypeInterfaces;
using PepperDash.Essentials.Core.Queues;
using SonyBraviaEpi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SonyBraviaEpi
{
#if SERIES4
public class SonyBraviaInputs : ISelectableItems<string>
{
private Dictionary<string, ISelectableItem> _items = new Dictionary<string, ISelectableItem>();

public Dictionary<string, ISelectableItem> Items
{
get
{
return _items;
}
set
{
if (_items == value)
return;

_items = value;

ItemsUpdated?.Invoke(this, null);
}
}

private string _currentItem;

public string CurrentItem
{
get
{
return _currentItem;
}
set
{
if (_currentItem == value)
return;

_currentItem = value;

CurrentItemChanged?.Invoke(this, null);
}
}

public event EventHandler ItemsUpdated;
public event EventHandler CurrentItemChanged;

}

public class SonyBraviaInput : ISelectableItem
{
private bool _isSelected;

private readonly IQueueMessage _inputCommand;
private readonly SonyBraviaDevice _parent;

public SonyBraviaInput(string key, string name, SonyBraviaDevice parent, IQueueMessage inputCommand)
{
Key = key;
Name = name;
_parent = parent;
_inputCommand = inputCommand;
}

public string Key { get; private set; }
public string Name { get; private set; }

public event EventHandler ItemUpdated;

public bool IsSelected
{
get { return _isSelected; }
set
{
if (value == _isSelected)
return;

_isSelected = value;
var handler = ItemUpdated;
if (handler != null)
handler(this, EventArgs.Empty);
}
}

public void Select()
{
_parent.EnqueueCommand(_inputCommand);
}
}
#endif
}

0 comments on commit 0794030

Please sign in to comment.