-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalvin.ino
221 lines (187 loc) · 5.26 KB
/
alvin.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
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <MFRC522.h>
#include "./images.h"
#include "./item.h"
#include "./game.h"
// Declare the wiring
MFRC522 mfrc522(7, 8);
Adafruit_PCD8544 display = Adafruit_PCD8544(18, 20, 19);
const uint8_t blPin = 10;
// Global state
const uint8_t availableItemsLen = 4;
Item availableItems[availableItemsLen];
GameState s;
void setup() {
pinMode(blPin, OUTPUT);
analogWrite(blPin, 100);
display.begin();
display.setRotation(2);
display.setContrast(60);
display.display();
delay(1000);
display.clearDisplay();
display.fillScreen(WHITE);
display.setTextSize(2);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("Get");
display.println("Ready!");
display.display();
randomSeed(analogRead(21)); // Pin 21 (A3) is expected to be unconnected
loadItems(availableItems);
s.reset();
delay(2000);
mfrc522.PCD_Init();
}
void loop() {
static uint8_t selectedIdx;
static MFRC522::Uid activeUid;
if (!s.playerTurn) {
if (s.gameLevel == s.patternLen) {
victoryAnimation();
display.clearDisplay();
display.setTextSize(2);
display.println("You");
display.println("win!");
display.display();
s.reset();
delay(5000);
} else {
for (uint8_t l = 0; l <= s.gameLevel; l++) {
display.fillScreen(WHITE);
display.display();
delay(1000);
showItem(s.pattern[l]);
display.setCursor(0, 40);
display.println(l);
delay(1000);
}
display.clearDisplay();
display.setTextSize(2);
display.println("Go!");
display.display();
s.playerTurn = true;
}
return;
}
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
copyUid(&(mfrc522.uid), &activeUid);
mfrc522.PICC_HaltA();
display.clearDisplay();
selectedIdx = findItemByUid(availableItems, availableItemsLen, &activeUid);
if (selectedIdx > availableItemsLen) {
display.setTextSize(1);
display.println("Not found!");
printUid(&activeUid);
display.display();
delay(2000);
} else if (selectedIdx == s.pattern[s.gameStep]) {
showItem(selectedIdx);
display.setCursor(0, 40);
display.setTextSize(1);
display.println("Great!");
display.display();
s.gameStep++;
delay(1000);
display.fillScreen(WHITE);
display.setTextSize(2);
display.println("Go!");
display.display();
if (s.gameStep > s.gameLevel) {
s.gameLevel++;
s.gameStep = 0;
s.attempt = 0;
s.playerTurn = false;
}
return;
} else {
showItem(selectedIdx);
s.attempt++;
if (s.attempt >= 3) {
delay(2000);
s.reset();
display.clearDisplay();
display.setTextSize(2);
display.println("Game");
display.println("over!");
display.display();
delay(2000);
return;
}
s.gameStep = 0;
display.setCursor(0, 40);
display.setTextSize(1);
display.println("Try again...");
display.display();
delay(1000);
display.fillScreen(WHITE);
display.display();
delay(1000);
s.playerTurn = false;
return;
}
}
void copyUid(MFRC522::Uid* src, MFRC522::Uid* dest) {
dest->size = src->size;
dest->sak = src->sak;
for(uint8_t i = 0; i < src->size; i++) {
dest->uidByte[i] = src->uidByte[i];
}
}
void printUid(MFRC522::Uid* uid) {
display.print(F("Card UID:"));
for (uint8_t i = 0; i < uid->size; i++) {
if(uid->uidByte[i] < 0x10)
display.print(F(" 0"));
else
display.print(F(" "));
display.print(uid->uidByte[i], HEX);
}
display.println();
}
void showItem(uint8_t i) {
showItem(i, 0, 0);
}
void showItem(uint8_t i, int16_t x, int16_t y) {
uint16_t w = availableItems[i].dim[0];
uint16_t h = availableItems[i].dim[1];
display.fillScreen(WHITE);
if (i == 0) {
display.drawBitmap(x, y, blackCar, w, h, BLACK);
} else if (i == 1) {
display.drawBitmap(x, y, datsun, w, h, BLACK);
} else if (i == 2) {
display.drawBitmap(x, y, redCar, w, h, BLACK);
} else if (i == 3) {
display.drawBitmap(x, y, snowplow, w, h, BLACK);
}
display.display();
}
void victoryAnimation() {
cruiseAninmation(2, 700);
cruiseAninmation(1, 1000);
cruiseAninmation(0, 1000);
cruiseAninmation(3, 1500);
}
void cruiseAninmation(uint8_t car, uint16_t ms) {
uint8_t steps = 20;
uint16_t adjust = (84 * 2) / steps;
uint16_t wait = ms / steps;
int16_t x = 84;
for (int8_t i = steps + 2; i > 0; i--) { // plus one to get item off screen,
// plus two for looks.
showItem(car, x, 0);
x = x - adjust;
delay(wait);
}
return;
}