Refactor HyperX Alloy Origins Core to use KLM

This commit is contained in:
Carlos Jordão
2025-11-03 22:53:46 +00:00
committed by Adam Honse
parent 665069d98f
commit f0a1ed2c0f
5 changed files with 118 additions and 178 deletions

View File

@@ -93,7 +93,7 @@ unsigned int HyperXAlloyOriginsCoreController::GetVariant()
if(actual > 0)
variant = packet[56];
else
variant = 0x09;
variant = 0;
LOG_DEBUG("[HyperX Alloy Origins Core] variant: 0x%02X", variant);
return variant;
@@ -112,8 +112,7 @@ void HyperXAlloyOriginsCoreController::SetBrightness(unsigned int brightness)
hid_write(dev, packet, 65);
}
void HyperXAlloyOriginsCoreController::SetLEDsDirect(std::vector<RGBColor> colors)
void HyperXAlloyOriginsCoreController::SetLEDsDirect(std::vector<led> leds, std::vector<RGBColor> colors)
{
/*------------------------------------------------------------------------------*\
| * Always send 380 bytes to the keyboard and a total of 94 led indexes. |
@@ -132,8 +131,7 @@ void HyperXAlloyOriginsCoreController::SetLEDsDirect(std::vector<RGBColor> color
\*------------------------------------------------------------------------------*/
unsigned int segment = 0, sector = 0, sequence = 0;
unsigned int total_colors = 0;
unsigned char buf[380];
memset(buf, 0x00, sizeof(buf));
memset(color_buf, 0x00, sizeof(color_buf));
/*---------------------------------------------------------------------------*\
| transfer the colors to the buffer. Max 94 colors to avoid buffer overflow. |
@@ -147,22 +145,27 @@ void HyperXAlloyOriginsCoreController::SetLEDsDirect(std::vector<RGBColor> color
total_colors = (unsigned int)colors.size();
}
for(unsigned int color_idx = 0; color_idx < total_colors; color_idx++)
unsigned int pos = 0, color_idx = 0;
for(unsigned int i = 0; i < total_colors; i++)
{
unsigned int pos = 0;
segment = (color_idx / 12) * 48;
sector = ((color_idx / 6) & 1) * 8;
sequence = color_idx % 6;
color_idx = leds[i].value;
segment = (color_idx / 12) * 48;
sector = ((color_idx / 6) & 1) * 8;
sequence = color_idx % 6;
pos = segment + sector + sequence;
pos = segment + sector + sequence;
buf[pos ] = RGBGetGValue(colors[color_idx]);
buf[pos + 16] = RGBGetRValue(colors[color_idx]);
buf[pos + 32] = RGBGetBValue(colors[color_idx]);
color_buf[pos ] = RGBGetGValue(colors[i]);
color_buf[pos + 16] = RGBGetRValue(colors[i]);
color_buf[pos + 32] = RGBGetBValue(colors[i]);
}
}
void HyperXAlloyOriginsCoreController::SendRGBToDevice()
{
unsigned int sentBytes = 0;
unsigned int bytesToSend = sizeof(buf);
unsigned int bytesToSend = sizeof(color_buf);
unsigned int payloadSize = 60;
unsigned int seq = 0;
@@ -180,7 +183,7 @@ void HyperXAlloyOriginsCoreController::SetLEDsDirect(std::vector<RGBColor> color
packet[2] = seq++;
packet[4] = payloadSize;
memcpy(&packet[5], &buf[sentBytes], payloadSize);
memcpy(&packet[5], &color_buf[sentBytes], payloadSize);
hid_write(dev, packet, payloadSize + 5);
sentBytes += payloadSize;

View File

@@ -15,6 +15,9 @@
#include <hidapi.h>
#include "RGBController.h"
#define HYPERX_ALLOY_ORIGINS_CORE_ANSI 0x09
#define HYPERX_ALLOY_ORIGINS_CORE_ABNT2 0x10
class HyperXAlloyOriginsCoreController
{
public:
@@ -27,7 +30,8 @@ public:
std::string GetFirmwareVersion();
unsigned int GetVariant();
void SetLEDsDirect(std::vector<RGBColor> colors);
void SetLEDsDirect(std::vector<led> leds, std::vector<RGBColor> colors);
void SendRGBToDevice();
void SetBrightness(unsigned int brightness);
private:
@@ -35,4 +39,5 @@ private:
std::string location;
std::string firmware_version;
std::string name;
unsigned char color_buf[380];
};

View File

@@ -12,6 +12,7 @@
#include "RGBControllerKeyNames.h"
#include "RGBController_HyperXAlloyOriginsCore.h"
#include "KeyboardLayoutManager.h"
using namespace std::chrono_literals;
@@ -24,16 +25,6 @@ using namespace std::chrono_literals;
| Maps LED position number to keys |
| * based on ANSI QWERTY |
\*----------------------------------*/
static unsigned int matrix_map[6][19] =
{
{ 0, NA, 1, 2, 3, 4, 5, 6, 7, 48, NA, 49, 50, 51, 52, NA, 53, 54, 55 },
{ 8, 9, 10, 11, 12, 13, 14, 15, 16, 56, 57, 58, 59, 60, NA, NA, 61, 62, 63 },
{ 17, NA, 18, 19, 20, 21, 22, 23, 24, 64, 65, 66, 67, 68, 69, NA, 70, 71, 72 },
{ 25, NA, 26, 27, 28, 29, 30, 31, 32, 73, 74, 75, 76, 78, NA, NA, NA, NA, NA },
{ 33, NA, 35, 36, 37, 38, 39, 40, 79, 80, 81, NA, 83, 84, NA, NA, NA, 85, NA },
{ 41, 42, 43, NA, NA, NA, 45, NA, NA, NA, NA, 86, 87, 88, 89, NA, 90, 91, 92 }
};
static const char* zone_names[] =
{
ZONE_EN_KEYBOARD,
@@ -46,131 +37,46 @@ static zone_type zone_types[] =
/*--------------------------------------------------------------------------------*\
| This keyboard (TKL) always receives 93 led colors. |
| This keyboard (TKL) always receives 94 led colors. |
| * Some indexes are just blank (unused). |
| * Regional layouts have a few different enabled or disabled led indexes. |
| * All indexes need to be sent anyway. Declaring all of them makes it easier to |
| add new layouts without creating a new matrix map. |
| * led_names contains the indexes used by the HyperX keyboard |
| * Below there is the association of the led indexes and the keyboard keys for |
| DEFAULT layout. |
\*--------------------------------------------------------------------------------*/
struct led_index
std::vector<unsigned int> hyperx_core_default_values
{
const char *name;
unsigned int index;
0, 1, 2, 3, 4, 5, 6, 7, 48, 49, 50, 51, 52, 53, 54, 55,
8, 9, 10, 11, 12, 13, 14, 15, 16, 56, 57, 58, 59, 60, 61, 62, 63,
17, 18, 19, 20, 21, 22, 23, 24, 64, 65, 66, 67, 68, 69, 70, 71, 72,
25, 26, 27, 28, 29, 30, 31, 32, 73, 74, 75, 76, 77, 78,
33, 34, 35, 36, 37, 38, 39, 40, 79, 80, 81, 83, 84, 85,
41, 42, 43, 45, 86, 87, 88, 89, 90, 91, 92,
};
static led_index led_names[] =
layout_values hyperx_core_layout
{
{KEY_EN_ESCAPE, 0},
{KEY_EN_F1, 1},
{KEY_EN_F2, 2},
{KEY_EN_F3, 3},
{KEY_EN_F4, 4},
{KEY_EN_F5, 5},
{KEY_EN_F6, 6},
{KEY_EN_F7, 7},
hyperx_core_default_values,
{
},
};
{KEY_EN_BACK_TICK, 8},
{KEY_EN_1, 9},
{KEY_EN_2, 10},
{KEY_EN_3, 11},
{KEY_EN_4, 12},
{KEY_EN_5, 13},
{KEY_EN_6, 14},
{KEY_EN_7, 15},
{KEY_EN_8, 16},
{KEY_EN_TAB, 17},
{KEY_EN_Q, 18},
{KEY_EN_W, 19},
{KEY_EN_E, 20},
{KEY_EN_R, 21},
{KEY_EN_T, 22},
{KEY_EN_Y, 23},
{KEY_EN_U, 24},
{KEY_EN_CAPS_LOCK, 25},
{KEY_EN_A, 26},
{KEY_EN_S, 27},
{KEY_EN_D, 28},
{KEY_EN_F, 29},
{KEY_EN_G, 30},
{KEY_EN_H, 31},
{KEY_EN_J, 32},
{KEY_EN_LEFT_SHIFT, 33},
{KEY_EN_UNUSED, 34},
{KEY_EN_Z, 35},
{KEY_EN_X, 36},
{KEY_EN_C, 37},
{KEY_EN_V, 38},
{KEY_EN_B, 39},
{KEY_EN_N, 40},
{KEY_EN_LEFT_CONTROL, 41},
{KEY_EN_LEFT_WINDOWS, 42},
{KEY_EN_LEFT_ALT, 43},
{KEY_EN_UNUSED, 44},
{KEY_EN_SPACE, 45},
{KEY_EN_UNUSED, 46},
{KEY_EN_UNUSED, 47},
// End of first section
{KEY_EN_F8, 48},
{KEY_EN_F9, 49},
{KEY_EN_F10, 50},
{KEY_EN_F11, 51},
{KEY_EN_F12, 52},
{KEY_EN_PRINT_SCREEN, 53},
{KEY_EN_SCROLL_LOCK, 54},
{KEY_EN_PAUSE_BREAK, 55},
{KEY_EN_9, 56},
{KEY_EN_0, 57},
{KEY_EN_MINUS, 58},
{KEY_EN_EQUALS, 59},
{KEY_EN_BACKSPACE, 60},
{KEY_EN_INSERT, 61},
{KEY_EN_HOME, 62},
{KEY_EN_PAGE_UP, 63},
{KEY_EN_I, 64},
{KEY_EN_O, 65},
{KEY_EN_P, 66},
{KEY_EN_LEFT_BRACKET, 67},
{KEY_EN_RIGHT_BRACKET, 68},
{KEY_EN_ANSI_BACK_SLASH, 69},
{KEY_EN_DELETE, 70},
{KEY_EN_END, 71},
{KEY_EN_PAGE_DOWN, 72},
{KEY_EN_K, 73},
{KEY_EN_L, 74},
{KEY_EN_SEMICOLON, 75},
{KEY_EN_QUOTE, 76},
{KEY_EN_POUND, 77},
{KEY_EN_ANSI_ENTER, 78},
{KEY_EN_M, 79},
{KEY_EN_COMMA, 80},
{KEY_EN_PERIOD, 81},
{KEY_EN_UNUSED, 82},
{KEY_EN_FORWARD_SLASH, 83},
{KEY_EN_RIGHT_SHIFT, 84},
{KEY_EN_UP_ARROW, 85},
{KEY_EN_RIGHT_ALT, 86},
{KEY_EN_RIGHT_FUNCTION, 87},
{KEY_EN_MENU, 88},
{KEY_EN_RIGHT_CONTROL, 89},
{KEY_EN_LEFT_ARROW, 90},
{KEY_EN_DOWN_ARROW, 91},
{KEY_EN_RIGHT_ARROW, 92}
/*--------------------------------------------*\
| Provide values to keys that has been changed |
| in DEFAULT layout. |
\*--------------------------------------------*/
std::map<KEYBOARD_LAYOUT, key_set> regional_overlay_abnt2
{
{
KEYBOARD_LAYOUT_ABNT2,
{
/*-------------------------------------------------------------------------------------------------------------------------------------*\
| Edit Keys |
| Zone, Row, Column, Value, Key, Alternate Name, OpCode, |
\*-------------------------------------------------------------------------------------------------------------------------------------*/
{ 0, 4, 11, 82, KEY_EN_SEMICOLON, KEY_EN_UNUSED, KEYBOARD_OPCODE_SWAP_ONLY, },
},
}
};
/**------------------------------------------------------------------*\
@@ -241,8 +147,9 @@ RGBController_HyperXAlloyOriginsCore::~RGBController_HyperXAlloyOriginsCore()
void RGBController_HyperXAlloyOriginsCore::SetupZones()
{
const unsigned int total_leds = sizeof(led_names) / sizeof(led_names[0]);
unsigned int total_leds = 0;
zone new_zone;
KEYBOARD_LAYOUT layout_name;
for(unsigned int zone_idx = 0; zone_idx < 1; zone_idx++)
{
@@ -250,38 +157,24 @@ void RGBController_HyperXAlloyOriginsCore::SetupZones()
new_zone.type = zone_types[zone_idx];
/*-----------------------------------------------------*\
| Regional configuration can be done by setting |
| the relevant keys based on the led map. |
| Regional configuration |
| * variant is extracted from keyboard info |
\*-----------------------------------------------------*/
if(variant == HYPERX_ALLOY_ORIGINS_CORE_ABNT2)
switch(variant)
{
// Sets the led indexes of this variant
matrix_map[2][14] = NA;
matrix_map[3][13] = 77; // ]
matrix_map[3][14] = 78; // enter
matrix_map[4][ 1] = 34; // backslash
matrix_map[4][11] = 82; // ;
case HYPERX_ALLOY_ORIGINS_CORE_ABNT2:
layout_name = KEYBOARD_LAYOUT_ABNT2;
hyperx_core_layout.regional_overlay = regional_overlay_abnt2;
break;
// corrects the visual representantion
led_names[ 8].name = KEY_EN_QUOTE;
led_names[34].name = KEY_EN_ISO_BACK_SLASH;
led_names[67].name = KEY_NORD_ACUTE_GRAVE;
led_names[68].name = KEY_EN_LEFT_BRACKET;
led_names[69].name = KEY_EN_UNUSED;
led_names[75].name = KEY_FR_CEDILLA_C;
led_names[76].name = KEY_BR_TILDE;
led_names[77].name = KEY_EN_RIGHT_BRACKET;
led_names[78].name = KEY_EN_ISO_ENTER;
led_names[82].name = KEY_EN_SEMICOLON;
case HYPERX_ALLOY_ORIGINS_CORE_ANSI:
default:
layout_name = KEYBOARD_LAYOUT_ANSI_QWERTY;
break;
}
KeyboardLayoutManager new_kb(layout_name, KEYBOARD_SIZE_TKL, hyperx_core_layout);
for(unsigned int led_idx = 0; led_idx < total_leds; led_idx++)
{
led new_led;
new_led.name = led_names[led_idx].name;
new_led.value = led_names[led_idx].index;
leds.push_back(new_led);
}
total_leds = new_kb.GetKeyCount();
matrix_map_type * keyboard_map = new matrix_map_type;
new_zone.leds_count = total_leds;
@@ -291,15 +184,27 @@ void RGBController_HyperXAlloyOriginsCore::SetupZones()
if(zone_types[zone_idx] == ZONE_TYPE_MATRIX)
{
new_zone.matrix_map = keyboard_map;
new_zone.matrix_map->height = 6;
new_zone.matrix_map->width = 19;
new_zone.matrix_map->map = (unsigned int *)&matrix_map;
}
keyboard_map->height = new_kb.GetRowCount();
keyboard_map->width = new_kb.GetColumnCount();
keyboard_map->map = new unsigned int[keyboard_map->height * keyboard_map->width];
new_kb.GetKeyMap(keyboard_map->map, KEYBOARD_MAP_FILL_TYPE_COUNT);
}
else
{
new_zone.matrix_map = NULL;
}
zones.push_back(new_zone);
for(unsigned int led_idx = 0; led_idx < total_leds; led_idx++)
{
led new_led;
new_led.name = new_kb.GetKeyAltNameAt(led_idx);
if(new_led.name == KEY_EN_UNUSED)
new_led.name = new_kb.GetKeyNameAt(led_idx);
new_led.value = new_kb.GetKeyValueAt(led_idx);
leds.push_back(new_led);
}
}
SetupColors();
}
@@ -313,7 +218,7 @@ void RGBController_HyperXAlloyOriginsCore::ResizeZone(int /*zone*/, int /*new_si
void RGBController_HyperXAlloyOriginsCore::DeviceUpdateLEDs()
{
controller->SetLEDsDirect(colors);
controller->SetLEDsDirect(leds, colors);
}
void RGBController_HyperXAlloyOriginsCore::UpdateZoneLEDs(int /*zone*/)
@@ -339,7 +244,7 @@ void RGBController_HyperXAlloyOriginsCore::KeepaliveThread()
{
if((std::chrono::steady_clock::now() - last_update_time) > std::chrono::milliseconds(50))
{
UpdateLEDs();
controller->SendRGBToDevice();
}
}
std::this_thread::sleep_for(10ms);

View File

@@ -20,6 +20,7 @@ const char* KEYBOARD_NAME_JIS = "JIS";
const char* KEYBOARD_NAME_AZERTY = "AZERTY";
const char* KEYBOARD_NAME_QWERTY = "QWERTY";
const char* KEYBOARD_NAME_QWERTZ = "QWERTZ";
const char* KEYBOARD_NAME_ABNT2 = "ABNT2";
const char* KEYBOARD_NAME_FULL = "Full 104 key ";
const char* KEYBOARD_NAME_TKL = "Tenkeyless ";
@@ -293,6 +294,26 @@ keyboard_keymap_overlay jis
}
};
keyboard_keymap_overlay abnt2
{
KEYBOARD_SIZE_FULL,
{
/*-------------------------------------------------------------------------------------------------------------------------------------*\
| Edit Keys |
| Zone, Row, Column, Value, Name, Alternate Name, OpCode |
\*-------------------------------------------------------------------------------------------------------------------------------------*/
{ 0, 1, 0, 0, KEY_EN_UNUSED, KEY_EN_QUOTE, KEYBOARD_OPCODE_ADD_ALT_NAME, },
{ 0, 2, 11, 0, KEY_EN_UNUSED, KEY_NORD_ACUTE_GRAVE, KEYBOARD_OPCODE_ADD_ALT_NAME, },
{ 0, 2, 12, 0, KEY_EN_UNUSED, KEY_EN_LEFT_BRACKET, KEYBOARD_OPCODE_ADD_ALT_NAME, },
{ 0, 3, 10, 0, KEY_EN_UNUSED, KEY_FR_CEDILLA_C, KEYBOARD_OPCODE_ADD_ALT_NAME, },
{ 0, 3, 11, 0, KEY_EN_UNUSED, KEY_BR_TILDE, KEYBOARD_OPCODE_ADD_ALT_NAME, },
{ 0, 3, 12, 0, KEY_EN_UNUSED, KEY_EN_RIGHT_BRACKET, KEYBOARD_OPCODE_ADD_ALT_NAME, },
{ 0, 4, 11, 0, KEY_EN_SEMICOLON, KEY_EN_UNUSED, KEYBOARD_OPCODE_INSERT_SHIFT_RIGHT, },
{ 0, 2, 13, 0, KEY_EN_UNUSED, KEY_EN_UNUSED, KEYBOARD_OPCODE_SWAP_ONLY, },
{ 0, 4, 13, 0, KEY_EN_UNUSED, KEY_EN_UNUSED, KEYBOARD_OPCODE_REMOVE_SHIFT_LEFT, },
}
};
KeyboardLayoutManager::KeyboardLayoutManager(KEYBOARD_LAYOUT layout, KEYBOARD_SIZE size) : KeyboardLayoutManager(layout, size, {})
{
}
@@ -387,6 +408,11 @@ KeyboardLayoutManager::KeyboardLayoutManager(KEYBOARD_LAYOUT layout, KEYBOARD_SI
ChangeKeys(jis);
tmp_name = KEYBOARD_NAME_JIS;
break;
case KEYBOARD_LAYOUT::KEYBOARD_LAYOUT_ABNT2:
ChangeKeys(abnt2);
tmp_name = KEYBOARD_NAME_ABNT2;
break;
}
/*---------------------------------------------------------------------*\

View File

@@ -50,6 +50,7 @@ enum KEYBOARD_LAYOUT
KEYBOARD_LAYOUT_ISO_QWERTZ,
KEYBOARD_LAYOUT_ISO_AZERTY,
KEYBOARD_LAYOUT_JIS,
KEYBOARD_LAYOUT_ABNT2,
};
enum KEYBOARD_MAP_FILL_TYPE