forked from evilgeniuslabs/torch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Noise.h
280 lines (235 loc) · 7.7 KB
/
Noise.h
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
/*
* Torch: https://github.com/evilgeniuslabs/torch
* Copyright (C) 2015 Jason Coon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define MAX_DIMENSION ((MATRIX_WIDTH > MATRIX_HEIGHT) ? MATRIX_WIDTH : MATRIX_HEIGHT)
// The 16 bit version of our coordinates
static uint16_t noisex;
static uint16_t noisey;
static uint16_t noisez;
// We're using the x/y dimensions to map to the x/y pixels on the matrix. We'll
// use the z-axis for "time". speed determines how fast time moves forward. Try
// 1 for a very slow moving effect, or 60 for something that ends up looking like
// water.
uint32_t noisespeedx = 1;
uint32_t noisespeedy = 1;
uint32_t noisespeedz = 1;
// Scale determines how far apart the pixels in our noise matrix are. Try
// changing these values around to see how it affects the motion of the display. The
// higher the value of scale, the more "zoomed out" the noise will be. A value
// of 1 will be so zoomed in, you'll mostly see solid colors.
uint16_t noisescale = 30; // scale is set dynamically once we've started up
// This is the array that we keep our computed noise values in
uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
uint8_t colorLoop = 0;
CRGBPalette16 blackAndWhiteStripedPalette;
// This function sets up a palette of black and white stripes,
// using code. Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndWhiteStripedPalette()
{
// 'black out' all 16 palette entries...
fill_solid( blackAndWhiteStripedPalette, 16, CRGB::Black);
// and set every fourth one to white.
blackAndWhiteStripedPalette[0] = CRGB::White;
blackAndWhiteStripedPalette[4] = CRGB::White;
blackAndWhiteStripedPalette[8] = CRGB::White;
blackAndWhiteStripedPalette[12] = CRGB::White;
}
CRGBPalette16 blackAndBlueStripedPalette;
// This function sets up a palette of black and blue stripes,
// using code. Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndBlueStripedPalette()
{
// 'black out' all 16 palette entries...
fill_solid( blackAndBlueStripedPalette, 16, CRGB::Black);
for(uint8_t i = 0; i < 6; i++) {
blackAndBlueStripedPalette[i] = CRGB::Blue;
}
}
// There are several different palettes of colors demonstrated here.
//
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
//
// Additionally, you can manually define your own color palettes, or you can write
// code that creates color palettes on the fly.
boolean initialized = false;
// Fill the x/y array of 8-bit noise values using the inoise8 function.
void fillnoise8() {
if(!initialized) {
initialized = true;
// Initialize our coordinates to some random values
noisex = random16();
noisey = random16();
noisez = random16();
}
// If we're runing at a low "speed", some 8-bit artifacts become visible
// from frame-to-frame. In order to reduce this, we can do some fast data-smoothing.
// The amount of data smoothing we're doing depends on "speed".
uint8_t dataSmoothing = 0;
uint16_t lowestNoise = noisespeedx < noisespeedy ? noisespeedx : noisespeedy;
lowestNoise = lowestNoise < noisespeedz ? lowestNoise : noisespeedz;
if( lowestNoise < 8) {
dataSmoothing = 200 - (lowestNoise * 4);
}
for(int i = 0; i < MAX_DIMENSION; i++) {
int ioffset = noisescale * i;
for(int j = 0; j < MAX_DIMENSION; j++) {
int joffset = noisescale * j;
uint8_t data = inoise8(noisex + ioffset, noisey + joffset, noisez);
// The range of the inoise8 function is roughly 16-238.
// These two operations expand those values out to roughly 0..255
// You can comment them out if you want the raw noise data.
data = qsub8(data,16);
data = qadd8(data,scale8(data,39));
if( dataSmoothing ) {
uint8_t olddata = noise[i][j];
uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
data = newdata;
}
noise[i][j] = data;
}
}
noisex += noisespeedx;
noisey += noisespeedy;
noisez += noisespeedz;
}
void mapNoiseToLEDsUsingPalette(CRGBPalette16 palette, uint8_t hueReduce = 0)
{
static uint8_t ihue=0;
for(int i = 0; i < MATRIX_WIDTH; i++) {
for(int j = 0; j < MATRIX_HEIGHT; j++) {
// We use the value at the (i,j) coordinate in the noise
// array for our brightness, and the flipped value from (j,i)
// for our pixel's index into the color palette.
uint8_t index = noise[j][i];
uint8_t bri = noise[i][j];
// if this palette is a 'loop', add a slowly-changing base value
if( colorLoop) {
index += ihue;
}
// brighten up, as the color palette itself often contains the
// light/dark dynamic range desired
if( bri > 127 ) {
bri = 255;
} else {
bri = dim8_raw( bri * 2);
}
if(hueReduce > 0) {
if(index < hueReduce) index = 0;
else index -= hueReduce;
}
CRGB color = ColorFromPalette( palette, index, bri);
uint16_t n = XY(i, j);
leds[n] = color;
}
}
ihue+=1;
}
uint16_t drawNoise(CRGBPalette16 palette,uint8_t hueReduce = 0) {
// generate noise data
fillnoise8();
// convert the noise data to colors in the LED array
// using the current palette
mapNoiseToLEDsUsingPalette(palette, hueReduce);
return 10;
}
uint16_t rainbowNoise() {
noisespeedx = 9;
noisespeedy = 0;
noisespeedz = 0;
noisescale = 30;
colorLoop = 0;
return drawNoise(RainbowColors_p);
}
uint16_t rainbowStripeNoise() {
noisespeedx = 9;
noisespeedy = 0;
noisespeedz = 0;
noisescale = 20;
colorLoop = 0;
return drawNoise(RainbowStripeColors_p);
}
uint16_t partyNoise() {
noisespeedx = 9;
noisespeedy = 0;
noisespeedz = 0;
noisescale = 30;
colorLoop = 0;
return drawNoise(PartyColors_p);
}
uint16_t forestNoise() {
noisespeedx = 9;
noisespeedy = 0;
noisespeedz = 0;
noisescale = 120;
colorLoop = 0;
return drawNoise(ForestColors_p);
}
uint16_t cloudNoise() {
noisespeedx = 9;
noisespeedy = 0;
noisespeedz = 0;
noisescale = 30;
colorLoop = 0;
return drawNoise(CloudColors_p);
}
uint16_t fireNoise() {
noisespeedx = 8; // 24;
noisespeedy = 0;
noisespeedz = 8;
noisescale = 50;
colorLoop = 0;
return drawNoise(HeatColors_p, 60);
}
uint16_t lavaNoise() {
noisespeedx = 32;
noisespeedy = 0;
noisespeedz = 16;
noisescale = 50;
colorLoop = 0;
return drawNoise(LavaColors_p);
}
uint16_t oceanNoise() {
noisespeedx = 9;
noisespeedy = 0;
noisespeedz = 0;
noisescale = 90;
colorLoop = 0;
return drawNoise(OceanColors_p);
}
uint16_t blackAndWhiteNoise() {
SetupBlackAndWhiteStripedPalette();
noisespeedx = 9;
noisespeedy = 0;
noisespeedz = 0;
noisescale = 30;
colorLoop = 0;
return drawNoise(blackAndWhiteStripedPalette);
}
uint16_t blackAndBlueNoise() {
SetupBlackAndBlueStripedPalette();
noisespeedx = 9;
noisespeedy = 0;
noisespeedz = 0;
noisescale = 30;
colorLoop = 0;
return drawNoise(blackAndBlueStripedPalette);
}