-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (110 loc) · 3.2 KB
/
index.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
103
104
105
106
107
108
109
110
111
112
113
114
115
const memUsedColour = [ 245, 69, 198 ];
const memAvailColour = [ 0, 0, 0 ];
const lightIndexMem = 0;
const lightCountMem = 10;
const lightReverseMem = true;
const swapUsedColour = [ 41, 183, 249 ];
const swapAvailColour = [ 0, 0, 0 ];
const lightIndexSwap = 1;
const lightCountSwap = 10;
const lightReverseSwap = true;
const updateInterval = 1000;
const sdkClient = {
host: "localhost",
port: 6742,
name: "RAM lights"
};
const { spawn } = require("child_process");
const { OpenRGBClient, OpenRGBDevice } = require("openrgb");
function colour(e) {
return e.map(f => ({ red: f[0], green: f[1], blue: f[2] }));
}
function lerpN(a, b, t) {
return (b-a)*t+a;
}
function lerp(a, b, t) {
return a.map((_, i) => lerpN(a[i], b[i], t));
}
function round(a) {
return a.map(i => Math.floor(i));
}
function percArr(perc, len) {
let arr = new Array(len).fill(0);
let n = perc * len;
let nF = Math.floor(n);
let nC = Math.ceil(n);
for (let i = 0; i < nF; ++i) {
arr[i] = 1;
}
if (nC > nF) {
arr[nF] = n - nF;
}
return arr;
}
function parseFree(str) {
let lines = str.split("\n");
let mem = lines[1].split(/ +/g);
let swap = lines[2].split(/ +/g);
let memUsed = +mem[2];
let memTotal = +mem[1];
let swapUsed = +swap[2];
let swapTotal = +swap[1];
let memPerc = memTotal > 0 ? memUsed / memTotal : 0;
let swapPerc = swapTotal > 0 ? swapUsed / swapTotal : 0;
return { mem: memPerc, swap: swapPerc };
}
function run(bin, cmdline) {
var proc = spawn(bin, cmdline);
let buffers = [];
proc.stdout.on("data", b => buffers.push(b));
proc.stdin.end();
return new Promise((resolve, reject) => {
proc.once("error", reject);
proc.once("exit", (c) => {
if (c > 0) reject("exited with code", c);
resolve(Buffer.concat(buffers));
});
});
}
let exit = false;
async function setLight(client, index, count, reverse, perc, availColour, usedColour) {
if (exit) return;
if (index == null) return;
const device = await client.getDeviceController(index);
let colours = percArr(perc, count);
//console.log(`Setting ${device.name} (#${index}) to`, colours);
colours = colours.map(e => round(lerp(availColour, usedColour, e)));
if (reverse) colours = colours.reverse();
if (exit) return;
await client.updateLeds(index, colour(colours));
}
(async()=>{
const client = new OpenRGBClient(sdkClient);
try {
await client.connect();
} catch (err) {
if (err.code == "ECONNREFUSED") {
console.log(`Try starting the SDK server with 'openrgb --server --server-port ${sdkClient.port}'`);
}
throw err;
}
[ "SIGINT", "SIGQUIT", "exit", "beforeExit" ].forEach(i =>
process.on(i, async () => {
if (exit) return;
exit = true;
await client.disconnect();
}));
let last = performance.now();
while (true) {
if (exit) return;
let out = await run("/bin/free", [ ]);
if (exit) return;
let perc = parseFree(out.toString());
await setLight(client, lightIndexMem, lightCountMem, lightReverseMem, perc.mem, memAvailColour, memUsedColour);
await setLight(client, lightIndexSwap, lightCountSwap, lightReverseSwap, perc.swap, swapAvailColour, swapUsedColour);
let now = performance.now();
if (now - last < updateInterval)
await new Promise(r => setTimeout(() => r(), updateInterval - (now - last)));
last = performance.now();
}
})();