-
Notifications
You must be signed in to change notification settings - Fork 1
/
Life.h
130 lines (109 loc) · 3.35 KB
/
Life.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
/*
Fibonacci v3D: https://github.com/evilgeniuslabs/fibonacci-v3d
Copyright (C) 2014-2016 Jason Coon, Evil Genius Labs
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/>.
*/
class Cell
{
public:
byte alive = 1;
byte prev = 1;
byte hue = 6;
byte brightness;
};
Cell world[kMatrixWidth][kMatrixHeight];
uint8_t neighbors(uint8_t x, uint8_t y)
{
return (world[(x + 1) % kMatrixWidth][y].prev) +
(world[x][(y + 1) % kMatrixHeight].prev) +
(world[(x + kMatrixWidth - 1) % kMatrixWidth][y].prev) +
(world[x][(y + kMatrixHeight - 1) % kMatrixHeight].prev) +
(world[(x + 1) % kMatrixWidth][(y + 1) % kMatrixHeight].prev) +
(world[(x + kMatrixWidth - 1) % kMatrixWidth][(y + 1) % kMatrixHeight].prev) +
(world[(x + kMatrixWidth - 1) % kMatrixWidth][(y + kMatrixHeight - 1) % kMatrixHeight].prev) +
(world[(x + 1) % kMatrixWidth][(y + kMatrixHeight - 1) % kMatrixHeight].prev);
}
void randomFillWorld()
{
static uint8_t lifeDensity = 10;
for (uint8_t i = 0; i < kMatrixWidth; i++) {
for (uint8_t j = 0; j < kMatrixHeight; j++) {
if (random(100) < lifeDensity) {
world[i][j].alive = 1;
world[i][j].brightness = 255;
}
else {
world[i][j].alive = 0;
world[i][j].brightness = 0;
}
world[i][j].prev = world[i][j].alive;
world[i][j].hue = 0;
}
}
}
uint8_t life()
{
static uint8_t generation = 0;
// Display current generation
for (uint8_t i = 0; i < kMatrixWidth; i++)
{
for (uint8_t j = 0; j < kMatrixHeight; j++)
{
setPixelXY(i, j, ColorFromPalette(currentPalette, world[i][j].hue * 4, world[i][j].brightness, LINEARBLEND));
}
}
uint8_t liveCells = 0;
// Birth and death cycle
for (uint8_t x = 0; x < kMatrixWidth; x++)
{
for (uint8_t y = 0; y < kMatrixHeight; y++)
{
// Default is for cell to stay the same
if (world[x][y].brightness > 0 && world[x][y].prev == 0)
world[x][y].brightness *= 0.5;
uint8_t count = neighbors(x, y);
if (count == 3 && world[x][y].prev == 0)
{
// A new cell is born
world[x][y].alive = 1;
world[x][y].hue += 2;
world[x][y].brightness = 255;
}
else if ((count < 2 || count > 3) && world[x][y].prev == 1)
{
// Cell dies
world[x][y].alive = 0;
}
if (world[x][y].alive)
liveCells++;
}
}
// Copy next generation into place
for (uint8_t x = 0; x < kMatrixWidth; x++)
{
for (uint8_t y = 0; y < kMatrixHeight; y++)
{
world[x][y].prev = world[x][y].alive;
}
}
if (liveCells < 4 || generation >= 128)
{
fill_solid(leds, NUM_LEDS, CRGB::Black);
randomFillWorld();
generation = 0;
}
else
{
generation++;
}
return 60;
}