forked from k45514n/node-open-pixel-control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen-pixel-control.js
More file actions
129 lines (100 loc) · 2.96 KB
/
open-pixel-control.js
File metadata and controls
129 lines (100 loc) · 2.96 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
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
var EventEmitter = require("events").EventEmitter,
util = require('util'),
net = require('net');
function OPC(opts){
if(!(this instanceof OPC)){
return new OPC(opts);
}
//Add Event emitter methods
EventEmitter.call(this);
//set up server properties
this.address = opts.address;
this.port = opts.port;
this.opc_client = null;
this.strips = [];
}
//LEAVE THIS HERE- if you put extensions after this they will vanish
//because of the way inheritance in node works.
util.inherits(OPC, EventEmitter);
OPC.prototype.connect = function(){
var self = this;
self.opc_client = new net.Socket().connect(self.port, self.address,
function() {
self.emit('connected');
});
self.opc_client.on('end', function(){
self.emit('disconnected');
});
};
OPC.prototype.disconnect = function(){
var self = this;
if(self.opc_client === null){
throw new Error('You need to connect a board to disconnect it.');
} else {
self.opc_client.end();
}
};
OPC.prototype.add_strip = function(opts){
if(!opts.length){
throw new Error('you need to specify a length for an LED strip');
}
var strip_number = this.strips.length;
//TODO: Add case when there are too many strips?
//init strip
var new_pixels = [];
for(var i = 0; i < opts.length; i++){
new_pixels.push([0, 0, 0]);
}
var strip_info = {
id: strip_number,
length: opts.length,
lo_byte: (opts.length * 3) % 256,
hi_byte: parseInt((opts.length * 3) / 256, 10),
pixels: new_pixels
};
this.strips.push(strip_info);
//return the strip ID in case it's needed
return strip_info;
};
//replaces one pixel in a strip
OPC.prototype.put_pixel = function(strip_id, pixel_index, colors){
var self = this;
self.strips[strip_id].pixels[pixel_index] = colors;
var message = assemble_opc_message(this.strips[strip_id]);
self.opc_client.write(message, function(){
self.emit('data_sent');
});
};
//leave this- replaces all pixels in a strip
OPC.prototype.put_pixels = function(strip_id, pixels){
var self = this;
self.strips[strip_id].pixels = pixels;
var message = assemble_opc_message(this.strips[strip_id]);
this.opc_client.write(message, function(){
self.emit('data_sent');
});
};
var header, message_pixels, message;
function assemble_opc_message(strip){
//assemble our OPC message header
lengthBuffer = new Buffer(2);
lengthBuffer[0] = strip.hi_byte;
lengthBuffer[1] = strip.lo_byte;
header = new Buffer(4);
header[0] = 0;
header[1] = 0;
header[2] = lengthBuffer.readUInt8(0);
header[3] = lengthBuffer.readUInt8(1);
message_pixels = new Uint8Array(strip.length * 3);
for(var i = 0; i < strip.length; i++){
if(strip.pixels[i]){
message_pixels[i*3] = strip.pixels[i][0];
message_pixels[(i*3)+1] = strip.pixels[i][1];
message_pixels[(i*3)+2] = strip.pixels[i][2];
}
}
message = header;
message = Buffer.concat([message, new Buffer(message_pixels)]);
return message;
}
module.exports = OPC;