summaryrefslogtreecommitdiff
path: root/final_project/main.c
blob: 926cb9939e0f72dcf44d3755a196cbd366023e9f (plain)
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
/*
 * final_project.c
 *
 * Created: 4/3/2024 10:08:56 AM
 * Author : bsw9xd
 */ 
#define F_CPU 16000000UL

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdbool.h>

#include "serial.h"
#include "clock.h"


volatile int seconds_remaining; //will be used by ISR

#define SPEAKER_PORT PORTE
#define SPEAKER_PIN 4 
#define ELEMENT_COUNT 7




/** GETTING/DISPLAYING ELEMENTS
  * Two of the LEDs are connected to the RX1/TX1 pins, used for serial.
  * Because of this, there's 7 possible elements.
  * Here's how elements (guesses) are retrieved from buttons and displayed on LEDs:
  
  * If the button is on PINA, the element is set to the reading from PINA, 
  * unmodified (aside from the two invalid buttons being masked).
  * this allows easy LED outputting.
  
  * If the button is the single one connected to PORTE,
  * the 2nd bit is set. When displaying elements, we assume
  * the second bit is the middle PORTE LED.
  
  */

uint8_t get_button() {
	//debouncing is done by waiting for the user to stop pressing the button,
	//then waiting an amount of time for the bouncing to stop.
	const double debounce_wait = 100.0;
	if(~PINE & (1 << 6)) {
		while((~PINE & (1<<6)));
		_delay_ms(debounce_wait);
		return (1 << 2);
	}
	uint8_t porta_state = (~PINA) & ~(0b1100);
	if(porta_state) {
		while(PINA != 0xff);
		_delay_ms(debounce_wait);	
	}
	return porta_state;
}

//for documentation see above paragraph
void display_element(uint8_t element, unsigned int time) {
	uint8_t portd_state = 0;
	uint8_t porte_state = 0;
	if(element & (1 << 2)) porte_state = (1<<5);
	portd_state = element & ~(0b1100);
	
	PORTD ^= portd_state;
	PORTE ^= porte_state;
	beep(329.63, .25);
	_delay_ms(time * 1000);
	PORTD ^= portd_state;
	PORTE ^= porte_state;
}

void init_io() {
	//initilize IO registers
	//Buttons
	DDRA = 0x00;
	PORTA = 0xff;
	
	//LEDs
	DDRD = 0xff;
	PORTD = 0xff;
	
	//speaker and middle LED
	DDRE = (1 << 4) | (1 << 5); 
	PORTE = 0xff;	
}

int main(void) {
	cli();
	timer_init_ctc();
	init_io();
	usart_init();


	while(1) {	
		int level = 0;
	
		//get level
		usart_txstr(
		"SIMON GAME\n"
		"Enter your starting difficulty level:\n"
		"1. Easy\n"
		"2. Moderate\n"
		"3. Give me pain.");

		//ask until valid input
		while((level > 3) || (level < 1)) level = (int)(usart_rxt_blocking() - '0');
		
	
		//main simon game
		while(level < 3) {
			int display_time;
			int sets;
			int response_time;
			int elements_min;
			int elements_max;
			double score;
			uint8_t element_list[5]; // TODO
	
			//this is where the level properties are set depending on level
			switch(level) {
				case 1:
					sets = 3;
					display_time = 3;
					response_time = 5;
					elements_min = 3;
					elements_max = 5;
					break;
				case 2:
					sets = 4;
					display_time = 2;
					response_time = 7;
					elements_min = 3;
					elements_max = 10;
					break;
				case 3:
					sets = 5;
					display_time = 1;
					response_time = 10;
					elements_min = 5;
					elements_max = 15;
					break;
			}

	
			//it's easier to make a variable to count the number of guesses (max_score)
			//and increment current_score after each correct guess to calculate total score
			//as the number of elements per set scale.
			int max_score = 0;
			int current_score = 0; 
			for(int set = 0; set < sets; set++) {
				//scale from elements_min (first set) to elements_max (last set)
				int elements = elements_min + ceil(((elements_max - elements_min) / (float)(sets - 1)) * set);
				
				//randomly get, display elements
				for(int element = 0; element < elements; element++) {
					uint8_t element_bit = (rand() % ELEMENT_COUNT);
					// button 3 should never be pressed, so if 3 is randomly generated,
					// we make it the last LED. We only generate 7 potential elements. 
					if(element_bit == 3) element_bit = 7;  
					uint8_t this_element = 1 << element_bit;
					usart_txt('\n');
					usart_txt(element_bit + '0');
					usart_txt('\n');
					element_list[element] = this_element; //will be compared to guesses later
					display_element(this_element, display_time);
					
				}
				//get elements from buttono presses
				//we'll poll the timer to see if a second has passed
				//as timer only supports a max of 0xffff * (1024 / 16000000) seconds
				seconds_remaining = response_time;
				start_timer();
				uint16_t guess;
				for(int element = 0; element < elements; element++) {
					guess = 0;
					do {
						if(timer_done()) { //accounts for seconds passed
							seconds_remaining--;
							stop_timer(); //TODO only need one function
							start_timer();
							beep(261.63, .1);
						}
						else { guess = get_button(); }
					} while((!guess) && (seconds_remaining > 0));
					max_score++;
					if(guess == element_list[element]) {
						current_score++;
						correct_beep();
					}
					else {
						incorrect_beep();
					}
				}	
			}
			//where we check the score. Score is calculated per level.
			//If score under 80, we break back to the menu.
			score = (float)current_score / max_score;
			if(score >= .8) { 
				level++;
				usart_txstr("\nnext level\n"); 
			}
			else { 
				loose(); 
				break;
			}
		}
		if(level >= 3) win(); //you win if you get past level 3
	}
}

void correct_beep() {
	usart_txstr("\nCorrect!\n");
	beep(440.0, .1);
}

void incorrect_beep() {
	usart_txstr("\nIncorrect guess.\n");
	beep(261.62, .2);
	_delay_ms(25);
	beep(261.62, .2);
}

void win() {
	usart_txstr("\nYou beat the game!\n");
	beep(262., .5);
	beep(392., .5);
}

void loose() {
	usart_txstr("\nYou loose, try again?\n");
	beep(330., .5);
	beep(294., .5);
}

/** can handle specific frequencies for a durientation of time.
 * speaker_ms is caculated by taking the period, dividing by 2
 * (as we need to flip speaker state once per cycle)
 * then multiplies it by 1000 to convert to ms for _delay_ms.
 * We control how long its played by making it loop,
 * loop count is durientation of note / period. 
 * loop_count will be off by a max of 1 period.
 **/
void beep(double frequency, double durientation) {
	double speaker_ms = ((1.0 / frequency) / 2.0) * 1000.0; //TODO clean up
	int loop_count = durientation / (1.0 / frequency);
	for(unsigned int i = 0; i < loop_count; i++) {
		_delay_ms(speaker_ms);
		SPEAKER_PORT ^= (1 << SPEAKER_PIN);
	}
}