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
|
#include <math.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define CFG_DEBOUNCE_DELAY 200
#define CFG_MAX_GAME_LEN 512
#define CFG_DIFF_MIN 50
#define CFG_DIFF_CURVE 15
#define PINOUT_LED_BLU 0
#define PINOUT_SWC_BLU 1
#define PINOUT_LED_RED 2
#define PINOUT_SWC_RED 3
#define PINOUT_LED_YLW 4
#define PINOUT_SWC_YLW 5
#define PINOUT_LED_GRE 6
#define PINOUT_SWC_GRE 7
#define PINOUT_BUZZ A0
#define PINOUT_NOISE A1
#define TONE_BLU 659.26
#define TONE_YLW 277.18
#define TONE_RED 440.00
#define TONE_GRE 329.62
const int clockwise_leds[] = { PINOUT_LED_BLU, PINOUT_LED_YLW, PINOUT_LED_GRE, PINOUT_LED_RED };
const int clockwise_buttons[] = { PINOUT_SWC_BLU, PINOUT_SWC_YLW, PINOUT_SWC_GRE, PINOUT_SWC_RED };
const int clockwise_tones[] = { TONE_BLU, TONE_YLW, TONE_GRE, TONE_RED };
#define CFG_BN_COUNT 4
unsigned long bn_timings[CFG_BN_COUNT], led_timeouts[CFG_BN_COUNT];
unsigned char bn_state[CFG_BN_COUNT], bn_state_old[CFG_BN_COUNT], led_state[CFG_BN_COUNT];
unsigned long ignore_input_timing;
unsigned char ignore_input;
#define QUARTER_TL 0
#define QUARTER_TR 1
#define QUARTER_BL 2
#define QUARTER_BR 3
int turn_number, enter_number;
int combination[CFG_MAX_GAME_LEN];
typedef struct {
unsigned char index;
bool down;
unsigned long timestamp;
} bn_event;
void generate_combination() {
for (int i = 0; i < CFG_MAX_GAME_LEN; i++)
combination[i] = random(4);
}
void all_leds(unsigned char state) {
digitalWrite(PINOUT_LED_BLU, state);
digitalWrite(PINOUT_LED_GRE, state);
digitalWrite(PINOUT_LED_RED, state);
digitalWrite(PINOUT_LED_YLW, state);
}
void boot_animation() {
for (int i = 0; i < 8; i++) {
digitalWrite(clockwise_leds[i % 4], HIGH);
delay(100);
digitalWrite(clockwise_leds[i % 4], LOW);
}
for (int i = 0; i < 2; i++) {
all_leds(HIGH);
delay(200);
all_leds(LOW);
delay(200);
}
}
void setup() {
pinMode(PINOUT_LED_RED, OUTPUT);
pinMode(PINOUT_LED_GRE, OUTPUT);
pinMode(PINOUT_LED_YLW, OUTPUT);
pinMode(PINOUT_LED_BLU, OUTPUT);
pinMode(PINOUT_BUZZ, OUTPUT);
pinMode(PINOUT_SWC_RED, INPUT_PULLUP);
pinMode(PINOUT_SWC_GRE, INPUT_PULLUP);
pinMode(PINOUT_SWC_YLW, INPUT_PULLUP);
pinMode(PINOUT_SWC_BLU, INPUT_PULLUP);
pinMode(PINOUT_NOISE, INPUT); // random noise voor RNG
randomSeed(analogRead(PINOUT_NOISE));
delay(500);
boot_animation();
// laat de groene led knipperen tot de gebruiker die knop
// indrukt om het spel te laten beginnen
for (;;) {
digitalWrite(PINOUT_LED_GRE, millis() % 1000 < 500);
if(!digitalRead(PINOUT_SWC_GRE)) break;
}
generate_combination();
digitalWrite(PINOUT_LED_GRE, 0);
delay(500);
simon_says();
}
/**
* @brief difficulty calculator
*
* de uitvoer van deze functie wordt gebruikt om de timing van de simon_says()
* functie te berekenen. de timing is 2 * x voor de 'aan' periode, en x voor de
* tijd tussen de 'aan' periode's. x is de uitvoer van deze functie in
* milliseconden.
*/
unsigned int difficulty() {
/**
* resultaat van max(200 * 0.5^(x/a), b) waar
* x = lengte van huidige combinatie
* a = hoe snel het spel lastiger wordt
* en b = maximale lastigheid
*
* lagere waardes = lastiger
*/
return MAX((int)(200 * pow(0.5, (float) turn_number / CFG_DIFF_CURVE)), CFG_DIFF_MIN);
}
/** @brief laat de combinatie zien die moet worden nagespeeld */
void simon_says() {
ignore_input = true;
for (int i = 0; i <= turn_number; i++)
beep_quarter(combination[i]);
ignore_input = false;
}
void beep_quarter(unsigned char quarter) {
unsigned int diff = difficulty();
digitalWrite(clockwise_leds[quarter], HIGH);
tone(PINOUT_BUZZ, clockwise_tones[quarter]);
delay(2 * diff);
digitalWrite(clockwise_leds[quarter], LOW);
noTone(PINOUT_BUZZ);
delay(diff);
}
void bn_scan() {
memcpy(&bn_state_old, bn_state, sizeof(bn_state_old));
memset(&bn_state, 0, sizeof(bn_state));
for (int i = 0; i < CFG_BN_COUNT; i++)
bn_state[i] = !digitalRead(clockwise_buttons[i]);
}
void bad_press_routine() {
ignore_input = true;
unsigned long until = millis() + 2e3;
while (millis() < until) {
unsigned long offset = until - millis();
all_leds(offset % 300 < 150);
tone(PINOUT_BUZZ, 500 - (offset % 500));
}
noTone(PINOUT_BUZZ);
all_leds(LOW);
turn_number = 0;
enter_number = 0;
generate_combination();
delay(200);
simon_says();
ignore_input = false;
}
/** @brief wordt uitgevoerd voor elke toetsaanslag */
void check_press(int index) {
// verkeerde toets ingedrukt
if (combination[enter_number] != index) {
bad_press_routine();
return;
}
// als alle toetsen in de goede volgorde zijn ingetoetst
if(enter_number == turn_number) {
turn_number++;
// zorg dat er geen index out of bounds error kan gebeuren door uit te gaan
if (turn_number == CFG_MAX_GAME_LEN) {
all_leds(LOW);
for(;;) {}
}
// laat de nieuwe combinatie zien
enter_number = 0;
delay(200);
all_leds(LOW);
delay(500);
simon_says();
return;
}
// als nog niet alle toetsen ingetoetst zijn
enter_number++;
}
void bn_onevent(bn_event ev) {
// stop gelijk als ignore_input aan is of het een key up event is
if (ignore_input || !ev.down) return;
// debounce correctie
bool bounce = bn_timings[ev.index] + CFG_DEBOUNCE_DELAY > millis();
bn_timings[ev.index] = ev.timestamp;
if (bounce) return;
// als er een toets ingedrukt wordt
led_set_timeout(ev.index, 200);
tone(PINOUT_BUZZ, clockwise_tones[ev.index], 200);
ignore_input_for(200);
check_press(ev.index);
}
void bn_event_gen() {
for(int i = 0; i < CFG_BN_COUNT; i++) {
if (bn_state[i] == bn_state_old[i]) continue;
bn_event event = {
.index = (unsigned char) i,
.down = bn_state[i],
.timestamp = millis()
};
bn_onevent(event);
}
}
/** @brief helper voor timeout variabelen */
void set_timeout(unsigned char *state_ref, unsigned long *timeout_ref, unsigned long duration_millis) {
*timeout_ref = millis() + duration_millis;
*state_ref = 1;
}
void led_set_timeout(unsigned int led, unsigned long duration_millis) {
set_timeout(&led_state[led], &led_timeouts[led], duration_millis);
digitalWrite(clockwise_leds[led], HIGH);
}
void ignore_input_for(unsigned long duration_millis) {
set_timeout(&ignore_input, &ignore_input_timing, duration_millis);
}
/** @brief update alle timers */
void timer_update() {
unsigned long current_time = millis();
// led update
for (int i = 0; i < CFG_BN_COUNT; i++) {
if (led_timeouts[i] > current_time) continue;
if (led_state[i] == 0) continue;
led_state[i] = 0;
digitalWrite(clockwise_leds[i], LOW);
}
// input ignore mode
if (ignore_input == 1 && current_time > ignore_input_timing) ignore_input = 0;
}
void loop() {
bn_scan();
bn_event_gen();
timer_update();
}
|