-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
102 lines (88 loc) · 2.44 KB
/
test.js
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
const sdk = require('.');
const readline = require('readline');
const input_queue = [];
readline.emitKeypressEvents(process.stdin);
process.stdin.on('keypress', (key, data) => {
if (data.sequence === '\u0003') {
// ^C
exit();
}
input_queue.push(key);
});
function exit(code = 0) {
console.log('Exiting.');
process.exit(code);
}
function getAvailableLeds() {
const leds = [];
const { error, data: devices } = sdk.CorsairGetDevices({
deviceTypeMask: sdk.CorsairDeviceType.CDT_All
});
if (error != sdk.CorsairError.CE_Success) {
return leds;
}
for (let di = 0; di < devices.length; ++di) {
const { data: ledPositions } = sdk.CorsairGetLedPositions(devices[di].id);
leds.push({
deviceId: devices[di].id,
leds: ledPositions.map(p => ({ id: p.id, r: 0, g: 0, b: 0, a: 0 }))
});
}
return leds;
}
function performPulseEffect(allLeds, x) {
const cnt = allLeds.length;
let val = ~~((1 - (x - 1) * (x - 1)) * 255);
for (let di = 0; di < cnt; ++di) {
const deviceLeds = allLeds[di];
deviceLeds.leds.forEach(led => {
led.r = 0;
led.g = val;
led.b = 0;
led.a = 255;
});
sdk.CorsairSetLedColors(deviceLeds.deviceId, deviceLeds.leds);
}
}
function main() {
sdk.CorsairSubscribeForEvents(e => {
console.log(e.data);
});
const availableLeds = getAvailableLeds();
if (!availableLeds.length) {
console.error('No devices found');
exit(1);
}
console.log(
'Working... Use "+" or "-" to increase or decrease speed.\n' +
'Press "q" to close program...'
);
function loop(leds, waveDuration, x) {
const TIME_PER_FRAME = 25;
if (input_queue.length > 0) {
const input = input_queue.shift();
if (input === 'q' || input === 'Q') {
exit(0);
} else if (input === '+') {
waveDuration = Math.max(100, waveDuration - 100);
} else if (input === '-') {
waveDuration = Math.min(2000, waveDuration + 100);
}
}
performPulseEffect(leds, x);
return setTimeout(
loop,
TIME_PER_FRAME,
leds,
waveDuration,
(x + TIME_PER_FRAME / waveDuration) % 2
);
}
return loop(availableLeds, 500, 0);
}
sdk.CorsairConnect(evt => {
console.log(sdk.CorsairSessionStateToString(evt.data.state));
if (evt.data.state == sdk.CorsairSessionState.CSS_Connected) {
main();
}
});