Skip to content

Commit 68df4d4

Browse files
YanzgzCalcProgrammer1
authored andcommitted
[New Device/Implements] CH551G open source keyboard (Rebase)
1 parent 389cdf3 commit 68df4d4

File tree

6 files changed

+580
-0
lines changed

6 files changed

+580
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*---------------------------------------------------------------*\
2+
| GaiZhongGaiKeyboardController.cpp |
3+
| |
4+
| https://oshwlab.com/yangdsada/GaiZhongGai-Keyboard-68-4PRO |
5+
| |
6+
| An Yang 2022/6/12 |
7+
\*---------------------------------------------------------------*/
8+
9+
#include <cstring>
10+
#include "GaiZhongGaiKeyboardController.h"
11+
12+
GaiZhongGaiKeyboardController::GaiZhongGaiKeyboardController(hid_device* dev_handle, hid_device_info* info)
13+
{
14+
dev = dev_handle;
15+
location = info->path;
16+
usb_pid = info->product_id;
17+
/*-----------------------------------------------------*\
18+
| Obtaining the Firmware Version |
19+
\*-----------------------------------------------------*/
20+
char str[10];
21+
sprintf(str, "Ver%04X", info->release_number);
22+
version = str;
23+
}
24+
25+
GaiZhongGaiKeyboardController::~GaiZhongGaiKeyboardController()
26+
{
27+
/*-----------------------------------------------------*\
28+
| Restore built-in light effect |
29+
\*-----------------------------------------------------*/
30+
unsigned char usb_buf[65];
31+
memset(usb_buf, 0x00, sizeof(usb_buf));
32+
usb_buf[1] = 0xFF;
33+
hid_write(dev, usb_buf, 65);
34+
std::this_thread::sleep_for(std::chrono::milliseconds(2));
35+
36+
hid_close(dev);
37+
}
38+
39+
std::string GaiZhongGaiKeyboardController::GetDeviceLocation()
40+
{
41+
return("HID: " + location);
42+
}
43+
44+
std::string GaiZhongGaiKeyboardController::GetSerialString()
45+
{
46+
wchar_t serial_string[128];
47+
int ret = hid_get_serial_number_string(dev, serial_string, 128);
48+
49+
if(ret != 0)
50+
{
51+
return("");
52+
}
53+
54+
std::wstring return_wstring = serial_string;
55+
std::string return_string(return_wstring.begin(), return_wstring.end());
56+
57+
return(return_string);
58+
}
59+
60+
std::string GaiZhongGaiKeyboardController::GetVersion()
61+
{
62+
return(version);
63+
}
64+
65+
unsigned short GaiZhongGaiKeyboardController::GetUSBPID()
66+
{
67+
return(usb_pid);
68+
}
69+
70+
void GaiZhongGaiKeyboardController::SendColors
71+
(
72+
unsigned char* color_data,
73+
unsigned int color_data_size
74+
)
75+
{
76+
unsigned char usb_buf[65];
77+
78+
memset(usb_buf, 0x00, sizeof(usb_buf));
79+
80+
switch(usb_pid)
81+
{
82+
case GAIZHONGGAI_17_TOUCH_PRO_PID: //17PAD+Touch
83+
case GAIZHONGGAI_20_PRO_PID: //20PAD
84+
usb_buf[1] = 0x10;
85+
memcpy(usb_buf + 2, color_data + 68 * 3, 60);
86+
hid_write(dev, usb_buf, 65);
87+
break;
88+
89+
case GAIZHONGGAI_17_PRO_PID: //17PAD
90+
usb_buf[1] = 0x10;
91+
memcpy(usb_buf + 2, color_data + 68 * 3, 51);
92+
hid_write(dev, usb_buf, 65);
93+
break;
94+
95+
case GAIZHONGGAI_68_PRO_PID: //68%
96+
usb_buf[1] = 0x10;
97+
memcpy(usb_buf + 2, color_data + 0 * 3, 63);
98+
hid_write(dev, usb_buf, 65);
99+
std::this_thread::sleep_for(std::chrono::milliseconds(2));
100+
101+
usb_buf[1] = 0x11;
102+
memcpy(usb_buf + 2, color_data + 21 * 3, 63);
103+
hid_write(dev, usb_buf, 65);
104+
std::this_thread::sleep_for(std::chrono::milliseconds(2));
105+
106+
usb_buf[1] = 0x12;
107+
memcpy(usb_buf + 2, color_data + 42 * 3, 63);
108+
hid_write(dev, usb_buf, 65);
109+
std::this_thread::sleep_for(std::chrono::milliseconds(2));
110+
111+
memset(usb_buf, 0x00, sizeof(usb_buf));
112+
usb_buf[1] = 0x13;
113+
memcpy(usb_buf + 2, color_data + 63 * 3, 15);
114+
hid_write(dev, usb_buf, 65);
115+
break;
116+
}
117+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*---------------------------------------------------------------*\
2+
| GaiZhongGaiKeyboardController.h |
3+
| |
4+
| https://oshwlab.com/yangdsada/GaiZhongGai-Keyboard-68-4PRO |
5+
| |
6+
| An Yang 2022/6/12 |
7+
\*---------------------------------------------------------------*/
8+
9+
#include "RGBController.h"
10+
11+
#include <string>
12+
#include <hidapi/hidapi.h>
13+
14+
#pragma once
15+
16+
/*-----------------------------------------------------*\
17+
| GaiZhongGai vendor ID |
18+
\*-----------------------------------------------------*/
19+
#define GAIZHONGGAI_VID 0x3061
20+
21+
/*-----------------------------------------------------*\
22+
| Keyboard product IDs |
23+
\*-----------------------------------------------------*/
24+
#define GAIZHONGGAI_68_PRO_PID 0x4700
25+
#define GAIZHONGGAI_17_TOUCH_PRO_PID 0x4770
26+
#define GAIZHONGGAI_17_PRO_PID 0x4771
27+
#define GAIZHONGGAI_20_PRO_PID 0x4772
28+
29+
class GaiZhongGaiKeyboardController
30+
{
31+
public:
32+
GaiZhongGaiKeyboardController(hid_device* dev_handle, hid_device_info* info);
33+
~GaiZhongGaiKeyboardController();
34+
35+
std::string GetDeviceLocation();
36+
std::string GetSerialString();
37+
std::string GetVersion();
38+
unsigned short GetUSBPID();
39+
40+
void SendColors
41+
(
42+
unsigned char* color_data,
43+
unsigned int color_data_size
44+
);
45+
46+
private:
47+
hid_device* dev;
48+
std::string location;
49+
std::string version;
50+
unsigned short usb_pid;
51+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*---------------------------------------------------------------*\
2+
| GaiZhongGaiKeyboardControllerDetect.cpp |
3+
| |
4+
| https://oshwlab.com/yangdsada/GaiZhongGai-Keyboard-68-4PRO |
5+
| |
6+
| An Yang 2022/6/12 |
7+
\*---------------------------------------------------------------*/
8+
9+
#include "Detector.h"
10+
#include "GaiZhongGaiKeyboardController.h"
11+
#include "RGBController.h"
12+
#include "RGBController_GaiZhongGaiKeyboard.h"
13+
#include <hidapi/hidapi.h>
14+
15+
/******************************************************************************************\
16+
* *
17+
* DetectGaiZhongGaiKeyboardControllers *
18+
* *
19+
* Tests the USB address to see if a GaiZhongGai RGB Keyboard controller exists there.*
20+
* *
21+
\******************************************************************************************/
22+
23+
void DetectGaiZhongGaiKeyboardControllers(hid_device_info* info, const std::string& name)
24+
{
25+
hid_device* dev = hid_open_path(info->path);
26+
if( dev )
27+
{
28+
GaiZhongGaiKeyboardController* controller = new GaiZhongGaiKeyboardController(dev, info);
29+
RGBController_GaiZhongGaiKeyboard* rgb_controller = new RGBController_GaiZhongGaiKeyboard(controller);
30+
rgb_controller->name = name;
31+
ResourceManager::get()->RegisterRGBController(rgb_controller);
32+
}
33+
} /* DetectGaiZhongGaiKeyboardControllers() */
34+
35+
REGISTER_HID_DETECTOR_I("GaiZhongGai 68+4 PRO", DetectGaiZhongGaiKeyboardControllers, GAIZHONGGAI_VID, GAIZHONGGAI_68_PRO_PID, 3);
36+
REGISTER_HID_DETECTOR_I("GaiZhongGai 17+4+Touch PRO", DetectGaiZhongGaiKeyboardControllers, GAIZHONGGAI_VID, GAIZHONGGAI_17_TOUCH_PRO_PID, 3);
37+
REGISTER_HID_DETECTOR_I("GaiZhongGai 17 PRO", DetectGaiZhongGaiKeyboardControllers, GAIZHONGGAI_VID, GAIZHONGGAI_17_PRO_PID, 3);
38+
REGISTER_HID_DETECTOR_I("GaiZhongGai 20 PRO", DetectGaiZhongGaiKeyboardControllers, GAIZHONGGAI_VID, GAIZHONGGAI_20_PRO_PID, 3);

0 commit comments

Comments
 (0)