-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathQbitRGBLight.ts
235 lines (200 loc) · 6.62 KB
/
QbitRGBLight.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
/**
* QbitRGBLight package
*/
enum QbitRGBColors {
//% 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 Lights {
//% block="Light 1"
Light1 = 0x00,
//% block="Light 2"
Light2 = 0x01
}
/**
* Different modes for RGB or RGB+W RGBLight QbitRGBColors
*/
enum QbitRGBPixelMode {
//% block="RGB (GRB format)"
RGB = 0,
//% block="RGB+W"
RGBW = 1,
//% block="RGB (RGB format)"
RGB_RGB = 2
}
/**
* QbitRGBLight Functions
*/
namespace QbitRGBLight {
//% shim=sendBufferAsm
//% parts="QbitRGBLight"
function sendBuffer(buf: Buffer, pin: DigitalPin) {
}
/**
* A LHQbitRGBLight class
*/
export class LHQbitRGBLight {
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: QbitRGBPixelMode;
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
}
setPixelColor(pixeloffset: number, rgb: QbitRGBColors, flag: boolean): void {
this.setPixelRGB(pixeloffset, rgb, flag);
}
private setPixelRGB(pixeloffset: number, rgb: QbitRGBColors, flag: boolean): void {
if (pixeloffset < 0
|| pixeloffset >= this._length)
return;
let tureRgb = 0;
if ((pixeloffset == 2 || pixeloffset == 3)&&!flag)
{
switch (rgb)
{
case QbitRGBColors.Red:
tureRgb = 0x00FF00;
break;
case QbitRGBColors.Orange:
tureRgb = 0xF6BA41;
break;
case QbitRGBColors.Yellow:
tureRgb = 0xFFFF00;
break;
case QbitRGBColors.Green:
tureRgb = 0xFF0000;
break;
case QbitRGBColors.Blue:
tureRgb = 0x0000FF;
break;
case QbitRGBColors.Indigo:
tureRgb = 0x004b82;
break;
case QbitRGBColors.Violet:
tureRgb = 0x2B8AE2;
break;
case QbitRGBColors.Purple:
tureRgb = 0x00FFFF;
break;
case QbitRGBColors.White:
tureRgb = 0xFFFFFF;
break;
}
}
else
{
switch (rgb)
{
case QbitRGBColors.Red:
tureRgb = 0xFF0000;
break;
case QbitRGBColors.Orange:
tureRgb = 0xFFA500;
break;
case QbitRGBColors.Yellow:
tureRgb = 0xFFFF00;
break;
case QbitRGBColors.Green:
tureRgb = 0x00FF00;
break;
case QbitRGBColors.Blue:
tureRgb = 0x0000FF;
break;
case QbitRGBColors.Indigo:
tureRgb = 0x4b0082;
break;
case QbitRGBColors.Violet:
tureRgb = 0x8a2be2;
break;
case QbitRGBColors.Purple:
tureRgb = 0xFF00FF;
break;
case QbitRGBColors.White:
tureRgb = 0xFFFFFF;
break;
}
}
let stride = this._mode === QbitRGBPixelMode.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 === QbitRGBPixelMode.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 === QbitRGBPixelMode.RGBW ? 4 : 3;
this.buf.fill(0, this.start * stride, this._length * stride);
this.show();
}
}
export function create(pin: DigitalPin, numleds: number, mode: QbitRGBPixelMode): LHQbitRGBLight {
let light = new LHQbitRGBLight();
let stride = mode === QbitRGBPixelMode.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;
}
}