-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathneopixel.ts
More file actions
416 lines (371 loc) · 13.9 KB
/
neopixel.ts
File metadata and controls
416 lines (371 loc) · 13.9 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**
Copyright(c) Microsoft Corporation
All rights reserved.
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED * AS IS *, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* This file was modified from the official Neopixel extension.
* Unused functions was removed.
*/
/**
* Well known colors for a NeoPixel strip
*/
enum MotionBitRgbColors {
//% block=red
Red = 0xFF0000,
//% block=orange
Orange = 0xFFA500,
//% block=yellow
Yellow = 0xFFFF00,
//% block=green
Green = 0x00FF00,
//% block=blue
Blue = 0x0000FF,
//% block=indigo
Indigo = 0x4b0082,
//% block=violet
Violet = 0x8a2be2,
//% block=purple
Purple = 0xFF00FF,
//% block=white
White = 0xFFFFFF,
//% block=black
Black = 0x000000
}
/**
* Different modes for RGB or RGB+W NeoPixel strips
*/
enum MotionBitRgbMode {
//% block="RGB (GRB format)"
RGB = 1,
//% block="RGB+W"
RGBW = 2,
//% block="RGB (RGB format)"
RGB_RGB = 3
}
/**
* Functions to operate NeoPixel strips.
*/
namespace motionbit {
/**
* A NeoPixel strip
*/
export class Strip {
buf: Buffer;
pin: DigitalPin;
// TODO: encode as bytes instead of 32bit
brightness: number;
start: number; // start offset in LED strip
_length: number; // number of LEDs
_mode: MotionBitRgbMode;
/**
* Shows all LEDs to a given color (range 0-255 for r, g, b).
* @param rgb RGB color of the LED
*/
showColor(rgb: number) {
rgb = rgb >> 0;
this.setAllRGB(rgb);
this.show();
}
/**
* Shows a rainbow pattern on all LEDs.
* @param startHue the start hue value for the rainbow, eg: 1
* @param endHue the end hue value for the rainbow, eg: 360
* @param pixelsColor the array to store the color for each pixel.
*/
showRainbow(startHue: number = 1, endHue: number = 360, pixelsColor: number[] = null) {
if (this._length <= 0) return;
startHue = startHue >> 0;
endHue = endHue >> 0;
const saturation = 100;
const luminance = 50;
const steps = this._length;
const direction = HueInterpolationDirection.Clockwise;
//hue
const h1 = startHue;
const h2 = endHue;
const hDistCW = ((h2 + 360) - h1) % 360;
const hStepCW = Math.idiv((hDistCW * 100), steps);
const hDistCCW = ((h1 + 360) - h2) % 360;
const hStepCCW = Math.idiv(-(hDistCCW * 100), steps);
let hStep: number;
if (direction === HueInterpolationDirection.Clockwise) {
hStep = hStepCW;
} else if (direction === HueInterpolationDirection.CounterClockwise) {
hStep = hStepCCW;
} else {
hStep = hDistCW < hDistCCW ? hStepCW : hStepCCW;
}
const h1_100 = h1 * 100; //we multiply by 100 so we keep more accurate results while doing interpolation
//sat
const s1 = saturation;
const s2 = saturation;
const sDist = s2 - s1;
const sStep = Math.idiv(sDist, steps);
const s1_100 = s1 * 100;
//lum
const l1 = luminance;
const l2 = luminance;
const lDist = l2 - l1;
const lStep = Math.idiv(lDist, steps);
const l1_100 = l1 * 100
//interpolate
if (steps === 1) {
this.setPixelColor(0, hsl(h1 + hStep, s1 + sStep, l1 + lStep))
if (pixelsColor != null) pixelsColor[0] = hsl(h1 + hStep, s1 + sStep, l1 + lStep);
} else {
this.setPixelColor(0, hsl(startHue, saturation, luminance));
if (pixelsColor != null) pixelsColor[0] = hsl(startHue, saturation, luminance);
for (let i = 1; i < steps - 1; i++) {
const h = Math.idiv((h1_100 + i * hStep), 100) + 360;
const s = Math.idiv((s1_100 + i * sStep), 100);
const l = Math.idiv((l1_100 + i * lStep), 100);
this.setPixelColor(i, hsl(h, s, l));
if (pixelsColor != null) pixelsColor[i] = hsl(h, s, l);
}
this.setPixelColor(steps - 1, hsl(endHue, saturation, luminance));
if (pixelsColor != null) pixelsColor[steps - 1] = hsl(endHue, saturation, luminance);
}
this.show();
}
/**
* Set LED to a given color (range 0-255 for r, g, b).
* You need to call ``show`` to make the changes visible.
* @param pixeloffset position of the NeoPixel in the strip
* @param rgb RGB color of the LED
*/
setPixelColor(pixeloffset: number, rgb: number): void {
this.setPixelRGB(pixeloffset >> 0, rgb >> 0);
}
/**
* Send all the changes to the strip.
*/
show() {
// only supported in beta
// ws2812b.setBufferMode(this.pin, this._mode);
motionbit.ws2812bSendBuffer(this.buf, this.pin);
}
/**
* Turn off all LEDs.
* You need to call ``show`` to make the changes visible.
*/
clear(): void {
const stride = this._mode === MotionBitRgbMode.RGBW ? 4 : 3;
this.buf.fill(0, this.start * stride, this._length * stride);
}
/**
* Gets the number of pixels declared on the strip
*/
length() {
return this._length;
}
/**
* Set the brightness of the strip. This flag only applies to future operation.
* @param brightness a measure of LED brightness in 0-255. eg: 255
*/
setBrightness(brightness: number): void {
this.brightness = brightness & 0xff;
}
/**
* Create a range of LEDs.
* @param start offset in the LED strip to start the range
* @param length number of LEDs in the range. eg: 4
*/
range(start: number, length: number): Strip {
start = start >> 0;
length = length >> 0;
let strip = new Strip();
strip.buf = this.buf;
strip.pin = this.pin;
strip.brightness = this.brightness;
strip.start = this.start + Math.clamp(0, this._length - 1, start);
strip._length = Math.clamp(0, this._length - (strip.start - this.start), length);
strip._mode = this._mode;
return strip;
}
/**
* Shift LEDs forward and clear with zeros.
* You need to call ``show`` to make the changes visible.
* @param offset number of pixels to shift forward, eg: 1
*/
shift(offset: number = 1): void {
offset = offset >> 0;
const stride = this._mode === MotionBitRgbMode.RGBW ? 4 : 3;
this.buf.shift(-offset * stride, this.start * stride, this._length * stride)
}
/**
* Rotate LEDs forward.
* You need to call ``show`` to make the changes visible.
* @param offset number of pixels to rotate forward, eg: 1
*/
rotate(offset: number = 1): void {
offset = offset >> 0;
const stride = this._mode === MotionBitRgbMode.RGBW ? 4 : 3;
this.buf.rotate(-offset * stride, this.start * stride, this._length * stride)
}
/**
* Set the pin where the neopixel is connected, defaults to P0.
*/
setPin(pin: DigitalPin): void {
this.pin = pin;
pins.digitalWritePin(this.pin, 0);
// don't yield to avoid races on initialization
}
private setBufferRGB(offset: number, red: number, green: number, blue: number): void {
if (this._mode === MotionBitRgbMode.RGB_RGB) {
this.buf[offset + 0] = red;
this.buf[offset + 1] = green;
} else {
this.buf[offset + 0] = green;
this.buf[offset + 1] = red;
}
this.buf[offset + 2] = blue;
}
private setAllRGB(rgb: number) {
let red = unpackR(rgb);
let green = unpackG(rgb);
let blue = unpackB(rgb);
const br = this.brightness;
if (br < 255) {
red = (red * br) >> 8;
green = (green * br) >> 8;
blue = (blue * br) >> 8;
}
const end = this.start + this._length;
const stride = this._mode === MotionBitRgbMode.RGBW ? 4 : 3;
for (let i = this.start; i < end; ++i) {
this.setBufferRGB(i * stride, red, green, blue)
}
}
private setAllW(white: number) {
if (this._mode !== MotionBitRgbMode.RGBW)
return;
let br = this.brightness;
if (br < 255) {
white = (white * br) >> 8;
}
let buf = this.buf;
let end = this.start + this._length;
for (let i = this.start; i < end; ++i) {
let ledoffset = i * 4;
buf[ledoffset + 3] = white;
}
}
private setPixelRGB(pixeloffset: number, rgb: number): void {
if (pixeloffset < 0
|| pixeloffset >= this._length)
return;
let stride = this._mode === MotionBitRgbMode.RGBW ? 4 : 3;
pixeloffset = (pixeloffset + this.start) * stride;
let red = unpackR(rgb);
let green = unpackG(rgb);
let blue = unpackB(rgb);
let br = this.brightness;
if (br < 255) {
red = (red * br) >> 8;
green = (green * br) >> 8;
blue = (blue * br) >> 8;
}
this.setBufferRGB(pixeloffset, red, green, blue)
}
private setPixelW(pixeloffset: number, white: number): void {
if (this._mode !== MotionBitRgbMode.RGBW)
return;
if (pixeloffset < 0
|| pixeloffset >= this._length)
return;
pixeloffset = (pixeloffset + this.start) * 4;
let br = this.brightness;
if (br < 255) {
white = (white * br) >> 8;
}
let buf = this.buf;
buf[pixeloffset + 3] = white;
}
}
/**
* Create a new NeoPixel driver for `numleds` LEDs.
* @param pin the pin where the neopixel is connected.
* @param numleds number of leds in the strip, eg: 24,30,60,64
*/
export function create(pin: DigitalPin, numleds: number, mode: MotionBitRgbMode): Strip {
let strip = new Strip();
let stride = mode === MotionBitRgbMode.RGBW ? 4 : 3;
strip.buf = pins.createBuffer(numleds * stride);
strip.start = 0;
strip._length = numleds;
strip._mode = mode || MotionBitRgbMode.RGB;
strip.setBrightness(128)
strip.setPin(pin)
return strip;
}
function packRGB(a: number, b: number, c: number): number {
return ((a & 0xFF) << 16) | ((b & 0xFF) << 8) | (c & 0xFF);
}
function unpackR(rgb: number): number {
let r = (rgb >> 16) & 0xFF;
return r;
}
function unpackG(rgb: number): number {
let g = (rgb >> 8) & 0xFF;
return g;
}
function unpackB(rgb: number): number {
let b = (rgb) & 0xFF;
return b;
}
/**
* Converts a hue saturation luminosity value into a RGB color
* @param h hue from 0 to 360
* @param s saturation from 0 to 99
* @param l luminosity from 0 to 99
*/
export function hsl(h: number, s: number, l: number): number {
h = Math.round(h);
s = Math.round(s);
l = Math.round(l);
h = h % 360;
s = Math.clamp(0, 99, s);
l = Math.clamp(0, 99, l);
let c = Math.idiv((((100 - Math.abs(2 * l - 100)) * s) << 8), 10000); //chroma, [0,255]
let h1 = Math.idiv(h, 60);//[0,6]
let h2 = Math.idiv((h - h1 * 60) * 256, 60);//[0,255]
let temp = Math.abs((((h1 % 2) << 8) + h2) - 256);
let x = (c * (256 - (temp))) >> 8;//[0,255], second largest component of this color
let r$: number;
let g$: number;
let b$: number;
if (h1 == 0) {
r$ = c; g$ = x; b$ = 0;
} else if (h1 == 1) {
r$ = x; g$ = c; b$ = 0;
} else if (h1 == 2) {
r$ = 0; g$ = c; b$ = x;
} else if (h1 == 3) {
r$ = 0; g$ = x; b$ = c;
} else if (h1 == 4) {
r$ = x; g$ = 0; b$ = c;
} else if (h1 == 5) {
r$ = c; g$ = 0; b$ = x;
}
let m = Math.idiv((Math.idiv((l * 2 << 8), 100) - c), 2);
let r = r$ + m;
let g = g$ + m;
let b = b$ + m;
return packRGB(r, g, b);
}
export enum HueInterpolationDirection {
Clockwise,
CounterClockwise,
Shortest
}
}