-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
589 lines (529 loc) · 22.2 KB
/
main.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
enum LEDColorMode {
//% block="RGB (GRB format)"
GRB = 1,
//% block="RGB+W (GRBW format) "
GRBW = 2,
//% block="RGB (RGB format) "
RGB = 3
}
enum FirstLEDPosition {
//% block="top left"
topLeft = 1,
//% block="top right"
topRight = 2,
//% block="bottom left"
bottomLeft = 3,
//% block="bottom right"
bottomRight = 4
}
enum LEDDirection {
//% block="horizontally"
horizontal = 1,
//% block="vertically"
vertically = 2
}
enum LEDRowSetup {
//% block="progressive"
progressive = 1,
//% block="zig-zag"
zigZag = 2
}
//% weight=20 color=#7733ff icon="\uf2a1"
namespace LEDMatrix {
/**
* Support for ws2812b / LED Matrix / NeoPixel(TM) 2D Layouts
*
* Code partially copied and/or adapted from https://github.com/microsoft/pxt-neopixel/
*/
export class LEDMatrix {
private buffer: Buffer;
readonly pin: DigitalPin;
private start: number; // start offset in LED strip
readonly ledColorMode: LEDColorMode;
readonly matrixHeight: number;
readonly matrixWidth: number;
readonly stride: number; // number of bytes needed per LED
private colorMatrix: number[][];
private whiteMatrix: number[][];
//brightness in percent (from 0-100)
private brightness: number;
//brightness (fromm 0-255), adjusted for perceived brightness
private adjustedBrightness: number;
// the brightness curve to adjust for perceived brightness
private trueBrightnessCurve: number[];
// adjustedBrightnessCurve: the whole trueBrightnessCurve multiplied by the brightness (percentage)
private adjustedBrightnessCurve: number[];
//tries to adjust relative brightness of the RGBW leds
//so that the leds reflect real RGB monitor perceived color
private adjustColors: boolean;
// variables that define the physical LED matrix's properties
private firstLEDPosition: FirstLEDPosition;
private ledDirection: LEDDirection;
private ledRowSetup: LEDRowSetup;
private autoUpdate: boolean;
constructor(pin: DigitalPin, matrixWidth: number, matrixHeight: number, mode: LEDColorMode) {
this.pin = pin;
this.matrixWidth = matrixWidth >> 0;
this.matrixHeight = matrixHeight >> 0;
this.ledColorMode = mode;
this.adjustColors = true;
//is properly set in this.setBrightness call below
this.brightness = 0;
this.adjustedBrightness = 0;
this.firstLEDPosition = FirstLEDPosition.topLeft;
this.ledDirection = LEDDirection.horizontal;
this.ledRowSetup = LEDRowSetup.progressive;
this.stride = this.ledColorMode === LEDColorMode.GRBW ? 4 : 3;
this.autoUpdate = true;
this.buffer = pins.createBuffer(matrixWidth * matrixHeight * this.stride);
this.clear();
//see discussion at https://www.avrfreaks.net/comment/429531#comment-429531
this.trueBrightnessCurve = [
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 4, 4, 4,
4, 4, 4, 4, 4, 4, 5, 5,
5, 5, 5, 5, 5, 5, 6, 6,
6, 6, 6, 6, 6, 7, 7, 7,
7, 7, 8, 8, 8, 8, 8, 9,
9, 9, 9, 9, 10, 10, 10, 10,
11, 11, 11, 11, 12, 12, 12, 12,
13, 13, 13, 14, 14, 14, 15, 15,
15, 16, 16, 16, 17, 17, 18, 18,
18, 19, 19, 20, 20, 21, 21, 22,
22, 23, 23, 24, 24, 25, 25, 26,
26, 27, 28, 28, 29, 30, 30, 31,
32, 32, 33, 34, 35, 35, 36, 37,
38, 39, 40, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 51, 52, 53,
54, 55, 56, 58, 59, 60, 62, 63,
64, 66, 67, 69, 70, 72, 73, 75,
77, 78, 80, 82, 84, 86, 88, 90,
91, 94, 96, 98, 100, 102, 104, 107,
109, 111, 114, 116, 119, 122, 124, 127,
130, 133, 136, 139, 142, 145, 148, 151,
155, 158, 161, 165, 169, 172, 176, 180,
184, 188, 192, 196, 201, 205, 210, 214,
219, 224, 229, 234, 239, 244, 250, 255];
this.adjustedBrightnessCurve = []
this.setBrightness(75);
}
/**
* Set information about how the physical LEDs are aligned in the matrix
* @param position: In which corner the first LED sits on the physical matrix
* @param direction: Whether the led direction is primarily veritcal or horizontal
* @param rowDirection: Whether LEDs always go in the same direction per row or zig-zag back and forth
*/
//% blockId="ledmatrix_set_matrix_layout" block="%ledMatrix physical layout:|First LED corner %position| LED direction: %direction| Rows/Columns continue %rowSetup"
//% weight=100 blockGap=8
//% parts="ledmatrix"
setMatrixLayout(position: FirstLEDPosition, direction: LEDDirection, rowSetup: LEDRowSetup) {
this.firstLEDPosition = position;
this.ledDirection = direction;
this.ledRowSetup = rowSetup;
}
/**
* Set a LED color at coordinates x,y to an rgb colorvalue
* @param x the x (horizontal) coordinate (0=leftmost)
* @param y the y (vertical) coordinate (0=top)
* @param rgb_color the rgb color value
*/
//% blockId="ledmatrix_set_led_color" block="%ledMatrix|set LED color at coordinate x:%x y:%y to %rgb_color=ledmatrix_rgb"
//% weight=95 blockGap=8
//% parts="ledmatrix"
setLEDColor(x: number, y: number, rgb_color: number) {
if (x < 0 || x >= this.matrixWidth || y < 0 || y >= this.matrixHeight) {
return;
}
this.colorMatrix[x][y] = rgb_color;
this.setRGBBuffer(x, y, unpackR(rgb_color), unpackG(rgb_color), unpackB(rgb_color));
if (this.autoUpdate) {
this.update();
}
}
/**
* Set a LED color at coordinates x,y to an rgb colorvalue
* @param x the x (horizontal) coordinate (0=leftmost)
* @param y the y (vertical) coordinate (0=top)
* @param rgb_color the rgb color value
*/
//% blockId="ledmatrix_fill_matrix" block="%ledMatrix|fill with color %rgb_color=ledmatrix_rgb"
//% weight=65 blockGap=8
//% parts="ledmatrix"
//% advanced=true
fillMatrix(rgb_color: number) {
for (let x = 0; x < this.matrixWidth; x++) {
for (let y = 0; y < this.matrixHeight; y++) {
this.colorMatrix[x][y] = rgb_color;
this.setRGBBuffer(x, y, unpackR(rgb_color), unpackG(rgb_color), unpackB(rgb_color));
}
}
if (this.autoUpdate) {
this.update();
}
}
/**
* Set the color of a led at coordinates x,y to an rgb colorvalue
* @param x the x (horizontal) coordinate (0=leftmost)
* @param y the y (vertical) coordinate (0=top)
* @param whitelevel the brightness of the white led
*/
//% blockId="ledmatrix_set_led_white" block="%ledMatrix|set LED white at coordinate x:%x y:%y to %whitelevel"
//% weight=92 blockGap=8
//% parts="ledmatrix"
//% whitelevel.max=255 whitelevel.min=0 whitelevel.defl=128
setLEDWhite(x: number, y: number, whitelevel: number) {
if (x < 0 || x >= this.matrixWidth || y < 0 || y >= this.matrixHeight || this.ledColorMode != LEDColorMode.GRBW) {
return;
}
this.colorMatrix[x][y] = whitelevel;
this.setBufferWhite(x, y, whitelevel);
if (this.autoUpdate) {
this.update();
}
}
/**
* Set the overall brightness of the LED matrix.
* @param brightness the brightness level
*/
//% blockId="ledmatrix_set_brightness" block="%ledMatrix|set brightness to %brightness"
//% weight=90 blockGap=8
//% parts="ledmatrix"
//% brightness.max=100 brightness.min=0 brightness.defl=50
setBrightness(brightness: number) {
// inspired by https://www.avrfreaks.net/comment/429531#comment-429531
this.brightness = Math.max(Math.min(100, brightness >> 0), 0);
// Math.pow does not work with floats on the actual micro:bit hence the LUT
//this.adjustedBrightness = (Math.pow(2.0, ((this.brightness + 1) / 32)) - 1);
this.adjustedBrightness = this.trueBrightnessCurve[(this.brightness * 2.55) >> 0];
//update adjustedBrightnessCurve
let brightnessFactor = this.brightness / 100;
for (let i = 0; i < 256; i++) {
this.adjustedBrightnessCurve[i] = (this.trueBrightnessCurve[i] * brightnessFactor) >> 0;
}
this.redraw();
}
/**
* Resets matrix to 'black'/off LEDS.
*/
//% blockId="ledmatrix_clear" block="%ledMatrix|clear"
//% weight=90 blockGap=8
//% parts="ledmatrix"
//% advanced=true
clear() {
this.buffer.fill(0);
if (this.autoUpdate) {
light.sendWS2812Buffer(this.buffer, this.pin);
}
this.colorMatrix = [];
for (let x = 0; x < this.matrixWidth; x++) {
this.colorMatrix[x] = [];
for (let y = 0; y < this.matrixHeight; y++) {
this.colorMatrix[x][y] = 0;
}
}
// initialize the white matrix if we are in GRBW Mode
if (this.ledColorMode == LEDColorMode.GRBW) {
this.whiteMatrix = [];
for (let x2 = 0; x2 < this.matrixWidth; x2++) {
this.whiteMatrix[x2] = [];
for (let y2 = 0; y2 < this.matrixHeight; y2++) {
this.whiteMatrix[x2][y2] = 0;
}
}
}
}
/**
* Updates the output buffer/every LED
*/
redraw() {
for (let x3 = 0; x3 < this.matrixWidth; x3++) {
for (let y3 = 0; y3 < this.matrixHeight; y3++) {
let rgb_color: number = this.colorMatrix[x3][y3];
this.setRGBBuffer(x3, y3, unpackR(rgb_color), unpackG(rgb_color), unpackB(rgb_color));
}
}
if (this.ledColorMode == LEDColorMode.GRBW) {
for (let x4 = 0; x4 < this.matrixWidth; x4++) {
for (let y4 = 0; y4 < this.matrixHeight; y4++) {
this.setBufferWhite(x4, y4, this.whiteMatrix[x4][y4]);
}
}
}
if (this.autoUpdate) {
this.update();
}
}
private setRGBBuffer(x: number, y: number, red: number, green: number, blue: number): void {
let byteOffset = this.getOffset(x, y) * this.stride;
if (this.adjustColors) {
//every color channel gets it's brightness adjustment individually
//should better reflect true RGB colors
red = this.adjustedBrightnessCurve[red];
green = this.adjustedBrightnessCurve[green];
blue = this.adjustedBrightnessCurve[blue];
} else {
// all RGB channels use the same adjusted brightness
// half the brightness setting should create a led that is perceived half as bright
// without this half the brightness will half the power consumption but is perceived
// as only minimally less bright (not half as bright)
red = (red * this.brightness) >> 8;
green = (green * this.brightness) >> 8;
blue = (blue * this.brightness) >> 8;
}
if (this.ledColorMode === LEDColorMode.RGB) {
this.buffer[byteOffset + 0] = red;
this.buffer[byteOffset + 1] = green;
} else {
this.buffer[byteOffset + 0] = green;
this.buffer[byteOffset + 1] = red;
}
this.buffer[byteOffset + 2] = blue;
}
private setBufferWhite(x: number, y: number, white: number) {
if (this.ledColorMode !== LEDColorMode.GRBW) {
return;
}
let byteOffset = this.getOffset(x, y) * this.stride;
if (this.adjustColors) {
//see comments in setBufferRGB
white = this.adjustedBrightnessCurve[white];
} else {
white = (white * this.brightness) >> 8;
}
this.buffer[byteOffset + 3] = white;
}
/**
* calculates the offset of the led at coordinates (x,y) in the led strip
* this depends on the layout of the physical matrix
*/
private getOffset(x: number, y: number): number {
let offset = 0;
let matrixWidth = this.matrixWidth;
let matrixHeight = this.matrixHeight;
// adjust x/y coordinates according to where the 'natural' 1st led lies on the physical matrix
switch (this.firstLEDPosition) {
// ignore topLeft, that is fine already
case FirstLEDPosition.topRight: {
x = -x + matrixWidth - 1;
break;
}
case FirstLEDPosition.bottomRight: {
x = -x + matrixWidth - 1;
y = -y + matrixHeight - 1;
break;
}
case FirstLEDPosition.bottomLeft: {
y = -y + matrixHeight - 1;
break;
}
}
if (this.ledDirection == LEDDirection.horizontal) {
//flip x<->y and matrixWidth<->matrixHeight
let tmp = x;
x = y;
y = tmp;
tmp = this.matrixWidth;
matrixWidth = this.matrixHeight;
matrixHeight = tmp;
}
if (this.ledRowSetup == LEDRowSetup.progressive) {
offset = x * matrixHeight + y;
} else {
//zig-zag
if (x % 2 == 1) {
offset = (x + 1) * matrixHeight - 1 - y;
} else {
offset = x * matrixHeight + y;
}
}
return offset;
}
/*
* Updates the LED matrix to update updated colors
*/
//% blockId="ledmatrix_set_auto_update" block="%ledMatrix| auto update LED Matrix %autoUpdate "
//% weight=95 blockGap=8
//% parts="ledmatrix"
//% advanced=true
//% autoUpdate.shadow="toggleOnOff"
setAutoUpdate(autoUpdate: boolean) {
this.autoUpdate = autoUpdate;
}
/*
* Adjust power to the LEDs to emulate true RGB color
* If this is enabled, the LEDs should show colors as seen on a computer screen.
* This is created by adjusting each RGB channel's brightness individually to human eye perception
* If this is disabled, the RGB Values (0-255) indicate the power consumption of a led (a value of
* 128 is 50% of the power consumption (but is perceived as only slightly dimmer)).
*/
//% blockId="ledmatrix_set_color_adjustment" block="%ledMatrix| emulate true RGB Color %enabled "
//% weight=95 blockGap=8
//% parts="ledmatrix"
//% advanced=true
//% enabled.shadow="toggleOnOff"
setColorAdjustment(enabled: boolean) {
this.adjustColors = enabled;
}
/*
* Updates the LED matrix to update updated colors
*/
//% blockId="ledmatrix_update" block="%ledMatrix| update "
//% weight=85 blockGap=8
//% parts="ledmatrix"
//% advanced=true
update() {
light.sendWS2812Buffer(this.buffer, this.pin);
}
/**
* Get the current led color at coordinates x,y
* @param x the x (horizontal) coordinate (0=leftmost)
* @param y the y (vertical) coordinate (0=top)
* @returns rgb_color the rgb color value
*/
//% blockId="ledmatrix_get_led_color" block="%ledMatrix|get the color of a LED at coordinate x:%x y:%y"
//% weight=85 blockGap=8
//% parts="ledmatrix"
//% advanced=true
getLEDColor(x: number, y: number): number {
if (x < 0 || x >= this.matrixWidth || y < 0 || y >= this.matrixHeight) {
return 0;
}
return this.colorMatrix[x][y];
}
/**
* Get the white led brightness/value at coordinates x,y
* @param x the x (horizontal) coordinate (0=leftmost)
* @param y the y (vertical) coordinate (0=top)
* @returns white the white led / brightness value
*/
//% blockId="ledmatrix_get_white_color" block="%ledMatrix|get the white brightness of a LED at coordinate x:%x y:%y"
//% weight=83 blockGap=8
//% parts="ledmatrix"
//% advanced=true
getWhiteColor(x: number, y: number): number {
if (x < 0 || x >= this.matrixWidth || y < 0 || y >= this.matrixHeight) {
return 0;
}
return this.whiteMatrix[x][y];
}
/*
* Updates the LED matrix to update updated colors
*/
//% blockId="ledmatrix_test_pattern" block="%ledMatrix| show test pattern "
//% weight=99 blockGap=8
//% parts="ledmatrix"
testPatternx() {
this.setLEDColor(0, 0, 0xAAAAAA);
this.setLEDColor(1, 0, 0x900000);
this.setLEDColor(2, 0, 0x900000);
this.update();
for (let j = 0; j < 10; j++) {
for (let y = 0; y < this.matrixHeight; y++) {
let x=0;
if (y==0) {
x=3;
}
for (; x < this.matrixWidth; x++) {
this.setLEDColor(x, y, 0x666666);
this.update();
basic.pause(60)
this.setLEDColor(x, y, 0);
this.update();
}
}
}
}
}
/**
* Create a new LED Matrix.
* @param pin the pin where the led matrix is connected.
* @param matrixWidth the number of leds horizontally
* @param matrixWidth the number of leds vertically
* @param mode the byte packing mode of the led matrix : RedGreenBlue or GreenRedBlue, with or without add. white led
*/
//% blockId="ledmatrix_create" block="ledMatrix at pin %pin with width %matrixWidth and height %matrixHeight using %mode"
//% weight=105 blockGap=8
//% parts="ledmatrix"
//% blockSetVariable=ledmatrix
//% matrixWidth.defl=32
//% matrixHeight.defl=8
//% inlineInputMode=inline
export function create(pin: DigitalPin, matrixWidth: number, matrixHeight: number, mode: LEDColorMode): LEDMatrix {
let matrix = new LEDMatrix(pin, matrixWidth, matrixHeight, mode);
return matrix;
}
/**
* 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=94
//% blockId="ledmatrix_rgb" block="rgb-color red %red|green %green|blue %blue"
//% red.max=255 red.min=0 red.defl=128
//% green.max=255 green.min=0 green.defl=128
//% blue.max=255 blue.min=0 blue.defl=128
export function rgb(red: number, green: number, blue: number): number {
return packRGB(red, green, blue);
}
/**
* Converts hue, saturation, luminance into a RGB color
* @param hue hue value between 0 and 255. eg: 255
* @param saturation saturation value between 0 and 255. eg: 255
* @param luminance luminance value between 0 and 255. eg: 255
*
*/
//% weight=93
//% blockId="ledmatrix_hs" block="hsl-color: hue %hue|saturation %saturation|luminance %luminance"
//% hue.max=360 hue.min=0 hue.defl=128
//% saturation.max=100 saturation.min=0 saturation.defl=100
//% luminance.max=100 luminance.min=0 luminance.defl=50
export function hsl(hue: number, saturation: number, luminance: number): number {
let h = hue;
let s = saturation / 100.0;
let l = luminance / 100.0;
let c = (1 - Math.abs(2 * l - 1)) * s;
let hp = h / 60.0;
let x = c * (1 - Math.abs((hp % 2) - 1));
let rgb1;
if (isNaN(h)) rgb1 = [0, 0, 0];
else if (hp <= 1) rgb1 = [c, x, 0];
else if (hp <= 2) rgb1 = [x, c, 0];
else if (hp <= 3) rgb1 = [0, c, x];
else if (hp <= 4) rgb1 = [0, x, c];
else if (hp <= 5) rgb1 = [x, 0, c];
else if (hp <= 6) rgb1 = [c, 0, x];
let m = l - c * 0.5;
let rgb2 = packRGB(Math.round(255 * (rgb1[0] + m)), Math.round(255 * (rgb1[1] + m)), Math.round(255 * (rgb1[2] + m)));
return rgb2
}
//% weight=94
//% blockId="ledmatrix_number_picker" block="color %color"
//% red.max=255 red.min=0 red.defl=128
//% color.shadow="colorNumberPicker"
export function numberPicker(color: number): number {
return color;
}
function packRGB(red: number, green: number, blue: number): number {
return ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 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;
}
}