forked from LOBOT-ROBOT/Handlebit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
HandleRGBLight.ts
213 lines (187 loc) · 6.17 KB
/
HandleRGBLight.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
/**
* HandleRGBLight package
*/
enum HandleRGBColors {
//% 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
}
/**
* Different modes for RGB or RGB+W RGBLight LHRGBLight
*/
enum HandleRGBPixelMode {
//% block="RGB (GRB format)"
RGB = 0,
//% block="RGB+W"
RGBW = 1,
//% block="RGB (RGB format)"
RGB_RGB = 2
}
/**
* RGBLight Functions
*/
namespace HandleRGBLight {
//% shim=sendBufferAsm
//% parts="RGBLight"
function sendBuffer(buf: Buffer, pin: DigitalPin) {
}
/**
* A RGBLight class
*/
export class LHRGBLight {
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: HandleRGBPixelMode;
setBrightness(brightness: number): void {
this.brightness = brightness & 0xff;
this.easeBrightness();
}
setPin(pin: DigitalPin): void {
this.pin = pin;
pins.digitalWritePin(this.pin, 0);
// don't yield to avoid races on initialization
}
setPixelColor(pixeloffset: number, rgb: HandleRGBColors): 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: HandleRGBColors): void {
if (pixeloffset < 0
|| pixeloffset >= this._length)
return;
let tureRgb = 0;
switch (rgb)
{
case HandleRGBColors.Red:
tureRgb = 0xFF0000;
break;
case HandleRGBColors.Orange:
tureRgb = 0xFFA500;
break;
case HandleRGBColors.Yellow:
tureRgb = 0xFFFF00;
break;
case HandleRGBColors.Green:
tureRgb = 0x00FF00;
break;
case HandleRGBColors.Blue:
tureRgb = 0x0000FF;
break;
case HandleRGBColors.Indigo:
tureRgb = 0x4b0082;
break;
case HandleRGBColors.Violet:
tureRgb = 0x8a2be2;
break;
case HandleRGBColors.Purple:
tureRgb = 0xFF00FF;
break;
case HandleRGBColors.White:
tureRgb = 0xFFFFFF;
break;
}
let stride = this._mode === HandleRGBPixelMode.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 === HandleRGBPixelMode.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 === HandleRGBPixelMode.RGBW ? 4 : 3;
this.buf.fill(0, this.start * stride, this._length * stride);
this.show();
}
easeBrightness(): void {
const stride = this._mode === HandleRGBPixelMode.RGBW ? 4 : 3;
const br = this.brightness;
const buf = this.buf;
const end = this.start + this._length;
const mid = this._length / 2;
for (let i = this.start; i < end; ++i) {
const k = i - this.start;
const ledoffset = i * stride;
const br = k > mid ? 255 * (this._length - 1 - k) * (this._length - 1 - k) / (mid * mid) : 255 * k * k / (mid * mid);
const r = (buf[ledoffset + 0] * br) >> 8; buf[ledoffset + 0] = r;
const g = (buf[ledoffset + 1] * br) >> 8; buf[ledoffset + 1] = g;
const b = (buf[ledoffset + 2] * br) >> 8; buf[ledoffset + 2] = b;
if (stride == 4) {
const w = (buf[ledoffset + 3] * br) >> 8; buf[ledoffset + 3] = w;
}
}
}
}
export function create(pin: DigitalPin, numleds: number, mode: HandleRGBPixelMode): LHRGBLight {
let light = new LHRGBLight();
let stride = mode === HandleRGBPixelMode.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;
}
}