From dbdc1c22e8957f99d71f8a1089c8a58e1dde5a2c Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sat, 26 Mar 2016 20:37:10 +0100 Subject: [PATCH] Fixed update mechanism --- CUE.NET.csproj | 1 + Devices/Generic/AbstractCueDevice.cs | 30 +++++++++---------- .../Generic/EventArgs/LedsUpdatedEventArgs.cs | 7 +++-- .../EventArgs/LedsUpdatingEventArgs.cs | 7 +++-- Devices/Generic/LedUpateRequest.cs | 28 +++++++++++++++++ Devices/Keyboard/CorsairKeyboard.cs | 2 +- 6 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 Devices/Generic/LedUpateRequest.cs diff --git a/CUE.NET.csproj b/CUE.NET.csproj index aaa9020..a378353 100644 --- a/CUE.NET.csproj +++ b/CUE.NET.csproj @@ -56,6 +56,7 @@ + diff --git a/Devices/Generic/AbstractCueDevice.cs b/Devices/Generic/AbstractCueDevice.cs index 59ed5a7..381386b 100644 --- a/Devices/Generic/AbstractCueDevice.cs +++ b/Devices/Generic/AbstractCueDevice.cs @@ -186,7 +186,7 @@ namespace CUE.NET.Devices.Generic DeviceUpdate(); UpdateEffects(); - IList> ledsToUpdate = (flushLeds ? Leds : Leds.Where(x => x.Value.IsDirty)).ToList(); + ICollection ledsToUpdate = (flushLeds ? Leds : Leds.Where(x => x.Value.IsDirty)).Select(x => new LedUpateRequest(x.Key, x.Value.RequestedColor)).ToList(); foreach (CorsairLed led in Leds.Values) led.Update(); @@ -244,35 +244,35 @@ namespace CUE.NET.Devices.Generic /// The effect to apply. protected abstract void ApplyEffect(IEffect effect); - private void UpdateLeds(ICollection> ledsToUpdate) + private void UpdateLeds(ICollection updateRequests) { - ledsToUpdate = ledsToUpdate.Where(x => x.Value.Color != Color.Transparent).ToList(); + updateRequests = updateRequests.Where(x => x.Color != Color.Transparent).ToList(); - OnLedsUpdating(ledsToUpdate); + OnLedsUpdating(updateRequests); - if (ledsToUpdate.Any()) // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array + if (updateRequests.Any()) // CUE seems to crash if 'CorsairSetLedsColors' is called with a zero length array { int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); - IntPtr ptr = Marshal.AllocHGlobal(structSize * ledsToUpdate.Count); + IntPtr ptr = Marshal.AllocHGlobal(structSize * updateRequests.Count); IntPtr addPtr = new IntPtr(ptr.ToInt64()); - foreach (KeyValuePair led in ledsToUpdate) + foreach (LedUpateRequest ledUpdateRequest in updateRequests) { _CorsairLedColor color = new _CorsairLedColor { - ledId = led.Key, - r = led.Value.Color.R, - g = led.Value.Color.G, - b = led.Value.Color.B + ledId = ledUpdateRequest.LedId, + r = ledUpdateRequest.Color.R, + g = ledUpdateRequest.Color.G, + b = ledUpdateRequest.Color.B }; Marshal.StructureToPtr(color, addPtr, false); addPtr = new IntPtr(addPtr.ToInt64() + structSize); } - _CUESDK.CorsairSetLedsColors(ledsToUpdate.Count, ptr); + _CUESDK.CorsairSetLedsColors(updateRequests.Count, ptr); Marshal.FreeHGlobal(ptr); } - OnLedsUpdated(ledsToUpdate); + OnLedsUpdated(updateRequests); } /// @@ -382,7 +382,7 @@ namespace CUE.NET.Devices.Generic /// /// Handles the needed event-calls before the leds are updated. /// - protected virtual void OnLedsUpdating(ICollection> updatingLeds) + protected virtual void OnLedsUpdating(ICollection updatingLeds) { try { @@ -397,7 +397,7 @@ namespace CUE.NET.Devices.Generic /// /// Handles the needed event-calls after the leds are updated. /// - protected virtual void OnLedsUpdated(IEnumerable> updatedLeds) + protected virtual void OnLedsUpdated(IEnumerable updatedLeds) { try { diff --git a/Devices/Generic/EventArgs/LedsUpdatedEventArgs.cs b/Devices/Generic/EventArgs/LedsUpdatedEventArgs.cs index fde5a31..a4a0e77 100644 --- a/Devices/Generic/EventArgs/LedsUpdatedEventArgs.cs +++ b/Devices/Generic/EventArgs/LedsUpdatedEventArgs.cs @@ -1,4 +1,5 @@ // ReSharper disable MemberCanBePrivate.Global +// ReSharper disable UnusedAutoPropertyAccessor.Global using System.Collections.Generic; @@ -8,15 +9,15 @@ namespace CUE.NET.Devices.Generic.EventArgs { #region Properties & Fields - public IEnumerable> UpdatedLeds { get; private set; } + public IEnumerable UpdatedLeds { get; } #endregion #region Constructors - public LedsUpdatedEventArgs(IEnumerable> updatedLeds) + public LedsUpdatedEventArgs(IEnumerable updatedLeds) { - this.UpdatedLeds = new List>(updatedLeds); // Copy this - we don't want anyone to change the original led list. + this.UpdatedLeds = updatedLeds; } #endregion diff --git a/Devices/Generic/EventArgs/LedsUpdatingEventArgs.cs b/Devices/Generic/EventArgs/LedsUpdatingEventArgs.cs index 7a05463..708a8a7 100644 --- a/Devices/Generic/EventArgs/LedsUpdatingEventArgs.cs +++ b/Devices/Generic/EventArgs/LedsUpdatingEventArgs.cs @@ -1,4 +1,5 @@ // ReSharper disable MemberCanBePrivate.Global +// ReSharper disable UnusedAutoPropertyAccessor.Global using System.Collections.Generic; @@ -8,15 +9,15 @@ namespace CUE.NET.Devices.Generic.EventArgs { #region Properties & Fields - public ICollection> UpdatingLeds { get; private set; } + public ICollection UpdatingLeds { get; } #endregion #region Constructors - public LedsUpdatingEventArgs(ICollection> updatingLeds) + public LedsUpdatingEventArgs(ICollection updatingLeds) { - this.UpdatingLeds = updatingLeds; // No copy here - before the update changing this is ok. + this.UpdatingLeds = updatingLeds; } #endregion diff --git a/Devices/Generic/LedUpateRequest.cs b/Devices/Generic/LedUpateRequest.cs new file mode 100644 index 0000000..123fa06 --- /dev/null +++ b/Devices/Generic/LedUpateRequest.cs @@ -0,0 +1,28 @@ +// ReSharper disable MemberCanBePrivate.Global +// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global + +using System.Drawing; + +namespace CUE.NET.Devices.Generic +{ + public class LedUpateRequest + { + #region Properties & Fields + + public int LedId { get; } + + public Color Color { get; set; } + + #endregion + + #region Constructors + + public LedUpateRequest(int ledId, Color color) + { + this.LedId = ledId; + this.Color = color; + } + + #endregion + } +} diff --git a/Devices/Keyboard/CorsairKeyboard.cs b/Devices/Keyboard/CorsairKeyboard.cs index 8c0c096..ab67eac 100644 --- a/Devices/Keyboard/CorsairKeyboard.cs +++ b/Devices/Keyboard/CorsairKeyboard.cs @@ -183,7 +183,7 @@ namespace CUE.NET.Devices.Keyboard brushRectangle.X = 0; brushRectangle.Y = 0; foreach (CorsairKey key in keys) - key.Led.Color = brush.GetColorAtPoint(KeyboardRectangle, key.KeyRectangle.GetCenter(offsetX, offsetY)); + key.Led.Color = brush.GetColorAtPoint(brushRectangle, key.KeyRectangle.GetCenter(offsetX, offsetY)); break; case BrushCalculationMode.Absolute: foreach (CorsairKey key in keys)