-
Notifications
You must be signed in to change notification settings - Fork 5
/
Adafruit_NeoPixel.js
185 lines (178 loc) · 6.47 KB
/
Adafruit_NeoPixel.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//Constants
const NEO_RGB = NEO_RBG = NEO_GRB = NEO_GBR = NEO_BRG = NEO_BGR = 'RGB'
const NEO_WRGB = NEO_WRBG = NEO_WGRB = NEO_WGBR = NEO_WBRG = NEO_WBGR = NEO_RWGB = NEO_RWBG = NEO_RGWB = NEO_RGBW = NEO_RBWG = NEO_RBGW = NEO_GWRB = NEO_GWBR = NEO_GRWB = NEO_GRBW = NEO_GBWR = NEO_GBRW = NEO_BWRG = NEO_BWGR = NEO_BRWG = NEO_BRGW = NEO_BGWR = NEO_BGRW = 'RGBW'
const NEO_KHZ800 = NEO_KHZ400 = ''
//Class
class Adafruit_NeoPixel {
constructor(numLEDs, pin, type) {
this.numLEDs = numLEDs
this.type = type
this.pixels = []
this.lastPixels = []
for (var i=0; i<numLEDs; i++)
this.pixels[i] = 0
this.brightness = 255
this.begun = false
this.pin = pin
this.setPin(pin)
}
begin() {
this.begun = true
this.setPin(this.pin)
if (typeof document != "undefined") {
this.stripDiv.innerHTML = ''
this.ledDivs = []
for (var i=0; i<this.numLEDs; i++) {
var newLedDiv = document.createElement("div")
newLedDiv.className = 'led'
var newLedDivReal = this.stripDiv.appendChild(newLedDiv)
this.ledDivs.push(newLedDivReal)
}
} else {
var codeToPost = `
window.stripDiv.innerHTML = ''
window.ledDivs = []
for (var i=0; i<${this.numLEDs}; i++) {
var newLedDiv = document.createElement("div")
newLedDiv.className = 'led'
var newLedDivReal = window.stripDiv.appendChild(newLedDiv)
window.ledDivs.push(newLedDivReal)
}
`;
//console.log("CodeThread:")
//console.log(codeToPost)
postMessage(codeToPost)
}
}
show() {
// TODO: find a way to convert rgbw to rgb
if (this.lastPixels !== this.pixels) {
if (typeof document != "undefined") {
for (var i=0; i<this.numLEDs; i++) {
var hexColor = this.pixels[i].toString(16)
var zeroesToAdd = 6-hexColor.length
for (var j=0; j<zeroesToAdd; j++)
hexColor = '0' + hexColor
this.ledDivs[i].style.color = '#' + hexColor
}
} else {
for (var i=0; i<this.numLEDs; i++) {
var codeToPost = `
var hexColor = '${this.pixels[i].toString(16)}'
var zeroesToAdd = 6-hexColor.length
for (var j=0; j<zeroesToAdd; j++)
hexColor = '0' + hexColor
window.ledDivs[${i}].style.color = '#' + hexColor
`;
//console.log("CodeThread:")
//console.log(codeToPost)
postMessage(codeToPost)
}
}
}
}
setPin(pin) {
if (typeof document != "undefined") {
var ledStripDivs = document.querySelectorAll('[data-pin]')
for (var i=0; i<ledStripDivs.length; i++)
if (ledStripDivs[i].getAttribute('data-pin') == pin)
this.stripDiv = ledStripDivs[i]
} else {
var codeToPost = `
var ledStripDivs = document.querySelectorAll('[data-pin]')
for (var i=0; i<ledStripDivs.length; i++)
if (ledStripDivs[i].getAttribute('data-pin') == ${pin})
window.stripDiv = ledStripDivs[i]
`;
//console.log("CodeThread:")
//console.log(codeToPost)
postMessage(codeToPost)
}
this.pin = pin
}
setPixelColor(nLed, red_or_color, green=undefined, blue=undefined, white=undefined) {
if (green === undefined) { //all colors are in param 2
if (this.type === 'RGBW')
white = (red_or_color >> 24) & 0xff
green = (red_or_color >> 8) & 0xff
blue = red_or_color & 0xff
red_or_color = (red_or_color >> 16) & 0xff
}
if (this.brightness) {
red_or_color = (red_or_color * this.brightness) >> 8
green = (green * this.brightness) >> 8
blue = (blue * this.brightness) >> 8
if (this.type === 'RGBW')
white = (white * this.brightness) >> 8
}
if (this.type === 'RGB')
this.pixels[nLed] = (red_or_color << 16) + (green << 8) + blue
else
this.pixels[nLed] = (white << 24) + (red_or_color << 16) + (green << 8) + blue
}
setBrightness(brightness) {
this.brightness = brightness
}
clear() {
for (var i; i<this.numLEDs; i++)
this.pixels[i] = 0
}
updateLength(numLEDs) {
this.numLEDs = numLEDs
}
updateType(type) {
this.type = type
}
getPixels() {
return this.pixels
}
getBrightness() {
return this.brightness
}
numPixels() {
return this.numLEDs
}
Color(red, green, blue, white=undefined) {
if (white !== undefined)
return (white << 24) + (red << 16) + (green << 8) + blue
else
return (red << 16) + (green << 8) + blue
}
getPixelColor(nLed) {
var red = (this.pixels[nLed] >> 16) & 0xff
var green = (this.pixels[nLed] >> 8) & 0xff
var blue = this.pixels[nLed] & 0xff
if (this.type === 'RGBW') {
var white = this.pixels[nLed]
return (((white << 8) / brightness) << 24) |
(((red << 8) / brightness) << 16) |
(((green << 8) / brightness) << 8) |
( (blue << 8) / brightness )
} else {
return (((red << 8) / brightness) << 16) |
(((green << 8) / brightness) << 8) |
( (blue << 8) / brightness )
}
}
//TODO: find out what that is
//canShow() { return (micros() - endTime) >= 50L; }
}
//Other basic arduino functions/callbacks
function micros() {
return Date.now()*1000
}
function millis() {
return Date.now()
}
function delay(millis) {
var now = Date.now();
while(Date.now() < now + millis){}
}
if (typeof document != "undefined") {
document.addEventListener("DOMContentLoaded", function() {
if (typeof setup != "undefined")
setup()
if (typeof loop != "undefined")
window.loopInterval = setInterval(loop, 10)
})
}