-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
76 lines (68 loc) · 1.92 KB
/
worker.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
const {
workerData,
parentPort
} = require('worker_threads');
const ntClient = require('wpilib-nt-client');
const client = new ntClient.Client();
var getPixels = require("get-pixels");
const tmp_out = workerData[0];
var height;
var width;
async function main() {
//while (true) {
try {
var j = 0;
var output = await new Promise(function (resolve, reject) {
getPixels(tmp_out, (err, pixels) => {
if (err) {
reject(err);
} else {
resolve(pixels);
}
});
});
while (j < height) {
var k = 0;
while (k < width) {
var key_target = "/display/row" + String(j) + "/column" + String(k);
if (Number(output.get(k, j, 0)) > 127) {
client.Assign(true, key_target);
} else {
client.Assign(false, key_target);
}
k++;
}
j++;
}
} catch (e) {
if (String(e) != "Error: Unexpected end of input") {
console.error(e);
}
}
//}
}
function messageHandler() {
try {
getPixels(tmp_out, function (err, pixels) {
width = pixels.shape.slice()[0];
height = pixels.shape.slice()[1];
setInterval(function () {
const loop = main();
loop.catch((e) => {
if (String(e) != "Error: Unexpected end of input") {
console.error(e);
}
});
}, 5);
});
} catch (e) {
if (String(e) != "Error: Unexpected end of input") {
console.error(e);
}
}
}
parentPort.on('message', messageHandler);
client.start((isConnected, err) => {
parentPort.postMessage(null);
client.Assign(9999, "/display/frame");
}, workerData[1]);