-
Notifications
You must be signed in to change notification settings - Fork 0
/
Frightful_Weather.ino
374 lines (323 loc) · 8.02 KB
/
Frightful_Weather.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
371
372
373
#include "SPI.h"
#include "WS2801.h"
#include <MemoryFree.h>
/**
* Patterns for Vermillion's "Ocillate" show. Seattle, 2012.
*/
/**
* Weather Configs ...
* 0 = Wait delay.
* 1 = Min sparseness of active pixels.
* 2 = Speed range of active pixels.
* 3 = Flurie chance (laternal moves).
*/
const unsigned int seasons = 3;
static unsigned int configs[][4] = {
// Snow.
{100, 25, 4, 2},
// Rain.
{25, 2, 1, 0},
// Leaves.
{70, 26, 5, 5}
};
uint32_t colors[seasons][5] = {
{
// Snow.
Color(40, 45, 60),
Color(40, 45, 60),
Color(40, 45, 60),
Color(40, 45, 60),
Color(40, 45, 60)
}, {
// Rain.
Color(79, 104, 239),
Color(21, 124, 197),
Color(39, 154, 207),
Color(79, 104, 239),
Color(21, 124, 197)
}, {
// Leaves.
Color(224, 95, 0),
Color(220, 167, 9),
Color(211, 46, 9),
Color(228, 64, 1),
Color(200, 44, 10)
}
};
/*
// The old way.
static unsigned int wait = 50;
static unsigned int chance_of_snow_min = 26;
static unsigned int speed_range = 5;
unsigned int fluries = 4; // Intended for sensor.
*/
// CONSTANTS...
boolean debug_mode = false;
// System configs.
int pins[] = {
2, // data #1
3, // clock #1
4, // data #2
5 // clock #2
};
const unsigned int num_cols = 4;
const unsigned int num_rows = 20;
const unsigned int num_pins_per_strip = 40;
// Light data holders.
WS2801 strip1 = WS2801(num_pins_per_strip, pins[0], pins[1]);
WS2801 strip2 = WS2801(num_pins_per_strip, pins[2], pins[3]);
// Snow holder (4X20).
int snow_matrix[num_cols][num_rows] = {0};
//memset(snow_matrix, false, sizeof(snow_matrix));
unsigned int chance_of_snow = 2; // Starting value.
unsigned int speed_throttle = 1;
unsigned int cycles = 0;
unsigned int master_season = 0;
static unsigned long lWaitMillis;
static unsigned int master_wait = (20 * 1000);
uint32_t bg_color = Color(0, 0, 0);
// REQUIRED...
void setup() {
strip1.begin();
strip2.begin();
randomSeed(analogRead(0));
//tester_flakes();
// Debug.
if(debug_mode) {
Serial.begin(115200);
Serial.println("Ready to send data.");
}
}
void loop() {
// Use season configs.
unsigned int wait = configs[master_season][0];
// Rotate through seasons.
if( (long)( millis() - lWaitMillis ) >= 0) {
// Rollover has occured...
master_season++;
lWaitMillis += master_wait; // Check again one second later.
// Draw even during a chance moment.
make_it_snow();
}
else {
// Still before the next rollover time.
make_it_snow();
}
// Keep within available seasons.
if (master_season >= seasons) {
master_season = 0;
}
if(debug_mode) {
//Serial.print("freeMemory()=");
//Serial.println(freeMemory());
cycles++;
String msg = "<< << << #";
msg += cycles;
//report(0, 0, 0, 0, msg);
}
report(5,5,5,5, "season: " + (String) master_season + " chance: " + (String) chance_of_snow);
delay(wait);
}
// WORK HORSES...
// Creation debug.
void tester_flakes() {
snow_matrix[1][15] = 1;
snow_matrix[0][8] = 1;
snow_matrix[1][17] = 1;
snow_matrix[0][3] = 1;
snow_matrix[2][5] = 1;
snow_matrix[3][19] = 1;
snow_matrix[2][10] = 1;
snow_matrix[3][4] = 1;
}
// Master function.
void make_it_snow() {
// Use season configs.
unsigned int speed_range = configs[master_season][2];
make_flakes();
// Loop through snow.
int r, c;
for (c=0;c<num_cols;c++) {
for (r=0;r<num_rows;r++) {
if (snow_matrix[c][r] > 0) {
//report(c, r, snow_matrix[c][r], speed_throttle, "bothering");
draw_flake(c, r, true);
move_flake(c, r);
}
else {
// @todo Could be more efficient.
// Don't need to wipe if was already dark.
draw_flake(c, r, false);
}
}
}
speed_throttle++;
if (speed_throttle > speed_range) {
speed_throttle = 1;
}
strip1.show();
strip2.show();
}
// Generate new snow.
void make_flakes() {
// Use season configs.
unsigned int chance_of_snow_min = configs[master_season][1];
unsigned int speed_range = configs[master_season][2];
// Change the weather?
switch (random(0,3)) {
case 1:
chance_of_snow += 1;
break;
case 2:
chance_of_snow -= 1;
break;
case 3:
default:
break;
}
// Protect boundaries.
if (chance_of_snow <= 1) {
chance_of_snow = 2;
}
else if (chance_of_snow > chance_of_snow_min) {
chance_of_snow = chance_of_snow_min;
}
/*
// Seed the chance.
chance_of_snow = random(1, chance_of_snow_min);
// Enhance distribution.
// (20 = 100, 12 = 4, 10 = 0, 5 = 25, 1 = 81)
int mid_diff = abs(chance_of_snow - (chance_of_snow_min/2));
chance_of_snow = pow(mid_diff, 2);
*/
// Add a new flake?
if( random(0, chance_of_snow) == 1) {
// Pick a speed
int s = random(1, speed_range);
// Pick a column
int c = random(0, num_cols);
// Prevent neighbors.
if (snow_matrix[c][num_rows-2] == 0) {
snow_matrix[c][num_rows-1] = s;
}
}
}
// Shift known flakes to new position.
void move_flake(int c, int r) {
// Use season configs.
unsigned int fluries = configs[master_season][3];
int s = snow_matrix[c][r];
//report(c, r, s, speed_throttle, "regardless");
if (s >= speed_throttle) {
// Out with the old.
snow_matrix[c][r] = 0;
// Can't move it on the ground.
if (r > 0) {
// Lateral movement.
if( random(0, fluries) == 1 ) {
// Pick direction.
int dir = random(0, 2);
switch (dir) {
case 1:
// Move left, and wrap around.
//report(c, r, s, speed_throttle, "left");
if (c > 0) {
snow_matrix[c-1][r-1] = s;
}
else {
snow_matrix[num_cols-1][r-1] = s;
}
break;
case 2:
// Move right, with care.
//report(c, r, s, speed_throttle, "right");
if (c < num_cols-1) {
snow_matrix[c+1][r-1] = s;
}
else {
snow_matrix[0][r-1] = s;
}
break;
default:
// Just in case.
//report(c, r, s, speed_throttle, "just in case");
if (r > 0) {
snow_matrix[c][r-1] = s;
}
}
}
// Move downward.
else {
//report(c, r, s, speed_throttle, "down");
snow_matrix[c][r-1] = s;
}
} // End row careful.
} // End speed.
}
// Translate flake positions into data output.
void draw_flake(int c, int r, boolean snow) {
// Calculate real pixel.
int strip_col = c % (num_pins_per_strip/num_rows);
int pixel = (strip_col * num_rows) + r;
/*
Serial.print("\t sc: ");
Serial.print(strip_col);
Serial.print("\t c,r: ");
Serial.print(c);
Serial.print(",");
Serial.print(r);
Serial.println();
*/
// Dealing with reversing second strip, should be linked to system vars.
if (strip_col == 1) {
pixel = map(pixel, 20, 39, 39, 20);
}
// Draw or delete.
if (snow) {
if (c < 2) {
//strip1.setPixelColor(pixel, colors[snow_matrix[c][r]]);
strip1.setPixelColor(pixel, colors[master_season][snow_matrix[c][r]]);
}
else {
//strip2.setPixelColor(pixel, colors[snow_matrix[c][r]]);
strip2.setPixelColor(pixel, colors[master_season][snow_matrix[c][r]]);
}
}
else {
if (c < 2) {
strip1.setPixelColor(pixel, bg_color);
}
else {
strip2.setPixelColor(pixel, bg_color);
}
}
}
/* Helper functions */
// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}
void report (int c, int r, int s, int st, String msg) {
if(debug_mode) {
Serial.print("c: ");
Serial.print(c);
Serial.print("\t r: ");
Serial.print(r);
Serial.print("\t s: ");
Serial.print(s);
Serial.print("\t st: ");
Serial.print(st);
Serial.print("\t c: ");
Serial.print(cycles);
Serial.print("\t msg: ");
Serial.print(msg);
Serial.println();
}
}