-
Notifications
You must be signed in to change notification settings - Fork 6
/
omen_light.cc
293 lines (250 loc) · 7.68 KB
/
omen_light.cc
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <hidapi/hidapi.h>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <string>
constexpr uint16_t VENDOR_ID = 0x103c;
constexpr uint16_t PRODUCT_ID = 0x84fd;
constexpr uint8_t VERSION = 0x12;
constexpr uint8_t MAX_BRIGHTNESS = 100;
constexpr uint8_t N_LEDS = 8;
struct __attribute__((packed)) Report {
enum class Mode : uint8_t { STATIC = 1, OFF = 5, BREATHING, CYCLE, BLINKING };
enum class Type : uint8_t {
OFF = 0,
STATIC = 2,
CHANGING = 10,
};
enum class Power : uint8_t { ON = 1, SUSPEND = 2 };
enum class Theme : uint8_t {
OFF = 0,
CUSTOM = 0,
GALAXY,
VOLCANO,
JUNGO,
OCEAN,
UNICORN
};
enum class Speed : uint8_t { OFF = 0, SLOW, MEDIUM, FAST };
struct __attribute__((packed)) Color {
uint8_t r;
uint8_t g;
uint8_t b;
};
uint8_t report_id; // always 0
uint8_t reserved;
uint8_t version;
Mode mode;
uint8_t custom_color_count; // total number of custom colors
uint8_t custom_color_id; // the custom color to set. Starting with 1
uint8_t reserved2[2];
Color colors[N_LEDS];
uint8_t reserved3[40 - sizeof(colors)];
uint8_t brightness; // 0-100
Type type;
uint8_t reserved4[4];
uint8_t led;
Power power;
Theme theme;
Speed speed;
};
void ShowUsage(char* arg0) {
std::cout << R"Usage(
Usage:
omen_light <led> <power> <mode> [options..]
led: the led module to control. Can be 'front', 'chase' or a number from 1 to 8.
power: the power state to which the setting is applied. Can be 'on', 'suspend'
mode: color mode. Can be 'off', 'static', 'breathing', 'cycle', 'blinking'
Options for static mode:
omen_light <led> <power> static <r> <g> <b>
r, g, b: the value of red, green, and blue. From 0 to 255.
Options for breathing, cycle, and blinking mode:
omen_light <led> <power> breathing|... <speed> <theme> [<r> <g> <b>]...
speed: the color changing speed. Can be 'slow', 'medium', 'fast.
theme: the theme of the colors. Can be 'galaxy, volcano', 'jungo', 'ocean', 'unicorn' or 'custom'.
For custom theme, it needs to be followed by 1 to 6 sets of r, g, b values.
Example:
# Turn off the chase led when the power is on.
$ omen_light chase on off
# Set the front led to breath in volcano theme slowly when suspend
$ omen_light front suspend breathing slow galaxy
# Set the front led to blink between red and blue when on
$ omen_light front on blinking medium custom 255 0 0 0 0 255
# Set led 4 to a static green color.
$ omen_light 4 on static 0 255 0
)Usage";
}
bool SendReport(const Report& report) {
if (hid_init()) {
std::cerr << "hid_init() failed" << std::endl;
return false;
}
hid_device* handle = hid_open(VENDOR_ID, PRODUCT_ID, nullptr);
if (!handle) {
std::cerr << "hid_open() failed" << std::endl;
return false;
}
auto* report_ptr = reinterpret_cast<const uint8_t*>(&report);
if (hid_write(handle, report_ptr, sizeof(report)) < 0) {
std::cerr << "hid_write() failed" << std::endl;
return false;
}
hid_close(handle);
if (hid_exit()) {
std::cerr << "hid_exit() failed" << std::endl;
return false;
}
return true;
}
bool ParseLed(const std::string& arg, uint8_t* led) {
if (arg == "front") {
*led = 1;
} else if (arg == "chase") {
*led = 2;
} else {
try {
*led = std::stoi(arg);
if ((*led < 1) || (*led > N_LEDS)) {
std::cerr << "led number out of range: " << arg << std::endl;
return false;
}
} catch (const std::exception& e) {
// probably not an int, move on
std::cerr << "unknown led: " << arg << std::endl;
return false;
}
}
return true;
}
bool ParsePower(const std::string& arg, Report::Power* power) {
if (arg == "on") {
*power = Report::Power::ON;
} else if (arg == "suspend") {
*power = Report::Power::SUSPEND;
} else {
std::cerr << "unknown power: " << arg << std::endl;
return false;
}
return true;
}
bool ParseMode(const std::string& arg, Report::Mode* mode) {
if (arg == "off") {
*mode = Report::Mode::OFF;
} else if (arg == "static") {
*mode = Report::Mode::STATIC;
} else if (arg == "breathing") {
*mode = Report::Mode::BREATHING;
} else if (arg == "cycle") {
*mode = Report::Mode::CYCLE;
} else if (arg == "blinking") {
*mode = Report::Mode::BLINKING;
} else {
std::cerr << "unknown mode: " << arg << std::endl;
return false;
}
return true;
}
void ParseColor(char** argv, Report* report) {
Report::Color* c = nullptr;
c = &report->colors[report->led - 1];
// Don't care about parsing error.
c->r = std::atoi(argv[0]);
c->g = std::atoi(argv[1]);
c->b = std::atoi(argv[2]);
}
bool ParseSpeed(const std::string& arg, Report::Speed* speed) {
if (arg == "slow") {
*speed = Report::Speed::SLOW;
} else if (arg == "medium") {
*speed = Report::Speed::MEDIUM;
} else if (arg == "fast") {
*speed = Report::Speed::FAST;
} else {
std::cerr << "unknown speed: " << arg << std::endl;
return false;
}
return true;
}
bool ParseTheme(const std::string& arg, Report::Theme* theme) {
if (arg == "custom") {
*theme = Report::Theme::CUSTOM;
} else if (arg == "galaxy") {
*theme = Report::Theme::GALAXY;
} else if (arg == "volcano") {
*theme = Report::Theme::VOLCANO;
} else if (arg == "jungo") {
*theme = Report::Theme::JUNGO;
} else if (arg == "ocean") {
*theme = Report::Theme::OCEAN;
} else if (arg == "unicorn") {
*theme = Report::Theme::UNICORN;
} else {
std::cerr << "unknown theme: " << arg << std::endl;
return false;
}
return true;
}
bool Run(int argc, char** argv) {
Report report{}; // zero-initialized.
report.version = VERSION;
if (argc < 4) return false;
if (!ParseLed(argv[1], &report.led)) return false;
if (!ParsePower(argv[2], &report.power)) return false;
if (!ParseMode(argv[3], &report.mode)) return false;
argc -= 4, argv += 4; // consume arguments
switch (report.mode) {
case Report::Mode::OFF:
return SendReport(report);
case Report::Mode::STATIC:
if (argc != 3) return false;
ParseColor(argv, &report);
report.custom_color_count = 1;
report.custom_color_id = 1;
report.brightness = MAX_BRIGHTNESS;
report.type = Report::Type::STATIC;
return SendReport(report);
case Report::Mode::BREATHING:
case Report::Mode::CYCLE:
case Report::Mode::BLINKING:
if (argc < 2) return false;
if (!ParseSpeed(argv[0], &report.speed)) return false;
if (!ParseTheme(argv[1], &report.theme)) return false;
report.brightness = MAX_BRIGHTNESS;
report.type = Report::Type::CHANGING;
if (report.theme == Report::Theme::CUSTOM) {
// custom theme
argc -= 2, argv += 2; // consume arguments
int mod = argc % 3; // check if it's a multiple of 3
if (mod != 0) {
std::cerr << "rgb values are not a multiple of 3" << std::endl;
return false;
}
report.custom_color_count = argc / 3;
if (report.custom_color_count < 1 || report.custom_color_count > 6) {
std::cerr << "incorrect amount of rgb sets: "
<< (int)report.custom_color_count << std::endl;
return false;
}
for (int i = 0; i < report.custom_color_count; ++i) {
report.custom_color_id = i + 1;
ParseColor(argv + i * 3, &report);
if (!SendReport(report)) return false;
}
return true;
} else {
// pre-defined theme
report.custom_color_count = 1;
report.custom_color_id = 1;
return SendReport(report);
}
break;
}
return true;
}
int main(int argc, char** argv) {
if (!Run(argc, argv)) {
ShowUsage(argv[0]);
return 1;
}
return 0;
}