Skip to content

Commit 8c315f5

Browse files
thombo2CalcProgrammer1
authored andcommitted
Update hidapi to version 0.12.0. Direct mode stabilization of Logitech G203 Lightsync (fixes #877).
1 parent 8fb373a commit 8c315f5

File tree

8 files changed

+337
-111
lines changed

8 files changed

+337
-111
lines changed

Controllers/LogitechController/LogitechG203LController.cpp

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,36 @@
22

33
#include <cstring>
44

5+
#define PACKET_SIZE 20
6+
7+
58
LogitechG203LController::LogitechG203LController(hid_device* dev_handle, const char* path)
69
{
710
dev = dev_handle;
811
location = path;
12+
13+
// enable software control
14+
unsigned char usb_buf[PACKET_SIZE];
15+
16+
memset(usb_buf, 0x00, PACKET_SIZE);
17+
18+
usb_buf[0x00] = 0x11;
19+
usb_buf[0x01] = 0xFF;
20+
usb_buf[0x02] = 0x0E;
21+
usb_buf[0x03] = 0x50;
22+
usb_buf[0x04] = 0x01;
23+
usb_buf[0x05] = 0x03;
24+
usb_buf[0x06] = 0x07;
25+
26+
SendPacket(usb_buf);
927
}
1028

1129
LogitechG203LController::~LogitechG203LController()
1230
{
13-
hid_close(dev);
31+
if(dev != nullptr)
32+
{
33+
hid_close(dev);
34+
}
1435
}
1536

1637
std::string LogitechG203LController::GetDeviceLocation()
@@ -36,29 +57,28 @@ std::string LogitechG203LController::GetSerialString()
3657

3758
void LogitechG203LController::SendApply()
3859
{
39-
unsigned char usb_buf[20];
60+
unsigned char usb_buf[PACKET_SIZE];
4061

41-
memset(usb_buf, 0x00, sizeof(usb_buf));
62+
memset(usb_buf, 0x00, PACKET_SIZE);
4263

4364
usb_buf[0x00] = 0x11;
4465
usb_buf[0x01] = 0xFF;
4566
usb_buf[0x02] = 0x12;
46-
usb_buf[0x03] = 0x7A;
67+
usb_buf[0x03] = 0x70;
4768

48-
hid_write(dev, usb_buf, 20);
49-
hid_read(dev, usb_buf, 20);
69+
SendPacket(usb_buf);
5070
}
5171

5272
void LogitechG203LController::SetSingleLED(int led, unsigned char red, unsigned char green, unsigned char blue)
5373
{
54-
unsigned char usb_buf[20];
74+
unsigned char usb_buf[PACKET_SIZE];
5575

56-
memset(usb_buf, 0x00, sizeof(usb_buf));
76+
memset(usb_buf, 0x00, PACKET_SIZE);
5777

5878
usb_buf[0x00] = 0x11;
5979
usb_buf[0x01] = 0xFF;
6080
usb_buf[0x02] = 0x12;
61-
usb_buf[0x03] = 0x19;
81+
usb_buf[0x03] = 0x10;
6282

6383
usb_buf[0x04] = (unsigned char)led;
6484
usb_buf[0x05] = red;
@@ -67,8 +87,7 @@ void LogitechG203LController::SetSingleLED(int led, unsigned char red, unsigned
6787

6888
usb_buf[0x08] = 0xFF;
6989

70-
hid_write(dev, usb_buf, 20);
71-
hid_read(dev, usb_buf, 20);
90+
SendPacket(usb_buf);
7291

7392
SendApply();
7493
}
@@ -82,15 +101,21 @@ void LogitechG203LController::SetMode(
82101
unsigned char green,
83102
unsigned char blue)
84103
{
85-
unsigned char usb_buf[20];
104+
unsigned char usb_buf[PACKET_SIZE];
105+
unsigned char brightness = bright * 5;
86106

87-
memset(usb_buf, 0x00, sizeof(usb_buf));
107+
if(brightness == 0)
108+
{
109+
brightness = 1;
110+
}
111+
112+
memset(usb_buf, 0x00, PACKET_SIZE);
88113

89114
//Header
90115
usb_buf[0x00] = 0x11;
91116
usb_buf[0x01] = 0xFF;
92117
usb_buf[0x02] = 0x0E;
93-
usb_buf[0x03] = 0x1A;
118+
usb_buf[0x03] = 0x10;
94119
//Common Data
95120
usb_buf[0x04] = 0x00;
96121
usb_buf[0x05] = (unsigned char)mode;
@@ -103,45 +128,44 @@ void LogitechG203LController::SetMode(
103128
{
104129
usb_buf[0x0B] = (unsigned char)((speed>>8) & 0x000000FF);
105130
usb_buf[0x0C] = (unsigned char)(speed & 0x000000FF);
106-
usb_buf[0x0D] = bright;
131+
usb_buf[0x0D] = brightness;
107132
}
108133
if(mode == LOGITECH_G203L_MODE_BREATHING)
109134
{
110135
usb_buf[0x09] = (unsigned char)((speed>>8) & 0x000000FF);
111136
usb_buf[0x0A] = (unsigned char)(speed & 0x000000FF);
112-
usb_buf[0x0C] = bright;
137+
usb_buf[0x0C] = brightness;
113138
}
114139
if(mode == LOGITECH_G203L_MODE_WAVE)
115140
{
116141
usb_buf[0x0C] = (unsigned char)(speed & 0x000000FF);
117142
usb_buf[0x0D] = dir ? 0x01 : 0x06; //0x01: Left->Right 0x06: Right->Left
118-
usb_buf[0x0E] = bright;
143+
usb_buf[0x0E] = brightness;
119144
usb_buf[0x0F] = (unsigned char)((speed>>8) & 0x000000FF);
120145
}
121146
if(mode == LOGITECH_G203L_MODE_COLORMIXING)
122147
{
123148
usb_buf[0x0C] = (unsigned char)(speed & 0x000000FF);
124149
usb_buf[0x0D] = (unsigned char)((speed>>8) & 0x000000FF);
125-
usb_buf[0x0E] = bright;
150+
usb_buf[0x0E] = brightness;
126151
}
127152

128153
//END BYTE
129154
usb_buf[0x10] = 0x01;
130155

131-
hid_write(dev, usb_buf, 20);
132-
hid_read(dev, usb_buf, 20);
156+
SendPacket(usb_buf);
133157
}
134158

135159
void LogitechG203LController::SetDevice(std::vector<RGBColor> colors)
136160
{
137-
unsigned char usb_buf[20];
161+
unsigned char usb_buf[PACKET_SIZE];
138162

139-
memset(usb_buf, 0x00, sizeof(usb_buf));
163+
memset(usb_buf, 0x00, PACKET_SIZE);
140164

141165
usb_buf[0x00] = 0x11;
142166
usb_buf[0x01] = 0xFF;
143167
usb_buf[0x02] = 0x12;
144-
usb_buf[0x03] = 0x1A;
168+
usb_buf[0x03] = 0x10;
145169

146170
usb_buf[0x04] = 0x01;
147171
usb_buf[0x05] = RGBGetRValue(colors[0]);
@@ -160,8 +184,30 @@ void LogitechG203LController::SetDevice(std::vector<RGBColor> colors)
160184

161185
usb_buf[0x10] = 0xFF;
162186

163-
hid_write(dev, usb_buf, 20);
164-
hid_read(dev, usb_buf, 20);
187+
SendPacket(usb_buf);
165188

166189
SendApply();
167190
}
191+
192+
void LogitechG203LController::SendPacket(unsigned char* buffer)
193+
{
194+
if(dev != nullptr)
195+
{
196+
if(hid_write(dev, buffer, PACKET_SIZE) == -1)
197+
{
198+
hid_close(dev);
199+
dev = hid_open_path(location.c_str());
200+
return;
201+
}
202+
}
203+
204+
if(dev != nullptr)
205+
{
206+
if(hid_read_timeout(dev, buffer, PACKET_SIZE, 10) <= 0)
207+
{
208+
hid_close(dev);
209+
dev = hid_open_path(location.c_str());
210+
return;
211+
}
212+
}
213+
}

Controllers/LogitechController/LogitechG203LController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ class LogitechG203LController
3434
std::string location;
3535

3636
void SendApply();
37+
void SendPacket(unsigned char* buffer);
3738
};

Controllers/LogitechController/RGBController_LogitechG203L.cpp

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,62 +23,74 @@ RGBController_LogitechG203L::RGBController_LogitechG203L(LogitechG203LController
2323
serial = controller->GetSerialString();
2424

2525
mode Direct;
26-
Direct.name = "Direct";
27-
Direct.value = LOGITECH_G203L_MODE_DIRECT;
28-
Direct.flags = MODE_FLAG_HAS_PER_LED_COLOR;
29-
Direct.color_mode = MODE_COLORS_PER_LED;
26+
Direct.name = "Direct";
27+
Direct.value = LOGITECH_G203L_MODE_DIRECT;
28+
Direct.flags = MODE_FLAG_HAS_PER_LED_COLOR;
29+
Direct.color_mode = MODE_COLORS_PER_LED;
3030
modes.push_back(Direct);
3131

3232
mode Off;
33-
Off.name = "Off";
34-
Off.value = LOGITECH_G203L_MODE_OFF;
35-
Off.flags = 0;
36-
Off.color_mode = MODE_COLORS_NONE;
33+
Off.name = "Off";
34+
Off.value = LOGITECH_G203L_MODE_OFF;
35+
Off.flags = 0;
36+
Off.color_mode = MODE_COLORS_NONE;
3737
modes.push_back(Off);
3838

3939
mode Static;
40-
Static.name = "Static";
41-
Static.value = LOGITECH_G203L_MODE_STATIC;
42-
Static.flags = MODE_FLAG_HAS_MODE_SPECIFIC_COLOR;
43-
Static.color_mode = MODE_COLORS_MODE_SPECIFIC;
40+
Static.name = "Static";
41+
Static.value = LOGITECH_G203L_MODE_STATIC;
42+
Static.flags = MODE_FLAG_HAS_MODE_SPECIFIC_COLOR;
43+
Static.color_mode = MODE_COLORS_MODE_SPECIFIC;
4444
Static.colors.resize(1);
4545
modes.push_back(Static);
4646

4747
mode Cycle;
48-
Cycle.name = "Cycle";
49-
Cycle.value = LOGITECH_G203L_MODE_CYCLE;
50-
Cycle.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS;
51-
Cycle.color_mode = MODE_COLORS_NONE;
52-
Cycle.speed_min = 0x4E20;
53-
Cycle.speed_max = 0x03E8;
48+
Cycle.name = "Cycle";
49+
Cycle.value = LOGITECH_G203L_MODE_CYCLE;
50+
Cycle.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS;
51+
Cycle.color_mode = MODE_COLORS_NONE;
52+
Cycle.speed_min = 0x4E20;
53+
Cycle.speed_max = 0x03E8;
54+
Cycle.brightness = 20;
55+
Cycle.brightness_min = 0;
56+
Cycle.brightness_max = 20;
5457
modes.push_back(Cycle);
5558

5659
mode Breathing;
57-
Breathing.name = "Breathing";
58-
Breathing.value = LOGITECH_G203L_MODE_BREATHING;
59-
Breathing.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR;
60-
Breathing.color_mode = MODE_COLORS_MODE_SPECIFIC;
61-
Breathing.speed_min = 0x4E20;
62-
Breathing.speed_max = 0x03E8;
60+
Breathing.name = "Breathing";
61+
Breathing.value = LOGITECH_G203L_MODE_BREATHING;
62+
Breathing.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_MODE_SPECIFIC_COLOR;
63+
Breathing.color_mode = MODE_COLORS_MODE_SPECIFIC;
64+
Breathing.speed_min = 0x4E20;
65+
Breathing.speed_max = 0x03E8;
66+
Breathing.brightness = 20;
67+
Breathing.brightness_min = 0;
68+
Breathing.brightness_max = 20;
6369
Breathing.colors.resize(1);
6470
modes.push_back(Breathing);
6571

6672
mode Wave;
67-
Wave.name = "Wave";
68-
Wave.value = LOGITECH_G203L_MODE_WAVE;
69-
Wave.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_DIRECTION_LR;
70-
Wave.color_mode = MODE_COLORS_NONE;
71-
Wave.speed_min = 0x4E20;
72-
Wave.speed_max = 0x03E8;
73+
Wave.name = "Wave";
74+
Wave.value = LOGITECH_G203L_MODE_WAVE;
75+
Wave.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS | MODE_FLAG_HAS_DIRECTION_LR;
76+
Wave.color_mode = MODE_COLORS_NONE;
77+
Wave.speed_min = 0x4E20;
78+
Wave.speed_max = 0x03E8;
79+
Wave.brightness = 20;
80+
Wave.brightness_min = 0;
81+
Wave.brightness_max = 20;
7382
modes.push_back(Wave);
7483

7584
mode Colormixing;
76-
Colormixing.name = "Colormixing";
77-
Colormixing.value = LOGITECH_G203L_MODE_COLORMIXING;
78-
Colormixing.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS;
79-
Colormixing.color_mode = MODE_COLORS_NONE;
80-
Colormixing.speed_min = 0x4E20;
81-
Colormixing.speed_max = 0x03E8;
85+
Colormixing.name = "Colormixing";
86+
Colormixing.value = LOGITECH_G203L_MODE_COLORMIXING;
87+
Colormixing.flags = MODE_FLAG_HAS_SPEED | MODE_FLAG_HAS_BRIGHTNESS;
88+
Colormixing.color_mode = MODE_COLORS_NONE;
89+
Colormixing.speed_min = 0x4E20;
90+
Colormixing.speed_max = 0x03E8;
91+
Colormixing.brightness = 20;
92+
Colormixing.brightness_min = 0;
93+
Colormixing.brightness_max = 20;
8294
modes.push_back(Colormixing);
8395

8496
SetupZones();
@@ -152,7 +164,6 @@ void RGBController_LogitechG203L::DeviceUpdateMode()
152164
unsigned char grn = 0;
153165
unsigned char blu = 0;
154166
unsigned char dir = 0;
155-
unsigned char val = 0xFF;
156167

157168
if(modes[active_mode].color_mode & MODE_COLORS_MODE_SPECIFIC)
158169
{
@@ -177,6 +188,6 @@ void RGBController_LogitechG203L::DeviceUpdateMode()
177188
}
178189
else
179190
{
180-
controller->SetMode(modes[active_mode].value, modes[active_mode].speed, val, dir, red, grn, blu);
191+
controller->SetMode(modes[active_mode].value, modes[active_mode].speed, modes[active_mode].brightness, dir, red, grn, blu);
181192
}
182193
}
110 KB
Binary file not shown.
1.06 KB
Binary file not shown.
93.5 KB
Binary file not shown.
1.08 KB
Binary file not shown.

0 commit comments

Comments
 (0)