Add support for another Sinowealth keyboard with PID 0016

Commits squashed and amended for code style by Adam Honse <calcprogrammer1@gmail.com>
This commit is contained in:
Alexey Zagorodnikov 2021-09-03 02:34:25 +00:00 committed by Adam Honse
parent 70024faabd
commit 1045af20b2
6 changed files with 1106 additions and 7 deletions

View file

@ -0,0 +1,465 @@
/*------------------------------------------*\
| RGBController_SinowealthKeyboard16.cpp |
| |
| Definitions and types for Sinowealth |
| Keyboard with PID:0016, |
| Hopefully generic for this PID, |
| this was made spefically for ZUOYA X51 |
| |
| Zagorodnikov Aleksey (glooom) 26.07.2021 |
| based on initial implementation from |
| Dmitri Kalinichenko (Dima-Kal) 23/06/2021 |
\*-----------------------------------------=*/
#include "RGBController_SinowealthKeyboard16.h"
#include <algorithm>
#define NA 0xFFFFFFFF
using namespace kbd16;
static unsigned int matrix_map[6][22] =
{ { 0, NA, 2, 3, 4, 5, NA, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, NA, NA, NA, NA },
{ 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, NA, 36, 37, 38, 39, 40, 41, 42, 43 },
{ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, NA, 59, 60, 61, 62, 63, 64, 65 },
{ 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, NA, 79, NA, NA, NA, NA, 84, 85, 86, 87 },
{ 88, NA, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, NA, NA, 102, NA, 104, NA, 106, 107, 108, 109 },
{ 110, 111, 112, NA, NA, 115, NA, NA, 118, 119, 120, NA, 122, NA, NA, 125, 126, 127, 128, 129, 130, 131 }
};
static const char *led_names_tkl[] =
{
"Key: Escape",
"",
"Key: F1",
"Key: F2",
"Key: F3",
"Key: F4",
"",
"Key: F5",
"Key: F6",
"Key: F7",
"Key: F8",
"Key: F9",
"Key: F10",
"Key: F11",
"Key: F12",
"Key: Print Screen",
"Key: Scroll Lock",
"Key: Pause/Break",
"",
"",
"",
"",
"Key: `",
"Key: 1",
"Key: 2",
"Key: 3",
"Key: 4",
"Key: 5",
"Key: 6",
"Key: 7",
"Key: 8",
"Key: 9",
"Key: 0",
"Key: -",
"Key: =",
"Undefined",
"Key: Backspace",
"Key: Insert",
"Key: Home",
"Key: Page Up",
"Key: Num Lock",
"Key: Number Pad /",
"Key: Number Pad *",
"Key: Number Pad -",
"Key: Tab",
"Key: Q",
"Key: W",
"Key: E",
"Key: R",
"Key: T",
"Key: Y",
"Key: U",
"Key: I",
"Key: O",
"Key: P",
"Key: [",
"Key: ]",
"Key: \\ (ANSI)",
"",
"Key: Delete",
"Key: End",
"Key: Page Down",
"Key: Number Pad 7",
"Key: Number Pad 8",
"Key: Number Pad 9",
"Key: Number Pad +",
"Key: Caps Lock",
"Key: A",
"Key: S",
"Key: D",
"Key: F",
"Key: G",
"Key: H",
"Key: J",
"Key: K",
"Key: L",
"Key: ;",
"Key: '",
"",
"Key: Enter",
"",
"",
"",
"",
"Key: Number Pad 4",
"Key: Number Pad 5",
"Key: Number Pad 6",
"Key: Number Pad +",
"Key: Left Shift",
"",
"Key: Z",
"Key: X",
"Key: C",
"Key: V",
"Key: B",
"Key: N",
"Key: M",
"Key: ,",
"Key: .",
"Key: /",
"",
"",
"Key: Right Shift",
"",
"Key: Up Arrow",
"",
"Key: Number Pad 1",
"Key: Number Pad 2",
"Key: Number Pad 3",
"Key: Number Pad Enter",
"Key: Left Control",
"Key: Left Windows",
"Key: Left Alt",
"",
"",
"Key: Space",
"","",
"Key: Right Alt",
"Key: Right Fn",
"Key: Right Windows",
"",
"Key: Right Control",
"",
"",
"Key: Left Arrow",
"Key: Down Arrow",
"Key: Right Arrow",
"Key: Number Pad 0",
"Key: Number Pad 0",
"Key: Number Pad .",
"Key: Number Pad Enter"
};
RGBController_SinowealthKeyboard16::RGBController_SinowealthKeyboard16(SinowealthKeyboard16Controller* sinowealth_ptr)
{
sinowealth = sinowealth_ptr;
name = "Sinowealth Keyboard";
type = DEVICE_TYPE_KEYBOARD;
description = "Generic Sinowealth Keyboard";
location = sinowealth->GetLocation();
serial = sinowealth->GetSerialString();
std::vector<ModeCfg> modes_cfg = sinowealth->GetDeviceModes();
std::vector<ModeColorCfg> color_presets = sinowealth->GetDeviceColors();
int mode_id = 0;
for(const ModeCfg &cfg : modes_cfg)
{
mode Mode = getModeItem(mode_id);
Mode.brightness = cfg.brightness;
Mode.speed = cfg.speed;
Mode.direction = cfg.direction_left;
if(cfg.color == COLOR_PRESET_RANDOM)
{
Mode.color_mode = MODE_COLORS_RANDOM;
}
else
{
Mode.color_mode = MODE_COLORS_MODE_SPECIFIC;
}
ModeColorCfg mode_color = color_presets[mode_id];
for(unsigned int i = Mode.colors_min; i < Mode.colors_max; i++)
{
RGBColor color = (mode_color.preset[i].blue << 16) | (mode_color.preset[i].green << 8) | (mode_color.preset[i].red);
Mode.colors.push_back(color);
}
modes.push_back(Mode);
mode_id++;
}
/*-----------------------------------------------------------------*\
| This keyboard supports 5 configurable custom profiles. |
| The first one is named "Custom" and the others are "Custom 2" to |
| "Custom 5". Leaving the first one just "Custom" to adhere to |
| common mode names scheme. |
\*-----------------------------------------------------------------*/
for(unsigned int i = 0; i < 5; i++)
{
mode PerLed;
PerLed.name = "Custom";
if(i > 0)
{
PerLed.name += " " + std::to_string(i + 1);
}
PerLed.flags = MODE_FLAG_HAS_PER_LED_COLOR;
PerLed.color_mode = MODE_COLORS_PER_LED;
PerLed.value = MODE_PER_KEY1+i;
modes.push_back(PerLed);
}
SetupZones();
int device_mode = sinowealth->GetCurrentMode();
if(device_mode >= MODE_PER_KEY1)
{
device_mode = mode_id+(device_mode & 0x0F);
colors = sinowealth->GetPerLedColors();
}
active_mode = device_mode;
}
RGBController_SinowealthKeyboard16::~RGBController_SinowealthKeyboard16()
{
delete sinowealth;
}
mode RGBController_SinowealthKeyboard16::getModeItem(unsigned int mode_id)
{
mode Mode;
Mode.value = mode_id;
Mode.brightness_min = BRIGHTNESS_OFF;
Mode.brightness_max = BRIGHTNESS_FULL;
Mode.speed_min = SPEED_SLOW;
Mode.speed_max = SPEED_FASTEST;
Mode.colors_min = 0;
Mode.colors_max = COLOR_PRESETS_IN_MODE;
switch(mode_id)
{
case 0:
Mode.name = "Off";
Mode.flags = 0;
Mode.color_mode = MODE_COLORS_NONE;
break;
case 1:
Mode.name = "Spectrum Cycle";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS;
Mode.color_mode = MODE_COLORS_NONE;
break;
case 2:
Mode.name = "Breathing";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR | MODE_FLAG_HAS_RANDOM_COLOR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
case 3:
Mode.name = "Static";
Mode.flags = MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR | MODE_FLAG_HAS_RANDOM_COLOR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
case 4:
Mode.name = "Ripples Shining";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR | MODE_FLAG_HAS_RANDOM_COLOR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
case 5:
Mode.name = "Reactive";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR | MODE_FLAG_HAS_RANDOM_COLOR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
case 6:
Mode.name = "Flash Away";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR | MODE_FLAG_HAS_RANDOM_COLOR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
case 7:
Mode.name = "Sine Wave";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR | MODE_FLAG_HAS_RANDOM_COLOR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
case 8:
Mode.name = "Raindrops";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR | MODE_FLAG_HAS_RANDOM_COLOR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
case 9:
Mode.name = "Rainbow Wave";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_DIRECTION_LR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
case 10:
Mode.name = "Rainbow Wheel";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_DIRECTION_LR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
case 11:
Mode.name = "Adorn";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
case 12:
Mode.name = "Stars Twinkle";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR | MODE_FLAG_HAS_RANDOM_COLOR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
case 13:
Mode.name = "Shadow Disappear";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR | MODE_FLAG_HAS_RANDOM_COLOR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
case 14:
Mode.name = "Retro Snake";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR | MODE_FLAG_HAS_RANDOM_COLOR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
default:
Mode.name = "Unknown Mode";
Mode.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR | MODE_FLAG_HAS_RANDOM_COLOR | MODE_FLAG_HAS_PER_LED_COLOR;
Mode.color_mode = MODE_COLORS_RANDOM;
break;
}
return Mode;
}
void RGBController_SinowealthKeyboard16::SetupZones()
{
/*---------------------------------------------------------*\
| Set up zones |
\*---------------------------------------------------------*/
zone new_zone;
new_zone.name = "Keyboard";
new_zone.type = ZONE_TYPE_MATRIX;
new_zone.leds_count = sinowealth->GetLEDCount();
new_zone.leds_min = new_zone.leds_count;
new_zone.leds_max = new_zone.leds_count;
new_zone.matrix_map = new matrix_map_type;
new_zone.matrix_map->height = 6;
new_zone.matrix_map->width = 22;
new_zone.matrix_map->map = (unsigned int *)&matrix_map;
zones.push_back(new_zone);
/*---------------------------------------------------------*\
| Set up LEDs |
\*---------------------------------------------------------*/
for(unsigned int led_idx = 0; led_idx < new_zone.leds_count; led_idx++)
{
led new_led;
new_led.name = led_names_tkl[led_idx];
leds.push_back(new_led);
}
SetupColors();
}
void RGBController_SinowealthKeyboard16::ResizeZone(int /*zone*/, int /*new_size*/)
{
/*---------------------------------------------------------*\
| This device does not support resizing zones |
\*---------------------------------------------------------*/
}
void RGBController_SinowealthKeyboard16::DeviceUpdateLEDs()
{
sinowealth->SetLEDsDirect(colors);
}
void RGBController_SinowealthKeyboard16::UpdateZoneLEDs(int /*zone*/)
{
DeviceUpdateLEDs();
}
void RGBController_SinowealthKeyboard16::UpdateSingleLED(int /*led*/)
{
DeviceUpdateLEDs();
}
void RGBController_SinowealthKeyboard16::SetCustomMode()
{
/*---------------------------------------------------------*\
| Search the mode list and find Custom mode |
\*---------------------------------------------------------*/
for(unsigned int mode_idx = 0; mode_idx < modes.size(); mode_idx++)
{
if(modes[mode_idx].name == "Custom")
{
active_mode = mode_idx;
break;
}
}
}
void RGBController_SinowealthKeyboard16::DeviceUpdateMode()
{
mode ActiveMode = modes[active_mode];
int color_mode;
if(ActiveMode.color_mode == MODE_COLORS_MODE_SPECIFIC)
{
color_mode = COLOR_PRESET_0;
}
else
{
color_mode = COLOR_PRESET_RANDOM;
}
sinowealth->ClearMode();
if(ActiveMode.colors.size())
{
sinowealth->SetColorsForMode(ActiveMode.value, &ActiveMode.colors[0]);
}
sinowealth->SetMode(ActiveMode.value, ActiveMode.brightness, ActiveMode.speed, ActiveMode.direction, color_mode);
if(ActiveMode.value >= MODE_PER_KEY1)
{
colors = sinowealth->GetPerLedColors();
}
else
{
if(color_mode == COLOR_PRESET_RANDOM)
{
std::generate(colors.begin(), colors.end(), std::rand);
}
else
{
std::fill(colors.begin(), colors.end(), ActiveMode.colors[0]);
}
}
SignalUpdate();
}

View file

@ -0,0 +1,41 @@
/*------------------------------------------*\
| RGBController_SinowealthKeyboard16.h |
| |
| Definitions and types for Sinowealth |
| Keyboard with PID:0016, |
| Hopefully generic for this PID, |
| this was made spefically for ZUOYA X51 |
| |
| Zagorodnikov Aleksey (glooom) 26.07.2021 |
| based on initial implementation from |
| Dmitri Kalinichenko (Dima-Kal) 23/06/2021 |
\*-----------------------------------------=*/
#pragma once
#include "RGBController.h"
#include "SinowealthKeyboard16Controller.h"
class RGBController_SinowealthKeyboard16 : public RGBController
{
public:
RGBController_SinowealthKeyboard16(SinowealthKeyboard16Controller* sinowealth_ptr);
~RGBController_SinowealthKeyboard16();
void SetupZones();
void ResizeZone(int zone, int new_size);
void DeviceUpdateLEDs();
void UpdateZoneLEDs(int zone);
void UpdateSingleLED(int led);
void SetCustomMode();
void DeviceUpdateMode();
private:
SinowealthKeyboard16Controller* sinowealth;
mode getModeItem(unsigned int mode_id);
};

View file

@ -1,10 +1,13 @@
#include "Detector.h"
#include "SinowealthController.h"
#include "SinowealthKeyboardController.h"
#include "SinowealthKeyboard16Controller.h"
#include "RGBController.h"
#include "RGBController_Sinowealth.h"
#include "RGBController_SinowealthKeyboard.h"
#include "RGBController_SinowealthKeyboard16.h"
#include <hidapi/hidapi.h>
#include "LogManager.h"
#define SINOWEALTH_VID 0x258A
@ -12,6 +15,7 @@
#define Glorious_Model_D_PID 0x0033
#define Everest_GT100_PID 0x0029
#define Fl_Esports_F11_PID 0x0049
#define RGB_KEYBOARD_0016PID 0x0016
/******************************************************************************************\
* *
@ -93,7 +97,55 @@ void DetectSinowealthMouse(hid_device_info* info, const std::string& name)
#endif
}
void DetectSinowealthKeyboard(hid_device_info* info, const std::string& name)
bool DetectCmdAndDataUsages(hid_device* usages[], int usage_count, std::string name, hid_device** dev_cmd, hid_device** dev_data)
{
unsigned char tmp_buf[1032];
// Try to find which device allow us to send ReportID 0x05
for(int i = 0; i < usage_count; i++)
{
memset(tmp_buf, 0x00, sizeof(tmp_buf));
tmp_buf[0] = 0x05;
tmp_buf[1] = 0x83;
if(hid_send_feature_report(usages[i], tmp_buf, 6) != -1)
{
*dev_cmd = usages[i];
break;
}
}
if(*dev_cmd == nullptr)
{
LOG_ERROR("[%s] Can't find working hid_device for ReportId 0x05", name.c_str());
return false;
}
// Try other devices for support ReportID 0x06
for(int i = 0; i < usage_count; i++)
{
if(usages[i] == *dev_cmd)
{
continue;
}
memset(tmp_buf, 0x00, sizeof(tmp_buf));
tmp_buf[0] = 0x06;
if(hid_get_feature_report(usages[i], tmp_buf, 1032) != -1)
{
*dev_data = usages[i];
break;
}
}
if(*dev_data == nullptr)
{
LOG_ERROR("[%s] Can't find working hid_device for ReportId 0x06", name.c_str());
return false;
}
return true;
}
void DetectSinowealthKeyboard(hid_device_info* info, const std::string& name, unsigned int pid)
{
#ifdef USE_HID_USAGE
hid_device* usages[3];
@ -102,8 +154,24 @@ void DetectSinowealthKeyboard(hid_device_info* info, const std::string& name)
if(usage_count == 3)
{
SinowealthKeyboardController* controller = new SinowealthKeyboardController(usages[1], usages[2], info->path);
RGBController_SinowealthKeyboard* rgb_controller = new RGBController_SinowealthKeyboard(controller);
RGBController *rgb_controller;
if(pid == RGB_KEYBOARD_0016PID)
{
hid_device* dev_cmd = nullptr;
hid_device* dev_data = nullptr;
if(!DetectCmdAndDataUsages(usages, usage_count, name, &dev_cmd, &dev_data))
{
return;
}
SinowealthKeyboard16Controller* controller = new SinowealthKeyboard16Controller(dev_cmd, dev_data, info->path, name);
rgb_controller = new RGBController_SinowealthKeyboard16(controller);
}
else
{
SinowealthKeyboardController* controller = new SinowealthKeyboardController(usages[1], usages[2], info->path);
rgb_controller = new RGBController_SinowealthKeyboard(controller);
}
rgb_controller->name = name;
ResourceManager::get()->RegisterRGBController(rgb_controller);
}
@ -121,23 +189,45 @@ void DetectSinowealthKeyboard(hid_device_info* info, const std::string& name)
hid_device* dev = hid_open_path(info->path);
if(dev)
{
SinowealthController* controller = new SinowealthController(dev, dev, info->path);
RGBController_Sinowealth* rgb_controller = new RGBController_Sinowealth(controller);
RGBController *rgb_controller;
if(pid == RGB_KEYBOARD_0016PID)
{
SinowealthKeyboard16Controller* controller = new SinowealthKeyboard16Controller(dev, dev, info->path, name);
rgb_controller = new RGBController_SinowealthKeyboard16(controller);
}
else
{
SinowealthController* controller = new SinowealthController(dev, dev, info->path);
rgb_controller = new RGBController_Sinowealth(controller);
}
rgb_controller->name = name;
ResourceManager::get()->RegisterRGBController(rgb_controller);
}
#endif
}
void DetectSinowealthKeyboard49(hid_device_info* info, const std::string& name)
{
DetectSinowealthKeyboard(info, name, Fl_Esports_F11_PID);
}
void DetectSinowealthKeyboard16(hid_device_info* info, const std::string& name)
{
DetectSinowealthKeyboard(info, name, RGB_KEYBOARD_0016PID);
}
#ifdef USE_HID_USAGE
REGISTER_HID_DETECTOR_P("Glorious Model O / O-", DetectSinowealthMouse, SINOWEALTH_VID, Glorious_Model_O_PID, 0xFF00);
REGISTER_HID_DETECTOR_P("Glorious Model D / D-", DetectSinowealthMouse, SINOWEALTH_VID, Glorious_Model_D_PID, 0xFF00);
REGISTER_HID_DETECTOR_P("Everest GT-100 RGB", DetectSinowealthMouse, SINOWEALTH_VID, Everest_GT100_PID, 0xFF00);
REGISTER_HID_DETECTOR_P("FL ESPORTS F11", DetectSinowealthKeyboard, SINOWEALTH_VID, Fl_Esports_F11_PID, 0xFF00);
REGISTER_HID_DETECTOR_P("FL ESPORTS F11", DetectSinowealthKeyboard49, SINOWEALTH_VID, Fl_Esports_F11_PID, 0xFF00);
REGISTER_HID_DETECTOR_P("Sinowealth Keyboard", DetectSinowealthKeyboard16, SINOWEALTH_VID, RGB_KEYBOARD_0016PID, 0xFF00);
#else
REGISTER_HID_DETECTOR_I("Glorious Model O / O-", DetectSinowealthMouse, SINOWEALTH_VID, Glorious_Model_O_PID, 1);
REGISTER_HID_DETECTOR_I("Glorious Model D / D-", DetectSinowealthMouse, SINOWEALTH_VID, Glorious_Model_D_PID, 1);
REGISTER_HID_DETECTOR_I("Everest GT-100 RGB", DetectSinowealthMouse, SINOWEALTH_VID, Everest_GT100_PID, 1);
REGISTER_HID_DETECTOR_I("FL ESPORTS F11", DetectSinowealthKeyboard, SINOWEALTH_VID, Fl_Esports_F11_PID, 1);
REGISTER_HID_DETECTOR_I("FL ESPORTS F11", DetectSinowealthKeyboard49, SINOWEALTH_VID, Fl_Esports_F11_PID, 1);
REGISTER_HID_DETECTOR_I("Sinowealth Keyboard", DetectSinowealthKeyboard16, SINOWEALTH_VID, RGB_KEYBOARD_0016PID, 1);
#endif

View file

@ -0,0 +1,337 @@
/*------------------------------------------*\
| SinowealthKeyboard16Controller.cpp |
| |
| Definitions and types for Sinowealth |
| Keyboard with PID:0016, |
| Hopefully generic for this PID, |
| this was made spefically for ZUOYA X51 |
| |
| Zagorodnikov Aleksey (glooom) 26.07.2021 |
| based on initial implementation from |
| Dmitri Kalinichenko (Dima-Kal) 23/06/2021 |
\*-----------------------------------------=*/
#include "SinowealthKeyboard16Controller.h"
#include "LogManager.h"
#include <cstring>
#include <chrono>
using namespace std::chrono_literals;
using namespace kbd16;
static unsigned char request_init[] = {0x05, 0x01, 0xAA, 0xBB, 0x2F, 0x3E};
static unsigned char request_modes[] = {0x05, 0x83, 0x00, 0x00, 0x00, 0x00};
static unsigned char request_colors[] = {0x05, 0x88, 0xA8, 0x00, 0x40, 0x00};
static unsigned char request_per_led_cm12[] = {0x05, 0x89, 0xAC, 0x00, 0x40, 0x00};
static unsigned char request_per_led_cm34[] = {0x05, 0x89, 0xB0, 0x00, 0x40, 0x00};
static unsigned char request_per_led_cm5[] = {0x05, 0x89, 0xB4, 0x00, 0x40, 0x00};
SinowealthKeyboard16Controller::SinowealthKeyboard16Controller(hid_device* cmd_handle, hid_device* data_handle, char* path, std::string dev_name)
{
dev_cmd = cmd_handle;
dev_data = data_handle;
name = dev_name;
led_count = 132;
current_mode = MODE_OFF;
location = path;
memset(mode_config_buf, 0x00, sizeof(mode_config_buf));
initCommunication();
UpdateConfigurationFromDevice();
}
SinowealthKeyboard16Controller::~SinowealthKeyboard16Controller()
{
hid_close(dev_cmd);
hid_close(dev_data);
}
std::string SinowealthKeyboard16Controller::GetLocation()
{
return("HID: " + location);
}
unsigned char SinowealthKeyboard16Controller::GetCurrentMode()
{
return current_mode;
}
unsigned int SinowealthKeyboard16Controller::GetLEDCount()
{
return led_count;
}
std::string SinowealthKeyboard16Controller::GetSerialString()
{
wchar_t serial_string[128];
int ret = hid_get_serial_number_string(dev_cmd, serial_string, 128);
if(ret != 0)
{
return("");
}
std::wstring return_wstring = serial_string;
std::string return_string(return_wstring.begin(), return_wstring.end());
return(return_string);
}
void SinowealthKeyboard16Controller::SetLEDsDirect(std::vector<RGBColor> colors)
{
const int colors_offset = led_count;
int i = colors_start_idx;
/*-------------------------------------------------------------------------*\
| CM2 and CM4 presets are located in second half of corresponding arrays |
\*-------------------------------------------------------------------------*/
if(current_custom_preset == 1 || current_custom_preset == 3)
{
i += (led_count * 3);
}
for(const RGBColor &color : colors)
{
per_button_color_buf[i] = RGBGetBValue(color);
per_button_color_buf[i + colors_offset] = RGBGetGValue(color);
per_button_color_buf[i + colors_offset + colors_offset] = RGBGetRValue(color);
i++;
}
sendConfig(per_button_color_buf);
}
void SinowealthKeyboard16Controller::SetMode(unsigned char mode, unsigned char brightness, unsigned char speed, bool direction_left, unsigned char color_mode)
{
if(mode >= MODE_PER_KEY1)
{
current_custom_preset = (mode & 0x0F);
mode_config_buf[current_mode_idx] = MODE_PER_KEY;
mode_config_buf[per_key_mode_idx] = 1;
mode_config_buf[current_mode_idx+1] = (0x20 | current_custom_preset);
GetButtonColorsConfig(per_button_color_buf);
}
else
{
mode_config_buf[current_mode_idx] = mode;
device_modes[mode].brightness = brightness;
device_modes[mode].speed = speed;
device_modes[mode].color = color_mode;
device_modes[mode].direction_left = direction_left;
mode_config_buf[per_key_mode_idx] = 0;
}
if(sendConfig(mode_config_buf))
{
current_mode = mode;
}
}
void SinowealthKeyboard16Controller::SetColorsForMode(unsigned char mode, RGBColor *profiles)
{
if(mode >= MODE_PER_KEY1)
{
return;
}
for(int i = 0; i < COLOR_PRESETS_IN_MODE; i++)
{
modes_colors[mode].preset[i].red = RGBGetRValue(profiles[i]);
modes_colors[mode].preset[i].green = RGBGetGValue(profiles[i]);
modes_colors[mode].preset[i].blue = RGBGetBValue(profiles[i]);
}
sendConfig(colors_config_buf);
}
void SinowealthKeyboard16Controller::ClearMode()
{
mode_config_buf[per_key_mode_idx] = 0;
mode_config_buf[current_mode_idx] = 0;
mode_config_buf[current_mode_idx+1] = 0;
sendConfig(mode_config_buf);
/*-----------------------------------------------------------------*\
| Before apply new settings, keyboard needs turn off LEDs and wait |
| ~200ms, otherwise sometime glitches happens. |
\*-----------------------------------------------------------------*/
std::this_thread::sleep_for(200ms);
}
std::vector<ModeCfg> SinowealthKeyboard16Controller::GetDeviceModes()
{
std::vector<ModeCfg> modes;
for(int i = 0; i < profiles_count; i++)
{
modes.push_back(device_modes[i]);
}
return modes;
}
std::vector<ModeColorCfg> SinowealthKeyboard16Controller::GetDeviceColors()
{
std::vector<ModeColorCfg> presets;
for(int i = 0; i < profiles_count; i++)
{
presets.push_back(modes_colors[i]);
}
return presets;
}
std::vector<RGBColor> SinowealthKeyboard16Controller::GetPerLedColors()
{
const int colors_offset = led_count;
int start_idx = colors_start_idx;
/*-------------------------------------------------------------------------*\
| CM2 and CM4 presets are located in second half of corresponding arrays |
\*-------------------------------------------------------------------------*/
if(current_custom_preset == 1 || current_custom_preset == 3)
{
start_idx += (led_count * 3);
}
std::vector<RGBColor> res;
res.resize(led_count);
for(unsigned int i = 0; i < led_count; i++)
{
RGBColor color = (per_button_color_buf[start_idx+i] << 16)
| (per_button_color_buf[start_idx+i+colors_offset] << 8)
| (per_button_color_buf[start_idx+i+colors_offset+colors_offset]);
res[i] = color;
}
return res;
}
void SinowealthKeyboard16Controller::UpdateConfigurationFromDevice()
{
GetModesConfig(mode_config_buf);
GetColorsConfig(colors_config_buf);
current_mode = mode_config_buf[current_mode_idx];
device_modes = (struct ModeCfg*) &mode_config_buf[profiles_start_idx];
modes_colors = (struct ModeColorCfg*) &colors_config_buf[colors_start_idx];
/*-------------------------------------------------------------------------*\
| When OEM software switch keyboard to custom mode it set 0x0F into 0x15 |
| byte and 01 into 0x14 byte. However if user manually switch to this mode |
| through Fn+1 the keyboard only sets 0x14 byte by itself, but not touched |
| 0x15 byte. Not sure what part is more important, so managing both bytes. |
\*-------------------------------------------------------------------------*/
if(current_mode == MODE_PER_KEY || mode_config_buf[per_key_mode_idx] == 1)
{
current_custom_preset = (mode_config_buf[current_mode_idx+1] & 0x0F);
current_mode = ((MODE_PER_KEY&0xF) << 4) | current_custom_preset;
GetButtonColorsConfig(per_button_color_buf);
}
}
void SinowealthKeyboard16Controller::GetModesConfig(unsigned char *buf)
{
if(!getConfig(request_modes, buf))
{
LOG_ERROR("[%s] Could not read modes config table", name.c_str());
}
}
void SinowealthKeyboard16Controller::GetColorsConfig(unsigned char *buf)
{
if(!getConfig(request_colors, buf))
{
LOG_ERROR("[%s] Could not read colors config table", name.c_str());
}
}
void SinowealthKeyboard16Controller::GetButtonColorsConfig(unsigned char *buf)
{
unsigned char *req;
switch(current_custom_preset)
{
case 2: // CM3
case 3: // CM4
req = request_per_led_cm34;
break;
case 4: // CM5
req = request_per_led_cm5;
break;
case 0: // CM1
case 1: // CM2
default:
req = request_per_led_cm12;
break;
}
if(!getConfig(req, buf))
{
LOG_ERROR("[%s] Could not read per LED config table", name.c_str());
return;
}
}
void SinowealthKeyboard16Controller::initCommunication()
{
/*---------------------------------------------------------*\
| This needs to be sent first time after powerup keyboard. |
\*---------------------------------------------------------*/
hid_send_feature_report(dev_cmd, request_init, 6);
}
bool SinowealthKeyboard16Controller::getConfig(unsigned char *request, unsigned char *buf)
{
hid_send_feature_report(dev_cmd, request, 6);
unsigned char response[PAYLOAD_LEN];
/*---------------------------------------------------------*\
| Zero out buffer |
\*---------------------------------------------------------*/
memset(response, 0x00, PAYLOAD_LEN);
response[0] = 0x06;
/*---------------------------------------------------------*\
| Get the response and put it in the response buffer |
\*---------------------------------------------------------*/
if(hid_get_feature_report(dev_data, response, PAYLOAD_LEN) != -1)
{
response[1] &= ~0x80; // Clear response flag
response[2] = request[2];
response[3] = request[3];
response[4] = request[4];
response[5] = request[5];
memcpy(buf,response,PAYLOAD_LEN);
read_config_error = false;
return true;
}
read_config_error = true;
return false;
}
bool SinowealthKeyboard16Controller::sendConfig(unsigned char *buf)
{
if(read_config_error)
{
LOG_ERROR("[%s] Can't send new config if reading old config fail", name.c_str());
return false;
}
int result = hid_send_feature_report(dev_data, buf, PAYLOAD_LEN);
return (result != -1);
}

View file

@ -0,0 +1,162 @@
/*------------------------------------------*\
| SinowealthKeyboard16Controller.h |
| |
| Definitions and types for Sinowealth |
| Keyboard with PID:0016, |
| Hopefully generic for this PID, |
| this was made spefically for ZUOYA X51 |
| |
| Zagorodnikov Aleksey (glooom) 26.07.2021 |
| based on initial implementation from |
| Dmitri Kalinichenko (Dima-Kal) 23/06/2021 |
\*-----------------------------------------=*/
#include "RGBController.h"
#include <vector>
#include <hidapi/hidapi.h>
#pragma once
#define PAYLOAD_LEN 1032
#define COLOR_PRESETS_IN_MODE 7
namespace kbd16
{
enum
{
MODE_OFF = 0,
/*
MODE_COLOR_LOOP = 1,
MODE_RESPIRE = 2,
MODE_STATIC = 3,
MODE_RIPPLES_SHINING = 4,
MODE_REACTION = 5,
MODE_FLASH_AWAY = 6,
MODE_SINE_WAVE = 7,
MODE_RAINDROPS = 8,
MODE_NEON_STREAM = 9,
MODE_RAINBOW_WHEEL = 10,
MODE_ADORN = 11,
MODE_STARS_TWINKLE = 12,
MODE_SHADOW_DISAPPEAR = 13,
MODE_RETRO_SNAKE = 14,
*/
MODE_PER_KEY = 0x0F, // General mode for custom presets
MODE_PER_KEY1 = 0xF0,
MODE_PER_KEY2 = 0xF1,
MODE_PER_KEY3 = 0xF2,
MODE_PER_KEY4 = 0xF3,
MODE_PER_KEY5 = 0xF4,
};
enum
{
SPEED_SLOW = 0x00,
SPEED_NORMAL = 0x01,
SPEED_FAST = 0x02,
SPEED_FASTER = 0x03,
SPEED_FASTEST = 0x04,
};
enum
{
BRIGHTNESS_OFF = 0x00,
BRIGHTNESS_QUARTER = 0x01,
BRIGHTNESS_HALF = 0x02,
BRIGHTNESS_THREE_QUARTERS = 0x03,
BRIGHTNESS_FULL = 0x04,
};
enum
{
COLOR_PRESET_0 = 0,
COLOR_PRESET_1 = 1,
COLOR_PRESET_2 = 2,
COLOR_PRESET_3 = 3,
COLOR_PRESET_4 = 4,
COLOR_PRESET_5 = 5,
COLOR_PRESET_6 = 6,
COLOR_PRESET_RANDOM = 7,
};
}
struct ModeCfg
{
unsigned char color:3;
unsigned char :4;
unsigned char direction_left:1;
unsigned char brightness:4;
unsigned char speed:4;
};
struct ColorCfg
{
unsigned char blue:8;
unsigned char green:8;
unsigned char red:8;
};
struct ModeColorCfg
{
ColorCfg preset[COLOR_PRESETS_IN_MODE];
};
class SinowealthKeyboard16Controller
{
public:
SinowealthKeyboard16Controller(hid_device* cmd_handle, hid_device* data_handle, char *_path, std::string dev_name);
~SinowealthKeyboard16Controller();
unsigned int GetLEDCount();
std::string GetLocation();
std::string GetSerialString();
unsigned char GetCurrentMode();
std::vector<ModeCfg> GetDeviceModes();
std::vector<ModeColorCfg> GetDeviceColors();
std::vector<RGBColor> GetPerLedColors();
void SetLEDColor(RGBColor* color_buf);
void SetMode(unsigned char mode, unsigned char brightness, unsigned char speed, RGBColor* color_buf);
void ClearMode();
void SetMode(unsigned char mode, unsigned char brightness, unsigned char speed, bool direction_left, unsigned char color_mode);
void SetColorsForMode(unsigned char mode, RGBColor profiles[COLOR_PRESETS_IN_MODE]);
void GetProfile();
void ReadFirmwareInfo();
void SetLEDsDirect(std::vector<RGBColor> colors);
const int per_key_mode_idx = 20;
const int current_mode_idx = 21;
const int profiles_start_idx = 32;
const int profiles_count = 15;
const int colors_start_idx = 8;
private:
hid_device* dev_cmd;
hid_device* dev_data;
device_type type;
std::string name;
unsigned int led_count;
unsigned char current_mode;
struct ModeCfg* device_modes;
struct ModeColorCfg* modes_colors;
std::string location;
unsigned char mode_config_buf[PAYLOAD_LEN];
unsigned char colors_config_buf[PAYLOAD_LEN];
unsigned char per_button_color_buf[PAYLOAD_LEN];
bool read_config_error = false;
unsigned char current_custom_preset = 0;
void GetModesConfig(unsigned char *buf);
void GetColorsConfig(unsigned char *buf);
void GetButtonColorsConfig(unsigned char *buf);
void initCommunication();
bool getConfig(unsigned char reqest[], unsigned char *buf);
bool sendConfig(unsigned char *buf);
void UpdateConfigurationFromDevice();
};