From c4ac6f0e4073111463b962712117c0f4650918c4 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sat, 18 Nov 2017 11:17:25 +0100 Subject: [PATCH] Added methods to use the new GetColor method --- CueSDK.cs | 9 +++ Devices/Generic/AbstractCueDevice.cs | 84 +++++++++++++++++++++++++--- Devices/ICueDevice.cs | 20 ++++++- Examples/SimpleDevTest/Program.cs | 5 +- Native/_CUESDK.cs | 13 +++++ 5 files changed, 120 insertions(+), 11 deletions(-) diff --git a/CueSDK.cs b/CueSDK.cs index f38f6ee..32ae110 100644 --- a/CueSDK.cs +++ b/CueSDK.cs @@ -236,6 +236,15 @@ namespace CUE.NET IsInitialized = true; } + /// + /// Resets the colors of all devices back to the last saved color-data. (If there wasn't a manual save, that's the data from the time the SDK was initialized.) + /// + public static void Reset() + { + foreach (ICueDevice device in InitializedDevices) + device.RestoreColors(); + } + /// /// Reinitialize the CUE-SDK and temporarily hand back full control to CUE. /// diff --git a/Devices/Generic/AbstractCueDevice.cs b/Devices/Generic/AbstractCueDevice.cs index 7a253c3..5eb0346 100644 --- a/Devices/Generic/AbstractCueDevice.cs +++ b/Devices/Generic/AbstractCueDevice.cs @@ -29,6 +29,8 @@ namespace CUE.NET.Devices.Generic private static DateTime _lastUpdate = DateTime.Now; + private Dictionary _colorDataSave; + /// /// Gets generic information provided by CUE for the device. /// @@ -56,6 +58,7 @@ namespace CUE.NET.Devices.Generic /// /// Gets or sets the background brush of the keyboard. + /// If this is null the last saved color-data is used as background. /// public IBrush Brush { get; set; } @@ -153,6 +156,7 @@ namespace CUE.NET.Devices.Generic public virtual void Initialize() { DeviceRectangle = RectangleHelper.CreateRectangleFromRectangles((this).Select(x => x.LedRectangle)); + SaveColors(); } /// @@ -187,19 +191,26 @@ namespace CUE.NET.Devices.Generic /// Performs an update for all dirty keys, or all keys if flushLeds is set to true. /// /// Specifies whether all keys (including clean ones) should be updated. - public void Update(bool flushLeds = false) + /// Only updates the hardware-leds skippin effects and the render-pass. Only use this if you know what that means! + public void Update(bool flushLeds = false, bool noRender = false) { OnUpdating(); - // Update effects - foreach (ILedGroup ledGroup in LedGroups) - ledGroup.UpdateEffects(); + if (!noRender) + { + // Update effects + foreach (ILedGroup ledGroup in LedGroups) + ledGroup.UpdateEffects(); - // Render brushes - Render(this); - foreach (ILedGroup ledGroup in LedGroups.OrderBy(x => x.ZIndex)) - Render(ledGroup); + // Render brushes + if (Brush != null) + Render(this); + else + ApplyColorData(_colorDataSave); + foreach (ILedGroup ledGroup in LedGroups.OrderBy(x => x.ZIndex)) + Render(ledGroup); + } // Device-specific updates DeviceUpdate(); @@ -293,6 +304,63 @@ namespace CUE.NET.Devices.Generic OnLedsUpdated(updateRequests); } + /// + public void SyncColors() + { + Dictionary colorData = GetColors(); + ApplyColorData(colorData); + Update(true, true); + } + + /// + public void SaveColors() + { + _colorDataSave = GetColors(); + } + + /// + public void RestoreColors() + { + ApplyColorData(_colorDataSave); + Update(true, true); + } + + private void ApplyColorData(Dictionary colorData) + { + if (colorData == null) return; + + foreach (KeyValuePair corsairColor in _colorDataSave) + LedMapping[corsairColor.Key].Color = corsairColor.Value; + } + + private Dictionary GetColors() + { + int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); + IntPtr ptr = Marshal.AllocHGlobal(structSize * LedMapping.Count); + IntPtr addPtr = new IntPtr(ptr.ToInt64()); + foreach (KeyValuePair led in LedMapping) + { + _CorsairLedColor color = new _CorsairLedColor { ledId = (int)led.Value.Id }; + Marshal.StructureToPtr(color, addPtr, false); + addPtr = new IntPtr(addPtr.ToInt64() + structSize); + } + _CUESDK.CorsairGetLedsColors(LedMapping.Count, ptr); + + IntPtr readPtr = ptr; + Dictionary colorData = new Dictionary(); + for (int i = 0; i < LedMapping.Count; i++) + { + _CorsairLedColor ledColor = (_CorsairLedColor)Marshal.PtrToStructure(readPtr, typeof(_CorsairLedColor)); + colorData.Add((CorsairLedId)ledColor.ledId, new CorsairColor((byte)ledColor.r, (byte)ledColor.g, (byte)ledColor.b)); + + readPtr = new IntPtr(readPtr.ToInt64() + structSize); + } + + Marshal.FreeHGlobal(ptr); + + return colorData; + } + #endregion #region LedGroup diff --git a/Devices/ICueDevice.cs b/Devices/ICueDevice.cs index 3b2226a..2d95d18 100644 --- a/Devices/ICueDevice.cs +++ b/Devices/ICueDevice.cs @@ -140,10 +140,26 @@ namespace CUE.NET.Devices void Initialize(); /// - /// Perform an update for all dirty keys, or all keys if flushLeds is set to true. + /// Performs an update for all dirty keys, or all keys if flushLeds is set to true. /// /// Specifies whether all keys (including clean ones) should be updated. - void Update(bool flushLeds = false); + /// Only updates the hardware-leds skippin effects and the render-pass. Only use this if you know what that means! + void Update(bool flushLeds = false, bool noRender = false); + + /// + /// Reads the currently active colors from the device and sync them with the internal state. + /// + void SyncColors(); + + /// + /// Saves the currently active colors from the device. + /// + void SaveColors(); + + /// + /// Restores the last saved colors. + /// + void RestoreColors(); /// /// Attaches the given ledgroup. diff --git a/Examples/SimpleDevTest/Program.cs b/Examples/SimpleDevTest/Program.cs index 7b52a33..b55a9a4 100644 --- a/Examples/SimpleDevTest/Program.cs +++ b/Examples/SimpleDevTest/Program.cs @@ -39,7 +39,7 @@ namespace SimpleDevTest CueSDK.Initialize(); Console.WriteLine("Initialized with " + CueSDK.LoadedArchitecture + "-SDK"); - CueSDK.KeyboardSDK.Brush = (SolidColorBrush)Color.Black; + //CueSDK.KeyboardSDK.Brush = (SolidColorBrush)Color.Black; //CueSDK.KeyboardSDK[CorsairLedId.Z].Color = Color.Red; //CueSDK.KeyboardSDK[CorsairLedId.Z].IsLocked = true; @@ -51,12 +51,15 @@ namespace SimpleDevTest //CueSDK.KeyboardSDK.Brush = new LinearGradientBrush(new LinearGradient(true, new GradientStop(0, Color.Blue), new GradientStop(0.5f, Color.Red))); left.Brush = new ConicalGradientBrush(new PointF(0.6f, 0.7f), new RainbowGradient(360, 0)); left.Brush.AddEffect(new MoveGradientEffect()); + left.Brush.AddEffect(new FlashEffect { Attack = 2, Sustain = 1f, Release = 2, Interval = 1f }); mid.Brush = new ConicalGradientBrush(new PointF(0.5f, 0.3f), new RainbowGradient()); mid.Brush.AddEffect(new MoveGradientEffect()); + mid.Brush.AddEffect(new FlashEffect { Attack = 2, Sustain = 1f, Release = 2, Interval = 1f }); right.Brush = new ConicalGradientBrush(new PointF(0.4f, 0.7f), new RainbowGradient(360, 0)); right.Brush.AddEffect(new MoveGradientEffect()); + right.Brush.AddEffect(new FlashEffect { Attack = 2, Sustain = 1f, Release = 2, Interval = 1f }); //float halfKeyboardWidth = CueSDK.KeyboardSDK.DeviceRectangle.Width / 2f; //ILedGroup left = new RectangleLedGroup(CueSDK.KeyboardSDK, new RectangleF(CueSDK.KeyboardSDK.DeviceRectangle.X, CueSDK.KeyboardSDK.DeviceRectangle.Y, halfKeyboardWidth, CueSDK.KeyboardSDK.DeviceRectangle.Height)); diff --git a/Native/_CUESDK.cs b/Native/_CUESDK.cs index c6a18de..8d5d3a9 100644 --- a/Native/_CUESDK.cs +++ b/Native/_CUESDK.cs @@ -53,6 +53,7 @@ namespace CUE.NET.Native _dllHandle = LoadLibrary(dllPath); _corsairSetLedsColorsPointer = (CorsairSetLedsColorsPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairSetLedsColors"), typeof(CorsairSetLedsColorsPointer)); + _corsairGetLedsColorsPointer = (CorsairGetLedsColorsPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLedsColors"), typeof(CorsairGetLedsColorsPointer)); _corsairGetDeviceCountPointer = (CorsairGetDeviceCountPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetDeviceCount"), typeof(CorsairGetDeviceCountPointer)); _corsairGetDeviceInfoPointer = (CorsairGetDeviceInfoPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetDeviceInfo"), typeof(CorsairGetDeviceInfoPointer)); _corsairGetLedPositionsPointer = (CorsairGetLedPositionsPointer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(_dllHandle, "CorsairGetLedPositions"), typeof(CorsairGetLedPositionsPointer)); @@ -89,6 +90,7 @@ namespace CUE.NET.Native #region Pointers private static CorsairSetLedsColorsPointer _corsairSetLedsColorsPointer; + private static CorsairGetLedsColorsPointer _corsairGetLedsColorsPointer; private static CorsairGetDeviceCountPointer _corsairGetDeviceCountPointer; private static CorsairGetDeviceInfoPointer _corsairGetDeviceInfoPointer; private static CorsairGetLedPositionsPointer _corsairGetLedPositionsPointer; @@ -106,6 +108,9 @@ namespace CUE.NET.Native [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate bool CorsairSetLedsColorsPointer(int size, IntPtr ledsColors); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + private delegate bool CorsairGetLedsColorsPointer(int size, IntPtr ledsColors); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int CorsairGetDeviceCountPointer(); @@ -145,6 +150,14 @@ namespace CUE.NET.Native return _corsairSetLedsColorsPointer(size, ledsColors); } + /// + /// CUE-SDK: get current color for the list of requested LEDs. + /// + internal static bool CorsairGetLedsColors(int size, IntPtr ledsColors) + { + return _corsairGetLedsColorsPointer(size, ledsColors); + } + /// /// CUE-SDK: returns number of connected Corsair devices that support lighting control. ///