-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimon.c
280 lines (242 loc) · 6.53 KB
/
simon.c
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
/*
Copyright (C) 2024 Martin Saraceno
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#define NUMLEDS 4
#define MAXCOLS 128
#define DEBOUNCE_MS 44
// declartions
// classes
// LB
class LB {
private:
public:
volatile uint8_t* dd;
volatile uint8_t* port;
volatile uint8_t* pin;
uint8_t ddid;
uint8_t portid;
uint8_t pinid;
LB();
LB(volatile uint8_t* dd, uint8_t ddid,
volatile uint8_t* port, uint8_t portid,
volatile uint8_t* pin, uint8_t pinid);
virtual void enable() = 0;
bool get_state();
};
// LED
class LED : public LB {
private:
public:
LED();
LED(volatile uint8_t* dd, uint8_t ddid,
volatile uint8_t* port, uint8_t portid,
volatile uint8_t* pin, uint8_t pinid);
void enable();
void turn_on();
void turn_off();
};
// Button
class Button : public LB {
private:
public:
Button();
Button(volatile uint8_t* dd, uint8_t ddid,
volatile uint8_t* port, uint8_t portid,
volatile uint8_t* pin, uint8_t pinid);
void enable();
};
// functions
uint8_t random_number();
uint8_t wait_input(Button * buttons);
void led_score(LED * leds, uint8_t score);
void game_over(LED * leds, uint8_t score);
// definitions
//classes
// LB
LB::LB() {}
LB::LB(volatile uint8_t* dd, uint8_t ddid,
volatile uint8_t* port, uint8_t portid,
volatile uint8_t* pin, uint8_t pinid) : dd(dd), ddid(ddid),
port(port), portid(portid),
pin(pin), pinid(pinid) {}
bool LB::get_state() {
return !((*pin) & (1<<pinid));
}
//LED
LED::LED() {}
LED::LED(volatile uint8_t* dd, uint8_t ddid,
volatile uint8_t* port, uint8_t portid,
volatile uint8_t* pin, uint8_t pinid) : LB(dd, ddid,
port, portid,
pin, pinid) {
enable();
turn_on();
}
void LED::enable () {
*dd |= 1<<ddid;
}
void LED::turn_on() {
*port |= 1<<portid;
}
void LED::turn_off() {
*port &= ~(1<<portid);
}
// Button
Button::Button() {}
Button::Button (volatile uint8_t* dd, uint8_t ddid,
volatile uint8_t* port, uint8_t portid,
volatile uint8_t* pin, uint8_t pinid) : LB(dd, ddid,
port, portid,
pin, pinid) {
enable();
}
void Button::enable () {
*dd &= ~(1<<ddid);
*port |= 1<<portid;
}
// functions
uint8_t random_number() {
return rand()%NUMLEDS;
}
uint8_t wait_input(Button * buttons) {
uint8_t col;
bool exit = false;
while (1) {
for (uint8_t i=0; i<NUMLEDS; ++i) {
if (buttons[i].get_state()) {
_delay_ms(DEBOUNCE_MS);
col = i;
exit = true;
break;
}
}
if (exit) {
break;
}
}
return col;
}
void led_score(LED * leds, uint8_t score) {
uint8_t s10 = score/10;
uint8_t s1 = score - 10*s10;
for (uint8_t n=0; n<s10; ++n) {
leds[NUMLEDS-2].turn_on();
_delay_ms(200);
leds[NUMLEDS-2].turn_off();
_delay_ms(200);
}
for (uint8_t n=0; n<s1; ++n) {
leds[NUMLEDS-1].turn_on();
_delay_ms(200);
leds[NUMLEDS-1].turn_off();
_delay_ms(200);
}
}
void game_over(LED * leds, uint8_t score) {
for (uint8_t n=0; n<10; ++n) {
for (uint8_t i=0; i<NUMLEDS; ++i) {
leds[i].turn_on();
}
_delay_ms(100);
for (uint8_t i=0; i<NUMLEDS; ++i) {
leds[i].turn_off();
}
_delay_ms(100);
}
_delay_ms(500);
led_score(leds, score);
while (1) {}
}
// main
int main(void) {
uint16_t seed = 0;
bool exit = false;
uint8_t col;
uint8_t score = 0;
// set LEDs
LED leds[NUMLEDS];
leds[0] = LED(&DDRC, DDRC5,
&PORTC, PORTC5,
&PINC, PINC5);
leds[1] = LED(&DDRD, DDRD0,
&PORTD, PORTD0,
&PIND, PIND0);
leds[2] = LED(&DDRD, DDRD1,
&PORTD, PORTD1,
&PIND, PIND1);
leds[3] = LED(&DDRD, DDRD2,
&PORTD, PORTD2,
&PIND, PIND2);
// set buttons
Button buttons[NUMLEDS];
buttons[0] = Button(&DDRD, DDRD3,
&PORTD, PORTD3,
&PIND, PIND3);
buttons[1] = Button(&DDRD, DDRD4,
&PORTD, PORTD4,
&PIND, PIND4);
buttons[2] = Button(&DDRE, DDRE0,
&PORTE, PORTE0,
&PINE, PINE0);
buttons[3] = Button(&DDRE, DDRE1,
&PORTE, PORTE1,
&PINE, PINE1);
// set random seed by user
while(1) {
for (uint8_t i=0; i<NUMLEDS; ++i) {
exit |= buttons[i].get_state();
}
if (exit) {
break;
}
seed += 1;
}
srand(seed);
// turn off all LEDs
for (uint8_t i=0; i<NUMLEDS; ++i) {
leds[i].turn_off();
}
_delay_ms(1000);
// make initial color array
uint8_t sol[MAXCOLS];
for (uint8_t i=0; i<MAXCOLS; ++i) {
sol[i] = random_number();
}
while(1) {
// show colors
for (uint8_t i=0; i<score+1; ++i) {
leds[sol[i]].turn_on();
_delay_ms(500);
leds[sol[i]].turn_off();
_delay_ms(100);
}
// check colors
for (uint8_t i=0; i<score+1; ++i) {
col = wait_input(buttons);
if (col == sol[i]) {
leds[col].turn_on();
_delay_ms(100);
leds[col].turn_off();
_delay_ms(100);
} else {
game_over(leds, score);
}
}
_delay_ms(500);
score++;
}
return 0;
}