diff --git a/CUE.NET.csproj b/CUE.NET.csproj index 0d4f201..e56c83b 100644 --- a/CUE.NET.csproj +++ b/CUE.NET.csproj @@ -65,17 +65,19 @@ - + - + + + diff --git a/Enums/Keyboard/CorsairKeyboardLedId.cs b/Enums/Keyboard/CorsairKeyboardKeyId.cs similarity index 98% rename from Enums/Keyboard/CorsairKeyboardLedId.cs rename to Enums/Keyboard/CorsairKeyboardKeyId.cs index 6c84eb1..a94698b 100644 --- a/Enums/Keyboard/CorsairKeyboardLedId.cs +++ b/Enums/Keyboard/CorsairKeyboardKeyId.cs @@ -3,7 +3,7 @@ namespace CUE.NET.Enums.Keyboard { - public enum CorsairKeyboardLedId + public enum CorsairKeyboardKeyId { Escape = 1, F1 = 2, diff --git a/Enums/Mouse/CorsairMouseLedId.cs b/Enums/Mouse/CorsairMouseButtonId.cs similarity index 100% rename from Enums/Mouse/CorsairMouseLedId.cs rename to Enums/Mouse/CorsairMouseButtonId.cs diff --git a/Examples/SimpleDevTest/Program.cs b/Examples/SimpleDevTest/Program.cs index e1f4390..5638fb8 100644 --- a/Examples/SimpleDevTest/Program.cs +++ b/Examples/SimpleDevTest/Program.cs @@ -1,6 +1,7 @@ using System; using System.Drawing; using CUE.NET.Enums; +using CUE.NET.Enums.Keyboard; using CUE.NET.Exceptions; using CUE.NET.Wrapper; using CUE.NET.Wrapper.Keyboard; @@ -19,11 +20,17 @@ namespace SimpleDevTest if (keyboard == null) throw new WrapperException("No keyboard found"); - keyboard.SetKeyColor('r', Color.Red); - keyboard.SetKeyColor('g', Color.Green); - keyboard.SetKeyColor('b', Color.Blue); + keyboard[CorsairKeyboardKeyId.R].Led.Color = Color.Red; + keyboard[CorsairKeyboardKeyId.G].Led.Color = Color.Green; + keyboard[CorsairKeyboardKeyId.B].Led.Color = Color.Blue; - keyboard.SetKeyColors(new[] { 'w', 'h', 'i', 't', 'e' }, Color.White); + keyboard[CorsairKeyboardKeyId.W].Led.Color = Color.White; + keyboard[CorsairKeyboardKeyId.H].Led.Color = Color.White; + keyboard[CorsairKeyboardKeyId.I].Led.Color = Color.White; + keyboard[CorsairKeyboardKeyId.T].Led.Color = Color.White; + keyboard[CorsairKeyboardKeyId.E].Led.Color = Color.White; + + keyboard.UpdateLeds(); Console.WriteLine(CueSDK.LastError); } diff --git a/Wrapper/AbstractCueDevice.cs b/Wrapper/AbstractCueDevice.cs index 495059b..f38abde 100644 --- a/Wrapper/AbstractCueDevice.cs +++ b/Wrapper/AbstractCueDevice.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Runtime.InteropServices; using CUE.NET.Native; @@ -10,6 +12,8 @@ namespace CUE.NET.Wrapper public IDeviceInfo DeviceInfo { get; } + private Dictionary Leds { get; } = new Dictionary(); + #endregion #region Constructors @@ -23,18 +27,38 @@ namespace CUE.NET.Wrapper #region Methods - //TODO DarthAffe 19.09.2015: Wrap struct - protected void SetKeyColors(params _CorsairLedColor[] colors) + protected CorsairLed GetLed(int ledId) { + if (!Leds.ContainsKey(ledId)) + Leds.Add(ledId, new CorsairLed()); + + return Leds[ledId]; + } + + public virtual void UpdateLeds(bool fullUpdate = false) + { + IList> ledsToUpdate = (fullUpdate ? Leds : Leds.Where(x => x.Value.IsDirty)).ToList(); + + if (!ledsToUpdate.Any()) + return; // CUE seems to crash if 'CorsairSetLedsColors' with a zero length array + int structSize = Marshal.SizeOf(typeof(_CorsairLedColor)); - IntPtr ptr = Marshal.AllocHGlobal(structSize * colors.Length); + IntPtr ptr = Marshal.AllocHGlobal(structSize * ledsToUpdate.Count); IntPtr addPtr = new IntPtr(ptr.ToInt64()); - foreach (_CorsairLedColor color in colors) + foreach (KeyValuePair led in ledsToUpdate) { + _CorsairLedColor color = new _CorsairLedColor + { + ledId = led.Key, + r = led.Value.Color.R, + g = led.Value.Color.G, + b = led.Value.Color.B, + }; + Marshal.StructureToPtr(color, addPtr, false); addPtr = new IntPtr(addPtr.ToInt64() + structSize); } - _CUESDK.CorsairSetLedsColors(colors.Length, ptr); + _CUESDK.CorsairSetLedsColors(ledsToUpdate.Count, ptr); Marshal.FreeHGlobal(ptr); } diff --git a/Wrapper/CorsairLed.cs b/Wrapper/CorsairLed.cs new file mode 100644 index 0000000..1f5a033 --- /dev/null +++ b/Wrapper/CorsairLed.cs @@ -0,0 +1,34 @@ +using System.Drawing; + +namespace CUE.NET.Wrapper +{ + public class CorsairLed + { + #region Properties & Fields + + public bool IsDirty { get; set; } = false; + + private Color _color = Color.Black; + public Color Color + { + get { return _color; } + set + { + if (_color != value) + IsDirty = true; + + _color = value; + } + } + + //TODO DarthAffe 19.09.2015: Add effects and stuff + + #endregion + + #region Constructors + + internal CorsairLed() { } + + #endregion + } +} diff --git a/Wrapper/ICueDevice.cs b/Wrapper/ICueDevice.cs index a6064b9..45971d9 100644 --- a/Wrapper/ICueDevice.cs +++ b/Wrapper/ICueDevice.cs @@ -3,5 +3,7 @@ public interface ICueDevice { IDeviceInfo DeviceInfo { get; } + + void UpdateLeds(bool fullUpdate = false); } } diff --git a/Wrapper/Keyboard/CorsairKey.cs b/Wrapper/Keyboard/CorsairKey.cs new file mode 100644 index 0000000..a56d058 --- /dev/null +++ b/Wrapper/Keyboard/CorsairKey.cs @@ -0,0 +1,28 @@ +using CUE.NET.Enums.Keyboard; + +namespace CUE.NET.Wrapper.Keyboard +{ + public class CorsairKey + { + #region Properties & Fields + + public CorsairKeyboardKeyId KeyId { get; } + public CorsairLed Led { get; } + + #endregion + + #region Constructors + + internal CorsairKey(CorsairKeyboardKeyId keyId, CorsairLed led) + { + this.KeyId = keyId; + this.Led = led; + } + + #endregion + + #region Methods + + #endregion + } +} diff --git a/Wrapper/Keyboard/CorsairKeyboard.cs b/Wrapper/Keyboard/CorsairKeyboard.cs index b927970..e5915ed 100644 --- a/Wrapper/Keyboard/CorsairKeyboard.cs +++ b/Wrapper/Keyboard/CorsairKeyboard.cs @@ -1,5 +1,6 @@ -using System.Drawing; -using CUE.NET.Native; +using System; +using System.Collections.Generic; +using CUE.NET.Enums.Keyboard; namespace CUE.NET.Wrapper.Keyboard { @@ -9,6 +10,13 @@ namespace CUE.NET.Wrapper.Keyboard public CorsairKeyboardDeviceInfo KeyboardDeviceInfo { get; } + private Dictionary _keys = new Dictionary(); + public CorsairKey this[CorsairKeyboardKeyId keyId] + { + get { return _keys[keyId]; } + private set { throw new NotSupportedException(); } + } + #endregion #region Constructors @@ -17,28 +25,18 @@ namespace CUE.NET.Wrapper.Keyboard : base(info) { this.KeyboardDeviceInfo = info; + + InitializeKeys(); } #endregion #region Methods - public void SetKeyColor(char key, Color color) + private void InitializeKeys() { - int id = _CUESDK.CorsairGetLedIdForKeyName(key); - _CorsairLedColor ledColor = new _CorsairLedColor { ledId = id, r = color.R, g = color.G, b = color.B }; - SetKeyColors(ledColor); - } - - public void SetKeyColors(char[] keys, Color color) - { - _CorsairLedColor[] ledColors = new _CorsairLedColor[keys.Length]; - for (int i = 0; i < keys.Length; i++) - { - int id = _CUESDK.CorsairGetLedIdForKeyName(keys[i]); - ledColors[i] = new _CorsairLedColor { ledId = id, r = color.R, g = color.G, b = color.B }; - } - SetKeyColors(ledColors); + foreach (CorsairKeyboardKeyId keyId in Enum.GetValues(typeof(CorsairKeyboardKeyId))) + _keys.Add(keyId, new CorsairKey(keyId, GetLed((int)keyId))); } #endregion