-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWS2812.js
More file actions
108 lines (99 loc) · 2.71 KB
/
WS2812.js
File metadata and controls
108 lines (99 loc) · 2.71 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
+(function(factory) {
if (typeof exports === 'undefined') {
factory(webduino || {});
} else {
module.exports = factory;
}
}(function(scope) {
'use strict';
var self;
var proto;
var sendLength = 50;
var sendArray = [];
var sendAck = '';
var sendCallback;
var Module = scope.Module;
var _backlight;
function WS2812(board, pin, leds) {
Module.call(this);
this._board = board;
self = this;
self._pin = pin;
self._leds = leds;
var cmd = '';
if (leds <= 64) {
cmd = [0xF0, 0x04, 0x24, 0x0 /*init*/ , leds, pin, 0xF7];
} else {
var h6bit = leds >>> 6;
var l6bit = leds & 0x3F;
cmd = [0xF0, 0x04, 0x24, 0x4 /*init*/ , h6bit, l6bit, pin, 0xF7];
}
board.send(cmd);
board.on(webduino.BoardEvent.SYSEX_MESSAGE,
function(event) {
var m = event.message;
});
}
WS2812.prototype = proto = Object.create(Module.prototype, {
constructor: {
value: WS2812
},
backlight: {
get: function() {
return _backlight;
},
set: function(val) {
_backlight = val;
}
}
});
proto.setColor = function(led, color) {
if (led > 64) {
this.setColor64(led, color);
return;
}
var data = '';
var cmd = [0xF0, 0x04, 0x24, 0x03];
if (arguments.length == 1) {
data = led;
} else {
data = data.concat(toHex(led));
data = data.concat(color.substring(1));
}
for (var i = 0; i < data.length; i++) {
cmd.push(data.charCodeAt(i))
}
cmd.push(0xF7);
this._board.send(cmd);
this._board.flush();
}
proto.setColor64 = function(led, color) {
var data = '';
var cmd = [0xF0, 0x04, 0x24, 0x05];
cmd.push(led >>> 6);
cmd.push(led & 0x3F);
data = data.concat(color.substring(1));
for (var i = 0; i < data.length; i++) {
cmd.push(data.charCodeAt(i))
}
cmd.push(0xF7);
this._board.send(cmd);
this._board.flush();
}
proto.off = function() {
this._board.send([0xF0, 0x04, 0x24, 0x02, 0xF7]);
}
proto.brightness = function(b) {
var data = toHex(b);
this._board.send([0xF0, 0x04, 0x24, 0x01, b, 0xF7]);
this._board.flush();
}
function toHex(num) {
var str = num.toString(16);
if (parseInt(num) < 16) {
str = '0' + str;
}
return str;
}
scope.module.WS2812 = WS2812;
}));