Changed all existing device-providers to use update-queues to decouple the physical update from the internal update-loop (first draft, code-documention is missing)

This commit is contained in:
DarthAffe committed 2018-03-25 15:53:13 +02:00
1 parent 3965bfb9a0
commit 66d03cdf4f
61 files changed
+1125 -440

No files matched your search

@@ -7,6 +7,6 @@ namespace RGB.NET.Devices.Novation
/// </summary>
internal interface INovationRGBDevice : IRGBDevice
{
void Initialize();
void Initialize(IUpdateTrigger updateTrigger);
}
}
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using RGB.NET.Core;
using Sanford.Multimedia.Midi;
namespace RGB.NET.Devices.Novation
{
public class LimitedColorUpdateQueue : MidiUpdateQueue
{
#region Constructors
public LimitedColorUpdateQueue(IUpdateTrigger updateTrigger, int deviceId)
: base(updateTrigger, deviceId)
{ }
#endregion
#region Methods
/// <inheritdoc />
protected override ShortMessage CreateMessage(KeyValuePair<object, Color> data)
{
NovationLedId ledId = (NovationLedId)data.Key;
return new ShortMessage(Convert.ToByte(ledId.GetStatus()), Convert.ToByte(ledId.GetId()), Convert.ToByte(ConvertColor(data.Value)));
}
/// <summary>
/// Convert a <see cref="Color"/> to its novation-representation depending on the <see cref="NovationColorCapabilities"/> of the <see cref="NovationRGBDevice{TDeviceInfo}"/>.
/// The conversion uses only a limited amount of colors (3 red, 3 yellow, 3 green).
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>The novation-representation of the <see cref="Color"/>.</returns>
protected virtual int ConvertColor(Color color)
{
if ((color.Hue >= 330) || (color.Hue < 30))
return (int)Math.Ceiling(color.Value * 3); // red with brightness 1, 2 or 3
if ((color.Hue >= 30) && (color.Hue < 90)) // yellow with brightness 17, 34 or 51
return (int)Math.Ceiling(color.Value * 3) * 17;
if ((color.Hue >= 90) && (color.Hue < 150)) // green with brightness 16, 32 or 48
return (int)Math.Ceiling(color.Value * 3) * 16;
return 0;
}
/// <inheritdoc />
public override void Reset()
{
base.Reset();
SendMessage(new ShortMessage(Convert.ToByte(0xB0), Convert.ToByte(0), Convert.ToByte(0)));
}
#endregion
}
}
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using RGB.NET.Core;
using Sanford.Multimedia.Midi;
namespace RGB.NET.Devices.Novation
{
public abstract class MidiUpdateQueue : UpdateQueue, IDisposable
{
#region Properties & Fields
private readonly OutputDevice _outputDevice;
#endregion
#region Constructors
public MidiUpdateQueue(IUpdateTrigger updateTrigger, int deviceId)
: base(updateTrigger)
{
_outputDevice = new OutputDevice(deviceId);
}
#endregion
#region Methods
/// <inheritdoc />
protected override void Update(Dictionary<object, Color> dataSet)
{
foreach (KeyValuePair<object, Color> data in dataSet)
SendMessage(CreateMessage(data));
}
protected virtual void SendMessage(ShortMessage message) => _outputDevice.SendShort(message.Message);
protected abstract ShortMessage CreateMessage(KeyValuePair<object, Color> data);
public void Dispose()
{
_outputDevice.Dispose();
}
#endregion
}
}
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using RGB.NET.Core;
using Sanford.Multimedia.Midi;
namespace RGB.NET.Devices.Novation
{
@@ -16,14 +15,13 @@ namespace RGB.NET.Devices.Novation
{
#region Properties & Fields
private readonly OutputDevice _outputDevice;
private readonly TDeviceInfo _deviceInfo;
/// <inheritdoc />
/// <summary>
/// Gets information about the <see cref="T:RGB.NET.Devices.Novation.NovationRGBDevice" />.
/// </summary>
public override TDeviceInfo DeviceInfo => _deviceInfo;
public override TDeviceInfo DeviceInfo { get; }
protected MidiUpdateQueue UpdateQueue { get; set; }
#endregion
@@ -35,20 +33,17 @@ namespace RGB.NET.Devices.Novation
/// <param name="info">The generic information provided by Novation for the device.</param>
protected NovationRGBDevice(TDeviceInfo info)
{
this._deviceInfo = info;
_outputDevice = new OutputDevice(info.DeviceId);
this.DeviceInfo = info;
}
#endregion
#region Methods
/// <summary>
/// Initializes the device.
/// </summary>
public void Initialize()
public void Initialize(IUpdateTrigger updateTrigger)
{
InitializeLayout();
@@ -57,6 +52,9 @@ namespace RGB.NET.Devices.Novation
Rectangle ledRectangle = new Rectangle(this.Select(x => x.LedRectangle));
Size = ledRectangle.Size + new Size(ledRectangle.Location.X, ledRectangle.Location.Y);
}
if (DeviceInfo.ColorCapabilities == NovationColorCapabilities.LimitedRG)
UpdateQueue = new LimitedColorUpdateQueue(updateTrigger, DeviceInfo.DeviceId);
}
/// <summary>
@@ -65,96 +63,19 @@ namespace RGB.NET.Devices.Novation
protected abstract void InitializeLayout();
/// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate)
{
List<Led> leds = ledsToUpdate.Where(x => x.Color.A > 0).ToList();
if (leds.Count > 0)
{
foreach (Led led in leds)
{
NovationLedId ledId = (NovationLedId)led.CustomData;
int color = ConvertColor(led.Color);
SendMessage(ledId.GetStatus(), ledId.GetId(), color);
}
}
}
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate) => UpdateQueue.SetData(ledsToUpdate.Where(x => x.Color.A > 0));
/// <summary>
/// Resets the <see cref="NovationRGBDevice{TDeviceInfo}"/> back top default.
/// Resets the <see cref="NovationRGBDevice{TDeviceInfo}"/> back to default.
/// </summary>
public virtual void Reset() => SendMessage(0xB0, 0, 0);
/// <summary>
/// Convert a <see cref="Color"/> to its novation-representation depending on the <see cref="NovationColorCapabilities"/> of the <see cref="NovationRGBDevice{TDeviceInfo}"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>The novation-representation of the <see cref="Color"/>.</returns>
protected virtual int ConvertColor(Color color)
{
switch (_deviceInfo.ColorCapabilities)
{
case NovationColorCapabilities.RGB:
return ConvertColorFull(color);
case NovationColorCapabilities.LimitedRG:
return ConvertColorLimited(color);
default:
return 0;
}
}
/// <summary>
/// Convert a <see cref="Color"/> to its novation-representation depending on the <see cref="NovationColorCapabilities"/> of the <see cref="NovationRGBDevice{TDeviceInfo}"/>.
/// The conversion uses the full rgb-range.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>The novation-representation of the <see cref="Color"/>.</returns>
protected virtual int ConvertColorFull(Color color)
{
//TODO DarthAffe 16.08.2017: How are colors for full rgb devices encoded?
return 0;
}
/// <summary>
/// Convert a <see cref="Color"/> to its novation-representation depending on the <see cref="NovationColorCapabilities"/> of the <see cref="NovationRGBDevice{TDeviceInfo}"/>.
/// The conversion uses only a limited amount of colors (3 red, 3 yellow, 3 green).
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>The novation-representation of the <see cref="Color"/>.</returns>
protected virtual int ConvertColorLimited(Color color)
{
if ((color.Hue >= 330) || (color.Hue < 30))
return (int)Math.Ceiling(color.Value * 3); // red with brightness 1, 2 or 3
if ((color.Hue >= 30) && (color.Hue < 90)) // yellow with brightness 17, 34 or 51
return (int)Math.Ceiling(color.Value * 3) * 17;
if ((color.Hue >= 90) && (color.Hue < 150)) // green with brightness 16, 32 or 48
return (int)Math.Ceiling(color.Value * 3) * 16;
return 0;
}
/// <summary>
/// Sends a message to the <see cref="NovationRGBDevice{TDeviceInfo}"/>.
/// </summary>
/// <param name="status">The status-code of the message.</param>
/// <param name="data1">The first data-package of the message.</param>
/// <param name="data2">The second data-package of the message.</param>
protected virtual void SendMessage(int status, int data1, int data2)
{
ShortMessage shortMessage = new ShortMessage(Convert.ToByte(status), Convert.ToByte(data1), Convert.ToByte(data2));
_outputDevice.SendShort(shortMessage.Message);
}
public virtual void Reset() => UpdateQueue.Reset();
/// <inheritdoc cref="IDisposable.Dispose" />
/// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}.Dispose" />
public override void Dispose()
{
Reset();
_outputDevice.Dispose();
UpdateQueue.Dispose();
base.Dispose();
}