-
Notifications
You must be signed in to change notification settings - Fork 0
/
button-driver.c
143 lines (117 loc) · 3.59 KB
/
button-driver.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
/*
* Filename : button-driver.c
* Author : Jon Cross
* Date : 18/08/2012
* Description : Driver for the keys and game loop..
*
*
* 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/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <inttypes.h>
#include "common.h"
#include "button-driver.h"
#include "speaker-driver.h"
#include "timer0.h"
volatile char KEY = KEY_NONE;
volatile bool KEY_VALID = FALSE;
/* ------------------------------------------------------------------ *
*
* Buttons:-
* Start : PORTC1 (PCINT9)
* Reset : PORTC3 (PCINT11)
* Game Loop: PORTB0 (PCINT0)
*
* ------------------------------------------------------------------ */
#define PINC_MASK ((1<<PINC1) | (1<<PINC3))
#define PINB_MASK ((1<<PINB0))
char CountdownTimerHandle;
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void button_change_interrupt(void)
{
uint8_t key = KEY_NONE;
uint8_t buttons = ~PINC;
uint8_t loop = ~PINB;
/* check buttons */
if (buttons & (1<<PINC1)) {
key = KEY_START;
}
else if (buttons & (1<<PINC3)) {
key = KEY_RESET;
}
else if (loop & (1<<PINB0)) {
/* check game loop */
key = KEY_LOOP;
}
/* TODO: Handle debounce!! */
if (key != KEY_NONE) {
if (!KEY_VALID) {
/* Store key in global key buffer */
KEY = key;
KEY_VALID = TRUE;
SPEAKER_Beep();
}
}
/* Delete pin change interrupt flags */
PCIFR = (1<<PCIF1) | (1<<PCIF0);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
ISR(PCINT0_vect)
{
button_change_interrupt();
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
ISR(PCINT1_vect)
{
button_change_interrupt();
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void BUTTON_Init(void)
{
/* setup port directions */
DDRB &= ~(PINB_MASK);
PORTB |= PINB_MASK; /* enable pullup */
DDRC &= ~(PINC_MASK);
/* setup interrupts */
//PCMSK0 |= (1<<PCINT0);
PCMSK1 |= ((1<<PCINT9) | (1<<PCINT11));
PCIFR = (1<<PCIF1) | (1<<PCIF0);
PCICR = (1<<PCIE1) | (1<<PCIE1);
CountdownTimerHandle = Timer0_AllocateCountdownTimer();
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
uint8_t BUTTON_GetKey(void)
{
uint8_t k = KEY_NONE;
cli();
/* check for unread key in buffer */
if (KEY_VALID) {
k = KEY;
KEY_VALID = FALSE;
}
sei();
return k;
}
/* EOF */