Skip to content

Commit df7b15a

Browse files
MerafourCalcProgrammer1
authored andcommitted
Adding OKS KeyBoard support
1 parent dbcb7a8 commit df7b15a

File tree

6 files changed

+676
-0
lines changed

6 files changed

+676
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*-----------------------------------------*\
2+
| OKSKeyboardController.cpp |
3+
| |
4+
| Driver for OKS RGB keyboardlighting |
5+
| controller |
6+
| |
7+
| Merafour (OKS) 2/24/2023 |
8+
\*-----------------------------------------*/
9+
10+
#include <cstring>
11+
#include "OKSKeyboardController.h"
12+
13+
OKSKeyboardController::OKSKeyboardController(hid_device* dev_handle, const char* path, const unsigned short pid)
14+
{
15+
dev = dev_handle;
16+
location = path;
17+
usb_pid = pid;
18+
19+
SendInitialize();
20+
}
21+
22+
OKSKeyboardController::~OKSKeyboardController()
23+
{
24+
hid_close(dev);
25+
}
26+
27+
std::string OKSKeyboardController::GetDeviceLocation()
28+
{
29+
return("HID: " + location);
30+
}
31+
32+
std::string OKSKeyboardController::GetSerialString()
33+
{
34+
wchar_t serial_string[128];
35+
int ret = hid_get_serial_number_string(dev, serial_string, 128);
36+
37+
if(ret != 0)
38+
{
39+
return("");
40+
}
41+
42+
std::wstring return_wstring = serial_string;
43+
std::string return_string(return_wstring.begin(), return_wstring.end());
44+
45+
return(return_string);
46+
}
47+
48+
unsigned short OKSKeyboardController::GetUSBPID()
49+
{
50+
return(usb_pid);
51+
}
52+
53+
void OKSKeyboardController::SendColors(unsigned char* color_data, unsigned int color_data_size)
54+
{
55+
char usb_buf[65];
56+
union kb2_port_t Pack;
57+
uint8_t cnt;
58+
uint8_t red,green,blue;
59+
uint16_t color_idx;
60+
uint32_t irgb[14];
61+
uint16_t pos=0;
62+
for(color_idx=0; color_idx<(6*21); color_idx += 14)
63+
{
64+
for(cnt=0; cnt<14; cnt++)
65+
{
66+
pos = color_idx+cnt;
67+
red = color_data[pos*3+0];
68+
green = color_data[pos*3+1];
69+
blue = color_data[pos*3+2];
70+
irgb[cnt] = blue&0xFF;
71+
irgb[cnt] <<= 8;
72+
irgb[cnt] |= green&0xFF;
73+
irgb[cnt] <<= 8;
74+
irgb[cnt] |= red&0xFF;
75+
irgb[cnt] <<= 8;
76+
irgb[cnt] |= pos&0xFF;
77+
}
78+
kb2M_wled(&Pack, irgb);
79+
usb_buf[0] = 0x04;
80+
for(uint8_t i=0; i<64; i++) usb_buf[i+1] = Pack.bin[i];
81+
/*-----------------------------------------------------*\
82+
| Send packet |
83+
\*-----------------------------------------------------*/
84+
hid_write(dev, (unsigned char *)usb_buf, 65);
85+
}
86+
std::this_thread::sleep_for(std::chrono::milliseconds(5));
87+
}
88+
89+
void OKSKeyboardController::SendKeyboardModeEx(const mode &m, unsigned char red, unsigned char green, unsigned char blue)
90+
{
91+
union kb2_port_t Pack;
92+
kb2M_wrgb(&Pack, m.brightness, m.value, m.speed, m.direction);
93+
Send(Pack.bin, Pack.length+KB2_HEAD_SIZE);
94+
}
95+
96+
void OKSKeyboardController::Send(const uint8_t bin[], const uint16_t len)
97+
{
98+
char usb_buf[65];
99+
uint16_t Len;
100+
uint16_t pos;
101+
pos=0;
102+
while(pos<len)
103+
{
104+
/*-----------------------------------------------------*\
105+
| Zero out buffer |
106+
\*-----------------------------------------------------*/
107+
memset(usb_buf, 0x00, sizeof(usb_buf));
108+
/*-----------------------------------------------------*\
109+
| Set send data packet |
110+
\*-----------------------------------------------------*/
111+
usb_buf[0] = 0x04;
112+
Len = len-pos;
113+
if(Len>64) Len=64;
114+
for(uint8_t i=0; i<Len; i++) usb_buf[i+1] = bin[i+pos];
115+
pos += Len;
116+
/*-----------------------------------------------------*\
117+
| Send packet |
118+
\*-----------------------------------------------------*/
119+
hid_write(dev, (unsigned char *)usb_buf, 65);
120+
std::this_thread::sleep_for(std::chrono::milliseconds(2));
121+
}
122+
}
123+
124+
void OKSKeyboardController::SendInitialize()
125+
{
126+
union kb2_port_t Pack;
127+
kb2M_wrgb(&Pack, 0, 0, 2, 1);
128+
Send(Pack.bin, Pack.length+KB2_HEAD_SIZE);
129+
}
130+
131+
uint8_t OKSKeyboardController::kb2_ComputeChecksum(const union kb2_port_t* const Pack)
132+
{
133+
uint8_t checksum = 0x35;
134+
const uint8_t len = Pack->length;
135+
136+
checksum += Pack->head;
137+
checksum += len;
138+
checksum += Pack->cmd;
139+
if((len>0) && (len<=KB2_DATA_SIZE)) checksum += Pack->data[len-1];
140+
return checksum;
141+
}
142+
int OKSKeyboardController::kb2_add_32b(union kb2_port_t* const Pack, const uint32_t value)
143+
{
144+
union uint32_kb2 Value;
145+
if((Pack->length+4)>KB2_DATA_SIZE) return -1;
146+
Value.data = value;
147+
Pack->data[Pack->length++] = Value.Byte0;
148+
Pack->data[Pack->length++] = Value.Byte1;
149+
Pack->data[Pack->length++] = Value.Byte2;
150+
Pack->data[Pack->length++] = Value.Byte3;
151+
return 4;
152+
}
153+
void OKSKeyboardController::kb2M_init(union kb2_port_t* const Pack, const enum kb2_cmd cmd)
154+
{
155+
Pack->head = KB2_PACK_HEAD;
156+
Pack->length = 0;
157+
Pack->cmd = cmd;
158+
Pack->checksum = 0;
159+
}
160+
void OKSKeyboardController::kb2M_wrgb(union kb2_port_t* const Pack, const uint8_t bright, const uint8_t mode, const uint8_t speed, const uint8_t dir)
161+
{
162+
kb2M_init(Pack, KB2_CMD_WRGB);
163+
kb2_add_32b(Pack, bright);
164+
Pack->data[Pack->length++] = mode;
165+
Pack->data[Pack->length++] = speed;
166+
Pack->data[Pack->length++] = dir;
167+
Pack->data[Pack->length++] = 0xFF;
168+
Pack->data[Pack->length++] = 0xFF;
169+
Pack->checksum = kb2_ComputeChecksum(Pack);
170+
}
171+
void OKSKeyboardController::kb2M_wled(union kb2_port_t* const Pack, const uint32_t irgb[14])
172+
{
173+
kb2M_init(Pack, KB2_CMD_WLED);
174+
for(uint8_t i=0; i<14; i++) kb2_add_32b(Pack, irgb[i]);
175+
Pack->data[Pack->length++] = 0xFF;
176+
Pack->data[Pack->length++] = 0xFF;
177+
Pack->checksum = kb2_ComputeChecksum(Pack);
178+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*-----------------------------------------*\
2+
| OKSKeyboardController.h |
3+
| |
4+
| Definitions and types for OKS RGB |
5+
| keyboard lighting controller |
6+
| |
7+
| Merafour (OKS) 2/24/2023 |
8+
\*-----------------------------------------*/
9+
10+
#include "RGBController.h"
11+
12+
#include <stdint.h>
13+
#include <string>
14+
#include <hidapi/hidapi.h>
15+
16+
#pragma once
17+
18+
/*-----------------------------------------------------*\
19+
| OKS vendor ID |
20+
\*-----------------------------------------------------*/
21+
#define OKS_VID 0x1C4F
22+
23+
/*-----------------------------------------------------*\
24+
| Keyboard product IDs |
25+
\*-----------------------------------------------------*/
26+
#define OKS_OPTICAL_RGB_PID 0xEE88
27+
28+
/*-----------------------------------------------------*\
29+
| communication protocol |
30+
\*-----------------------------------------------------*/
31+
#define KB2_PACK_HEAD (0x5C&0xFF)
32+
#define KB2_HEAD_SIZE 4
33+
#define KB2_PORT_SIZE (64*4)
34+
#define KB2_DATA_SIZE (KB2_PORT_SIZE-KB2_HEAD_SIZE)
35+
union kb2_port_t
36+
{
37+
uint8_t bin[KB2_PORT_SIZE];
38+
struct
39+
{
40+
uint8_t head;
41+
uint8_t length;
42+
uint8_t cmd;
43+
uint8_t checksum;
44+
uint8_t data[KB2_DATA_SIZE];
45+
};
46+
};
47+
enum kb2_cmd
48+
{
49+
KB2_CMD_RRGB = 0x14,
50+
KB2_CMD_WRGB = 0x15,
51+
KB2_CMD_RLED = 0x16,
52+
KB2_CMD_WLED = 0x17,
53+
};
54+
union uint32_kb2
55+
{
56+
uint32_t data;
57+
struct
58+
{
59+
uint8_t Byte0;
60+
uint8_t Byte1;
61+
uint8_t Byte2;
62+
uint8_t Byte3;
63+
};
64+
};
65+
66+
class OKSKeyboardController
67+
{
68+
public:
69+
OKSKeyboardController(hid_device* dev_handle, const char* path, const unsigned short pid);
70+
~OKSKeyboardController();
71+
72+
std::string GetDeviceLocation();
73+
std::string GetSerialString();
74+
unsigned short GetUSBPID();
75+
76+
void SendColors(unsigned char* color_data, unsigned int color_data_size);
77+
void SendKeyboardModeEx(const mode &m, unsigned char red, unsigned char green, unsigned char blue);
78+
79+
80+
private:
81+
hid_device* dev;
82+
std::string location;
83+
unsigned short usb_pid;
84+
85+
void Send(const uint8_t bin[64], const uint16_t len);
86+
void SendInitialize();
87+
uint8_t kb2_ComputeChecksum(const union kb2_port_t* const Pack);
88+
int kb2_add_32b(union kb2_port_t* const Pack, const uint32_t value);
89+
void kb2M_init(union kb2_port_t* const Pack, const enum kb2_cmd cmd);
90+
void kb2M_wrgb(union kb2_port_t* const Pack, const uint8_t bright, const uint8_t mode, const uint8_t speed, const uint8_t dir);
91+
void kb2M_wled(union kb2_port_t* const Pack, const uint32_t irgb[14]);
92+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "Detector.h"
2+
#include "OKSKeyboardController.h"
3+
#include "RGBController.h"
4+
#include "RGBController_OKSKeyboard.h"
5+
#include <hidapi/hidapi.h>
6+
/******************************************************************************************\
7+
* *
8+
* DetectOKSKeyboardControllers *
9+
* Reference: DuckyKeyboardController *
10+
* Tests the USB address to see if a OKS Optical Axis RGB Keyboard controller exists there.*
11+
* Reference:DetectDuckyKeyboardControllers *
12+
\******************************************************************************************/
13+
14+
void DetectOKSKeyboardControllers(hid_device_info* info, const std::string& name)
15+
{
16+
hid_device* dev = hid_open_path(info->path);
17+
18+
if(dev)
19+
{
20+
OKSKeyboardController* controller = new OKSKeyboardController(dev, info->path, info->product_id);
21+
RGBController_OKSKeyboard* rgb_controller = new RGBController_OKSKeyboard(controller);
22+
rgb_controller->name = name;
23+
ResourceManager::get()->RegisterRGBController(rgb_controller);
24+
}
25+
} /* DetectOKSKeyboardControllers() */
26+
27+
REGISTER_HID_DETECTOR_I("OKS Optical Axis RGB", DetectOKSKeyboardControllers, OKS_VID, OKS_OPTICAL_RGB_PID, 1);

0 commit comments

Comments
 (0)