-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallweaponbox.cpp
374 lines (333 loc) · 11 KB
/
allweaponbox.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//===========================================================================//
// //
// Desc: Arduino Code to implement a fencing scoring apparatus //
// Dev: Wnew //
// Date: Nov 2012 //
// Updated: Sept 2015 //
// Notes: 1. Basis of algorithm from digitalwestie on github. Thanks Mate //
// 2. Used uint8_t instead of int where possible to optimise //
// 3. Set ADC prescaler to 16 faster ADC reads //
// //
// To do: 1. Could use shift reg on lights and mode LEDs to save pins //
// 2. Implement short circuit LEDs (already provision for it) //
// 3. Set up debug levels correctly //
// Original Source: //
// https://github.com/wnew/fencing_scoring_box //
//===========================================================================//
#include "allweaponbox.h"
#include "neopix-box.h"
#include <Adafruit_GFX.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_NeoMatrix.h>
//============
// #defines
//============
//TODO: set up debug levels correctly
#define DEBUG 1
//#define TEST_LIGHTS // turns on lights for a second on start up
//#define TEST_ADC_SPEED // used to test sample rate of ADCs
//#define REPORT_TIMING // prints timings over serial interface
#define BUZZERTIME 1000 // length of time the buzzer is kept on after a hit (ms)
#define LIGHTTIME 3000 // length of time the lights are kept on after a hit (ms)
#define BEEP_PIN 10
//==========================
// Lockout & Depress Times
//==========================
// the lockout time between hits for foil is 300ms +/-25ms
// the minimum amount of time the tip needs to be depressed for foil 14ms +/-1ms
// the lockout time between hits for epee is 45ms +/-5ms (40ms -> 50ms)
// the minimum amount of time the tip needs to be depressed for epee 2ms
// the lockout time between hits for sabre is 170ms +/-10ms
// the minimum amount of time the tip needs to be depressed (in contact) for sabre 0.1ms -> 1ms
// These values are stored as micro seconds for more accuracy
// foil epee sabre
const long lockout [] = {300000, 45000, 170000}; // the lockout time between hits
const long depress [] = { 14000, 2000, 1000}; // the minimum amount of time the tip needs to be depressed
//================
// Configuration
//================
void AllWeaponBox::setup(Stream *arg_out, int arg_beep)
{
out = arg_out;
beep_pin = arg_beep;
if (out) {
out->println("3 Weapon Scoring Box");
out->println("====================");
out->print ("Weapon: ");
out->println(WEAPON_TXT[weapon]);
}
resetValues();
}
//============
// Main Loop
//============
void AllWeaponBox::loop(const Blade &a, const Blade &b)
{
if (weapon == FOIL)
foil(a, b);
else if (weapon == EPEE)
epee(a, b);
else if (weapon == SABRE)
sabre(a, b);
signalHits(a, b);
}
//=====================
// Mode pin interrupt
//=====================
static void AllWeaponBox::setWeapon(const WEAPON arg) {
weapon = arg;
resetGame();
}
//===================
// Main foil method
//===================
void AllWeaponBox::foil(const Blade &a, const Blade &b) {
long now = micros();
if (((hitOnTargA || hitOffTargA) && (depressAtime + lockout[0] < now)) ||
((hitOnTargB || hitOffTargB) && (depressBtime + lockout[0] < now))) {
lockedOut = true;
}
// weapon A
if (hitOnTargA == false && hitOffTargA == false) { // ignore if A has already hit
// off target
if (900 < a.weapon && b.lame < 100) {
if (!depressedA) {
depressAtime = micros();
depressedA = true;
} else {
if (depressAtime + depress[0] <= micros()) {
hitOffTargA = true;
}
}
} else {
// on target
if (400 < a.weapon && a.weapon < 600 && 400 < b.lame && b.lame < 600) {
if (!depressedA) {
depressAtime = micros();
depressedA = true;
} else {
if (depressAtime + depress[0] <= micros()) {
hitOnTargA = true;
}
}
} else {
// reset these values if the depress time is short.
depressAtime = 0;
depressedA = 0;
}
}
}
// weapon B
if (hitOnTargB == false && hitOffTargB == false) { // ignore if B has already hit
// off target
if (900 < b.weapon && a.lame < 100) {
if (!depressedB) {
depressBtime = micros();
depressedB = true;
} else {
if (depressBtime + depress[0] <= micros()) {
hitOffTargB = true;
}
}
} else {
// on target
if (400 < b.weapon && b.weapon < 600 && 400 < a.lame && a.lame < 600) {
if (!depressedB) {
depressBtime = micros();
depressedB = true;
} else {
if (depressBtime + depress[0] <= micros()) {
hitOnTargB = true;
}
}
} else {
// reset these values if the depress time is short.
depressBtime = 0;
depressedB = 0;
}
}
}
}
//===================
// Main epee method
//===================
void AllWeaponBox::epee(const Blade &a, const Blade &b) {
long now = micros();
if ((hitOnTargA && (depressAtime + lockout[1] < now)) || (hitOnTargB && (depressBtime + lockout[1] < now))) {
lockedOut = true;
}
// weapon A
// no hit for A yet && weapon depress && opponent lame touched
if (hitOnTargA == false) {
if (a.lame > 300 && b.ground < 300) {
if (!depressedA) {
depressAtime = micros();
depressedA = true;
} else {
if (depressAtime + depress[1] <= micros()) {
hitOnTargA = true;
}
}
} else {
// reset these values if the depress time is short.
if (depressedA == true) {
depressAtime = 0;
depressedA = 0;
}
}
}
// weapon B
// no hit for B yet && weapon depress && opponent lame touched
if (hitOnTargB == false) {
if (b.lame > 300 && a.ground < 300) {
if (!depressedB) {
depressBtime = micros();
depressedB = true;
} else {
if (depressBtime + depress[1] <= micros()) {
hitOnTargB = true;
}
}
} else {
// reset these values if the depress time is short.
if (depressedB == true) {
depressBtime = 0;
depressedB = 0;
}
}
}
}
//===================
// Main sabre method
//===================
void AllWeaponBox::sabre(const Blade &a, const Blade &b) {
long now = micros();
if (((hitOnTargA || hitOffTargA) && (depressAtime + lockout[2] < now)) ||
((hitOnTargB || hitOffTargB) && (depressBtime + lockout[2] < now))) {
lockedOut = true;
}
// weapon A
if (hitOnTargA == false && hitOffTargA == false) { // ignore if A has already hit
// on target
if (400 < a.weapon && a.weapon < 600 && 400 < b.lame && b.lame < 600) {
if (!depressedA) {
depressAtime = micros();
depressedA = true;
} else {
if (depressAtime + depress[2] <= micros()) {
hitOnTargA = true;
}
}
} else {
// reset these values if the depress time is short.
depressAtime = 0;
depressedA = 0;
}
}
// weapon B
if (hitOnTargB == false && hitOffTargB == false) { // ignore if B has already hit
// on target
if (400 < b.weapon && b.weapon < 600 && 400 < a.lame && a.lame < 600) {
if (!depressedB) {
depressBtime = micros();
depressedB = true;
} else {
if (depressBtime + depress[2] <= micros()) {
hitOnTargB = true;
}
}
} else {
// reset these values if the depress time is short.
depressBtime = 0;
depressedB = 0;
}
}
}
//==============
// Signal Hits
//==============
void AllWeaponBox::signalHits(const Blade &a, const Blade &b) {
// non time critical, this is run after a hit has been detected
if (lockedOut) {
#ifdef DEBUG
// read analog pins
out->print(" A.gnd: "); out->print(a.ground); out->print(" A.wea: "); out->print(a.weapon);
out->print(" A.lame: "); out->println(a.lame);
out->print(" B.gnd: "); out->print(b.ground); out->print(" B.wea: "); out->print(b.weapon);
out->print(" B.lame: "); out->println(b.lame);
out->print(" A: hitOn:"); out->print(hitOnTargA); out->print(" A: hitOff:"); out->println(hitOffTargA);
out->print(" B: hitOn:"); out->print(hitOnTargB); out->print(" B: hitOff:"); out->println(hitOffTargB);
out->print(" A: Score:"); out->print(scoreA); out->print(" B: Score:"); out->println(scoreB);
out->print(" LockedOut: "); out->println(lockedOut);
#endif
// Beep?
if (beep_on)
digitalWrite(BEEP_PIN, HIGH);
// Fill the screen with multiple levels of white to gauge the quality
leds->clear();
if (hitOnTargA) {
leds->fillRect(0,0, 8,8, LED_RED_HIGH);
scoreA +=1;
}
if (hitOnTargB) {
leds->fillRect(8,0, 8,8, LED_GREEN_HIGH);
scoreB +=1;
}
leds->show();
resetValues();
}
}
void AllWeaponBox::resetGame() {
scoreA = 0;
scoreB = 0;
resetValues();
};
//======================
// Reset all variables
//======================
void AllWeaponBox::resetValues() {
if (beep_on) {
delay(BUZZERTIME); // wait before turning off the buzzer
digitalWrite(BEEP_PIN, LOW);
}
delay(LIGHTTIME-BUZZERTIME); // wait before turning off the lights
leds->clear();
leds->show();
showScore();
lockedOut = false;
depressAtime = 0;
depressedA = false;
depressBtime = 0;
depressedB = false;
hitOnTargA = false;
hitOffTargA = false;
hitOnTargB = false;
hitOffTargB = false;
delay(100);
}
//======================
// Reset all variables
//======================
void AllWeaponBox::showScore() {
leds->clear();
leds->setTextWrap(false); // we don't wrap text so it scrolls nicely
leds->setTextSize(1);
leds->setRotation(0);
if (scoreA>scoreB)
leds->setTextColor(LED_WHITE_HIGH);
else
leds->setTextColor(LED_BLUE_MEDIUM);
leds->setCursor(2,0);
leds->print(scoreA);
if (scoreB>scoreA)
leds->setTextColor(LED_WHITE_HIGH);
else
leds->setTextColor(LED_BLUE_MEDIUM);
leds->setCursor(10,0);
leds->print(scoreB);
leds->show();
delay(LIGHTTIME-BUZZERTIME); // wait before turning off the lights
leds->clear();
leds->show();
delay(100);
}