forked from microsoft/pxt-neopixel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neopixel.ts
568 lines (526 loc) · 19.8 KB
/
neopixel.ts
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/**
* Well known colors for a NeoPixel strip
*/
enum NeoPixelColors {
//% 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 NeoPixelMode {
//% block="RGB (GRB format)"
RGB = 0,
//% block="RGB+W"
RGBW = 1,
//% block="RGB (RGB format)"
RGB_RGB = 2
}
/**
* Functions to operate NeoPixel strips.
*/
//% weight=5 color=#2699BF icon="\uf110"
namespace neopixel {
/**
* 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: NeoPixelMode;
_matrixWidth: number; // number of leds in a matrix - if any
/**
* Shows all LEDs to a given color (range 0-255 for r, g, b).
* @param rgb RGB color of the LED
*/
//% blockId="neopixel_set_strip_color" block="%strip|show color %rgb=neopixel_colors"
//% weight=85 blockGap=8
//% parts="neopixel"
showColor(rgb: number) {
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
*/
//% blockId="neopixel_set_strip_rainbow" block="%strip|show rainbow from %startHue|to %endHue"
//% weight=85 blockGap=8
//% parts="neopixel"
showRainbow(startHue: number = 1, endHue: number = 360) {
if (this._length <= 0) return;
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))
} else {
this.setPixelColor(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));
}
this.setPixelColor(steps - 1, hsl(endHue, saturation, luminance));
}
this.show();
}
/**
* Displays a vertical bar graph based on the `value` and `high` value.
* If `high` is 0, the chart gets adjusted automatically.
* @param value current value to plot
* @param high maximum value, eg: 255
*/
//% weight=84
//% blockId=neopixel_show_bar_graph block="%strip|show bar graph of %value|up to %high"
//% icon="\uf080"
//% parts="neopixel"
showBarGraph(value: number, high: number): void {
if (high <= 0) {
this.clear();
this.setPixelColor(0, NeoPixelColors.Yellow);
this.show();
return;
}
value = Math.abs(value);
const n = this._length;
const n1 = n - 1;
let v = Math.idiv((value * n), high);
if (v == 0) {
this.setPixelColor(0, 0x666600);
for (let i = 1; i < n; ++i)
this.setPixelColor(i, 0);
} else {
for (let i = 0; i < n; ++i) {
if (i <= v) {
const b = Math.idiv(i * 255, n1);
this.setPixelColor(i, neopixel.rgb(b, 0, 255 - b));
}
else this.setPixelColor(i, 0);
}
}
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
*/
//% blockId="neopixel_set_pixel_color" block="%strip|set pixel color at %pixeloffset|to %rgb=neopixel_colors"
//% blockGap=8
//% weight=80
//% parts="neopixel" advanced=true
setPixelColor(pixeloffset: number, rgb: number): void {
this.setPixelRGB(pixeloffset, rgb);
}
/**
* Sets the number of pixels in a matrix shaped strip
* @param width number of pixels in a row
*/
//% blockId=neopixel_set_matrix_width block="%strip|set matrix width %width"
//% blockGap=8
//% weight=5
//% parts="neopixel" advanced=true
setMatrixWidth(width: number) {
this._matrixWidth = Math.min(this._length, width);
}
/**
* Set LED to a given color (range 0-255 for r, g, b) in a matrix shaped strip
* You need to call ``show`` to make the changes visible.
* @param x horizontal position
* @param y horizontal position
* @param rgb RGB color of the LED
*/
//% blockId="neopixel_set_matrix_color" block="%string|set matrix color at x %x|y %y|to %rgb=neopixel_colors"
//% weight=4
//% parts="neopixel" advanced=true
setMatrixColor(x: number, y: number, rgb: number) {
if (this._matrixWidth <= 0) return; // not a matrix, ignore
const cols = Math.idiv(this._length, this._matrixWidth);
if (x < 0 || x >= this._matrixWidth || y < 0 || y >= cols) return;
let i = x + y * this._matrixWidth;
this.setPixelColor(i, rgb);
}
/**
* For NeoPixels with RGB+W LEDs, set the white LED brightness. This only works for RGB+W NeoPixels.
* @param pixeloffset position of the LED in the strip
* @param white brightness of the white LED
*/
//% blockId="neopixel_set_pixel_white" block="%strip|set pixel white LED at %pixeloffset|to %white"
//% blockGap=8
//% weight=80
//% parts="neopixel" advanced=true
setPixelWhiteLED(pixeloffset: number, white: number): void {
if (this._mode === NeoPixelMode.RGBW) {
this.setPixelW(pixeloffset, white);
}
}
/**
* Send all the changes to the strip.
*/
//% blockId="neopixel_show" block="%strip|show" blockGap=8
//% weight=79
//% parts="neopixel"
show() {
ws2812b.sendBuffer(this.buf, this.pin);
}
/**
* Turn off all LEDs.
* You need to call ``show`` to make the changes visible.
*/
//% blockId="neopixel_clear" block="%strip|clear"
//% weight=76
//% parts="neopixel"
clear(): void {
const stride = this._mode === NeoPixelMode.RGBW ? 4 : 3;
this.buf.fill(0, this.start * stride, this._length * stride);
}
/**
* Gets the number of pixels declared on the strip
*/
//% blockId="neopixel_length" block="%strip|length" blockGap=8
//% weight=60 advanced=true
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
*/
//% blockId="neopixel_set_brightness" block="%strip|set brightness %brightness" blockGap=8
//% weight=59
//% parts="neopixel" advanced=true
setBrightness(brightness: number): void {
this.brightness = brightness & 0xff;
}
/**
* Apply brightness to current colors using a quadratic easing function.
**/
//% blockId="neopixel_each_brightness" block="%strip|ease brightness" blockGap=8
//% weight=58
//% parts="neopixel" advanced=true
easeBrightness(): void {
const stride = this._mode === NeoPixelMode.RGBW ? 4 : 3;
const br = this.brightness;
const buf = this.buf;
const end = this.start + this._length;
const mid = Math.idiv(this._length, 2);
for (let i = this.start; i < end; ++i) {
const k = i - this.start;
const ledoffset = i * stride;
const br = k > mid
? Math.idiv(255 * (this._length - 1 - k) * (this._length - 1 - k), (mid * mid))
: Math.idiv(255 * k * k, (mid * mid));
serial.writeLine(k + ":" + br);
const r = (buf[ledoffset + 0] * br) >> 8; buf[ledoffset + 0] = r;
const g = (buf[ledoffset + 1] * br) >> 8; buf[ledoffset + 1] = g;
const b = (buf[ledoffset + 2] * br) >> 8; buf[ledoffset + 2] = b;
if (stride == 4) {
const w = (buf[ledoffset + 3] * br) >> 8; buf[ledoffset + 3] = w;
}
}
}
/**
* 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
*/
//% weight=89
//% blockId="neopixel_range" block="%strip|range from %start|with %length|leds"
//% parts="neopixel"
//% blockSetVariable=range
range(start: number, length: number): Strip {
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._matrixWidth = 0;
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
*/
//% blockId="neopixel_shift" block="%strip|shift pixels by %offset" blockGap=8
//% weight=40
//% parts="neopixel"
shift(offset: number = 1): void {
const stride = this._mode === NeoPixelMode.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
*/
//% blockId="neopixel_rotate" block="%strip|rotate pixels by %offset" blockGap=8
//% weight=39
//% parts="neopixel"
rotate(offset: number = 1): void {
const stride = this._mode === NeoPixelMode.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.
*/
//% weight=10
//% parts="neopixel" advanced=true
setPin(pin: DigitalPin): void {
this.pin = pin;
pins.digitalWritePin(this.pin, 0);
// don't yield to avoid races on initialization
}
/**
* Estimates the electrical current (mA) consumed by the current light configuration.
*/
//% weight=9 blockId=neopixel_power block="%strip|power (mA)"
//% advanced=true
power(): number {
const stride = this._mode === NeoPixelMode.RGBW ? 4 : 3;
const end = this.start + this._length;
let p = 0;
for (let i = this.start; i < end; ++i) {
const ledoffset = i * stride;
for (let j = 0; j < stride; ++j) {
p += this.buf[i + j];
}
}
return Math.idiv(this.length(), 2) /* 0.5mA per neopixel */
+ Math.idiv(p * 433, 10000); /* rought approximation */
}
private setBufferRGB(offset: number, red: number, green: number, blue: number): void {
if (this._mode === NeoPixelMode.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 === NeoPixelMode.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 !== NeoPixelMode.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 === NeoPixelMode.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 !== NeoPixelMode.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
*/
//% blockId="neopixel_create" block="NeoPixelAlex at pin %pin|with %numleds|leds as %mode"
//% weight=90 blockGap=8
//% parts="neopixel"
//% trackArgs=0,2
//% blockSetVariable=strip
export function create(pin: DigitalPin, numleds: number, mode: NeoPixelMode): Strip {
let strip = new Strip();
let stride = mode === NeoPixelMode.RGBW ? 4 : 3;
strip.buf = pins.createBuffer(numleds * stride);
strip.start = 0;
strip._length = numleds;
strip._mode = mode;
strip._matrixWidth = 0;
strip.setBrightness(255)
strip.setPin(pin)
return strip;
}
/**
* Converts red, green, blue channels into a RGB color
* @param red value of the red channel between 0 and 255. eg: 255
* @param green value of the green channel between 0 and 255. eg: 255
* @param blue value of the blue channel between 0 and 255. eg: 255
*/
//% weight=1
//% blockId="neopixel_rgb" block="red %red|green %green|blue %blue"
//% advanced=true
export function rgb(red: number, green: number, blue: number): number {
return packRGB(red, green, blue);
}
/**
* Gets the RGB value of a known color
*/
//% weight=2 blockGap=8
//% blockId="neopixel_colors" block="%color"
//% advanced=true
export function colors(color: NeoPixelColors): number {
return color;
}
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
*/
//% blockId=neopixelHSL block="hue %h|saturation %s|luminosity %l"
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
}
}