-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrgb_bit.ts
327 lines (281 loc) · 8.71 KB
/
rgb_bit.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
/*******************************************************************************
* Functions for edu:bit - RGB Bit.
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
// Default pin.
const RGB_BIT_PIN = DigitalPin.P13;
// Number of LEDs in RGB Bit.
const RGB_BIT_LENGTH = 4;
// RGB Colors
enum RgbColors {
//% 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
}
/**
* Blocks for RGB Bit.
*/
//% weight=12 color=#ff8000 icon="\uf110" block="RGB Bit"
namespace edubitRgbBit {
// Colors array for each pixel.
let colorsArray: number[] = [];
for (let i = 0; i < RGB_BIT_LENGTH; i++) {
colorsArray.push(0);
}
// Create a Neo Pixel object for RGB Bit.
let rgbBit = neopixel.create(RGB_BIT_PIN, 4, NeoPixelMode.RGB);
rgbBit.clear();
// Reduce the default brightness.
rgbBit.setBrightness(25);
/**
* Turn off all RGB pixels.
*/
//% weight=20
//% blockGap=8
//% blockId="edubit_clear_pixels"
//% block="clear all RGB pixels"
export function clear(): void {
for (let i = 0; i < RGB_BIT_LENGTH; i++) {
colorsArray[i] = 0;
}
rgbBit.clear();
rgbBit.show();
basic.pause(0);
}
/**
* Set the brightness of the RGB pixels (0-255).
* @param brightness Pixel brightness. eg: 25
*/
//% weight=19
//% blockGap=40
//% blockId="edubit_set_brightness"
//% block="set RGB pixels brightness to %brightness"
//% brightness.min=0 brightness.max=255
export function setBrightness(brightness: number): void {
rgbBit.setBrightness(brightness);
// Restore the original color.
for (let i = 0; i < RGB_BIT_LENGTH; i++) {
rgbBit.setPixelColor(i, colorsArray[i]);
}
rgbBit.show();
basic.pause(0);
}
/**
* Show a rainbow pattern on all RGB pixels.
*/
//% weight=18
//% blockGap=8
//% blockId="edubit_show_rainbow"
//% block="show rainbow on RGB pixels"
export function showRainbow(): void {
colorsArray[0] = RgbColors.Red;
colorsArray[1] = RgbColors.Yellow;
colorsArray[2] = RgbColors.Green;
colorsArray[3] = RgbColors.Indigo;
for (let i = 0; i < RGB_BIT_LENGTH; i++) {
rgbBit.setPixelColor(i, colorsArray[i]);
}
rgbBit.show();
basic.pause(0);
}
/**
* Show the same color on all RGB pixels.
* @param color RGB color of the pixel.
*/
//% weight=17
//% blockGap=8
//% blockId="edubit_show_color"
//% block="set all RGB pixels to %color"
//% color.shadow="colorNumberPicker"
export function showColor(color: number): void {
for (let i = 0; i < RGB_BIT_LENGTH; i++) {
colorsArray[i] = color;
}
rgbBit.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.
*/
//% weight=16
//% blockGap=40
//% blockId="edubit_set_pixel_color"
//% block="set RGB pixel %pixel to %color"
//% color.shadow="colorNumberPicker"
//% pixel.min=0 pixel.max=3
export function setPixelColor(pixel: number, color: number): void {
colorsArray[pixel] = color;
rgbBit.setPixelColor(pixel, color);
rgbBit.show();
basic.pause(0);
}
/**
* Shift the color of RGB pixels (-3 to 3).
* @param offset Number of pixels to shift. eg: 1
*/
//% weight=15
//% blockGap=8
//% blockId="edubit_shift_pixels"
//% block="shift RGB pixels color by %offset"
//% offset.min=-3 offset.max=3
export function shiftPixels(offset: number): void {
// Do nothing if offset is 0.
if (offset == 0) {
return;
}
// Shift forward.
else if (offset > 0) {
while (offset-- > 0) {
for (let i = RGB_BIT_LENGTH - 1; i > 0; i--) {
colorsArray[i] = colorsArray[i - 1];
}
colorsArray[0] = 0;
}
}
// Shift backward.
else {
offset = -offset;
while (offset-- > 0) {
for (let i = 0; i < RGB_BIT_LENGTH - 1; i++) {
colorsArray[i] = colorsArray[i + 1];
}
colorsArray[RGB_BIT_LENGTH - 1] = 0;
}
}
// Show the new color.
for (let i = 0; i < RGB_BIT_LENGTH; i++) {
rgbBit.setPixelColor(i, colorsArray[i]);
}
rgbBit.show();
basic.pause(0);
}
/**
* Rotate the color of RGB pixels(-3 to 3).
* @param offset Number of pixels to rotate. eg: 1
*/
//% weight=14
//% blockGap=50
//% blockId="edubit_rotate_pixels"
//% block="rotate RGB pixels color by %offset"
//% offset.min=-3 offset.max=3
export function rotatePixels(offset: number): void {
// Do nothing if offset is 0.
if (offset == 0) {
return;
}
// Rotate forward.
else if (offset > 0) {
while (offset-- > 0) {
let lastLed = colorsArray[RGB_BIT_LENGTH - 1];
for (let i = RGB_BIT_LENGTH - 1; i > 0; i--) {
colorsArray[i] = colorsArray[i - 1];
}
colorsArray[0] = lastLed;
}
}
// Rotate backward.
else {
offset = -offset;
while (offset-- > 0) {
let lastLed = colorsArray[0];
for (let i = 0; i < RGB_BIT_LENGTH - 1; i++) {
colorsArray[i] = colorsArray[i + 1];
}
colorsArray[RGB_BIT_LENGTH - 1] = lastLed;
}
}
// Show the new color.
for (let i = 0; i < RGB_BIT_LENGTH; i++) {
rgbBit.setPixelColor(i, colorsArray[i]);
}
rgbBit.show();
basic.pause(0);
}
/**
* Return the RGB value of a known color.
*/
//% weight=13
//% blockGap=8
//% blockId="edubit_colors"
//% block="%color"
export function colors(color: RgbColors): number {
return <number>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
*/
//% weight=12
//% blockGap=30
//% blockId="edubit_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 {
// Limit the value.
red = edubit.limit(red, 0, 255);
green = edubit.limit(green, 0, 255);
blue = edubit.limit(blue, 0, 255);
return ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF);
}
/**
* Helper to converts hexadecimal to integer.
*/
function convert_hex_to_int(hex_color: string): number {
return parseInt(hex_color, 16)
}
/**
* Converts hexadecimal string to color format.
* @param color Hex value (0x000000 - 0xFFFFFF). eg: "FFFFFF"
*/
//% weight=11
//% blockGap=8
//% blockId="edubit_colors_hex"
//% block="#|%color"
//% advanced=true
export function colors_hex(color: string): number {
if (color.length == 6) {
return edubit.limit(convert_hex_to_int(color), 0, 16777215)
}
return 0x000000
}
/**
* Converts red, green, blue channels in hexadecimal into RGB color.
* @param red Hex value of the red channel (0x00 - 0xFF). eg: "FF"
* @param green Hex value of the green channel (0x00 - 0xFF). eg: "FF"
* @param blue Hex value of the blue channel (0x00 - 0xFF). eg: "FF"
*/
//% weight=10
//% blockGap=50
//% blockId="edubit_rgb_value_hex"
//% block="red %red green %green blue %blue"
//% advanced=true
export function rgb_hex(red: string, green: string, blue: string): number {
return edubitRgbBit.rgb(convert_hex_to_int(red), convert_hex_to_int(green), convert_hex_to_int(blue))
}
}