-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
160 lines (136 loc) · 3.9 KB
/
Matrix.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
#include "Matrix.h"
uint8_t Matrix::buffer_new[LCD_WIDTH * LCD_HEIGHT8];
uint8_t Matrix::buffer[LCD_WIDTH * LCD_HEIGHT8];
/* Data strucutre initialization & Game of Life algorithm */
void Matrix::InitializeRandom() {
FOR_i {
FOR_j {
Matrix::buffer[MAP(i, j)] = random(256);
}
}
}
void Matrix::InitializeProbability(uint8_t chance_in_255) {
FOR_x {
FOR_y {
if (random(256) <= chance_in_255) {
BIT_SET_xy(x, y);
} else {
BIT_CLEAR_xy(x, y);
}
}
}
}
void Matrix::InitializePattern() {
FOR_i {
FOR_j {
if ((i + j) % 2 == 0)
Matrix::buffer[MAP(i, j)] = random(256);
}
}
}
void Matrix::InitializeBitmap() {
FOR_i {
FOR_j {
Matrix::buffer[MAP(i, j)] = random(256);
}
}
}
void Matrix::GameOfLife() {
// new state
uint8_t neighbour[8] = {0};
uint8_t byte_cell;
uint8_t byte_cell_new;
uint8_t byte_cell_count;
// calculate new state
FOR_i {
FOR_j {
byte_cell = Matrix::buffer[MAP(i, j)];
/* * get each bit's neighbour one byte at a time * */
/* east and west */
neighbour[WEST] = 0b00000000;
if (i > 0) {
neighbour[WEST] = Matrix::buffer[MAP((i - 1), j)];
}
neighbour[EAST] = 0b00000000;
if (i < (LCD_WIDTH - 1)) {
neighbour[EAST] = Matrix::buffer[MAP((i + 1), j)];
}
/* north */
neighbour[NORTH] = 0b00000000;
neighbour[NORTHEAST] = 0b00000000;
neighbour[NORTHWEST] = 0b00000000;
if (j > 0) {
neighbour[NORTH] = Matrix::buffer[MAP(i, j - 1)];
if (i > 0) {
neighbour[NORTHWEST] = Matrix::buffer[MAP(i - 1, j - 1)];
}
if (i < (LCD_WIDTH - 1)) {
neighbour[NORTHEAST] = Matrix::buffer[MAP(i + 1, j - 1)];
}
}
neighbour[NORTH] = (byte_cell << 1) | ((neighbour[NORTH] & 0b10000000) >> 7);
neighbour[NORTHEAST] = (neighbour[EAST] << 1) | ((neighbour[NORTHEAST] & 0b1000000) >> 7);
neighbour[NORTHWEST] = (neighbour[WEST] << 1) | ((neighbour[NORTHWEST] & 0b1000000) >> 7);
/* south */
neighbour[SOUTH] = 0b00000000;
neighbour[SOUTHEAST] = 0b00000000;
neighbour[SOUTHWEST] = 0b00000000;
if (j < (LCD_HEIGHT8 - 1)) {
neighbour[SOUTH] = Matrix::buffer[MAP(i, j + 1)];
if (i > 0) {
neighbour[SOUTHWEST] = Matrix::buffer[MAP(i - 1, j + 1)];
}
if (i < (LCD_WIDTH - 1)) {
neighbour[SOUTHEAST] = Matrix::buffer[MAP(i + 1, j + 1)];
}
}
neighbour[SOUTH] = ((neighbour[SOUTH] & 0b00000001) << 7) | (byte_cell >> 1);
neighbour[SOUTHEAST] = ((neighbour[SOUTHEAST] & 0b00000001) << 7) | (neighbour[EAST] >> 1);
neighbour[SOUTHWEST] = ((neighbour[SOUTHWEST] & 0b00000001) << 7) | (neighbour[WEST] >> 1);
/* calculate each bit of next gen */
byte_cell_new = 0b00000000;
FOR_k {
byte_cell_count = 0;
FOR_n {
byte_cell_count += (neighbour[n] & 0b00000001);
}
byte_cell_new >>= 1;
if ((byte_cell_count == 3) || ((byte_cell_count == 2) && (byte_cell & 0b00000001))) {
byte_cell_new |= 0b10000000;
}
FOR_n {
neighbour[n] >>= 1;
}
byte_cell >>= 1;
}
Matrix::buffer_new[MAP(i, j)] = byte_cell_new;
}
}
// apply new state
FOR_i {
FOR_j {
Matrix::buffer[MAP(i, j)] = Matrix::buffer_new[MAP(i, j)];
}
}
}
void Matrix::Flip() {
FOR_i {
FOR_j {
Matrix::buffer[MAP(i, j)] = ~Matrix::buffer[MAP(i, j)];
}
}
}
void Matrix::GetInfo(uint16_t &count_live, uint16_t &count_dead, uint16_t &percent) {
count_live = 0;
count_dead = 0;
FOR_x {
FOR_y {
if (BIT_READ_xy(x, y)) {
count_live++;
} else {
count_dead++;
}
}
}
percent = round((count_live / (float)count_dead) * 100.0);
}