Skip to content

Commit 29702c2

Browse files
committed
optimize speed
1 parent 0d46f42 commit 29702c2

File tree

1 file changed

+28
-54
lines changed

1 file changed

+28
-54
lines changed

snowflake.ts

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ namespace snowflake {
4343
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
4444
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
4545
let _snow: number[][] = []
46-
let _BG: number[] = default_BG
46+
let _BG: number[] = []
47+
_BG = default_BG
4748

4849
control.inBackground(function update() {
4950
while (true) {
@@ -74,13 +75,13 @@ namespace snowflake {
7475
r = t % 16
7576
g = (t >> 4) % 16
7677
b = (t >> 8) % 16
77-
if (x % 2) setpixel(y * 2, x, neopixel.rgb(r, g, b))
78-
else setpixel(15 - y * 2, x, neopixel.rgb(r, g, b))
78+
if (x % 2) setpixel(y * 2, x, [r, g, b])
79+
else setpixel(15 - y * 2, x, [r, g, b])
7980
r = (t >> 12) % 16
8081
g = (t >> 16) % 16
8182
b = (t >> 20) % 16
82-
if (x % 2) setpixel(y * 2 + 1, x, neopixel.rgb(r, g, b))
83-
else setpixel(14 - y * 2, x, neopixel.rgb(r, g, b))
83+
if (x % 2) setpixel(y * 2 + 1, x, [r, g, b])
84+
else setpixel(14 - y * 2, x, [r, g, b])
8485
}
8586
}
8687

@@ -106,48 +107,21 @@ namespace snowflake {
106107
load_bg(_BG)
107108
}
108109

109-
// get red channel
110-
function getR(color: number): number {
111-
return (color >> 16) % 256
112-
}
113-
114-
// get green channel
115-
function getG(color: number): number {
116-
return (color >> 8) % 256
117-
}
118-
119-
// get blue chnnel
120-
function getB(color: number): number {
121-
return color % 256
122-
}
123-
124110
// overlying two color
125-
function overlying(row: number, col: number, color: number, add: boolean): void {
111+
function overlying(row: number, col: number, color: number[], add: boolean): void {
126112
let c = getpixel(row, col)
127-
let r0 = 0
128-
let g0 = 0
129-
let b0 = 0
130-
let r1 = 0
131-
let g1 = 0
132-
let b1 = 0
133-
r0 = getR(c)
134-
g0 = getG(c)
135-
b0 = getB(c)
136-
r1 = getR(color)
137-
g1 = getG(color)
138-
b1 = getB(color)
139-
if (add) setpixel(row, col, neopixel.rgb(r0 + r1, g0 + g1, b0 + b1))
140-
else setpixel(row, col, neopixel.rgb(r0 - r1, g0 - g1, b0 - b1))
113+
if (add) setpixel(row, col, [c[0] + color[0], c[1] + color[1], c[2] + color[2]])
114+
else setpixel(row, col, [c[0] - color[0], c[1] - color[1], c[2] - color[2]])
141115
}
142116

143117
//% block="set pixel row %row|col %col|color %color"
144-
function setpixel(row: number, col: number, color: number): void {
145-
if (col % 2) _np.setPixelColor(col * 16 + 15 - row, color)
146-
else _np.setPixelColor(col * 16 + row, color)
118+
function setpixel(row: number, col: number, color: number[]): void {
119+
if (col % 2) _np.setPixelColor(col * 16 + 15 - row, neopixel.rgb(color[0], color[1], color[2]))
120+
else _np.setPixelColor(col * 16 + row, neopixel.rgb(color[0], color[1], color[2]))
147121
}
148122

149123
//% block="get pixel row %row|col %col"
150-
function getpixel(row: number, col: number): number {
124+
function getpixel(row: number, col: number): number[] {
151125
let r = 0
152126
let g = 0
153127
let b = 0
@@ -166,15 +140,15 @@ namespace snowflake {
166140
r = _np.buf[offset + 1];
167141
}
168142
b = _np.buf[offset + 2];
169-
return neopixel.rgb(r, g, b)
143+
return [r, g, b]
170144
}
171145

172146
/**
173147
* config snwo flake
174148
*
175149
*/
176150
//% block="config Pin %pin|cover %cover|threshold %threshold|snowfall %snowfall|most at a time %MostAtATime|speed %speed"
177-
//% cover.defl=true
151+
//% cover.defl=false
178152
//% pin.defl=DigitalPin.P1
179153
//% threshold.defl=8 threshold.max=100 threshold.min=1
180154
//% snowfall.defl=50 snowfall.max=100 snowfall.min=1
@@ -198,7 +172,7 @@ namespace snowflake {
198172
/**
199173
* start running
200174
*/
201-
//% block="start snow"
175+
//% block="start"
202176
//% weight = 80
203177
export function start(): void {
204178
_update = true
@@ -207,7 +181,7 @@ namespace snowflake {
207181
/**
208182
* pause running
209183
*/
210-
//% block="pause snow"
184+
//% block="pause"
211185
//% weight = 70
212186
export function pause(): void {
213187
_update = false
@@ -216,7 +190,7 @@ namespace snowflake {
216190
/**
217191
* Restart again
218192
*/
219-
//% block="reset snow"
193+
//% block="reset"
220194
//% weight = 60
221195
export function reset() {
222196
_pile = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
@@ -245,12 +219,12 @@ namespace snowflake {
245219
for (let i = 0; i < _snow.length; i++) {
246220
let c = _snow[i]
247221
if (c[0] > -1) {
248-
overlying(c[0], c[1], neopixel.rgb(c[2], c[2], c[2]), false)
222+
overlying(c[0], c[1], [c[2], c[2], c[2]], false)
249223
}
250224
c[0] += 1
251225
c[1] += Math.randomRange(-1, 1)
252226
c[1] = Math.max(0, Math.min(c[1], 15))
253-
overlying(c[0], c[1], neopixel.rgb(c[2], c[2], c[2]), true)
227+
overlying(c[0], c[1], [c[2], c[2], c[2]], true)
254228
}
255229
}
256230

@@ -276,12 +250,12 @@ namespace snowflake {
276250
b = _pile[row - 11][col + 1] >= _threshold ? true : false
277251
}
278252
if (_pile[row - 11][col] >= _threshold) {
279-
overlying(c[0], c[1], neopixel.rgb(c[2], c[2], c[2]), false)
253+
overlying(c[0], c[1], [c[2], c[2], c[2]], false)
280254
if (a && b) {
281255
if (_pile[row - 12][col] < _threshold) {
282256
_pile[row - 12][col] += c[2]
283257
if (_pile[row - 12][col] >= _threshold) {
284-
overlying(c[0], c[1], neopixel.rgb(8, 8, 8), true)
258+
overlying(c[0], c[1], [8, 8, 8], true)
285259
_line()
286260
}
287261
}
@@ -294,7 +268,7 @@ namespace snowflake {
294268
for (let i = 0; i < n; i++) {
295269
let c = _snow[n - 1 - i]
296270
if (c[0] > 14) {
297-
overlying(c[0], c[1], neopixel.rgb(c[2], c[2], c[2]), false)
271+
overlying(c[0], c[1], [c[2], c[2], c[2]], false)
298272
_snow.removeAt(n - 1 - i)
299273
}
300274
}
@@ -309,29 +283,29 @@ namespace snowflake {
309283
return
310284
}
311285
for (let i = 0; i < 16; i++) {
312-
overlying(15, i, neopixel.rgb(15, 0, 0), true)
286+
overlying(15, i, [15, 0, 0], true)
313287
}
314288
_np.show()
315289
basic.pause(300)
316290
for (let i = 0; i < 16; i++) {
317-
overlying(15, i, neopixel.rgb(15, 0, 0), false)
291+
overlying(15, i, [15, 0, 0], false)
318292
}
319293
for (let j = 0; j < 3; j++) {
320294
for (let i = 0; i < 16; i++) {
321295
if (_pile[3 - j][i] >= _threshold)
322-
overlying(15 - j, i, neopixel.rgb(8, 8, 8), false)
296+
overlying(15 - j, i, [8, 8, 8], false)
323297
}
324298
_np.show()
325299
basic.pause(300)
326300
for (let i = 0; i < 16; i++) {
327301
_pile[3 - j][i] = _pile[2 - j][i]
328302
if (_pile[3 - j][i] >= _threshold)
329-
overlying(15 - j, i, neopixel.rgb(8, 8, 8), true)
303+
overlying(15 - j, i, [8, 8, 8], true)
330304
}
331305
}
332306
for (let i = 0; i < 16; i++)
333307
if (_pile[0][i] >= _threshold)
334-
overlying(12, i, neopixel.rgb(8, 8, 8), false)
308+
overlying(12, i, [8, 8, 8], false)
335309
_pile[0] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
336310
}
337311
}

0 commit comments

Comments
 (0)