-
Notifications
You must be signed in to change notification settings - Fork 9
/
HelperFunctions.ino
386 lines (267 loc) · 7.97 KB
/
HelperFunctions.ino
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
Helpfull functions to keep the actual animation code short.
Contains so far:
XY()
FillNoise(byte layer)
BasicVariablesSetup
ShowFrame
DimAll(byte value)
CLS
MergeMethod1(byte colorrepeat)
MergeMethod2(byte colorrepeat)
MergeMethod3(byte colorrepeat)
MergeMethod4(byte colorrepeat)
ConstrainedMapping(byte layer, byte lower_limit, byte upper_limit, byte colorrepeat)
ShowAll(uint16_t frames_per_animation)
ColorCorrection
(uncomplete list)
-----------------------------------------------------------------
*/
// Translate the x/y coordinates into the right index in the
// framebuffer.
// The Smartmatrix has a simple line by line layout, no
// serpentines. It safed 2 fps to keep this function short.
// The function is called (sometimes) over 200 000 times per second!
uint16_t XY( uint8_t x, uint8_t y) {
uint16_t i;
i = (y * kMatrixWidth) + x;
return i;
}
// Fill the x/y array with 16-bit noise values
void FillNoise(byte layer) {
for(uint8_t i = 0; i < kMatrixWidth; i++) {
uint32_t ioffset = scale_x[layer] * (i-CentreX);
for(uint8_t j = 0; j < kMatrixHeight; j++) {
uint32_t joffset = scale_y[layer] * (j-CentreY);
byte data = inoise16(x[layer] + ioffset, y[layer] + joffset, z[layer]) >> 8;
uint8_t olddata = noise[layer][i][j];
uint8_t newdata = scale8( olddata, noisesmoothing ) + scale8( data, 256 - noisesmoothing );
data = newdata;
noise[layer][i][j] = data;
}
}
}
// Initialise the coordinates of the noise space with random
// values for an altering starting point.
// Set the zoom factor to a moderate level.
// Fill the delta values with random stepwidths.
void BasicVariablesSetup() {
// set to reasonable values to avoid a black out
colorshift = 0;
noisesmoothing = 200;
currentPalette = RainbowStripeColors_p;
// just any free input pin
random16_add_entropy(analogRead(18));
// fill coordinates with random values
// set zoom levels
for(int i = 0; i < NUM_LAYERS; i++) {
x[i] = random16();
y[i] = random16();
z[i] = random16();
scale_x[i] = 6000;
scale_y[i] = 6000;
}
// for the random movement
dx = random8();
dy = random8();
dz = random8();
dsx = random8();
dsy = random8();
// everything for the menu
mode = 0;
spd = 10;
brightness = 255;
red_level = 255;
green_level = 255;
blue_level = 255;
LEDS.setBrightness(brightness);
}
// Update leds and show fps
void ShowFrame() {
// update leds
LEDS.show();
// count and output the fps
LEDS.countFPS();
// output debugging infos
ShowMenuValues();
}
// Dim everything in leds a bit down.
void DimAll(byte value)
{
for(int i = 0; i < NUM_LEDS; i++) {
leds[i].nscale8(value);
}
}
// Delete the leds array.
void CLS()
{
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = 0;
}
}
// overlay layers 0&1&2 for color, layer 2 is brightness
void MergeMethod1(byte colorrepeat) {
for(uint8_t i = 0; i < kMatrixWidth; i++) {
for(uint8_t j = 0; j < kMatrixHeight; j++) {
uint8_t color = ( ( noise[0][i][j] )
+ ( noise[1][i][j] )
+ ( noise[2][i][j] ) )
/ 3;
// layer 2 gives the brightness
uint8_t bri = (noise[2][i][j]);
// assign a color depending on the actual palette
CRGB pixel = ColorFromPalette( currentPalette, colorrepeat * (color + colorshift), bri );
leds[XY(i,j)] = pixel;
}
}
}
// overlay layers 0&1 for color, layer 2 is brightness
void MergeMethod2(byte colorrepeat) {
for(uint8_t i = 0; i < kMatrixWidth; i++) {
for(uint8_t j = 0; j < kMatrixHeight; j++) {
// map the noise values down to a byte range
// layer 0 and 2 interfere for the color
uint8_t color = ( ( noise[0][i][j] )
+ ( noise[1][i][j] ) )
/ 2;
// layer 2 gives the brightness
uint8_t bri = (noise[2][i][j]);
// assign a color depending on the actual palette
CRGB pixel = ColorFromPalette( currentPalette, colorrepeat * (color + colorshift), bri );
leds[XY(i,j)] = pixel;
}
}
}
// overlay layers 0&1 for color, brightness is layer1
void MergeMethod3(byte colorrepeat) {
for(uint8_t i = 0; i < kMatrixWidth; i++) {
for(uint8_t j = 0; j < kMatrixHeight; j++) {
// map the noise values down to a byte range
// layer 0 and 2 interfere for the color
uint8_t color = ( ( noise[0][i][j] )
+ ( noise[1][i][j] ) )
/ 2;
// layer 1 gives the brightness
uint8_t bri = noise[1][i][j];
// assign a color depending on the actual palette
CRGB pixel = ColorFromPalette( currentPalette, colorrepeat * (color + colorshift), bri );
leds[XY(i,j)] = pixel;
}
}
}
// overlay layers 0&1&2 for color, layer 0 is brightness
void MergeMethod4(byte colorrepeat) {
for(uint8_t i = 0; i < kMatrixWidth; i++) {
for(uint8_t j = 0; j < kMatrixHeight; j++) {
uint8_t color = ( ( noise[0][i][j] )
+ ( noise[1][i][j] )
+ ( noise[2][i][j] ) )
/ 3;
uint8_t bri = (noise[0][i][j]);
// assign a color depending on the actual palette
CRGB pixel = ColorFromPalette( currentPalette, colorrepeat * (color + colorshift), bri );
leds[XY(i,j)] = pixel;
}
}
}
// draw the part between lower and upper limit of one layer
void ConstrainedMapping(byte layer, byte lower_limit, byte upper_limit, byte colorrepeat) {
for(uint8_t i = 0; i < kMatrixWidth; i++) {
for(uint8_t j = 0; j < kMatrixHeight; j++) {
uint8_t data = noise[layer][i][j] ;
if ( data >= lower_limit && data <= upper_limit) {
CRGB pixel = ColorFromPalette( currentPalette, colorrepeat * (data + colorshift), data );
leds[XY(i,j)] = pixel;
}
}
}
}
// one possibility for a basic scripting / frame line composition
void ShowAll(uint16_t count) {
for(uint16_t i = 0; i < count; i++) {
MirroredNoise();
ShowFrame();
}
for(uint16_t i = 0; i < count; i++) {
RedClouds();
ShowFrame();
}
for(uint16_t i = 0; i < count; i++) {
Lavalamp1();
ShowFrame();
}
for(uint16_t i = 0; i < count; i++) {
Lavalamp2();
ShowFrame();
}
for(uint16_t i = 0; i < count; i++) {
Lavalamp3();
ShowFrame();
}
for(uint16_t i = 0; i < count; i++) {
Lavalamp4();
ShowFrame();
}
for(uint16_t i = 0; i < count; i++) {
Lavalamp5();
ShowFrame();
}
for(uint16_t i = 0; i < count; i++) {
Constrained1();
ShowFrame();
}
for(uint16_t i = 0; i < count; i++) {
RelativeMotion1();
ShowFrame();
}
for(uint16_t i = 0; i < count; i++) {
Water();
ShowFrame();
}
for(uint16_t i = 0; i < count; i++) {
Bubbles1();
ShowFrame();
}
for(uint16_t i = 0; i < count; i++) {
TripleMotion();
ShowFrame();
}
for(uint16_t i = 0; i < count; i++) {
CrossNoise2();
ShowFrame();
}
}
// allows to dim colors down
// works on the screenbuffer, after the image is computed
void ColorCorrection() {
for(uint16_t i = 0; i < NUM_LEDS; i++) {
leds[i].r = scale8(leds[i].r, red_level);
leds[i].g = scale8(leds[i].g, green_level);
leds[i].b = scale8(leds[i].b, blue_level);
}
}
// a constrained noise the fills the holes with a mirrored and recolored version of the same noise
void CrossMapping(byte colorrepeat, byte limit) {
for(uint8_t i = 0; i < kMatrixWidth; i++) {
for(uint8_t j = 0; j < kMatrixHeight; j++) {
uint8_t color1 = noise[0][i][j];
uint8_t color2 = noise[0][j][i];
CRGB pixel;
if (color1 > limit) {
pixel = ColorFromPalette( currentPalette, colorrepeat * (color1 + colorshift), color2 );
}
else {
pixel = ColorFromPalette( currentPalette, colorrepeat * (color2 + colorshift + 128), color1 );
}
leds[XY(i,j)] = pixel;
}
}
}
// a brightness mask based on layer 0 for the complete screenbuffer
void FilterAll() {
for(uint8_t i = 0; i < kMatrixWidth; i++) {
for(uint8_t j = 0; j < kMatrixHeight; j++) {
leds[XY(i,j)] %= noise[0][i][j];
}
}
}