-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlaser_tag.ino
350 lines (308 loc) · 9.43 KB
/
laser_tag.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "laser_tag.h"
#include <Arduino.h>
#include "laser_tag_tests.h"
#include "laser_tag_utils.h"
//#define TESTING
// Global game variables
int shot_duration = 500;
int shot_cooldown = 200;
int hit_cooldown = 5000;
//int game_duration = 300000;
int game_duration = 10000;
int poll_game_start_interval = 2000;
int vest_threshold = 500;
// FSM variables
int deaths;
int game_start_time;
int saved_clock;
int curr_time;
// FSM inputs
int trigger_pressed;
int sensor_value;
state CURRENT_STATE;
// Sound
SimpleTimer sound_player;
// ########### WIFI CODE ############
//#include <HttpClient.h>
#include <NTPClient.h>
#include <SPI.h>
#include <WiFi101.h>
#include <WiFiUdp.h>
WiFiUDP ntpUDP;
WiFiClient client;
// HttpClient http(client);
NTPClient timeClient(ntpUDP);
byte mac[6];
char player_id[18];
char server_url[] = "http://8188-138-16-111-203.ngrok.io"; // URL for our server
char host_name[] = "8188-138-16-111-203.ngrok.io";
// char server_url[] = "http://104.131.46.88/"; // URL for our server
char ssid[] = "Brown-Guest"; // network SSID (name)
char pass[] = ""; // for networks that require a password
// char ssid[] = "29 CREIGHTON - 1";
// char pass[] = "R3m0t3L3@rn1ng!";
// Attempt to connect to WiFi network.
// Returns true on success and false on failure.
bool attempt_connect() {
int status = WiFi.begin(ssid); // WiFi.begin(ssid, pass) for password
Serial.println(status);
return status == WL_CONNECTED;
}
// Connects to the WiFi network specified by ssid and sets player_id to the
// Arduino's MAC address.
void setup_wifi() {
// attempt to connect to WiFi network:
Serial.print("Attempting to connect to: ");
Serial.println(ssid);
while (!attempt_connect()) {
Serial.println("Trying again");
delay(5000);
}
WiFi.macAddress(mac);
sprintf(player_id, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println(player_id);
}
bool connect_to_webpage() {
if (client.connect(server_url, 80)) {
Serial.println("player_id: " + String(player_id));
client.println("GET /api/score/hit?id=" + String(player_id) + " HTTP/1.0");
client.println("Host: " + String(host_name));
client.println();
return true;
} else {
Serial.println("Failed to fetch webpage");
return false;
}
}
bool register_for_game() {
// http.get(server_url, "/api/score/register");
if (client.connect(server_url, 80)) {
String response = "";
Serial.println("player_id: " + String(player_id));
client.println("GET /api/score/register?id=" + String(player_id) + " HTTP/1.0");
client.println("Host: " + String(host_name));
client.println();
while (client.connected()) {
if (client.available()) {
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
response += c;
}
}
// the server's disconnected, stop the client:
client.stop();
response = response.substring(response.length() - 2);
return response.equals("OK");
} else {
Serial.println("Failed to fetch webpage");
return false;
}
}
int get_start_time() {
#ifdef TESTING
return 100;
#else
String response = "";
if (client.connect(server_url, 80)) {
// Serial.println("player_id: " + String(player_id));
client.println("GET /api/score/start HTTP/1.0");
client.println("Host: " + String(host_name));
client.println();
while (client.connected()) {
if (client.available()) {
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
response += c;
if (c == '\n') {
response = "";
}
}
}
// the server's disconnected, stop the client:
client.stop();
Serial.println(response);
return response.toInt();
} else {
Serial.println("Failed to fetch webpage");
return 0;
}
#endif
}
int get_current_time() {
#ifdef TESTING
return ++curr_time;
#else
timeClient.update();
return timeClient.getEpochTime();
#endif
}
// ##################################
void setup() {
Serial.begin(9600);
while (!Serial)
;
#ifndef TESTING
setup_wifi();
initialize_system();
calibrate();
#endif
// test_calibration();
CURRENT_STATE = sGAME_NOT_STARTED;
set_vest_lights(ON);
saved_clock = millis();
game_start_time = 0;
curr_time = 0;
#ifndef TESTING
Serial.println("Trying to register for game!");
while (!register_for_game()) {
Serial.println("Trying again");
delay(1000);
}
Serial.println("Registration successful");
#endif
// Watchdog configuration
NVIC_DisableIRQ(WDT_IRQn);
NVIC_ClearPendingIRQ(WDT_IRQn);
NVIC_SetPriority(WDT_IRQn, 0);
NVIC_EnableIRQ(WDT_IRQn);
// Configure and enable WDT GCLK:
GCLK->GENDIV.reg = GCLK_GENDIV_DIV(4) | GCLK_GENDIV_ID(5);
while (GCLK->STATUS.bit.SYNCBUSY)
;
// set GCLK->GENCTRL.reg and GCLK->CLKCTRL.reg
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(5) | GCLK_GENCTRL_DIVSEL;
while (GCLK->STATUS.bit.SYNCBUSY)
;
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_GEN(5) | GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_ID_WDT;
// Configure and enable WDT:
// WDT->CONFIG.reg, WDT->EWCTRL.reg, WDT->CTRL.reg
WDT->CONFIG.reg = 0x9;
WDT->EWCTRL.reg = 0x8;
WDT->CTRL.reg = WDT_CTRL_ENABLE;
while (WDT->STATUS.bit.SYNCBUSY)
;
#ifdef TESTING
test_all_tests();
#endif
}
void loop() {
#ifndef TESTING
WDT->CLEAR.reg = 0xA5;
update_inputs();
WDT->CLEAR.reg = 0xA5;
CURRENT_STATE = update_fsm(CURRENT_STATE, millis(), trigger_pressed, sensor_value);
WDT->CLEAR.reg = 0xA5;
sound_player.run();
delay(12);
#endif
}
state update_fsm(state cur_state, long mils, int trigger_pressed, int sensor_value) {
state next_state = cur_state;
switch (cur_state) {
case sGAME_NOT_STARTED:
if (game_start_time > 0) { // Transition from 0-1
curr_time = get_current_time();
next_state = sCOUNTDOWN_TILL_START;
} else if ((mils - saved_clock) >= poll_game_start_interval) {
game_start_time = get_start_time();
saved_clock = mils;
next_state = sGAME_NOT_STARTED; // Transition from 0-0
} else {
next_state = sGAME_NOT_STARTED; // Transition from 0-0
}
break;
case sCOUNTDOWN_TILL_START: {
if (curr_time >= game_start_time) { // Transition from 1-2
make_sound(GAME_STARTING);
set_vest_lights(ON);
game_start_time = mils;
next_state = sNEUTRAL;
} else { // Transition from 1-1
curr_time = get_current_time();
next_state = sCOUNTDOWN_TILL_START;
}
break;
}
case sNEUTRAL:
if ((mils - game_start_time) >= game_duration) { // Transition 2-5
make_sound(GAME_OVER);
set_vest_lights(OFF);
next_state = sGAME_OVER;
} else if (sensor_value >= vest_threshold) { // Transition from 2-4
set_laser(LOW);
set_vest_lights(OFF);
make_sound(HIT);
report_hit();
saved_clock = mils;
deaths = deaths + 1;
next_state = sHIT;
} else if (trigger_pressed == 1) { // Transition 2-3
set_laser(HIGH);
make_sound(PEW);
saved_clock = mils;
next_state = sJUST_FIRED;
} else {
next_state = sNEUTRAL;
}
break;
case sJUST_FIRED:
if ((mils - game_start_time) >= game_duration) { // Transition from 3-5
make_sound(GAME_OVER);
set_vest_lights(OFF);
next_state = sGAME_OVER;
} else if (sensor_value >= vest_threshold) { // Transition from 3-4
set_laser(LOW);
set_vest_lights(OFF);
make_sound(HIT);
report_hit();
saved_clock = mils;
deaths = deaths + 1;
next_state = sHIT;
} else if ((mils - saved_clock) >= shot_duration) { // Transition from 3-6
set_laser(LOW);
saved_clock = mils;
next_state = sGUN_COOLDOWN;
} else {
next_state = sJUST_FIRED;
}
break;
case sHIT:
if ((mils - game_start_time) >= game_duration) { // Transition 4-5
make_sound(GAME_OVER);
set_vest_lights(OFF);
next_state = sGAME_OVER;
} else if ((mils - saved_clock) >= hit_cooldown) { // Transition from 4-2
make_sound(REVIVED);
set_vest_lights(ON);
next_state = sNEUTRAL;
} else {
next_state = sHIT;
}
break;
case sGAME_OVER:
// There is no other state to transition to
next_state = sGAME_OVER;
break;
case sGUN_COOLDOWN:
if ((mils - game_start_time) >= game_duration) { // Transition from 6-5
make_sound(GAME_OVER);
set_vest_lights(OFF);
next_state = sGAME_OVER;
} else if (sensor_value >= vest_threshold) { // Transition 6-4
set_laser(LOW);
set_vest_lights(OFF);
make_sound(HIT);
report_hit();
deaths = deaths + 1;
saved_clock = mils;
next_state = sHIT;
} else if ((mils - saved_clock) >= shot_cooldown) { // Transition 6-2
set_vest_lights(ON);
next_state = sNEUTRAL;
} else {
next_state = sGUN_COOLDOWN;
}
break;
}
return next_state;
}