-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathConway.cpp
183 lines (153 loc) · 7.46 KB
/
Conway.cpp
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
#include "settings.h"
#include "Conway.h"
// for a random board
// static uint8_t Conway::board[BOARD_ROW_SIZE] = {
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
// };
// test pattern
// static uint8_t Conway::board[BOARD_ROW_SIZE] = {
// 0b10000000, 0b01000000, 0b00100000, 0b00010000, 0b00001000, 0b00000100, 0b00000010, 0b00000001,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
//
// 0b10000000, 0b01000000, 0b00100000, 0b00010000, 0b00001000, 0b00000100, 0b00000010, 0b00000001,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// };
// glider
// NOTE: These arrays are BIG ENDIAN. The very first bit you see is the 0x0 pixel in the top left corner of the screen.
// rotate the bytes 90 degrees clockwise to visualize how they map to the screen
static uint8_t Conway::board[BOARD_ROW_SIZE] = {
// first 8 rows
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000001, 0b00000010, 0b00000010, 0b00000000,
0b00000001, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000011, 0b00000011, 0b00000100,
0b00000000, 0b00001100, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000011, 0b00000011, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
// second 8 rows
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b11000000, 0b11000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b11100000, 0b00010000, 0b00001000, 0b00001000, 0b01000000,
0b00010000, 0b11100000, 0b01000000, 0b00000000, 0b00000000, 0b10000000, 0b10000000, 0b01000000,
0b00000000, 0b01100000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
};
static uint8_t Conway::new_board[BOARD_ROW_SIZE] = { 0 };
Conway::Conway(SSD1306Device* _oled) {
oled = _oled;
}
void Conway::run() {
updateScreen();
while (true) {
updateGame();
updateScreen();
}
}
void Conway::updateGame() {
for (uint8_t x = 0; x < BOARD_X; x++) {
for (uint8_t y = 0; y < BOARD_Y; y++) {
updateCell({x, y});
}
}
updateBoard();
}
void Conway::updateBoard() {
for (uint8_t row = 0; row < BOARD_ROW_SIZE; row++) {
board[row] = new_board[row];
}
}
void Conway::updateCell(UPosition position) {
uint8_t livingNeighbors = getLivingNeighbors(position);
if (livingNeighbors < 2 || livingNeighbors > 3) {
setCell(position, 0);
} else if (livingNeighbors == 3) {
setCell(position, 1);
} else {
setCell(position, getCell(position));
}
}
uint8_t Conway::getLivingNeighbors(UPosition position) {
const Position positionDeltas[] = {
{-1, -1}, {0,-1}, {1,-1},
{-1, 0}, /*cell*/ {1, 0},
{-1, 1}, {0, 1}, {1, 1},
};
const uint8_t NUM_DELTAS = 8;
uint8_t livingNeighbors = 0;
for (uint8_t i = 0; i < NUM_DELTAS; i++) {
UPosition newPosition = { position.x + positionDeltas[i].x, position.y + positionDeltas[i].y };
livingNeighbors += getCell(newPosition);
// after 4 neighbors the outcome is always the same, so we can just abort
if (livingNeighbors > 3) {
break;
}
}
return livingNeighbors;
}
uint8_t Conway::getCell(UPosition position) {
// uint's don't have negative values, so you don't have to check < 0
if (position.x >= BOARD_X || position.y >= BOARD_Y) {
return 0;
}
uint16_t boundaryPosition = positionToRowIndex(position);
uint8_t yValueInsideRow = position.y % 8;
return (board[boundaryPosition] & (1 << (7 - yValueInsideRow))) >> (7 - yValueInsideRow);
}
uint16_t Conway::positionToRowIndex(UPosition position) {
return (position.x + (position.y >> 3) * BOARD_X);
}
void Conway::setCell(UPosition position, uint8_t value) {
uint16_t boundaryPosition = positionToRowIndex(position);
uint8_t yValueInsideRow = position.y % 8;
uint8_t row = new_board[boundaryPosition];
row = (row & ~(1 << (7 - yValueInsideRow))) | (value << (7 - yValueInsideRow));
new_board[boundaryPosition] = row;
}
void Conway::updateScreen() {
// oled->setCursor(0,0);
oled->startData();
for (uint8_t y = 0; y < (BOARD_Y >> 2); y++) {
for(uint8_t x = 0; x < BOARD_X; x++){
uint8_t row = board[x + (y >> 1) * BOARD_X];
bool bottomOfData = (y % 2) == 0;
if (bottomOfData) {
row = row >> 4;
}
row = (row & 1) << 7 | (row & 1) << 6 | (row & 2) << 4 | (row & 2) << 3 | (row & 4) << 1 | (row & 4) << 0 | (row & 8) >> 2 | (row & 8) >> 3;
oled->sendData(row);
oled->sendData(row);
}
}
oled->endData();
oled->switchFrame();
}