forked from LOBOT-ROBOT/Startbit
-
Notifications
You must be signed in to change notification settings - Fork 16
/
StartbitRGBLight.ts
262 lines (233 loc) · 7.1 KB
/
StartbitRGBLight.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
/**
* StartbitRGBLight package
*/
enum StartbitRGBColors {
//% block=red
Red = 1,
//% block=orange
Orange = 2,
//% block=yellow
Yellow = 3,
//% block=green
Green = 4,
//% block=blue
Blue = 5,
//% block=indigo
Indigo = 6,
//% block=violet
Violet = 7,
//% block=purple
Purple = 8,
//% block=white
White = 9
}
enum StartbitLights {
//% block="Light 1"
Light1 = 0x00,
//% block="Light 2"
Light2 = 0x01,
//% block="Light 3"
Light3 = 0x02,
//% block="Light 4"
Light4 = 0x03,
//% block="Light 5"
Light5 = 0x04,
//% block="Light 6"
Light6 = 0x05,
//% block="All"
All = 0x06
}
enum StartbitLightsBelt {
//% block="Light 1"
Light1 = 0x00,
//% block="Light 2"
Light2 = 0x01,
//% block="Light 3"
Light3 = 0x02,
//% block="Light 4"
Light4 = 0x03,
//% block="Light 5"
Light5 = 0x04,
//% block="Light 6"
Light6 = 0x05,
//% block="Light 7"
Light7 = 0x06,
//% block="Light 8"
Light8 = 0x07,
//% block="Light 9"
Light9 = 0x08,
//% block="Light 10"
Light10 = 0x09,
//% block="Light 11"
Light11 = 0x0A,
//% block="Light 12"
Light12 = 0x0B,
//% block="Light 13"
Light13 = 0x0C,
//% block="Light 14"
Light14 = 0x0D,
//% block="Light 15"
Light15 = 0x0E,
//% block="All"
All = 0x0F
}
/**
* Different modes for RGB or RGB+W RGBLight QbitRGBColors
*/
enum StartbitRGBPixelMode {
//% block="RGB (GRB format)"
RGB = 0,
//% block="RGB+W"
RGBW = 1,
//% block="RGB (RGB format)"
RGB_RGB = 2
}
/**
* QbitRGBLight Functions
*/
namespace StartbitRGBLight {
//% shim=sendBufferAsm
//% parts="QbitRGBLight"
function sendBuffer(buf: Buffer, pin: DigitalPin) {
}
/**
* A LHQbitRGBLight class
*/
export class LHstartbitRGBLight {
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: StartbitRGBPixelMode;
setBrightness(brightness: number): void {
this.brightness = brightness & 0xff;
}
setPin(pin: DigitalPin): void {
this.pin = pin;
pins.digitalWritePin(this.pin, 0);
// don't yield to avoid races on initialization
}
setBeltPixelColor(pixeloffset: number, rgb: StartbitRGBColors): void {
if (pixeloffset == 15)//全部
{
for (let i = 0; i < this._length; i++)
{
this.setPixelRGB(i, rgb);
}
}
else
{
this.setPixelRGB(pixeloffset * 3, rgb);
this.setPixelRGB(pixeloffset * 3 + 1, rgb);
this.setPixelRGB(pixeloffset*3 + 2, rgb);
}
}
setPixelColor(pixeloffset: number, rgb: StartbitRGBColors): void {
if (pixeloffset == this._length)//全部
{
for (let i = 0; i < this._length; i++)
{
this.setPixelRGB(i, rgb);
}
}
else
{
this.setPixelRGB(pixeloffset, rgb);
}
}
private setPixelRGB(pixeloffset: number, rgb: StartbitRGBColors): void {
if (pixeloffset < 0
|| pixeloffset >= this._length)
return;
let tureRgb = 0;
switch (rgb)
{
case StartbitRGBColors.Red:
tureRgb = 0xFF0000;
break;
case StartbitRGBColors.Orange:
tureRgb = 0xFFA500;
break;
case StartbitRGBColors.Yellow:
tureRgb = 0xFFFF00;
break;
case StartbitRGBColors.Green:
tureRgb = 0x00FF00;
break;
case StartbitRGBColors.Blue:
tureRgb = 0x0000FF;
break;
case StartbitRGBColors.Indigo:
tureRgb = 0x4b0082;
break;
case StartbitRGBColors.Violet:
tureRgb = 0x8a2be2;
break;
case StartbitRGBColors.Purple:
tureRgb = 0xFF00FF;
break;
case StartbitRGBColors.White:
tureRgb = 0xFFFFFF;
break;
}
let stride = this._mode === StartbitRGBPixelMode.RGBW ? 4 : 3;
pixeloffset = (pixeloffset + this.start) * stride;
let red = unpackR(tureRgb);
let green = unpackG(tureRgb);
let blue = unpackB(tureRgb);
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 setBufferRGB(offset: number, red: number, green: number, blue: number): void {
if (this._mode === StartbitRGBPixelMode.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;
}
show() {
sendBuffer(this.buf, this.pin);
}
clear(): void {
const stride = this._mode === StartbitRGBPixelMode.RGBW ? 4 : 3;
this.buf.fill(0, this.start * stride, this._length * stride);
this.show();
}
}
export function create(pin: DigitalPin, numleds: number, mode: StartbitRGBPixelMode): LHstartbitRGBLight {
let light = new LHstartbitRGBLight();
let stride = mode === StartbitRGBPixelMode.RGBW ? 4 : 3;
light.buf = pins.createBuffer(numleds * stride);
light.start = 0;
light._length = numleds;
light._mode = mode;
light.setBrightness(255);
light.setPin(pin);
return light;
}
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;
}
}