-
Notifications
You must be signed in to change notification settings - Fork 3
/
buttons.c
176 lines (157 loc) · 3.53 KB
/
buttons.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
/*
* This file is part of the W1209 firmware replacement project
* (https://github.com/mister-grumbler/w1209-firmware).
*
* 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, version 3.
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* Control functions for buttons.
* The EXTI2 interrupt (5) is used to get signal on changing buttons state.
*/
#include "buttons.h"
#include "stm8s003/gpio.h"
#include "menu.h"
/* Definition for buttons */
// Port C control input from buttons.
#define BUTTONS_PORT PC_IDR
// PC.3
#define BUTTON1_BIT 0x08
// PC.4
#define BUTTON2_BIT 0x10
// PC.5
#define BUTTON3_BIT 0x20
static unsigned char status;
static unsigned char diff;
/**
* @brief Configure approptiate pins of MCU as digital inputs. Set
* initial value of status by getting current state of buttons.
* Configure external interrupt for these inputs.
*/
void initButtons()
{
PC_CR1 |= BUTTON1_BIT | BUTTON2_BIT | BUTTON3_BIT;
PC_CR2 |= BUTTON1_BIT | BUTTON2_BIT | BUTTON3_BIT;
status = ~ (BUTTONS_PORT & (BUTTON1_BIT | BUTTON2_BIT | BUTTON3_BIT) );
diff = 0;
EXTI_CR1 |= 0x30; // generate interrupt on falling and rising front.
}
/**
* @brief Gets value of buttons status which was when last interrupt
* request being handled.
* @return status byte of buttons
*/
unsigned char getButton()
{
return status;
}
/**
* @brief
* @return
*/
unsigned char getButtonDiff()
{
return diff;
}
/**
* @brief
* @return
*/
bool getButton1()
{
return status & BUTTON1_BIT;
}
/**
* @brief
* @return
*/
bool getButton2()
{
return status & BUTTON2_BIT;
}
/**
* @brief
* @return
*/
bool getButton3()
{
return status & BUTTON3_BIT;
}
/**
* @brief
* @return
*/
bool isButton1()
{
if (diff & BUTTON1_BIT) {
diff &= ~BUTTON1_BIT;
return true;
}
return false;
}
/**
* @brief
* @return
*/
bool isButton2()
{
if (diff & BUTTON2_BIT) {
diff &= ~BUTTON2_BIT;
return true;
}
return false;
}
/**
* @brief
* @return
*/
bool isButton3()
{
if (diff & BUTTON3_BIT) {
diff &= ~BUTTON3_BIT;
return true;
}
return false;
}
/**
* @brief This function is button's interrupt request handler
* so keep it extremely small and fast.
*/
void EXTI2_handler() __interrupt (5)
{
unsigned char event;
diff = status ^ ~ (BUTTONS_PORT & (BUTTON1_BIT | BUTTON2_BIT | BUTTON3_BIT) );
status = ~ (BUTTONS_PORT & (BUTTON1_BIT | BUTTON2_BIT | BUTTON3_BIT) );
// Send appropriate event to menu.
if (isButton1() ) {
if (getButton1() ) {
event = MENU_EVENT_PUSH_BUTTON1;
} else {
event = MENU_EVENT_RELEASE_BUTTON1;
}
} else if (isButton2() ) {
if (getButton2() ) {
event = MENU_EVENT_PUSH_BUTTON2;
} else {
event = MENU_EVENT_RELEASE_BUTTON2;
}
} else if (isButton3() ) {
if (getButton3() ) {
event = MENU_EVENT_PUSH_BUTTON3;
} else {
event = MENU_EVENT_RELEASE_BUTTON3;
}
} else {
return;
}
feedMenu (event);
}