1
0
mirror of https://github.com/DarthAffe/CUE.NET.git synced 2025-12-12 16:58:29 +00:00

Improved key-access on keyboard, implemented single "batch update" method

This commit is contained in:
unknown 2015-09-19 14:06:20 +02:00
parent 575804c8bf
commit 7efdb9ca7d
9 changed files with 124 additions and 29 deletions

View File

@ -65,17 +65,19 @@
<Compile Include="Enums\CorsairDeviceType.cs" />
<Compile Include="Enums\Keyboard\CorsairLogicalKeyboardLayout.cs" />
<Compile Include="Enums\Headset\CorsairHeadsetLedId.cs" />
<Compile Include="Enums\Keyboard\CorsairKeyboardLedId.cs" />
<Compile Include="Enums\Keyboard\CorsairKeyboardKeyId.cs" />
<Compile Include="Enums\Keyboard\CorsairPhysicalKeyboardLayout.cs" />
<Compile Include="Enums\Mouse\CorsairMouseLedId.cs" />
<Compile Include="Enums\Mouse\CorsairMouseButtonId.cs" />
<Compile Include="Enums\Mouse\CorsairPhysicalMouseLayout.cs" />
<Compile Include="Exceptions\CUEException.cs" />
<Compile Include="Exceptions\WrapperException.cs" />
<Compile Include="Wrapper\AbstractCueDevice.cs" />
<Compile Include="Wrapper\CorsairLed.cs" />
<Compile Include="Wrapper\GenericDeviceInfo.cs" />
<Compile Include="Wrapper\Headset\CorsairHeadset.cs" />
<Compile Include="Wrapper\Headset\CorsairHeadsetDeviceInfo.cs" />
<Compile Include="Wrapper\IDeviceInfo.cs" />
<Compile Include="Wrapper\Keyboard\CorsairKey.cs" />
<Compile Include="Wrapper\Keyboard\CorsairKeyboard.cs" />
<Compile Include="Wrapper\Mouse\CorsairMouseDeviceInfo.cs" />
<Compile Include="Wrapper\Mouse\CorsairMouse.cs" />

View File

@ -3,7 +3,7 @@
namespace CUE.NET.Enums.Keyboard
{
public enum CorsairKeyboardLedId
public enum CorsairKeyboardKeyId
{
Escape = 1,
F1 = 2,

View File

@ -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);
}

View File

@ -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<int, CorsairLed> Leds { get; } = new Dictionary<int, CorsairLed>();
#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<KeyValuePair<int, CorsairLed>> 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<int, CorsairLed> 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);
}

34
Wrapper/CorsairLed.cs Normal file
View File

@ -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
}
}

View File

@ -3,5 +3,7 @@
public interface ICueDevice
{
IDeviceInfo DeviceInfo { get; }
void UpdateLeds(bool fullUpdate = false);
}
}

View File

@ -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
}
}

View File

@ -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<CorsairKeyboardKeyId, CorsairKey> _keys = new Dictionary<CorsairKeyboardKeyId, CorsairKey>();
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