added some more features, including x-y based setting

This commit is contained in:
Ell 2021-05-01 22:35:45 +02:00
parent 1085b303aa
commit 0ed8644b9e
6 changed files with 178 additions and 37 deletions

View file

@ -9,17 +9,36 @@ namespace Demo {
private static async Task Main(string[] args) { private static async Task Main(string[] args) {
await IllumilibLighting.Initialize(); await IllumilibLighting.Initialize();
Console.WriteLine("Setting specific positions");
IllumilibLighting.SetKeyboardLighting(0, 1, 0);
IllumilibLighting.SetKeyboardLighting(6, 1, 1, 0, 1);
IllumilibLighting.SetKeyboardLighting(16, 5, 1, 0, 1);
Thread.Sleep(TimeSpan.FromSeconds(5));
IllumilibLighting.SetKeyboardLighting(1, 0, 0);
IllumilibLighting.SetKeyboardLighting(8, 2, 2, 2, 0, 1, 0);
Thread.Sleep(TimeSpan.FromSeconds(5));
Console.WriteLine("Setting all lights to blue"); Console.WriteLine("Setting all lights to blue");
IllumilibLighting.SetAllLighting(0, 0, 1); IllumilibLighting.SetAllLighting(0, 0, 1);
Thread.Sleep(TimeSpan.FromSeconds(3)); Thread.Sleep(TimeSpan.FromSeconds(3));
IllumilibLighting.SetAllLighting(0, 0, 0); IllumilibLighting.SetAllLighting(0, 0, 0);
Console.WriteLine("Doing a fun effect");
for (var x = 0; x < IllumilibLighting.KeyboardWidth; x++) {
IllumilibLighting.SetKeyboardLighting(x, 0, 1, IllumilibLighting.KeyboardHeight, 0, 0, 1);
Thread.Sleep(TimeSpan.FromSeconds(0.25F));
}
for (var x = IllumilibLighting.KeyboardWidth - 1; x >= 0; x--) {
IllumilibLighting.SetKeyboardLighting(x, 0, 1, IllumilibLighting.KeyboardHeight, 0, 0, 0);
Thread.Sleep(TimeSpan.FromSeconds(0.25F));
}
Console.WriteLine("Going through the alphabet"); Console.WriteLine("Going through the alphabet");
for (var i = 65; i <= 90; i++) { for (var i = 65; i <= 90; i++) {
var key = (KeyboardKeys) i; var key = (KeyboardKeys) i;
IllumilibLighting.SetKeyLighting(key, 0, 1, 0); IllumilibLighting.SetKeyboardLighting(key, 0, 1, 0);
Thread.Sleep(TimeSpan.FromSeconds(0.25F)); Thread.Sleep(TimeSpan.FromSeconds(0.25F));
IllumilibLighting.SetKeyLighting(key, 0, 0, 0); IllumilibLighting.SetKeyboardLighting(key, 0, 0, 0);
} }
Thread.Sleep(TimeSpan.FromSeconds(1)); Thread.Sleep(TimeSpan.FromSeconds(1));
@ -33,7 +52,7 @@ namespace Demo {
Console.WriteLine("Setting all supported keys"); Console.WriteLine("Setting all supported keys");
foreach (var key in Enum.GetValues<KeyboardKeys>()) { foreach (var key in Enum.GetValues<KeyboardKeys>()) {
IllumilibLighting.SetKeyLighting(key, 1, 0, 0); IllumilibLighting.SetKeyboardLighting(key, 1, 0, 0);
Thread.Sleep(50); Thread.Sleep(50);
} }
Thread.Sleep(TimeSpan.FromSeconds(15)); Thread.Sleep(TimeSpan.FromSeconds(15));

View file

@ -10,6 +10,15 @@ namespace Illumilib {
/// </summary> /// </summary>
public static class IllumilibLighting { public static class IllumilibLighting {
/// <summary>
/// The maximum width that a keyboard can have, in amount of keys
/// </summary>
public const int KeyboardWidth = 22;
/// <summary>
/// The maximum height that a keyboard can have, in amount of keys
/// </summary>
public const int KeyboardHeight = 6;
private static List<LightingSystem> systems; private static List<LightingSystem> systems;
/// <summary> /// <summary>
/// A property that returns whether Illumilib is currently initialized /// A property that returns whether Illumilib is currently initialized
@ -54,8 +63,7 @@ namespace Illumilib {
} }
/// <summary> /// <summary>
/// Sets the lighting for all keyboards to the given color. /// Sets the lighting for all keyboards to the given color
/// Note that, if Logitech is used, some keyboards do not support this method.
/// </summary> /// </summary>
/// <param name="r">The color's red value, between 0 and 1</param> /// <param name="r">The color's red value, between 0 and 1</param>
/// <param name="g">The color's green value, between 0 and 1</param> /// <param name="g">The color's green value, between 0 and 1</param>
@ -64,6 +72,57 @@ namespace Illumilib {
ForEach(s => s.SetKeyboardLighting(r, g, b)); ForEach(s => s.SetKeyboardLighting(r, g, b));
} }
/// <summary>
/// Sets the lighting for the given x, y position on the keyboard to the given color.
/// The position is zero-based, with 0, 0 being in the top left corner of the keyboard.
/// </summary>
/// <param name="x">The zero-based x position of the key</param>
/// <param name="y">The zero-based y position of the key</param>
/// <param name="r">The color's red value, between 0 and 1</param>
/// <param name="g">The color's green value, between 0 and 1</param>
/// <param name="b">The color's blue value, between 0 and 1</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if the positions are out of range in relation to <see cref="KeyboardWidth"/> and <see cref="KeyboardHeight"/></exception>
public static void SetKeyboardLighting(int x, int y, float r, float g, float b) {
if (x < 0 || x >= KeyboardWidth)
throw new ArgumentOutOfRangeException(nameof(x));
if (y < 0 || y >= KeyboardHeight)
throw new ArgumentOutOfRangeException(nameof(y));
ForEach(s => s.SetKeyboardLighting(x, y, r, g, b));
}
/// <summary>
/// Sets the lighting in the given area on the keyboard to the given color.
/// The position is zero-based, with 0, 0 being in the top left corner of the keyboard.
/// The position is the top left corner of the rectangle that represents the area to set colors in.
/// </summary>
/// <param name="x">The zero-based x position of the key</param>
/// <param name="y">The zero-based y position of the key</param>
/// <param name="width">The width of the area to set the color in</param>
/// <param name="height">The height of the area to set the color in</param>
/// <param name="r">The color's red value, between 0 and 1</param>
/// <param name="g">The color's green value, between 0 and 1</param>
/// <param name="b">The color's blue value, between 0 and 1</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if the positions are out of range in relation to <see cref="KeyboardWidth"/> and <see cref="KeyboardHeight"/></exception>
public static void SetKeyboardLighting(int x, int y, int width, int height, float r, float g, float b) {
if (x < 0 || x + width > KeyboardWidth)
throw new ArgumentOutOfRangeException(nameof(x));
if (y < 0 || y + height > KeyboardHeight)
throw new ArgumentOutOfRangeException(nameof(y));
ForEach(s => s.SetKeyboardLighting(x, y, width, height, r, g, b));
}
/// <summary>
/// Sets the lighting for the specified <see cref="KeyboardKeys"/> to the given color.
/// Only a single key can be specified at a time.
/// </summary>
/// <param name="key">The key value to set the lighting for</param>
/// <param name="r">The color's red value, between 0 and 1</param>
/// <param name="g">The color's green value, between 0 and 1</param>
/// <param name="b">The color's blue value, between 0 and 1</param>
public static void SetKeyboardLighting(KeyboardKeys key, float r, float g, float b) {
ForEach(s => s.SetKeyboardLighting(key, r, g, b));
}
/// <summary> /// <summary>
/// Sets the lighting for all mice to the given color /// Sets the lighting for all mice to the given color
/// </summary> /// </summary>
@ -74,18 +133,6 @@ namespace Illumilib {
ForEach(s => s.SetMouseLighting(r, g, b)); ForEach(s => s.SetMouseLighting(r, g, b));
} }
/// <summary>
/// Sets the lighting for the specified <see cref="KeyboardKeys"/> to the given color.
/// Only a single key can be specified at a time.
/// </summary>
/// <param name="key">The key value to set the lighting for</param>
/// <param name="r">The color's red value, between 0 and 1</param>
/// <param name="g">The color's green value, between 0 and 1</param>
/// <param name="b">The color's blue value, between 0 and 1</param>
public static void SetKeyLighting(KeyboardKeys key, float r, float g, float b) {
ForEach(s => s.SetKeyLighting(key, r, g, b));
}
private static void ForEach(Action<LightingSystem> action) { private static void ForEach(Action<LightingSystem> action) {
if (!Initialized) if (!Initialized)
throw new InvalidOperationException("Illumilib has not been initialized yet"); throw new InvalidOperationException("Illumilib has not been initialized yet");

View file

@ -12,9 +12,13 @@ namespace Illumilib.System {
public abstract void SetKeyboardLighting(float r, float g, float b); public abstract void SetKeyboardLighting(float r, float g, float b);
public abstract void SetMouseLighting(float r, float g, float b); public abstract void SetKeyboardLighting(int x, int y, float r, float g, float b);
public abstract void SetKeyLighting(KeyboardKeys key, float r, float g, float b); public abstract void SetKeyboardLighting(int x, int y, int width, int height, float r, float g, float b);
public abstract void SetKeyboardLighting(KeyboardKeys key, float r, float g, float b);
public abstract void SetMouseLighting(float r, float g, float b);
} }
} }

View file

@ -4,6 +4,9 @@ using Illumilib.Lib;
namespace Illumilib.System { namespace Illumilib.System {
internal class LogitechLighting : LightingSystem { internal class LogitechLighting : LightingSystem {
private readonly byte[] bitmap = new byte[LogitechGsdk.LogiLedBitmapSize];
private bool bitmapDirty;
public override Task<bool> Initialize() { public override Task<bool> Initialize() {
try { try {
LogitechGsdk.LogiLedInit(); LogitechGsdk.LogiLedInit();
@ -14,25 +17,65 @@ namespace Illumilib.System {
} }
public override void Dispose() { public override void Dispose() {
this.ClearBitmap();
LogitechGsdk.LogiLedShutdown(); LogitechGsdk.LogiLedShutdown();
} }
public override void SetAllLighting(float r, float g, float b) { public override void SetAllLighting(float r, float g, float b) {
LogitechGsdk.LogiLedSetLighting((int) (r * 100F), (int) (g * 100F), (int) (b * 100F)); this.ClearBitmap();
LogitechGsdk.LogiLedSetLighting((int) (r * 100), (int) (g * 100), (int) (b * 100));
} }
public override void SetKeyboardLighting(float r, float g, float b) { public override void SetKeyboardLighting(float r, float g, float b) {
for (var i = 0; i <= 3; i++) // setting keyboard zone lighting doesn't work for some keyboards, so we fill the bitmap instead
LogitechGsdk.LogiLedSetLightingForTargetZone(DeviceType.Keyboard, i, (int) (r * 100F), (int) (g * 100F), (int) (b * 100F)); for (var x = 0; x < LogitechGsdk.LogiLedBitmapWidth; x++) {
for (var y = 0; y < LogitechGsdk.LogiLedBitmapHeight; y++)
this.SetBitmapColor(x, y, r, g, b, 1);
}
LogitechGsdk.LogiLedSetLightingFromBitmap(this.bitmap);
}
public override void SetKeyboardLighting(int x, int y, float r, float g, float b) {
this.SetBitmapColor(x, y, r, g, b, 1);
LogitechGsdk.LogiLedSetLightingFromBitmap(this.bitmap);
}
public override void SetKeyboardLighting(int x, int y, int width, int height, float r, float g, float b) {
for (var xAdd = 0; xAdd < width; xAdd++) {
for (var yAdd = 0; yAdd < height; yAdd++)
this.SetBitmapColor(x + xAdd, y + yAdd, r, g, b, 1);
}
LogitechGsdk.LogiLedSetLightingFromBitmap(this.bitmap);
}
public override void SetKeyboardLighting(KeyboardKeys key, float r, float g, float b) {
this.ClearBitmap();
LogitechGsdk.LogiLedSetLightingForKeyWithKeyName(ConvertKey(key), (int) (r * 100), (int) (g * 100), (int) (b * 100));
} }
public override void SetMouseLighting(float r, float g, float b) { public override void SetMouseLighting(float r, float g, float b) {
for (var i = 0; i <= 2; i++) for (var i = 0; i <= 2; i++)
LogitechGsdk.LogiLedSetLightingForTargetZone(DeviceType.Mouse, i, (int) (r * 100F), (int) (g * 100F), (int) (b * 100F)); LogitechGsdk.LogiLedSetLightingForTargetZone(DeviceType.Mouse, i, (int) (r * 100), (int) (g * 100), (int) (b * 100));
} }
public override void SetKeyLighting(KeyboardKeys key, float r, float g, float b) { private void SetBitmapColor(int x, int y, float r, float g, float b, float a) {
LogitechGsdk.LogiLedSetLightingForKeyWithKeyName(ConvertKey(key), (int) (r * 100F), (int) (g * 100F), (int) (b * 100F)); // since illumilib constants are a bit bigger than logi's constants, we need to make sure here
if (x >= LogitechGsdk.LogiLedBitmapWidth || y >= LogitechGsdk.LogiLedBitmapHeight)
return;
var i = LogitechGsdk.LogiLedBitmapBytesPerKey * (y * LogitechGsdk.LogiLedBitmapWidth + x);
this.bitmap[i + 0] = (byte) (b * 255);
this.bitmap[i + 1] = (byte) (g * 255);
this.bitmap[i + 2] = (byte) (r * 255);
this.bitmap[i + 3] = (byte) (a * 255);
this.bitmapDirty = true;
}
private void ClearBitmap() {
if (this.bitmapDirty) {
for (var i = 0; i < this.bitmap.Length; i++)
this.bitmap[i] = 0;
this.bitmapDirty = false;
}
} }
private static KeyboardNames ConvertKey(KeyboardKeys key) { private static KeyboardNames ConvertKey(KeyboardKeys key) {

View file

@ -6,11 +6,13 @@ using Colore.Effects.Keyboard;
namespace Illumilib.System { namespace Illumilib.System {
internal class RazerLighting : LightingSystem { internal class RazerLighting : LightingSystem {
private static IChroma chroma; private IChroma chroma;
private CustomKeyboardEffect effect = new CustomKeyboardEffect(Color.Black);
private bool effectOutdated;
public override async Task<bool> Initialize() { public override async Task<bool> Initialize() {
try { try {
chroma = await ColoreProvider.CreateNativeAsync(); this.chroma = await ColoreProvider.CreateNativeAsync();
return true; return true;
} catch { } catch {
return false; return false;
@ -18,23 +20,49 @@ namespace Illumilib.System {
} }
public override void Dispose() { public override void Dispose() {
chroma?.UninitializeAsync(); this.chroma.UninitializeAsync();
this.effectOutdated = true;
} }
public override void SetAllLighting(float r, float g, float b) { public override void SetAllLighting(float r, float g, float b) {
chroma?.SetAllAsync(new Color(r, g, b)); this.chroma.SetAllAsync(new Color(r, g, b));
this.effectOutdated = true;
} }
public override void SetKeyboardLighting(float r, float g, float b) { public override void SetKeyboardLighting(float r, float g, float b) {
chroma?.Keyboard.SetAllAsync(new Color(r, g, b)); this.chroma.Keyboard?.SetAllAsync(new Color(r, g, b));
this.effectOutdated = true;
}
public override void SetKeyboardLighting(int x, int y, float r, float g, float b) {
this.chroma.Keyboard?.SetPositionAsync(y, x, new Color(r, g, b));
this.effectOutdated = true;
}
public override void SetKeyboardLighting(int x, int y, int width, int height, float r, float g, float b) {
if (this.chroma.Keyboard == null)
return;
if (this.effectOutdated) {
for (var fullX = 0; fullX < KeyboardConstants.MaxColumns; fullX++) {
for (var fullY = 0; fullY < KeyboardConstants.MaxRows; fullY++)
this.effect[fullY, fullX] = this.chroma.Keyboard[fullY, fullX];
}
this.effectOutdated = false;
}
for (var xAdd = 0; xAdd < width; xAdd++) {
for (var yAdd = 0; yAdd < height; yAdd++)
this.effect[y + yAdd, x + xAdd] = new Color(r, g, b);
}
this.chroma.Keyboard.SetCustomAsync(this.effect);
}
public override void SetKeyboardLighting(KeyboardKeys key, float r, float g, float b) {
this.chroma.Keyboard?.SetKeyAsync(ConvertKey(key), new Color(r, g, b));
this.effectOutdated = true;
} }
public override void SetMouseLighting(float r, float g, float b) { public override void SetMouseLighting(float r, float g, float b) {
chroma?.Mouse.SetAllAsync(new Color(r, g, b)); this.chroma.Mouse?.SetAllAsync(new Color(r, g, b));
}
public override void SetKeyLighting(KeyboardKeys key, float r, float g, float b) {
chroma.Keyboard?.SetKeyAsync(ConvertKey(key), new Color(r, g, b));
} }
private static Key ConvertKey(KeyboardKeys key) { private static Key ConvertKey(KeyboardKeys key) {

View file

@ -1,4 +1,4 @@
# Illumilib # Illumilib
A simple keyboard and mouse lighting library with support for Razer and Logitech devices. A simple keyboard and mouse lighting library with support for Razer and Logitech devices.
- See the [Demo](https://github.com/Ellpeck/Illumilib/blob/main/Demo/Program.cs) for a demonstration.
- See [IllumilibLighting](https://github.com/Ellpeck/Illumilib/blob/main/Illumilib/IllumilibLighting.cs) for the API documentation. See the [Demo](https://github.com/Ellpeck/Illumilib/blob/main/Demo/Program.cs) to check out the features, or the [IllumilibLighting](https://github.com/Ellpeck/Illumilib/blob/main/Illumilib/IllumilibLighting.cs) class for API documentation.