-
Notifications
You must be signed in to change notification settings - Fork 2
/
brightness.h
253 lines (219 loc) · 6.87 KB
/
brightness.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <iostream>
#include <vector>
#include <conio.h>
#include "hidapi.h"
using std::vector;
const uint16_t vendor_id = 0x43e;
const uint16_t product_id = 0x9a40;
const uint16_t max_brightness = 0xd2f0;
const uint16_t min_brightness = 0x0190;
const std::vector<uint16_t> small_steps = {
0x0190, 0x01af, 0x01d2, 0x01f7,
0x021f, 0x024a, 0x0279, 0x02ac,
0x02e2, 0x031d, 0x035c, 0x03a1,
0x03eb, 0x043b, 0x0491, 0x04ee,
0x0553, 0x05c0, 0x0635, 0x06b3,
0x073c, 0x07d0, 0x086f, 0x091b,
0x09d5, 0x0a9d, 0x0b76, 0x0c60,
0x0d5c, 0x0e6c, 0x0f93, 0x10d0,
0x1227, 0x1399, 0x1529, 0x16d9,
0x18aa, 0x1aa2, 0x1cc1, 0x1f0b,
0x2184, 0x2430, 0x2712, 0x2a2e,
0x2d8b, 0x312b, 0x3516, 0x3951,
0x3de2, 0x42cf, 0x4822, 0x4de1,
0x5415, 0x5ac8, 0x6203, 0x69d2,
0x7240, 0x7b5a, 0x852d, 0x8fc9,
0x9b3d, 0xa79b, 0xb4f5, 0xc35f,
0xd2f0,
};
const std::vector<uint16_t> big_steps = {
0x0190, 0x021f, 0x02e2, 0x03eb,
0x0553, 0x073c, 0x09d5, 0x0d5c,
0x1227, 0x18aa, 0x2184, 0x2d8b,
0x3de2, 0x5415, 0x7240, 0x9b3d,
0xd2f0,
};
uint16_t next_step(uint16_t val, const vector<uint16_t>& steps)
{
auto start = 0;
auto end = steps.size() - 1;
while (start + 1 < end) {
auto mid = start + (end - start) / 2;
if (steps[mid] > val) {
end = mid;
}
else {
start = mid;
}
}
return steps[end];
}
uint16_t prev_step(uint16_t val, const vector<uint16_t>& steps)
{
auto start = 0;
auto end = steps.size() - 1;
while (start + 1 < end) {
auto mid = start + (end - start) / 2;
if (steps[mid] >= val) {
end = mid;
}
else {
start = mid;
}
}
return steps[start];
}
uint16_t get_brightness(hid_device* handle) {
uint8_t data[7] = { 0 };
int res = hid_get_feature_report(handle, data, sizeof(data));
if (res < 0) {
printf("Unable to get a feature report.\n");
printf("%ls", hid_error(handle));
}
return data[1] + (data[2] << 8);
}
void set_brightness(hid_device* handle, uint16_t val) {
uint8_t data[7] = {
0x00,
uint8_t(val & 0x00ff),
uint8_t((val >> 8) & 0x00ff),
0x00, 0x00, 0x00, 0x00 };
int res = hid_send_feature_report(handle, data, sizeof(data));
if (res < 0) {
printf("Unable to set a feature report.\n");
printf("%ls", hid_error(handle));
}
}
int initmain() {
hid_device* handle;
struct hid_device_info* devs, * cur_dev;
if (hid_init())
return -1;
char* ultrafine_monitor_path = NULL;
devs = hid_enumerate(0x0, 0x0);
cur_dev = devs;
while (cur_dev) {
if (cur_dev->vendor_id == 0x043e) //0x043e means LG
{
//printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
//printf("\n");
//printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
//printf(" Product: %ls\n", cur_dev->product_string);
//printf(" Release: %hx\n", cur_dev->release_number);
//printf(" Interface: %d\n", cur_dev->interface_number);
//printf("\n");
if (wcsstr(cur_dev->product_string, L"BRIGHTNESS"))
{
ultrafine_monitor_path = cur_dev->path;
break;
}
}
cur_dev = cur_dev->next;
}
if (ultrafine_monitor_path == NULL)
{
// printf("device not found!");
exit(-1);
}
handle = hid_open_path(ultrafine_monitor_path);
if (!handle) {
// printf("unable to open device\n");
return 1;
}
auto brightness = get_brightness(handle);
//printf("Press '-' or '=' to adjust brightness.\n");
//printf("Press '[' or: ']' to fine tune.\n");
//printf("Press 'p' to use the minimum brightness\n");
//printf("Press '\\' to use the maximum brightness\n");
//printf("Press 'q' to quit.\n");
while (true) {
// printf("Current brightness = %d%4s\r", int((float(brightness) / 54000) * 100.0), " ");
return int((float(brightness) / 54000) * 100.0);
int c = _getch();
if (c == 'q') {
break;
}
switch (c) {
case '+':
case '=':
brightness = next_step(brightness, big_steps);
set_brightness(handle, brightness);
break;
case '-':
case '_':
brightness = prev_step(brightness, big_steps);
set_brightness(handle, brightness);
break;
case ']':
brightness = next_step(brightness, small_steps);
set_brightness(handle, brightness);
break;
case '[':
brightness = prev_step(brightness, small_steps);
set_brightness(handle, brightness);
break;
case '\\':
brightness = max_brightness;
set_brightness(handle, brightness);
break;
case 'p':
brightness = min_brightness;
set_brightness(handle, brightness);
break;
default:
break;
}
}
hid_close(handle);
hid_exit();
return 0;
}
char* getdeviceinfo() {
hid_device* handle;
struct hid_device_info* devs, * cur_dev;
char* ultrafine_monitor_path = NULL;
if (hid_init())
return NULL;
devs = hid_enumerate(0x0, 0x0);
cur_dev = devs;
while (cur_dev) {
if (cur_dev->vendor_id == 0x043e) //0x043e means LG
{
//printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
//printf("\n");
//printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
//printf(" Product: %ls\n", cur_dev->product_string);
//printf(" Release: %hx\n", cur_dev->release_number);
//printf(" Interface: %d\n", cur_dev->interface_number);
//printf("\n");
if (cur_dev->product_string && wcsstr(cur_dev->product_string, L"BRIGHTNESS"))
{
ultrafine_monitor_path = cur_dev->path;
break;
}
}
cur_dev = cur_dev->next;
}
return ultrafine_monitor_path;
}
//char* getlgultrawidepath() {
// hid_device_info* cur_dev;
// cur_dev = getdeviceinfo();
//
// char* ultrafine_monitor_path;
//
// if (wcsstr(cur_dev->product_string, L"BRIGHTNESS"))
// {
// ultrafine_monitor_path = cur_dev->path;
// }
// return ultrafine_monitor_path;
//}
hid_device* handledevice() {
hid_device* handle;
char* ultrafine_monitor_path = getdeviceinfo();
handle = hid_open_path(ultrafine_monitor_path);
if (!handle) {
return handle ;
}
return handle;
}