-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
595 lines (476 loc) · 15.9 KB
/
main.c
File metadata and controls
595 lines (476 loc) · 15.9 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/**********************************************************************/
//** Lab 02
//** Name: Joshua Miller
//** RIN: 662039082
//** Name: Philip Paterson
//** RIN: 662031059
/**********************************************************************/
#include "engr2350_msp432.h"
// Consts
typedef enum _RGB_LED_COLOR {
RGB_LED_OFF = -1,
RGB_LED_RED = 0,
RGB_LED_GREEN = 1,
RGB_LED_BLUE = 2,
RGB_LED_MAGENTA = 3,
RGB_LED_CYAN = 4,
RGB_LED_YELLOW = 5
} RGB_LED_COLOR;
typedef enum _BILED_COLOR {
BILED_OFF = -1,
BILED_RED = 0,
BILED_GREEN = 1
} BILED_COLOR;
uint8_t seed = 1;
// Initializations
void GPIO_Init();
void Timers_Init();
void Pattern_Init();
// Helper functions
void wait_seconds(uint8_t seconds);
void set_RGB_LED(RGB_LED_COLOR color);
void set_RGB_LED_by_BMP();
void set_BILED(BILED_COLOR color);
void show_pattern(uint8_t removed);
// State Functions
void show_colors_state();
void test_buttons_state();
void input_colors_state();
void game_done_state(BILED_COLOR color);
// Tests
void wait_5_seconds_test();
void RGB_LED_color_test();
void RGB_LED_by_BMP_test();
void BILED_color_test();
void pb_interrupt_test();
void bmp_interrupt_test();
void pattern_init_test();
// Interrupts
void timer50ms_interrupt();
void same_port_interrupt();
void bmp_interrupt();
void pb_interrupt();
// Pins
uint8_t RGB_LED_PORT = GPIO_PORT_P2;
uint8_t RGB_LED_PINS[] = {GPIO_PIN0, GPIO_PIN1, GPIO_PIN2};
uint8_t BILED_PORT = GPIO_PORT_P3;
uint8_t BILED_PINS[] = {GPIO_PIN2, GPIO_PIN3};
uint8_t PB_PORT_PIN[] = {GPIO_PORT_P4, GPIO_PIN1};
uint8_t BMP_PORT = GPIO_PORT_P4;
uint8_t BMP_PINS[] = {GPIO_PIN0, GPIO_PIN2, GPIO_PIN3, GPIO_PIN5, GPIO_PIN6, GPIO_PIN7};
// Globals
uint8_t count_50ms; // How many 50ms have passed
Timer_A_UpModeConfig timerConfig50ms;
uint8_t pb_pressed;
int8_t bmp_pressed;
uint8_t state; // What program state we in?
uint8_t reminders_left;
uint8_t removed_patterns;
RGB_LED_COLOR color_pattern[5];
int main (void) /* Main Function */
{
SysInit();
GPIO_Init();
Timers_Init();
srand(seed);
//bmp_interrupt_test();
while(1){ // Loop per game
// Initialize variables
pb_pressed = 0;
bmp_pressed = -1;
state = 0;
reminders_left = 1;
removed_patterns = 0;
Pattern_Init();
printf("playing instructions\r\n");
while (1) { // Main Game Loop
switch (state) { // What state we in?
case 0: // Start of game, waits till pb pressed
test_buttons_state();
break;
case 1: // Waiting 1 sec 'til game start
wait_seconds(1);
state = 2;
break;
case 2: // Blinky bloopy colors
show_colors_state();
break;
case 3:
input_colors_state();
break;
case 4:
game_done_state(BILED_RED);
break;
case 5:
game_done_state(BILED_GREEN);
break;
}
if (state == 6) { // New Game
set_BILED(BILED_OFF);
break;
}
}
}
}
// Init
void GPIO_Init() {
// Input Output
{
// RGB LED Boi
GPIO_setAsOutputPin(RGB_LED_PORT, RGB_LED_PINS[0] | RGB_LED_PINS[1] | RGB_LED_PINS[2]);
set_RGB_LED(RGB_LED_OFF);
// BILED Lad
GPIO_setAsOutputPin(BILED_PORT, BILED_PINS[0] | BILED_PINS[1]);
set_BILED(BILED_OFF);
// Pushbutton
GPIO_setAsInputPin(PB_PORT_PIN[0], PB_PORT_PIN[1]);
// Bumpers
GPIO_setAsInputPinWithPullUpResistor(BMP_PORT, BMP_PINS[0] | BMP_PINS[1] | BMP_PINS[2] | BMP_PINS[3] | BMP_PINS[4] | BMP_PINS[5]);
}
// Interrupt
{
GPIO_enableInterrupt(BMP_PORT, BMP_PINS[0] | BMP_PINS[1] | BMP_PINS[2] | BMP_PINS[3] | BMP_PINS[4] | BMP_PINS[5]);
GPIO_interruptEdgeSelect(BMP_PORT, BMP_PINS[0] | BMP_PINS[1] | BMP_PINS[2] | BMP_PINS[3] | BMP_PINS[4] | BMP_PINS[5], GPIO_LOW_TO_HIGH_TRANSITION);
GPIO_enableInterrupt(PB_PORT_PIN[0], PB_PORT_PIN[1]);
GPIO_interruptEdgeSelect(PB_PORT_PIN[0], PB_PORT_PIN[1], GPIO_HIGH_TO_LOW_TRANSITION);
if (BMP_PORT == PB_PORT_PIN[0]) { // If the port is the same for the two isrs
GPIO_registerInterrupt(BMP_PORT, same_port_interrupt);
} else { // Not the same, don't worry
GPIO_registerInterrupt(BMP_PORT, bmp_interrupt);
GPIO_registerInterrupt(PB_PORT_PIN[0], pb_interrupt);
}
}
}
void Timers_Init() {
timerConfig50ms = (Timer_A_UpModeConfig){
.clockSource = TIMER_A_CLOCKSOURCE_SMCLK,
.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_64,
.timerPeriod = 18750, // Update
.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_ENABLE,
.timerClear = TIMER_A_DO_CLEAR
};
Timer_A_configureUpMode(TIMER_A1_BASE, &timerConfig50ms);
Timer_A_registerInterrupt(TIMER_A1_BASE, TIMER_A_CCRX_AND_OVERFLOW_INTERRUPT, timer50ms_interrupt);
count_50ms = 0;
}
void Pattern_Init() {
uint8_t i;
for (i = 0; i < 5; i++) {
color_pattern[i] = (RGB_LED_COLOR) (rand(seed)%6);
}
}
// Helpers
void wait_seconds(uint8_t seconds){
Timer_A_stopTimer(TIMER_A1_BASE);
Timer_A_clearTimer(TIMER_A1_BASE);
count_50ms = 0;
Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);
while (seconds*20 > count_50ms) {} // 1000ms/50ms
Timer_A_stopTimer(TIMER_A1_BASE);
}
void set_RGB_LED(RGB_LED_COLOR color) {
switch (color) {
case RGB_LED_RED:
GPIO_setOutputHighOnPin(RGB_LED_PORT, RGB_LED_PINS[0]);
GPIO_setOutputLowOnPin(RGB_LED_PORT, RGB_LED_PINS[1]);
GPIO_setOutputLowOnPin(RGB_LED_PORT, RGB_LED_PINS[2]);
break;
case RGB_LED_GREEN:
GPIO_setOutputLowOnPin(RGB_LED_PORT, RGB_LED_PINS[0]);
GPIO_setOutputHighOnPin(RGB_LED_PORT, RGB_LED_PINS[1]);
GPIO_setOutputLowOnPin(RGB_LED_PORT, RGB_LED_PINS[2]);
break;
case RGB_LED_BLUE:
GPIO_setOutputLowOnPin(RGB_LED_PORT, RGB_LED_PINS[0]);
GPIO_setOutputLowOnPin(RGB_LED_PORT, RGB_LED_PINS[1]);
GPIO_setOutputHighOnPin(RGB_LED_PORT, RGB_LED_PINS[2]);
break;
case RGB_LED_MAGENTA:
GPIO_setOutputHighOnPin(RGB_LED_PORT, RGB_LED_PINS[0]);
GPIO_setOutputLowOnPin(RGB_LED_PORT, RGB_LED_PINS[1]);
GPIO_setOutputHighOnPin(RGB_LED_PORT, RGB_LED_PINS[2]);
break;
case RGB_LED_CYAN:
GPIO_setOutputLowOnPin(RGB_LED_PORT, RGB_LED_PINS[0]);
GPIO_setOutputHighOnPin(RGB_LED_PORT, RGB_LED_PINS[1]);
GPIO_setOutputHighOnPin(RGB_LED_PORT, RGB_LED_PINS[2]);
break;
case RGB_LED_YELLOW:
GPIO_setOutputHighOnPin(RGB_LED_PORT, RGB_LED_PINS[0]);
GPIO_setOutputHighOnPin(RGB_LED_PORT, RGB_LED_PINS[1]);
GPIO_setOutputLowOnPin(RGB_LED_PORT, RGB_LED_PINS[2]);
break;
default:
GPIO_setOutputLowOnPin(RGB_LED_PORT, RGB_LED_PINS[0]);
GPIO_setOutputLowOnPin(RGB_LED_PORT, RGB_LED_PINS[1]);
GPIO_setOutputLowOnPin(RGB_LED_PORT, RGB_LED_PINS[2]);
}
}
void set_RGB_LED_by_BMP() { // Whatever bmp is pressed is the color that is being colored
if (!GPIO_getInputPinValue(BMP_PORT, BMP_PINS[0])) {
set_RGB_LED(RGB_LED_RED);
} else if (!GPIO_getInputPinValue(BMP_PORT, BMP_PINS[1])) {
set_RGB_LED(RGB_LED_GREEN);
} else if (!GPIO_getInputPinValue(BMP_PORT, BMP_PINS[2])) {
set_RGB_LED(RGB_LED_BLUE);
} else if (!GPIO_getInputPinValue(BMP_PORT, BMP_PINS[3])) {
set_RGB_LED(RGB_LED_MAGENTA);
} else if (!GPIO_getInputPinValue(BMP_PORT, BMP_PINS[4])) {
set_RGB_LED(RGB_LED_CYAN);
} else if (!GPIO_getInputPinValue(BMP_PORT, BMP_PINS[5])) {
set_RGB_LED(RGB_LED_YELLOW);
} else {
set_RGB_LED(RGB_LED_OFF);
}
}
void set_BILED(BILED_COLOR color) {
switch (color) {
case BILED_OFF:
GPIO_setOutputLowOnPin(BILED_PORT, BILED_PINS[0]);
GPIO_setOutputLowOnPin(BILED_PORT, BILED_PINS[1]);
break;
case BILED_RED:
GPIO_setOutputHighOnPin(BILED_PORT, BILED_PINS[0]);
GPIO_setOutputLowOnPin(BILED_PORT, BILED_PINS[1]);
break;
case BILED_GREEN:
GPIO_setOutputLowOnPin(BILED_PORT, BILED_PINS[0]);
GPIO_setOutputHighOnPin(BILED_PORT, BILED_PINS[1]);
break;
}
}
void show_pattern(uint8_t removed) {
uint8_t pattern_on = 0;
while (pattern_on < 5-removed) { // While still colors to show
set_RGB_LED(color_pattern[pattern_on]); // Set the led to the color
Timer_A_clearTimer(TIMER_A1_BASE);
count_50ms = 0;
Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);
while (20 > count_50ms) { // Still within blink time
if (10 < count_50ms) { // Half way through blink (turn off)
set_RGB_LED(RGB_LED_OFF);
}
}
pattern_on++;
}
}
// State functions
void show_colors_state() {
uint8_t pattern_on = 0; // Reset variables
set_BILED(BILED_RED); // Tell it is showin colors
Timer_A_stopTimer(TIMER_A1_BASE);
show_pattern(removed_patterns);
// State closing
set_BILED(BILED_OFF);
set_RGB_LED(RGB_LED_OFF);
state = 3;
}
void test_buttons_state() {
pb_pressed = 0; // Reset pushbutton just in case
while (pb_pressed == 0) { // While the pb hasn't been pressed, show the bumper color
set_RGB_LED_by_BMP();
}
// Button Pressed
printf("Starting Game:\r\n"); // Maybe Temporary? Can we say it startin?
state = 1;
}
void input_colors_state() {
// Reset variables
uint8_t pattern_on = 0;
bmp_pressed = -1;
pb_pressed = 0;
Timer_A_stopTimer(TIMER_A1_BASE);
set_BILED(BILED_GREEN); // Tell user it input time
if (removed_patterns > 0 ) { // Not the first time?
// Clear & restart timer (just in case)
Timer_A_clearTimer(TIMER_A1_BASE);
count_50ms = 0;
Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);
while (1) {
set_RGB_LED_by_BMP();
if (reminders_left > 0) { // Still Have reminders?
if (pb_pressed == 1) { // PB Pressed (requesting reminder)
reminders_left--;
set_BILED(BILED_RED);
show_pattern(0);
state = 2;
break;
}
}
if (count_50ms < 60) { // Still within time limit? (3000ms/50ms)
if (bmp_pressed != -1) { // Button been fully pressed yet?
if (bmp_pressed == color_pattern[5-removed_patterns+pattern_on]) { // Correct button?
pattern_on++;
bmp_pressed = -1;
Timer_A_clearTimer(TIMER_A1_BASE);
count_50ms = 0;
if (pattern_on >= removed_patterns) { // we entered enough?
state = 2;
removed_patterns++;
set_RGB_LED(RGB_LED_OFF);
if (removed_patterns > 5) { // We win?
printf("You won, congrats!\r\n");
state = 5;
break;
}
printf("Correct, waiting 5sec\r\n");
set_BILED(BILED_RED);
wait_seconds(5);
break;
}
} else {
printf("Incorrect\r\n");
state = 4;
break;
}
} else {
set_RGB_LED_by_BMP();
}
} else {
state = 4;
printf("Took too long to enter\r\n");
break;
}
}
} else { // If there is no patters, we only wait 2 sec... thx
set_BILED(BILED_RED);
wait_seconds(2);
state = 2;
removed_patterns++;
}
// Ending state
Timer_A_stopTimer(TIMER_A1_BASE);
set_BILED(BILED_OFF);
set_RGB_LED(RGB_LED_OFF);
}
void game_done_state(BILED_COLOR color) {
pb_pressed = 0;
Timer_A_clearTimer(TIMER_A1_BASE);
count_50ms = 0;
Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);
while (pb_pressed == 0) { // 250ms/50ms
if (count_50ms >= 2*5) { // If blink has passed subtract that time
count_50ms -= 2*5;
}
if (count_50ms < 5) {
set_BILED(color);
} else {
set_BILED(BILED_OFF);
}
}
Timer_A_stopTimer(TIMER_A1_BASE);
state = 6;
}
// Tests
void wait_5_seconds_test(){
printf("Waiting 5 Seconds\r\n");
wait_seconds(5);
printf("5 Seconds have elapsed\r\n");
}
void RGB_LED_color_test() { // Go through the colors and test em
RGB_LED_COLOR i;
for (i = RGB_LED_OFF; i <= RGB_LED_YELLOW; i++) {
printf("ENUM POS: %d\r\n",i);
set_RGB_LED(i);
// Crude delay
uint32_t count;
for(count=10000000; count>0; count--);
}
}
void RGB_LED_by_BMP_test(){ // Just constantly show the color from bmp
while (1) {
set_RGB_LED_by_BMP();
}
}
void BILED_color_test() { // Show red then green
printf("RED\r\n");
set_BILED(BILED_RED);
// Crude delay
uint32_t count;
for(count=10000000; count>0; count--);
printf("GREEN\r\n");
set_BILED(BILED_GREEN);
// Crude delay
for(count=10000000; count>0; count--);
printf("OFF\r\n");
set_BILED(BILED_OFF);
}
void pb_interrupt_test() {
printf("Waiting for PB\r\n");
while (1) {
if (pb_pressed) {
printf("PB Pressed\r\n");
pb_pressed = 0;
}
}
}
void bmp_interrupt_test() {
while (1) {
if (bmp_pressed != -1) {
printf("BMP%d Pressed!", bmp_pressed);
bmp_pressed = -1;
}
}
}
void pattern_init_test() {
Pattern_Init();
printf("Pattern is: [");
uint8_t i;
for (i = 0; i < 5; i++) {
printf("%d", color_pattern[i]);
if (i != 4) printf(", ");
}
printf("]\r\n");
}
// Interrupts
void timer50ms_interrupt() {
Timer_A_clearInterruptFlag(TIMER_A1_BASE);
count_50ms++;
}
void same_port_interrupt() {
__delay_cycles(240e3); // Debounceage
uint8_t active_pins = GPIO_getEnabledInterruptStatus(GPIO_PORT_P4);
if(active_pins & PB_PORT_PIN[1]){ // Pushbutton?
GPIO_clearInterruptFlag(PB_PORT_PIN[0],PB_PORT_PIN[1]);
if(!GPIO_getInputPinValue(PB_PORT_PIN[0],PB_PORT_PIN[1])){
pb_pressed = 1;
}
} else {
uint8_t i;
for (i = 0; i < 6; i++) {
if(active_pins & BMP_PINS[i]){
GPIO_clearInterruptFlag(BMP_PORT, BMP_PINS[i]);
if(GPIO_getInputPinValue(BMP_PORT,BMP_PINS[i])){
bmp_pressed = i;
} else {
bmp_pressed = -1; // May need to remove
}
}
}
}
}
void bmp_interrupt() {
__delay_cycles(240e3); // Debounceage
uint8_t active_pins = GPIO_getEnabledInterruptStatus(GPIO_PORT_P4);
uint8_t i;
for (i = 0; i < 6; i++) {
if(active_pins & BMP_PINS[i]){
GPIO_clearInterruptFlag(BMP_PORT, BMP_PINS[i]);
if(GPIO_getInputPinValue(BMP_PORT,BMP_PINS[i])){
bmp_pressed = i;
} else {
bmp_pressed = -1; // May need to remove
}
}
}
}
void pb_interrupt() {
__delay_cycles(240e3); // Debounceage
uint8_t active_pins = GPIO_getEnabledInterruptStatus(PB_PORT_PIN[0]);
if(active_pins & PB_PORT_PIN[1]){
GPIO_clearInterruptFlag(PB_PORT_PIN[0],PB_PORT_PIN[1]);
if(!GPIO_getInputPinValue(PB_PORT_PIN[0],PB_PORT_PIN[1])){
pb_pressed = 1;
}
}
}