-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
78 lines (71 loc) · 1.86 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
const serialosc = require('serialosc');
let activeDevice;
let grid = {
ready: false,
keyCb: () => {}
};
grid.key = (cb) => grid.keyCb = cb;
grid.refresh = (led) => {
if (!activeDevice) {
return;
}
for (let yOffset = 0; yOffset < activeDevice.sizeY; yOffset += 8) {
for (let xOffset = 0; xOffset < activeDevice.sizeX; xOffset += 8) {
let mapLed = [];
for (let y = yOffset; y < yOffset + 8; y++) {
if (typeof led[y] == 'undefined') {
continue;
}
for (let x = xOffset; x < xOffset + 8; x++) {
if (typeof led[y][x] == 'undefined') {
led[y][x] = 0;
}
if (typeof led[y][x] != 'number') {
led[y][x] = 0;
}
if (!grid.varibright && !mapLed[y - yOffset]) {
mapLed[y - yOffset] = [];
}
if (grid.varibright) {
mapLed[((y - yOffset) * 8) + x - xOffset] = led[y][x];
} else {
mapLed[y - yOffset][x - xOffset] = led[y][x] ? 1 : 0;
}
}
}
if (grid.varibright) {
activeDevice.levelMap(xOffset, yOffset, mapLed);
} else {
activeDevice.map(xOffset, yOffset, mapLed);
}
}
}
};
module.exports = (id) => {
return new Promise((resolve, reject) => {
let addEvent = id ? id + ':add' : 'device:add';
serialosc.start({
startDevices: false
});
serialosc.on(addEvent, (device) => {
if (activeDevice) {
return;
}
if (device.type != 'grid') {
return;
}
if (device.id.match(/^m\d+$/)) {
grid.varibright = true;
}
activeDevice = device;
device.on('initialized', () => {
device.on('key', (press) => {
grid.keyCb(press.x, press.y, press.s);
});
grid.ready = true;
resolve(grid);
});
device.start();
});
});
};