-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrgb.ts
400 lines (333 loc) · 11.4 KB
/
rgb.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
/*******************************************************************************
* Functions for MOTION:BIT - RGB LED.
* These are the wrapper functions for the neopixel functions in neopixel.ts
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
// Default pin.
const RGB_LED_PIN = DigitalPin.P8;
// Number of RGB LED.
const RGB_LED_LENGTH = 2;
namespace motionbit {
// Colors array for each pixel.\
let extColorsArray: number[] = [];
let colorsArray: number[] = [];
for (let i = 0; i < RGB_LED_LENGTH; i++) {
colorsArray.push(0);
}
// Create a Neo Pixel object for RGB LED.
let extRgbLed: motionbit.Strip;
let rgbLed = motionbit.create(RGB_LED_PIN, RGB_LED_LENGTH, MotionBitRgbMode.RGB);
rgbLed.clear();
let rgbBrightness = 25;
// Reduce the default brightness.
rgbLed.setBrightness(25);
/**
* Turn off all RGB pixels.
*/
//% group="RGB LED"
//% weight=16
//% blockGap=8
//% blockId="motionbit_clear_all_rgb_pixels"
//% block="clear all RGB pixels"
export function clearAllRgbPixels(): void {
for (let i = 0; i < RGB_LED_LENGTH; i++) {
colorsArray[i] = 0;
}
rgbLed.clear();
rgbLed.show();
basic.pause(0);
}
/**
* Set the brightness of the RGB pixels (0-255).
* @param brightness Pixel brightness. eg: 25
*/
//% group="RGB LED"
//% weight=15
//% blockGap=40
//% blockId="motionbit_set_rgb_brightness"
//% block="set RGB pixels brightness to %brightness"
//% brightness.min=0 brightness.max=255
export function setRgbBrightness(brightness: number): void {
rgbLed.setBrightness(brightness);
rgbBrightness = brightness;
// Restore the original color.
for (let i = 0; i < RGB_LED_LENGTH; i++) {
rgbLed.setPixelColor(i, colorsArray[i]);
}
rgbLed.show();
basic.pause(0);
}
/**
* Show the same color on all RGB pixels.
* @param color RGB color of the pixel. eg: 0xff0000
*/
//% group="RGB LED"
//% weight=14
//% blockGap=8
//% blockId="motionbit_set_all_rgb_pixels_color"
//% block="set all RGB pixels to %color"
//% color.shadow="colorNumberPicker"
export function setAllRgbPixelsColor(color: number): void {
for (let i = 0; i < RGB_LED_LENGTH; i++) {
colorsArray[i] = color;
}
rgbLed.showColor(color);
basic.pause(0);
}
/**
* Show color on individual RGB pixel.
* @param pixel The pixel number we want to change the color.
* @param color RGB color of the pixel. eg: 0xff0000
*/
//% group="RGB LED"
//% weight=13
//% blockGap=40
//% blockId="motionbit_set_rgb_pixel_color"
//% block="set RGB pixel %pixel to %color"
//% color.shadow="colorNumberPicker"
//% pixel.min=0 pixel.max=1
export function setRgbPixelColor(pixel: number, color: number): void {
colorsArray[pixel] = color;
rgbLed.setPixelColor(pixel, color);
rgbLed.show();
basic.pause(0);
}
/**
* Return the RGB value of a known color.
*/
//% group="RGB LED"
//% weight=12
//% blockGap=8
//% blockId="motionbit_colors"
//% block="%color"
export function colors(color: MotionBitRgbColors): number {
return color;
}
/**
* Converts red, green, blue channels into a RGB color.
* @param red Value of the red channel (0 - 255). eg: 255
* @param green Value of the green channel (0 - 255). eg: 255
* @param blue Value of the blue channel (0 - 255). eg: 255
*/
//% group="RGB LED"
//% weight=11
//% blockGap=30
//% blockId="motionbit_rgb_value"
//% block="red %red green %green blue %blue"
//% red.min=0 red.max=255
//% green.min=0 green.max=255
//% blue.min=0 blue.max=255
export function rgb(red: number, green: number, blue: number): number {
return ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF);
}
/**
* Set the length for external RGB pixels.
* @param length Number of external RGB pixels. eg: 8
*/
//% advanced=true
//% group="External RGB LED"
//% weight=30
//% blockGap=40
//% blockId="motionbit_init_ext_rgb"
//% block="set external RGB pixels length to %length"
export function initExternalRgb(length: number): void {
if (length == 0) return;
// Create the colors array.
for (let i = 0; i < length; i++) {
extColorsArray.push(0);
}
// Create a Neo Pixel object for RGB LED.
extRgbLed = motionbit.create(RGB_LED_PIN, length + RGB_LED_LENGTH, MotionBitRgbMode.RGB);
rgbLed = extRgbLed.range(0, RGB_LED_LENGTH);
extRgbLed = extRgbLed.range(2, length);
extRgbLed.clear();
// Reduce the default brightness.
extRgbLed.setBrightness(25);
// Restore the original color and brightness for onboard RGB.
rgbLed.setBrightness(rgbBrightness);
for (let i = 0; i < RGB_LED_LENGTH; i++) {
rgbLed.setPixelColor(i, colorsArray[i]);
}
rgbLed.show();
basic.pause(0);
}
/**
* Turn off all External RGB pixels.
*/
//% advanced=true
//% group="External RGB LED"
//% weight=29
//% blockGap=8
//% blockId="motionbit_clear_all_ext_rgb_pixels"
//% block="clear all external RGB pixels"
export function clearAllExternalRgbPixels(): void {
if (extRgbLed == null) return;
for (let i = 0; i < extColorsArray.length; i++) {
extColorsArray[i] = 0;
}
extRgbLed.clear();
extRgbLed.show();
basic.pause(0);
}
/**
* Set the brightness of the external RGB pixels (0-255).
* @param brightness Pixel brightness. eg: 25
*/
//% advanced=true
//% group="External RGB LED"
//% weight=28
//% blockGap=40
//% blockId="motionbit_set_ext_rgb_brightness"
//% block="set external RGB pixels brightness to %brightness"
//% brightness.min=0 brightness.max=255
export function setExternalRgbBrightness(brightness: number): void {
if (extRgbLed == null) return;
extRgbLed.setBrightness(brightness);
// Restore the original color.
for (let i = 0; i < extColorsArray.length; i++) {
extRgbLed.setPixelColor(i, colorsArray[i]);
}
extRgbLed.show();
basic.pause(0);
}
/**
* Show the same color on all external RGB pixels.
* @param color RGB color of the pixel. eg: 0xff0000
*/
//% advanced=true
//% group="External RGB LED"
//% weight=27
//% blockGap=8
//% blockId="motionbit_set_all_ext_rgb_pixels_color"
//% block="set all external RGB pixels to %color"
//% color.shadow="colorNumberPicker"
export function setAllExternalRgbPixelsColor(color: number): void {
if (extRgbLed == null) return;
for (let i = 0; i < extColorsArray.length; i++) {
extColorsArray[i] = color;
}
extRgbLed.showColor(color);
basic.pause(0);
}
/**
* Show color on individual external RGB pixels.
* @param pixel The pixel number we want to change the color.
* @param color RGB color of the pixel. eg: 0xff0000
*/
//% advanced=true
//% group="External RGB LED"
//% weight=26
//% blockGap=8
//% blockId="motionbit_set_ext_rgb_pixel_color"
//% block="set external RGB pixel %pixel to %color"
//% color.shadow="colorNumberPicker"
export function setExternalRgbPixelColor(pixel: number, color: number): void {
if (extRgbLed == null) return;
extColorsArray[pixel] = color;
extRgbLed.setPixelColor(pixel, color);
extRgbLed.show();
basic.pause(0);
}
/**
* Show a rainbow pattern on all external RGB pixels.
* @param startHue the start hue value for the rainbow, eg: 1
* @param endHue the end hue value for the rainbow, eg: 360
*/
//% advanced=true
//% group="External RGB LED"
//% weight=25
//% blockGap=40
//% blockId="motionbit_set_all_ext_rgb_pixels_rainbow"
//% block="show rainbow on external RGB pixels with hue from %startHue to %endHue"
//% startHue.min=1 startHue.max=360
//% endHue.min=1 endHue.max=360
export function setAllExternalRgbPixelsRainbow(startHue: number = 1, endHue: number = 360): void {
if (extRgbLed == null) return;
extRgbLed.showRainbow(startHue, endHue, extColorsArray);
basic.pause(0);
}
/**
* Shift the color of external RGB pixels.
* @param offset Number of pixels to shift. eg: 1
*/
//% advanced=true
//% group="External RGB LED"
//% weight=24
//% blockGap=8
//% blockId="motionbit_shift_ext_rgb_pixels"
//% block="shift external RGB pixels color by %offset"
export function shiftExternalRgbPixels(offset: number): void {
if (extRgbLed == null) return;
// Do nothing if offset is 0.
if (offset == 0) return;
// Shift forward.
else if (offset > 0) {
while (offset-- > 0) {
for (let i = extColorsArray.length - 1; i > 0; i--) {
extColorsArray[i] = extColorsArray[i - 1];
}
extColorsArray[0] = 0;
}
}
// Shift backward.
else {
offset = -offset;
while (offset-- > 0) {
for (let i = 0; i < extColorsArray.length - 1; i++) {
extColorsArray[i] = extColorsArray[i + 1];
}
extColorsArray[extColorsArray.length - 1] = 0;
}
}
// Show the new color.
for (let i = 0; i < extColorsArray.length; i++) {
extRgbLed.setPixelColor(i, extColorsArray[i]);
}
extRgbLed.show();
basic.pause(0);
}
/**
* Rotate the color of RGB pixels(-3 to 3).
* @param offset Number of pixels to rotate. eg: 1
*/
//% advanced=true
//% weight=23
//% blockGap=40
//% blockId="motionbit_rotate_ext_rgb_pixels"
//% block="rotate external RGB pixels color by %offset"
export function rotateExternalRgbPixels(offset: number): void {
if (extRgbLed == null) return;
// Do nothing if offset is 0.
if (offset == 0) return;
// Rotate forward.
else if (offset > 0) {
while (offset-- > 0) {
let lastLed = extColorsArray[extColorsArray.length - 1];
for (let i = extColorsArray.length - 1; i > 0; i--) {
extColorsArray[i] = extColorsArray[i - 1];
}
extColorsArray[0] = lastLed;
}
}
// Rotate backward.
else {
offset = -offset;
while (offset-- > 0) {
let lastLed = extColorsArray[0];
for (let i = 0; i < extColorsArray.length - 1; i++) {
extColorsArray[i] = extColorsArray[i + 1];
}
extColorsArray[extColorsArray.length - 1] = lastLed;
}
}
// Show the new color.
for (let i = 0; i < extColorsArray.length; i++) {
extRgbLed.setPixelColor(i, extColorsArray[i]);
}
extRgbLed.show();
basic.pause(0);
}
}