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

@@ -29,6 +29,8 @@ namespace RGB.NET.Devices.Corsair
// ReSharper disable once MemberCanBePrivate.Global
protected Dictionary<CorsairLedId, Led> InternalLedMapping { get; } = new Dictionary<CorsairLedId, Led>();
protected CorsairUpdateQueue UpdateQueue { get; set; }
#endregion
#region Indexer
@@ -61,8 +63,10 @@ namespace RGB.NET.Devices.Corsair
/// <summary>
/// Initializes the device.
/// </summary>
public void Initialize()
public void Initialize(CorsairUpdateQueue updateQueue)
{
UpdateQueue = updateQueue;
InitializeLayout();
foreach (Led led in LedMapping.Values)
@@ -86,31 +90,7 @@ namespace RGB.NET.Devices.Corsair
/// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate)
{
List<Led> leds = ledsToUpdate.Where(x => (x.Color.A > 0) && (x.CustomData is CorsairLedId ledId && (ledId != CorsairLedId.Invalid))).ToList();
if (leds.Count > 0) // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array
{
int structSize = Marshal.SizeOf(typeof(_CorsairLedColor));
IntPtr ptr = Marshal.AllocHGlobal(structSize * leds.Count);
IntPtr addPtr = new IntPtr(ptr.ToInt64());
foreach (Led led in leds)
{
_CorsairLedColor color = new _CorsairLedColor
{
ledId = (int)led.CustomData,
r = led.Color.R,
g = led.Color.G,
b = led.Color.B
};
Marshal.StructureToPtr(color, addPtr, false);
addPtr = new IntPtr(addPtr.ToInt64() + structSize);
}
_CUESDK.CorsairSetLedsColors(leds.Count, ptr);
Marshal.FreeHGlobal(ptr);
}
}
=> UpdateQueue.SetData(ledsToUpdate.Where(x => (x.Color.A > 0) && (x.CustomData is CorsairLedId ledId && (ledId != CorsairLedId.Invalid))));
/// <inheritdoc cref="IRGBDevice.SyncBack" />
public override void SyncBack()
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
namespace RGB.NET.Devices.Corsair
{
public class CorsairUpdateQueue : UpdateQueue
{
#region Constructors
public CorsairUpdateQueue(IUpdateTrigger updateTrigger)
: base(updateTrigger)
{ }
#endregion
#region Methods
/// <inheritdoc />
protected override void Update(Dictionary<object, Color> dataSet)
{
int structSize = Marshal.SizeOf(typeof(_CorsairLedColor));
IntPtr ptr = Marshal.AllocHGlobal(structSize * dataSet.Count);
IntPtr addPtr = new IntPtr(ptr.ToInt64());
foreach (KeyValuePair<object, Color> data in dataSet)
{
_CorsairLedColor color = new _CorsairLedColor
{
ledId = (int)data.Key,
r = data.Value.R,
g = data.Value.G,
b = data.Value.B
};
Marshal.StructureToPtr(color, addPtr, false);
addPtr = new IntPtr(addPtr.ToInt64() + structSize);
}
_CUESDK.CorsairSetLedsColors(dataSet.Count, ptr);
Marshal.FreeHGlobal(ptr);
}
#endregion
}
}
@@ -7,6 +7,6 @@ namespace RGB.NET.Devices.Corsair
/// </summary>
internal interface ICorsairRGBDevice : IRGBDevice
{
void Initialize();
void Initialize(CorsairUpdateQueue updateQueue);
}
}