-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacker.ino
254 lines (207 loc) · 5.13 KB
/
stacker.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
// #define LATCH_PIN 8
// #define CLOCK_PIN 12
// #define DATA_PIN 11
// #define BUTTON_PIN 2
#define LATCH_PIN 0
#define CLOCK_PIN 4
#define DATA_PIN 1
#define BUTTON_PIN 2
#define BUTTON_TIMEOUT 250
const char HEART[8] = {
0b00000000,
0b01100110,
0b11111111,
0b11111111,
0b01111110,
0b00111100,
0b00011000,
0b00000000
};
const char BIRD[8] = {
0b01110000,
0b11010000,
0b01110000,
0b01111000,
0b01111111,
0b01111110,
0b01111100,
0b00100000
};
const char SPEEDS[4] = { 40, 50, 60, 70 };
enum Direction {
RIGHT,
LEFT,
};
enum State {
PLAYING,
PAUSED,
};
// State
State currentState;
char PIXELS[8];
char currentRow;
char currentCol;
char size;
Direction direction;
bool isButtonPressed;
unsigned long buttonTime;
unsigned long lastUpdateTime;
void setup() {
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonPressed, RISING);
isButtonPressed = false;
buttonTime = millis();
lastUpdateTime = millis();
setPlaying();
}
void setPlaying() {
currentState = PLAYING;
memset(PIXELS, 0, 8);
currentRow = 7;
currentCol = 0;
size = 3;
direction = RIGHT;
}
void setPaused(char pixels[8]) {
currentState = PAUSED;
memcpy(PIXELS, pixels, 8 * sizeof(char));
}
void buttonPressed() {
if (millis() - buttonTime < BUTTON_TIMEOUT) return;
buttonTime = millis();
isButtonPressed = true;
}
void loop() {
updatePixels();
displayPixels();
}
/*
* Update PIXELS without updating the display.
*/
void updatePixels() {
if (millis() - lastUpdateTime < SPEEDS[currentRow / 2]) return;
switch (currentState) {
case PLAYING:
updatePlaying();
break;
case PAUSED:
updatePause();
break;
}
lastUpdateTime = millis();
}
void updatePlaying() {
if (isButtonPressed) {
isButtonPressed = false;
int firstOverlappingColumn = -1;
int numOverlapping = 0;
if (currentRow == 7) {
firstOverlappingColumn = currentCol;
numOverlapping = size;
} else {
for (int col = 0; col < 8; col++) {
bool overlapping = PIXELS[currentRow + 1] >> (7 - col) & 1 == 1
&& col >= currentCol
&& col < currentCol + size;
if (overlapping) {
if (firstOverlappingColumn < 0) {
firstOverlappingColumn = col;
}
numOverlapping++;
}
}
}
if (numOverlapping == 0) {
setPaused(BIRD);
} else if (currentRow == 0 && numOverlapping > 0) {
setPaused(HEART);
} else {
if (firstOverlappingColumn > 0) {
currentCol = firstOverlappingColumn;
}
size = numOverlapping;
drawCursorRow();
currentRow--;
}
} else {
bool swapDirection = (direction == RIGHT && currentCol + size > 7)
|| (direction == LEFT && currentCol == 0);
if (swapDirection) {
direction = direction == RIGHT ? LEFT : RIGHT;
}
currentCol += direction == RIGHT ? 1 : -1;
drawCursorRow();
}
}
void drawCursorRow() {
PIXELS[currentRow] = 0;
for (int col = 0; col < 8; col++) {
PIXELS[currentRow] |= col >= currentCol && col < currentCol + size ? 1 << (7 - col) : 0;
}
}
void updatePause() {
if (isButtonPressed) {
isButtonPressed = false;
setPlaying();
}
}
struct Mask {
char left;
char right;
};
struct Mask rowMask(int row) {
switch (row) {
case 0: return { 0b00000000, 0b00000001 };
case 1: return { 0b00100000, 0b00000000 };
case 2: return { 0b00000000, 0b00010000 };
case 3: return { 0b00000000, 0b00001000 };
case 4: return { 0b00001000, 0b00000000 };
case 5: return { 0b00000000, 0b00100000 };
case 6: return { 0b00000100, 0b00000000 };
case 7: return { 0b00000000, 0b10000000 };
default: return { 0b00000000, 0b00000000 };
}
}
struct Mask colMask(int col) {
switch (col) {
case 0: return { 0b00010000, 0b00000000 };
case 1: return { 0b00000010, 0b00000000 };
case 2: return { 0b00000001, 0b00000000 };
case 3: return { 0b00000000, 0b00000010 };
case 4: return { 0b00000000, 0b01000000 };
case 5: return { 0b00000000, 0b00000100 };
case 6: return { 0b01000000, 0b00000000 };
case 7: return { 0b10000000, 0b00000000 };
default: return { 0b00000000, 0b00000000 };
}
}
const struct Mask FULL_COL_MASK = { 0b11010011, 0b01000110 };
/*
* Send two bytes of data to the shift registers.
*/
void writeBits(char left, char right) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, left);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, right);
digitalWrite(LATCH_PIN, HIGH);
}
/*
* Update the display with the stored data in PIXELS.
*/
void displayPixels() {
for (int row = 0; row < 8; row++) {
struct Mask row_mask = rowMask(row);
row_mask.left |= FULL_COL_MASK.left;
row_mask.right |= FULL_COL_MASK.right;
for (int col = 0; col < 8; col++) {
struct Mask col_mask = colMask(col);
if (PIXELS[row] >> (7 - col) & 1 == 1) {
row_mask.left ^= col_mask.left;
row_mask.right ^= col_mask.right;
}
}
writeBits(row_mask.left, row_mask.right);
}
}