This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (144 loc) · 4.78 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var TerminalScreen = require('terminal-pixelscreen')
, _ = require('lodash')
, extend = require('extend');
function SubScreen (name, options, callback) {
var opts = extend({
x: 0,
y: 0,
width: 1,
height: 1,
channels: 3,
dmx: false
},options);
this.name = name;
this.x = opts.x;
this.y = opts.y;
this.width = opts.width;
this.height = opts.height;
this.channels = opts.channels;
this.dmx = opts.dmx;
this.callback = callback;
this.image = createImageArray(this.width, this.height, this.channels);
}
function createImageArray (width, height, channels) {
var array = [];
for (var i = 0; i < height; i++) {
array.push([]);
for (var j = 0; j < width; j++) {
array[i].push([]);
for (var k = 0; k < channels; k++) {
array[i][j].push(0);
}
}
}
return array;
}
function PixelScreen (options) {
var opts = extend({
width: 1,
height: 1,
channels: 3,
displayScreen: false
}, options);
this.width = opts.width;
this.height = opts.height;
this.channels = opts.channels;
this.image = createImageArray(this.width, this.height, this.channels);
this.screens = [];
this.displayScreen = opts.displayScreen;
this.terminalScreen = new TerminalScreen(this.width,this.height,0);
}
PixelScreen.prototype.registerScreen = function (name, options, callback) {
if (!name || this.screens[name]) throw new Error('Screen "' + name + '" already exists!');
this.screens[name] = new SubScreen(name, options, callback);
return this;
};
PixelScreen.prototype.unregisterScreen = function (name) {
if (!name || !this.screens[name]) throw new Error('Screen "' + name + '" doesn\'t exist!');
this.screen[name] = null;
return this;
};
PixelScreen.prototype.forceUpdate = function () {
for (var screenName in this.screens) {
var screen = this.screens[screenName]
, pixelImage = this.getImage(screen.name);
if(screen.dmx === true) pixelImage = this.arrayToDMX(pixelImage);
screen.callback(null, pixelImage);
}
if (this.displayScreen === true) {
this.terminalScreen.update(this.image);
this.terminalScreen.paint();
}
return this;
};
PixelScreen.prototype.update = function (input) {
if (!input || !input.length) throw new Error('No input!');
if (input[0][0].length !== this.channels) throw new Error('Invalid no. of channels!');
if (input.length !== this.height ||
input[0].length !== this.width) throw new Error('Invalid dimensions! (' + input[0].length + 'x' + input.length +
' instead of ' + this.width + 'x' + this.height + ')');
this.image = _.cloneDeep(input);
this.forceUpdate();
return this;
};
PixelScreen.prototype.getImage = function (name) {
if (!name) return this.image;
if (!this.screens[name]) return;
var screen = this.screens[name]
, image = [];
for (var i = 0; i < screen.height; i++) {
image.push([]);
for (var j = 0; j < screen.width; j++) {
if ( // out of bound ranges ranges
i + screen.y < 0 ||
j + screen.x < 0 ||
i + screen.y > this.image.length ||
j + screen.x > this.image[i+screen.y].length
) {
// return flat channels ([0,...,0])
image[i].push(this.enerateArray(3,0));
} else {
image[i].push(this.createBalancedChannels(this.image[i+screen.y][j+screen.x], 3));
}
}
}
return image;
};
PixelScreen.prototype.createBalancedChannels = function (inputArray, expectedChannels) {
// return channels and flatten or cut channels
// if we have a difference in no. of channels
var array = [];
for (var k = 0; k < inputArray.length; k++) {
if (k < expectedChannels) {
array.push(inputArray[k]);
} else {
array.push(0);
}
}
// fill up with flat channels if we are missing channels
if (array.length < expectedChannels) {
for (var k = array.length; k < expectedChannels; k++) {
array.push(0);
}
}
return array;
};
PixelScreen.prototype.generateArray = function (length, value) {
var array = [];
for (var i = 0; i < length; i++) {
array.push(value);
}
return array;
};
PixelScreen.prototype.arrayToDMX = function (input) {
var dmx = [];
for (var i = 0; i < input.length; i++) {
for (var j = 0; j < input[i].length; j++) {
for (var k = 0; k < input[i][j].length; k++) {
dmx.push(input[i][j][k]);
}
}
}
return dmx;
};
module.exports = PixelScreen;