-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrgb.ts
182 lines (150 loc) · 5.18 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
/*******************************************************************************
* Functions for RGB LED.
*
* Company: Cytron Technologies Sdn Bhd
* Website: http://www.cytron.io
* Email: [email protected]
*******************************************************************************/
// Preset RGB Colors for the selection gridpicker
enum SumobitRgbColors {
//% 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
}
namespace sumobit {
/**
* Turn off all RGB pixels.
*/
//% group="RGB LED"
//% weight=29
//% blockGap=8
//% blockId="sumobit_rgb_clear_all_pixels"
//% block="clear all RGB pixels"
export function clearAllRgbPixels(): void {
sumobit.i2cWrite(SUMOBIT_REG_ADD_R0, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_G0, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_B0, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_R1, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_G1, 0);
sumobit.i2cWrite(SUMOBIT_REG_ADD_B1, 0);
}
/**
* Set the brightness of the RGB pixels (0-255).
* @param brightness Pixel brightness. eg: 100
*/
//% group="RGB LED"
//% weight=28
//% blockGap=40
//% blockId="sumobit_rgb_set_brightness"
//% block="set RGB pixels brightness to %brightness"
//% brightness.min=0 brightness.max=255
//% blockHidden=true
export function setRgbBrightness(brightness: number): void {
}
/**
* Set all RGB pixels to the specified color.
* @param color RGB color of the pixel.
*/
//% group="RGB LED"
//% weight=27
//% blockGap=8
//% blockId="sumobit_rgb_set_all_pixels_color"
//% block="set all RGB pixels to %color"
//% color.shadow="colorNumberPicker"
export function setAllRgbPixelsColor(color: number): void {
// Extract the RGB values
let red = (color >> 16) & 0xFF; // Shift right by 16 bits and mask with 0xFF
let green = (color >> 8) & 0xFF; // Shift right by 8 bits and mask with 0xFF
let blue = color & 0xFF;
sumobit.i2cWrite(SUMOBIT_REG_ADD_R0, red);
sumobit.i2cWrite(SUMOBIT_REG_ADD_G0, green);
sumobit.i2cWrite(SUMOBIT_REG_ADD_B0, blue);
sumobit.i2cWrite(SUMOBIT_REG_ADD_R1, red);
sumobit.i2cWrite(SUMOBIT_REG_ADD_G1, green);
sumobit.i2cWrite(SUMOBIT_REG_ADD_B1, blue);
}
/**
* Set individual RGB pixel to a certain colour
* @param pixel The pixel number we want to change the color.
* @param color The desired color in RGB format.
*/
//% group="RGB LED"
//% weight=26
//% blockGap=40
//% blockId="sumobit_rgb_set_each_pixel_color"
//% block="set RGB pixel %pixel to %color"
//% color.shadow="colorNumberPicker"
//% pixel.min=0 pixel.max=1
//% pixel.fieldEditor="numberdropdown"
//% pixel.fieldOptions.decompileLiterals=false
//% pixel.fieldOptions.values='0,1'
//% pixel.defl='0'
export function setRgbPixelColor(pixel: number, color: number): void {
// Extract the RGB values
let red = (color >> 16) & 0xFF; // Shift right by 16 bits and mask with 0xFF
let green = (color >> 8) & 0xFF; // Shift right by 8 bits and mask with 0xFF
let blue = color & 0xFF;
switch (pixel) {
case 0:
sumobit.i2cWrite(SUMOBIT_REG_ADD_R0, red);
sumobit.i2cWrite(SUMOBIT_REG_ADD_G0, green);
sumobit.i2cWrite(SUMOBIT_REG_ADD_B0, blue);
break;
case 1:
sumobit.i2cWrite(SUMOBIT_REG_ADD_R1, red);
sumobit.i2cWrite(SUMOBIT_REG_ADD_G1, green);
sumobit.i2cWrite(SUMOBIT_REG_ADD_B1, blue);
break;
}
}
/**
* Get the RGB value for a specified known color.
*/
//% group="RGB LED"
//% weight=25
//% blockGap=8
//% blockId="sumobit_rgb_colors"
//% block="%color"
//% blockHidden=true
export function colors(color: SumobitRgbColors): number {
return <number>color;
}
/**
* Convert the specified red, green, and blue channel values into an RGB color value.
* @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=24
//% blockGap=30
//% blockId="sumobit_rgb_convert_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 = sumobit.limit(red, 0, 255);
green = sumobit.limit(green, 0, 255);
blue = sumobit.limit(blue, 0, 255);
return ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF);
}
}