Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Durgod Nebula K320 RGB support #16

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions Project-Aurora/Project-Aurora/Devices/Durgod/DurgodDevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using Aurora.Settings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using HidSharp;

namespace Aurora.Devices.Durgod
{
class DurgodDevice : DefaultDevice
{
public override string DeviceName => "Durgod";

HidDevice durgodKeyboard;
HidStream packetStream;

public byte[] colorCodeBytes = new byte[378];
public byte[] prevColorCodeBytes = new byte[378];
private int currentKeyOffset;


public override bool Initialize()
{

foreach (int keyboardID in DurgodRGBMappings.KeyboardIDs)
{
durgodKeyboard = GetDurgodKeyboard(DurgodRGBMappings.DurgodID, keyboardID);
if (durgodKeyboard != null)
{
try
{
IsInitialized = durgodKeyboard.TryOpen(out packetStream);
packetStream.Write(DurgodRGBMappings.RgbOff);
for (int c = 0; c < colorCodeBytes.Length; c++) colorCodeBytes[c] = 0xff;
}
catch
{
IsInitialized = false;
}
break;
}
else
{
IsInitialized = false;
}
}

return IsInitialized;
}

public override void Shutdown()
{
if (!IsInitialized)
return;
try
{
packetStream.Write(DurgodRGBMappings.RgbOn);
}
catch { }

packetStream?.Dispose();
packetStream?.Close();
IsInitialized = false;
}


public override bool UpdateDevice(Dictionary<DeviceKeys, Color> keyColors, DoWorkEventArgs e, bool forced = false)
{

int ROW_LENGTH = 42;
int ROW_COUNT = 9;
foreach (KeyValuePair<DeviceKeys, Color> kc in keyColors)
{

if (!DurgodRGBMappings.DurgodColourOffsetMap.TryGetValue(kc.Key, out currentKeyOffset))
{
continue;
}
colorCodeBytes[currentKeyOffset * 3] = (byte)kc.Value.R;
colorCodeBytes[currentKeyOffset * 3 + 1] = (byte)kc.Value.G;
colorCodeBytes[currentKeyOffset * 3 + 2] = (byte)kc.Value.B;
}

if (!prevColorCodeBytes.SequenceEqual(colorCodeBytes))
{
//Firstly we must send message about starting color changing
packetStream.Write(DurgodRGBMappings.RgbColormapStartMessage);
//Durgod requires 9 separated message for each key block
for (int row = 0; row < ROW_COUNT; row++)
{
int start = row * ROW_LENGTH;
int end = (row + 1) * ROW_LENGTH;

byte[] segment = new byte[end - start];


byte[] colourHeader = { 0x00, 0x03, 0x18, 0x08, (byte)row };
byte[] header = new byte[47];

//Let's compose your message for keyboard
//Firstly we need magic bytes for color change
Array.Copy(colourHeader, 0, header, 0, colourHeader.Length);
//Secondly we need a slice which will appropriate to row
Array.Copy(colorCodeBytes, start, segment, 0, segment.Length);
//Finally we copy this slice to final message
Array.Copy(segment, 0, header, 5, segment.Length);
packetStream.Write(header);

}

colorCodeBytes.CopyTo(prevColorCodeBytes, 0);

packetStream.Write(DurgodRGBMappings.RgbColormapEndMessage);
}

return true;
}

private HidDevice GetDurgodKeyboard(int VID, int PID) => DeviceList.Local.GetHidDevices(VID, PID).FirstOrDefault(HidDevice => HidDevice.GetMaxInputReportLength() == 65);
}
}
130 changes: 130 additions & 0 deletions Project-Aurora/Project-Aurora/Devices/Durgod/DurgodRGBMappings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System.Collections.Generic;

namespace Aurora.Devices.Durgod
{
static class DurgodRGBMappings
{
public const int DurgodID = 0x2f68;
public static int[] KeyboardIDs = new int[]
{
0x0082, // Durgod K310
0x0081 // Durgod K320
};

public static byte[] RgbOff => new byte[] { 0x00, 0x03, 0x06, 0x86, 0x01 };
public static byte[] RgbOn => new byte[] { 0x00, 0x03, 0x06, 0x86, 0x00 };
public static byte[] RgbColormapStartMessage => new byte[] { 0x00, 0x03, 0x19, 0x66 };
public static byte[] RgbColormapRowMessage => new byte[] { 0x00, 0x03, 0x18, 0x08 };
public static byte[] RgbColormapEndMessage => new byte[] { 0x00, 0x03, 0x19, 0x88 };

//Keyboard represents as matrix of keys
public static int HEIGHT = 16;
public static int WIDTH = 8;

public static Dictionary<DeviceKeys, int> DurgodColourOffsetMap => new Dictionary<DeviceKeys, int>
{
{DeviceKeys.ESC, 0 * WIDTH + 0 },

{DeviceKeys.F1, 0 * WIDTH + 2 },
{DeviceKeys.F2, 0 * WIDTH + 3 },
{DeviceKeys.F3, 0 * WIDTH + 4 },
{DeviceKeys.F4, 0 * WIDTH + 5 },
{DeviceKeys.F5, 0 * WIDTH + 6 },
{DeviceKeys.F6, 0 * WIDTH + 7 },
{DeviceKeys.F7, 1 * WIDTH + 0 },
{DeviceKeys.F8, 1 * WIDTH + 1 },
{DeviceKeys.F9, 1 * WIDTH + 2 },
{DeviceKeys.F10, 1 * WIDTH + 3 },
{DeviceKeys.F11, 1 * WIDTH + 4 },
{DeviceKeys.F12, 1 * WIDTH + 5 },
{DeviceKeys.PRINT_SCREEN, 1 * WIDTH + 6 },
{DeviceKeys.SCROLL_LOCK, 1 * WIDTH + 7 },
{DeviceKeys.PAUSE_BREAK, 2 * WIDTH + 0},

{DeviceKeys.TILDE, 2 * WIDTH + 5 },
{DeviceKeys.ONE, 2 * WIDTH + 6 },
{DeviceKeys.TWO, 2 * WIDTH + 7 },
{DeviceKeys.THREE, 3 * WIDTH + 0 },
{DeviceKeys.FOUR, 3 * WIDTH + 1 },
{DeviceKeys.FIVE, 3 * WIDTH + 2 },
{DeviceKeys.SIX, 3 * WIDTH + 3 },
{DeviceKeys.SEVEN, 3 * WIDTH + 4 },
{DeviceKeys.EIGHT, 3 * WIDTH + 5 },
{DeviceKeys.NINE, 3 * WIDTH + 6 },
{DeviceKeys.ZERO, 3 * WIDTH + 7 },
{DeviceKeys.MINUS, 4 * WIDTH + 0 },
{DeviceKeys.EQUALS, 4 * WIDTH + 1 },
{DeviceKeys.BACKSPACE, 4 * WIDTH + 2 },
{DeviceKeys.INSERT, 4 * WIDTH + 3 },
{DeviceKeys.HOME, 4 * WIDTH + 4 },
{DeviceKeys.PAGE_UP, 4 * WIDTH + 5 },

{DeviceKeys.TAB, 5 * WIDTH + 2 },
{DeviceKeys.Q, 5 * WIDTH + 3},
{DeviceKeys.W, 5 * WIDTH + 4},
{DeviceKeys.E, 5 * WIDTH + 5},
{DeviceKeys.R, 5 * WIDTH + 6},
{DeviceKeys.T, 5 * WIDTH + 7},
{DeviceKeys.Y, 6 * WIDTH + 0},
{DeviceKeys.U, 6 * WIDTH + 1},
{DeviceKeys.I, 6 * WIDTH + 2},
{DeviceKeys.O, 6 * WIDTH + 3},
{DeviceKeys.P, 6 * WIDTH + 4},
{DeviceKeys.OPEN_BRACKET, 6 * WIDTH + 5 },
{DeviceKeys.CLOSE_BRACKET, 6 * WIDTH + 6 },
{DeviceKeys.BACKSLASH, 6 * WIDTH + 7 },
{DeviceKeys.BACKSLASH_UK, 6 * WIDTH + 7 },


{DeviceKeys.CAPS_LOCK, 7 * WIDTH + 7 },
{DeviceKeys.A, 8 * WIDTH + 0},
{DeviceKeys.S, 8 * WIDTH + 1},
{DeviceKeys.D, 8 * WIDTH + 2},
{DeviceKeys.F, 8 * WIDTH + 3},
{DeviceKeys.G, 8 * WIDTH + 4},
{DeviceKeys.H, 8 * WIDTH + 5},
{DeviceKeys.J, 8 * WIDTH + 6},
{DeviceKeys.K, 8 * WIDTH + 7},
{DeviceKeys.L, 9 * WIDTH + 0},
{DeviceKeys.SEMICOLON, 9 * WIDTH + 1},
{DeviceKeys.APOSTROPHE, 9 * WIDTH + 2},
{DeviceKeys.HASHTAG, 9 * WIDTH + 3},
{DeviceKeys.ENTER, 9 * WIDTH + 4},
{DeviceKeys.DELETE, 7 * WIDTH + 0 },
{DeviceKeys.END, 7 * WIDTH + 1 },
{DeviceKeys.PAGE_DOWN, 7 * WIDTH + 2 },


{DeviceKeys.LEFT_SHIFT, 10 * WIDTH + 4 },
{DeviceKeys.Z, 10 * WIDTH + 6},
{DeviceKeys.X, 10 * WIDTH + 7},
{DeviceKeys.C, 11 * WIDTH + 0},
{DeviceKeys.V, 11 * WIDTH + 1},
{DeviceKeys.B, 11 * WIDTH + 2},
{DeviceKeys.N, 11 * WIDTH + 3},
{DeviceKeys.M, 11 * WIDTH + 4},
{DeviceKeys.COMMA, 11 * WIDTH + 5},
{DeviceKeys.PERIOD, 11 * WIDTH + 6 },
{DeviceKeys.FORWARD_SLASH, 11 * WIDTH + 7 },
{DeviceKeys.RIGHT_SHIFT, 12 * WIDTH + 1 },

{DeviceKeys.LEFT_CONTROL, 13 * WIDTH + 1 },
{DeviceKeys.LEFT_WINDOWS, 13 * WIDTH + 2 },
{DeviceKeys.LEFT_ALT, 13 * WIDTH + 3 },
{DeviceKeys.SPACE, 13 * WIDTH + 7 },
{DeviceKeys.RIGHT_ALT, 14 * WIDTH + 3 },
{DeviceKeys.FN_Key, 14 * WIDTH + 4 },
{DeviceKeys.RIGHT_WINDOWS, 14 * WIDTH + 5 }, //Context menu (???)
{DeviceKeys.APPLICATION_SELECT, 14 * WIDTH + 5 }, //Context menu (???)
{DeviceKeys.RIGHT_CONTROL, 14 * WIDTH + 6 },

{DeviceKeys.ARROW_UP, 12 * WIDTH + 3 },
{DeviceKeys.ARROW_DOWN, 15 * WIDTH + 0 },
{DeviceKeys.ARROW_LEFT, 14 * WIDTH + 7 },
{DeviceKeys.ARROW_RIGHT, 15 * WIDTH + 1 },

};


}
}
5 changes: 4 additions & 1 deletion Project-Aurora/Project-Aurora/Settings/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,10 @@ public enum PreferredKeyboard
//HyperX range is 1400-1499
[Description("HyperX Alloy Elite RGB")]
HyperX_Alloy_Elite_RGB = 1400,


//Durgod range is 1500-1599
[Description("Durgod K320 Nebula RGB")]
Durgod_K320_RGB = 1500,
}

public enum PreferredKeyboardLocalization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,8 @@ public void LoadBrand(PreferredKeyboard keyboard_preference = PreferredKeyboard.
layoutConfigPath = Path.Combine(layoutsPath, "omen_four_zone.json");
else if (keyboard_preference == PreferredKeyboard.HyperX_Alloy_Elite_RGB)
layoutConfigPath = Path.Combine(layoutsPath, "hyperx_alloy_elite_rgb.json");
else if (keyboard_preference == PreferredKeyboard.Durgod_K320_RGB)
layoutConfigPath = Path.Combine(layoutsPath, "durgod_k320_nebula_rgb.json");

else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"group_tag": "keyboard",
"origin_region": 1,
"grouped_keys": [
{
"visualName": "\\",
"tag": 51,
"margin_left": 500,
"margin_top": 74,
"width": 47.0,
"height": 30.0,
"font_size": 9.0,
"width_bits": 5,
"height_bits": 3,
"margin_left_bits": 0,
"margin_top_bits": 6,
"enabled": true,
"absolute_location": true
}
],
"key_conversion": {
77: 51
}
}
Loading