Fix Logitech G915 blue channel writes for little packet

This commit is contained in:
Arthur de Groot 2025-07-04 07:34:02 +00:00 committed by Adam Honse
parent 6d981e21ff
commit b6d4ded29a
3 changed files with 208 additions and 154 deletions

View file

@ -13,6 +13,12 @@
#include "LogitechG915Controller.h"
#include "StringUtils.h"
const size_t MIN_DATA_FRAME_SIZE = 4;
const size_t MAX_DATA_FRAME_SIZE = 16;
const size_t HEADER_SIZE = 4;
const size_t MESSAGE_LEN = 20;
const size_t RESPONSE_LEN = 20;
LogitechG915Controller::LogitechG915Controller(hid_device* dev_handle, bool wired)
{
this->dev_handle = dev_handle;
@ -61,10 +67,11 @@ void LogitechG915Controller::Commit()
void LogitechG915Controller::SetDirect
(
unsigned char frame_type,
unsigned char * frame_data
unsigned char * frame_data,
size_t length
)
{
SendDirectFrame(frame_type, frame_data);
SendDirectFrame(frame_type, frame_data, length);
}
void LogitechG915Controller::SetMode
@ -115,7 +122,7 @@ void LogitechG915Controller::SetMode
void LogitechG915Controller::SendCommit()
{
unsigned char usb_buf[20];
unsigned char usb_buf[MESSAGE_LEN];
/*-----------------------------------------------------*\
| Zero out buffer |
@ -133,13 +140,13 @@ void LogitechG915Controller::SendCommit()
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev_handle, usb_buf, 20);
hid_read_timeout(dev_handle, usb_buf, 20, LOGITECH_READ_TIMEOUT);
hid_write(dev_handle, usb_buf, 4);
hid_read_timeout(dev_handle, usb_buf, RESPONSE_LEN, LOGITECH_READ_TIMEOUT);
}
void LogitechG915Controller::BeginModeSet()
{
unsigned char usb_buf[20];
unsigned char usb_buf[MESSAGE_LEN];
/*-----------------------------------------------------*\
| Zero out buffer |
@ -157,8 +164,8 @@ void LogitechG915Controller::BeginModeSet()
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev_handle, usb_buf, 20);
hid_read(dev_handle, usb_buf, 20);
hid_write(dev_handle, usb_buf, 4);
hid_read(dev_handle, usb_buf, RESPONSE_LEN);
/*-----------------------------------------------------*\
| Zero out buffer |
@ -176,8 +183,8 @@ void LogitechG915Controller::BeginModeSet()
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev_handle, usb_buf, 20);
hid_read(dev_handle, usb_buf, 20);
hid_write(dev_handle, usb_buf, 4);
hid_read(dev_handle, usb_buf, RESPONSE_LEN);
}
void LogitechG915Controller::InitializeModeSet()
@ -203,13 +210,13 @@ void LogitechG915Controller::InitializeModeSet()
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev_handle, usb_buf, 20);
hid_read(dev_handle, usb_buf, 20);
hid_write(dev_handle, usb_buf, 7);
hid_read(dev_handle, usb_buf, RESPONSE_LEN);
}
void LogitechG915Controller::InitializeDirect()
{
unsigned char usb_buf[20];
unsigned char usb_buf[MESSAGE_LEN];
/*-----------------------------------------------------*\
| Zero out buffer |
@ -227,8 +234,8 @@ void LogitechG915Controller::InitializeDirect()
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev_handle, usb_buf, 20);
hid_read(dev_handle, usb_buf, 20);
hid_write(dev_handle, usb_buf, 4);
hid_read(dev_handle, usb_buf, RESPONSE_LEN);
/*-----------------------------------------------------*\
| Zero out buffer |
@ -246,8 +253,8 @@ void LogitechG915Controller::InitializeDirect()
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev_handle, usb_buf, 20);
hid_read(dev_handle, usb_buf, 20);
hid_write(dev_handle, usb_buf, 4);
hid_read(dev_handle, usb_buf, RESPONSE_LEN);
/*-----------------------------------------------------*\
| Zero out buffer |
@ -266,8 +273,8 @@ void LogitechG915Controller::InitializeDirect()
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev_handle, usb_buf, 20);
hid_read(dev_handle, usb_buf, 20);
hid_write(dev_handle, usb_buf, 17);
hid_read(dev_handle, usb_buf, MESSAGE_LEN);
/*-----------------------------------------------------*\
| Zero out buffer |
@ -287,8 +294,8 @@ void LogitechG915Controller::InitializeDirect()
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev_handle, usb_buf, 20);
hid_read(dev_handle, usb_buf, 20);
hid_write(dev_handle, usb_buf, 17);
hid_read(dev_handle, usb_buf, RESPONSE_LEN);
}
void LogitechG915Controller::SendSingleLed
@ -299,41 +306,27 @@ void LogitechG915Controller::SendSingleLed
unsigned char b
)
{
unsigned char usb_buf[20];
/*-----------------------------------------------------*\
| Zero out buffer |
\*-----------------------------------------------------*/
memset(usb_buf, 0x00, sizeof(usb_buf));
/*-----------------------------------------------------*\
| Set up a 6F packet with a single color |
\*-----------------------------------------------------*/
usb_buf[0x00] = 0x11;
usb_buf[0x01] = device_index;
usb_buf[0x02] = feature_8081_idx;
usb_buf[0x03] = LOGITECH_G915_ZONE_FRAME_TYPE_LITTLE;
usb_buf[0x04] = keyCode;
usb_buf[0x05] = r;
usb_buf[0x06] = g;
usb_buf[0x07] = b;
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev_handle, usb_buf, 20);
hid_read(dev_handle, usb_buf, 20);
unsigned char little_frame[4] = { keyCode, r, g, b };
SendDirectFrame(LOGITECH_G915_ZONE_FRAME_TYPE_LITTLE, little_frame, 4);
}
void LogitechG915Controller::SendDirectFrame
(
unsigned char frame_type,
unsigned char * frame_data
unsigned char * frame_data,
size_t length
)
{
unsigned char usb_buf[20];
if(length < MIN_DATA_FRAME_SIZE)
{
return;
}
else if(length > MAX_DATA_FRAME_SIZE)
{
length = MAX_DATA_FRAME_SIZE;
}
unsigned char usb_buf[MESSAGE_LEN];
/*-----------------------------------------------------*\
| Zero out buffer |
@ -351,13 +344,13 @@ void LogitechG915Controller::SendDirectFrame
/*-----------------------------------------------------*\
| Copy in frame data |
\*-----------------------------------------------------*/
memcpy(&usb_buf[0x04], frame_data, 16);
memcpy(&usb_buf[0x04], frame_data, length);
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev_handle, usb_buf, 20);
hid_read_timeout(dev_handle, usb_buf, 20, LOGITECH_READ_TIMEOUT);
hid_write(dev_handle, usb_buf, HEADER_SIZE + length);
hid_read_timeout(dev_handle, usb_buf, RESPONSE_LEN, LOGITECH_READ_TIMEOUT);
}
void LogitechG915Controller::SendMode
@ -371,7 +364,7 @@ void LogitechG915Controller::SendMode
unsigned char blue
)
{
unsigned char usb_buf[20];
unsigned char usb_buf[HEADER_SIZE + 13];
/*-----------------------------------------------------*\
| Zero out buffer |
@ -385,7 +378,7 @@ void LogitechG915Controller::SendMode
usb_buf[0x00] = 0x11;
usb_buf[0x01] = device_index;
usb_buf[0x02] = feature_8071_idx;
usb_buf[0x03] = 0x1E;
usb_buf[0x03] = LOGITECH_G915_ZONE_FRAME_TYPE_MODE;
usb_buf[0x04] = zone;
usb_buf[0x05] = mode;
@ -434,6 +427,6 @@ void LogitechG915Controller::SendMode
/*-----------------------------------------------------*\
| Send packet |
\*-----------------------------------------------------*/
hid_write(dev_handle, usb_buf, 20);
hid_read(dev_handle, usb_buf, 20);
hid_write(dev_handle, usb_buf, HEADER_SIZE + 13);
hid_read(dev_handle, usb_buf, RESPONSE_LEN);
}