-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife.h
167 lines (150 loc) · 4.09 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
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
/*
* refrigerator-lights: https://github.com/munichmakerlab/refrigerator-lights
* Copyright (C) 2016 Juergen Skrotzky alias Jorgen ([email protected])
*
* This sketch lights up a 15x10 WS2812b RGB LEDs.
* The lights are fully controllable over WiFi by MQTT.
*
* 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/>.
*
* Effect implementation for the GauntletII project.
* Created by Robert Atkins on 26/09/13.
* Copyright (c) 2013 Robert Atkins. All rights reserved.
*/
#ifndef life_h
#define life_h
#include "config.h"
#include "my_mqtt.h"
void resetBack();
const int deltaLen = 36;
int gol_framerate;
int gol_brightness;
int gol_time;
int gol_timeMax;
uint8_t density;
uint8_t hue;
uint16_t delta[deltaLen];
void seed(uint8_t hue) {
for (int i = 0; i < NUMPIXELS; i++) {
if (random(255) < density) {
ledsBack(i) = CHSV(hue, 255, 255);
}
}
}
bool inXRange(int x) {
return x >= 0 && x < MATRIX_WIDTH;
}
bool inYRange(int y) {
return y >= 0 && y < MATRIX_HEIGHT;
}
bool alive(int x, int y) {
return inXRange(x) && inYRange(y) && ledsBack(x, y);
}
int deltaIndex(int x, int y) {
return y * 2 + (x < 16 ? 0 : 1);
}
// xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
// xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
void setChanged(int x, int y) {
delta[deltaIndex(x, y)] |= 1 << (x < 16 ? x : x - 16);
}
void updateWithChanges(uint8_t time) {
for (int x = 0; x < MATRIX_WIDTH; x++) {
for (int y = 0; y < MATRIX_HEIGHT; y++) {
if (delta[deltaIndex(x, y)] & (1 << (x < 16 ? x : x - 16))) {
processMQTTLoop();
if (mqttNewMessage)
return;
if (ledsBack(x, y)) {
ledsBack(x, y) = CRGB::Black;
} else {
ledsBack(x, y) = CHSV(time, 255, 255);
}
}
}
}
}
/*
x - 1, y + 1|x = x, y + 1|x + 1, y + 1
x - 1, y = y| x |x + 1, y = y
x - 1, y - 1|x = x, y - 1|x + 1, y - 1
*/
int numNeighbours(int x, int y) {
int numNeighbours = 0;
numNeighbours += alive(x - 1, y + 1) ? 1 : 0;
numNeighbours += alive(x, y + 1) ? 1 : 0;
numNeighbours += alive(x + 1, y + 1) ? 1 : 0;
numNeighbours += alive(x - 1, y) ? 1 : 0;
numNeighbours += alive(x + 1, y) ? 1 : 0;
numNeighbours += alive(x - 1, y - 1) ? 1 : 0;
numNeighbours += alive(x, y - 1) ? 1 : 0;
numNeighbours += alive(x + 1, y - 1) ? 1 : 0;
return numNeighbours;
}
void resetLife() {
density = 56;
gol_brightness = 0;
gol_framerate = 100;
gol_time = 0;
gol_timeMax = random(96, 156);
resetBack();
uint8_t hue = random(256);
seed(hue);
}
void fadeout() {
if (gol_brightness < 256) {
for (int i = 0; i < NUMPIXELS; i++) {
processMQTTLoop();
if (mqttNewMessage)
return;
ledsBack(i)--;
}
gol_brightness++;
} else {
resetLife();
}
}
void life() {
if (abs(millis() - LastLoop) >= gol_framerate) {
LastLoop = millis();
if (gol_time < gol_timeMax) {
for (int x = 0; x < MATRIX_WIDTH; x++) {
for (int y = 0; y < MATRIX_HEIGHT; y++) {
processMQTTLoop();
if (mqttNewMessage)
return;
int neighbours = numNeighbours(x, y);
if (ledsBack(x, y)) {
if (neighbours < 2 || neighbours > 3) {
setChanged(x, y);
}
} else {
if (neighbours == 3) {
setChanged(x, y);
}
}
}
}
updateWithChanges(hue++);
for (int i = 0; i < deltaLen; i++) {
delta[i] = 0;
}
gol_time++;
} else {
gol_framerate = 25;
fadeout();
}
}
}
#endif //life_h