-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtboard.ino
187 lines (154 loc) · 3.6 KB
/
tboard.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
// Created 26 May 2017
#include "TetrisBoard.h"
// teensy pinout
#define BUT_LF 0
#define BUT_RT 23
#define BUT_DRP 19
#define BUT_ROT 17
#define UNCONNECTED 1
#define PRESSED 0 // for pullup buttons
#define NOTPRESSED 1
#define STEPTIME 300 // time between down steps, in msec
#define BUTTONTIME 50 // time between button presses
#define DROPTIME 10 // time between steps when dropping fast
TetrisBoard game;
uint32_t moveTime;
uint32_t inputTime;
bool keepGoing;
bool dropping = 0;
bool button_lf = NOTPRESSED;
bool button_rt = NOTPRESSED;
bool button_rot = NOTPRESSED;
bool button_drp = NOTPRESSED;
void setup()
{
pinMode(UNCONNECTED, INPUT);
pinMode(BUT_LF, INPUT_PULLUP);
pinMode(BUT_RT, INPUT_PULLUP);
pinMode(BUT_ROT, INPUT_PULLUP);
pinMode(BUT_DRP, INPUT_PULLUP);
randomSeed(analogRead(UNCONNECTED));
restartGame();
}
// return the number of whichever button went from unpressed to pressed
// if more than one button, select randomly among them
int readButtons() {
int b;
int changed = 0;
b = digitalRead(BUT_LF);
if (b != button_lf) {
if (b == NOTPRESSED)
button_lf = b;
else
changed |= 1;
}
b = digitalRead(BUT_RT);
if (b != button_rt) {
if (b == NOTPRESSED)
button_rt = b;
else
changed |= 2;
}
b = digitalRead(BUT_ROT);
if (b != button_rot) {
if (b == NOTPRESSED)
button_rot = b;
else
changed |= 4;
}
b = digitalRead(BUT_DRP);
if (b != button_drp) {
if (b == NOTPRESSED)
button_drp = b;
else
changed |= 8;
}
changed = button_resolve(changed);
switch (changed) {
case 0:
return(-1);
case 1:
button_lf = PRESSED;
return BUT_LF;
case 2:
button_rt = PRESSED;
return BUT_RT;
case 4:
button_rot = PRESSED;
return BUT_ROT;
case 8:
button_drp = PRESSED;
return BUT_DRP;
}
Serial.println("readButtons error!");
return(-1);
}
// resolve button conflicts by returning a bitmask with only one hot
// 1=left, 2=right, 4=rot, 8=drop
int button_resolve(int changed)
{
if (changed == 0 || changed == 1 || changed == 2 || changed ==4 || changed == 8) return changed;
// rotate is always first
if (changed & 4) return 4;
// drop is always last
if (changed & B1000) changed &= B0111;
// resolve lf/rt randomly
if ((changed & B0001) && (changed & B0010)) {
int resolve = random(2);
if (resolve) return 2; else return 1;
}
return changed;
}
void restartGame()
{
game.setupGame();
game.spawn();
moveTime = millis();
inputTime = millis();
dropping = 0;
button_lf = NOTPRESSED;
button_rt = NOTPRESSED;
button_rot = NOTPRESSED;
button_drp = NOTPRESSED;
}
void loop()
{
int nextTime;
if (millis() > inputTime + BUTTONTIME) {
inputTime = millis();
switch(readButtons()) {
case -1:
break;
case BUT_LF:
game.moveTet(-1,0);
break;
case BUT_RT:
game.moveTet(1,0);
break;
case BUT_ROT:
game.rotTet();
break;
case BUT_DRP:
dropping = 1;
break;
default:
break;
}
}
nextTime = dropping ? DROPTIME : STEPTIME;
if (millis() > moveTime + nextTime) {
moveTime += nextTime;
keepGoing = game.moveTet(0, -1);
if (keepGoing == 0) {
game.freezeTet();
dropping = 0;
if (game.spawn() == 0) {
game.gameOver();
while (digitalRead(BUT_LF) == NOTPRESSED || digitalRead(BUT_RT) == NOTPRESSED) {
;
}
restartGame();
}
}
}
}