-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuxDisplays.ts
292 lines (266 loc) · 9.76 KB
/
uxDisplays.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
/**
* microX support for LED matrices.
*/
//% block=uxDisplays
//% color="#303030" weight=46 icon="\uf0eb"
//% groups='["Initialize", "On-board", "Powerbrick"]'
namespace uxDisplays {
let onboardPixels: RGB_MATRIX = null
let powerbrickPixels: RGB_MATRIX = null
let initializedOnboardPixels = false
let initializedPowerbrickPixels = false
/**
* Abstraction for led matrices
*/
export class RGB_MATRIX {
rows: number
columns: number
digitalPin: DigitalPin
displayBuffer: Buffer
constructor(rows: number, columns: number, pinNumber: ux.PIN_NUMBER) {
this.rows = rows
this.columns = columns
this.digitalPin = ux.pinToDigitalPin(pinNumber)
this.displayBuffer = pins.createBuffer(rows * columns * 3)
}
/**
* Set Powerbrick all pixels' color
* @param r pixel red intensity [0,255]
* @param g pixel green intensity [0,255]
* @param b pixel blue intensity [0,255]
*/
public setAllPixels(r: number, g: number, b: number): void {
r = ux.inRange(r, 0, 255)
g = ux.inRange(g, 0, 255)
b = ux.inRange(b, 0, 255)
for (let y = 0; y < this.rows; y++) {
for (let x = 0; x < this.columns; x++) {
this._setPixel(y, x, r, g, b)
}
}
}
/**
* Set pixel color
* @param y pixel y-coordinate [0,7]
* @param x pixel y-coordinate [0,7]
* @param r pixel red intensity [0,255]
* @param g pixel green intensity [0,255]
* @param b pixel blue intensity [0,255]
*/
public setPixel(y: number, x: number, r: number, g: number, b: number): void {
y = ux.inRange(y, 0, this.rows - 1)
x = ux.inRange(x, 0, this.columns - 1)
r = ux.inRange(r, 0, 255)
g = ux.inRange(g, 0, 255)
b = ux.inRange(b, 0, 255)
this._setPixel(y, x, r, g, b)
}
private _setPixel(y: number, x: number, r: number, g: number, b: number): void {
// 3 channels/sub-pixels (RGB) per pixel
let pixelOffset = 0
pixelOffset = (y * this.columns + x) * 3
this.displayBuffer[pixelOffset + 0] = g
this.displayBuffer[pixelOffset + 1] = r
this.displayBuffer[pixelOffset + 2] = b
}
/**
* Refresh pixels
*/
public refresh() {
ws2812b.sendBuffer(this.displayBuffer, this.digitalPin)
}
}
/**
* Initialize onboard pixels
* @param pinNumber digital pin number
* @param rows number of rows
* @param columns number of columns
*/
//% block="initialize onboard pixels"
//% blockId="uxDisplays_intializeOnboardPixels|pin number %pinNumber|number of rows %rows|number of columns %columns"
//% advanced=true
//% weight=99
export function intializeOnboardPixels(pinNumber: ux.PIN_NUMBER, rows: number, columns: number): void {
if (initializedOnboardPixels)
return
onboardPixels = new RGB_MATRIX(rows, columns, pinNumber)
initializedOnboardPixels = true
// After initializing port set to black to prevent first refresh error
setAllOnboardPixels(0, 0, 0)
refreshOnboardPixels()
setAllOnboardPixels(0, 0, 0)
refreshOnboardPixels()
}
/**
* Initialize onboard pixels for Robotbit
* @param pinNumber digital pin number
*/
//% block="initialize onboard pixels Robotbit"
//% blockId="uxDisplays_intializeOnboardPixelsRobotbit"
//% group="Initialize"
//% weight=49
export function intializeOnboardPixelsRobotbit(): void {
intializeOnboardPixels(ux.PIN_NUMBER.PIN16, 1, 4)
}
/**
* Initialize onboard pixels for Superbit
* @param pinNumber digital pin number
*/
//% block="initialize onboard pixels Superbit"
//% blockId="uxDisplays_intializeOnboardPixelsSuperbit"
//% group="Initialize"
//% weight=48
export function intializeOnboardPixelsSuperbit(): void {
intializeOnboardPixels(ux.PIN_NUMBER.PIN12, 1, 4)
}
/**
* Initialize onboard pixels for Superbit
* @param pinNumber digital pin number
*/
//% block="initialize onboard pixels Ywrobot remote"
//% blockId="uxDisplays_intializeOnboardPixelsYwrobotRemote"
//% group="Initialize"
//% weight=47
export function intializeOnboardPixelsYwrobotRemote(): void {
intializeOnboardPixels(ux.PIN_NUMBER.PIN15, 1, 6)
}
/**
* Set all on-board pixels' color
* @param r pixel red intensity [0,255]
* @param g pixel green intensity [0,255]
* @param b pixel blue intensity [0,255]
*/
//% block="set all on-board pixels to color|red %r|green %g|blue %b"
//% blockId="uxDisplays_setAllOnboardPixels"
//% r.min=0 r.max=255 g.min=0 g.max=255 b.min=0 b.max=255
//% inlineInputMode=inline
//% group="On-board"
//% weight=48
export function setAllOnboardPixels(r: number, g: number, b: number): void {
if (initializedOnboardPixels == false)
return
onboardPixels.setAllPixels(r, g, b)
}
/**
* Set on-board pixel color 1D
* @param y pixel y-coordinate
* @param x pixel x-coordinate
* @param r pixel red intensity [0,255]
* @param g pixel green intensity [0,255]
* @param b pixel blue intensity [0,255]
*/
//% block="set on-board pixel to color (1D)|y %y|x %x|red %r|green %g|blue %b"
//% blockId="uxDisplays_setOnboardPixel1D"
//% x.min=0 r.min=0 r.max=255 g.min=0 g.max=255 b.min=0 b.max=255
//% inlineInputMode=inline
//% group="On-board"
//% weight=47
export function setOnboardPixel1D(x: number, r: number, g: number, b: number): void {
if (initializedOnboardPixels == false)
return
onboardPixels.setPixel(1, x, r, g, b)
}
/**
* Set on-board pixel color 2D
* @param y pixel y-coordinate
* @param x pixel x-coordinate
* @param r pixel red intensity [0,255]
* @param g pixel green intensity [0,255]
* @param b pixel blue intensity [0,255]
*/
//% block="set on-board pixel to color (2D)|y %y|x %x|red %r|green %g|blue %b"
//% blockId="uxDisplays_setOnboardPixel2D"
//% y.min=0 x.min=0 r.min=0 r.max=255 g.min=0 g.max=255 b.min=0 b.max=255
//% inlineInputMode=inline
//% group="On-board"
//% weight=47
export function setOnboardPixel2D(y: number, x: number, r: number, g: number, b: number): void {
if (initializedOnboardPixels == false)
return
onboardPixels.setPixel(y, x, r, g, b)
}
/**
* Refresh on-board pixels
*/
//% block="refresh/update on-board pixels"
//% blockId="uxDisplays_refreshOnboardPixels"
//% port.min=0 port.max=6
//% group="On-board"
//% weight=45
export function refreshOnboardPixels() {
if (initializedOnboardPixels == false)
return
onboardPixels.refresh()
}
/**
* Initialize Kittenbot Powerbrick pixels module
* @param pinNumber digital pin number
*/
//% block="initialize on-board pixels module connected to|digital pin %pinNumber"
//% blockId="uxDisplays_intializePowerbrickPixels"
//% group="Powerbrick"
//% weight=35
export function intializePowerbrickPixels(pinNumber: ux.PIN_NUMBER): void {
if (initializedPowerbrickPixels || pinNumber == null)
return
powerbrickPixels = new RGB_MATRIX(8, 8, pinNumber)
initializedPowerbrickPixels = true
// After initializing port set to black to prevent first refresh error
setPowerbrickAllPixels(0, 0, 0)
refreshPowerbrickPixels()
setPowerbrickAllPixels(0, 0, 0)
refreshPowerbrickPixels()
}
/**
* Set Powerbrick all pixels color
* @param r pixel red intensity [0,255]
* @param g pixel green intensity [0,255]
* @param b pixel blue intensity [0,255]
*/
//% block="set powerbrick pixels to color|red %r|green %g|blue %b"
//% blockId="uxDisplays_setPowerbrickAllPixels"
//% r.min=0 r.max=255 g.min=0 g.max=255 b.min=0 b.max=255
//% inlineInputMode=inline
//% group="Powerbrick"
//% weight=34
export function setPowerbrickAllPixels(r: number, g: number, b: number): void {
if (initializedPowerbrickPixels == false)
return
powerbrickPixels.setAllPixels(r, g, b)
}
/**
* Set Powerbrick pixel color
* @param y pixel y-coordinate [0,7]
* @param x pixel x-coordinate [0,7]
* @param r pixel red intensity [0,255]
* @param g pixel green intensity [0,255]
* @param b pixel blue intensity [0,255]
*/
//% block="set powerbrick pixel to color|y %y|x %x|red %r|green %g|blue %b"
//% blockId="uxDisplays_setPowerbrickPixel"
//% y.min=0 y.max=7 x.min=0 x.max=7 r.min=0 r.max=255 g.min=0 g.max=255 b.min=0 b.max=255
//% inlineInputMode=inline
//% group="Powerbrick"
//% weight=33
export function setPowerbrickPixel(y: number, x: number, r: number, g: number, b: number): void {
if (initializedPowerbrickPixels == false)
return
// The pixels are wired up in a weird order, workaround:
if (x % 2 == 0)
powerbrickPixels.setPixel(x, y, r, g, b)
else
powerbrickPixels.setPixel(x, 7 - y, r, g, b)
}
/**
* Refresh Powerbrick pixels
*/
//% block="refresh/update powerbrick pixels"
//% blockId="uxDisplays_refreshPowerbrickPixels"
//% group="Powerbrick"
//% weight=32
export function refreshPowerbrickPixels() {
if (initializedPowerbrickPixels == false)
return
powerbrickPixels.refresh()
}
}