-
Notifications
You must be signed in to change notification settings - Fork 0
/
beat-the-buzzer.c
171 lines (141 loc) · 4.48 KB
/
beat-the-buzzer.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
/*
* Filename : beat-the-buzzer.c
* Author : Jon Cross
* Date : 17/08/2012
* Description : Main entry point and control loop beat the buzzer game.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301 USA.
*
*/
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/delay.h>
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
#include "wdt_drv.h"
#include "common.h"
#include "timer0.h"
#include "button-driver.h"
#include "lcd-driver.h"
#include "speaker-driver.h"
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
typedef uint8_t (*func_p)(uint8_t);
// LEDs on POP-168 board: PD2 (Di2) & PD4 (Di4) - Tided high
// Switches on POP-168 board: PD2 (Di2) & PD4 (Di4)
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
#define LED1 (1 << PD2)
#define LED2 (1 << PD4)
#define InitLED() (DDRD |= (LED1 | LED2))
#define Led1On() (PORTD |= LED1)
#define Led1Off() (PORTD &= ~LED1)
#define Led1Toggle() (PIND |= LED1)
#define Led2On() (PORTD |= LED2)
#define Led2Off() (PORTD &= ~LED2)
#define Led2Toggle() (PIND |= LED2)
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
uint8_t topMenu(uint8_t key);
uint8_t gameRunning(uint8_t key);
uint8_t gameOver(uint8_t key);
enum {
ST_TOP_MENU = 0,
ST_GAME_RUNNING,
ST_GAME_OVER,
ST_MAX
};
func_p states[ST_MAX] = {topMenu, gameRunning, gameOver};
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
#define Clear_prescaler() (CLKPR = (1<<CLKPCE),CLKPR = 0)
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
uint8_t topMenu(uint8_t key)
{
static int enter = 1;
if (enter) {
LCD_WriteLine(0, 16, "Beat The Buzzer ");
LCD_WriteLine(1, 16, "Press Start ");
enter = 0;
}
if (key == KEY_START) {
enter = 1;
return ST_GAME_RUNNING;
}
return ST_TOP_MENU;
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
uint8_t gameRunning(uint8_t key)
{
static int enter = 1;
if (enter) {
LCD_WriteLine(0, 16, "Beat The Buzzer ");
LCD_WriteLine(1, 16, "Lives Left: ");
enter = 0;
}
if (key == KEY_RESET) {
enter = 1;
return ST_TOP_MENU;
}
else if (key == KEY_LOOP) {
// TODO: Sound buzzer
}
return ST_GAME_RUNNING;
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
uint8_t gameOver(uint8_t key)
{
return ST_GAME_OVER;
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
int main (void)
{
uint8_t state = ST_TOP_MENU;
uint8_t nextstate = ST_TOP_MENU;
uint8_t key = KEY_NONE;
func_p pStateFunc = states[state];
/* disable watchdog */
wdt_reset();
Wdt_clear_flag();
Wdt_change_enable();
Wdt_stop();
Clear_prescaler();
InitLED();
Led1On();
Timer0_Init();
SPEAKER_Init();
BUTTON_Init();
LCD_Init();
sei();
while (1)
{
key = BUTTON_GetKey();
nextstate = pStateFunc(key);
if (nextstate != state) {
pStateFunc = states[nextstate];
state = nextstate;
}
} /* end of while(1) */
return 0;
}
/* EOF */