-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapper.js
More file actions
87 lines (74 loc) · 2.04 KB
/
Copy pathMapper.js
File metadata and controls
87 lines (74 loc) · 2.04 KB
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
const { IO1, IO2, IO3, IO4, IO5, IO6 } = require('./constants');
const { pixelMap } = require('./pixelMap');
class Mapper {
constructor() {
this.IOArrays = {};
this.IOArrays[IO1] = {
data: new Array(128).fill(1),
wasEmpty: false
};
this.IOArrays[IO2] = {
data: new Array(128).fill(1),
wasEmpty: false
};
this.IOArrays[IO3] = {
data: new Array(128).fill(1),
wasEmpty: false
};
this.IOArrays[IO4] = {
data: new Array(128).fill(1),
wasEmpty: false
};
this.IOArrays[IO5] = {
data: new Array(128).fill(1),
wasEmpty: false
};
this.IOArrays[IO6] = {
data: new Array(128).fill(1),
wasEmpty: false
};
this.randomPixels = true;
}
getIOArrays(frameData) {
this.map(frameData);
return this.IOArrays;
}
map(frameData) {
if (frameData.length != pixelMap.length) {
throw 'In Mapper.Map(): Frame data does not match PixelMap size';
}
let hasData = false;
frameData.forEach((item, index) => {
const pixel = pixelMap[index];
if (pixel === null) return;
// Pixel.index is 1 indexed
this.IOArrays[pixel.io].data[pixel.index - 1] = item;
if (!this.hasData && item == 0) {
hasData = true;
}
});
// Only set random pixels if we don't have any frame data
if (this.randomPixels && !hasData) {
this.setRandomPixels();
}
}
setRandomPixels() {
for (const arrayName in this.IOArrays) {
const ioArray = this.IOArrays[arrayName];
// 0 to 10 pixels
const numPixels = this.getRandomInt(11);
const probability = 60; // Higher is lower probability
// set the number of pixels, with probability scalar
for (let i = 0; i < numPixels; i++) {
const index = this.getRandomInt(ioArray.data.length * probability);
if (index < ioArray.data.length) {
ioArray.data[index] = 0;
}
}
}
}
getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
}
module.exports = new Mapper();