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
|
/*
* Xytronic LF-1600
* Button routines
*
* Copyright (c) 2015 Michael Buesch <m@bues.ch>
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "buttons.h"
#include "timer.h"
#include <string.h>
#define BUTTONS_DDR DDRB
#define BUTTONS_PORT PORTB
#define BUTTONS_PIN PINB
#define BUTTON_BIT_IRON PB0
#define BUTTON_BIT_SET PB2
#define BUTTON_BIT_MINUS PB3
#define BUTTON_BIT_PLUS PB4
#define BUTTONS_MASK ((uint8_t)((1 << BUTTON_BIT_IRON) |\
(1 << BUTTON_BIT_SET) |\
(1 << BUTTON_BIT_MINUS) |\
(1 << BUTTON_BIT_PLUS)))
#define BUTTONS_DEBOUNCE_MS 10
#define BUTTONS_ACTIVE_LOW 1
struct buttons_context {
uint8_t prev_state;
struct timer debounce_timer;
button_handler_t handlers[NR_BUTTONS];
};
static struct buttons_context buttons;
static const uint8_t __flash button_id_to_port_mask[] = {
[BUTTON_SET] = 1u << BUTTON_BIT_SET,
[BUTTON_MINUS] = 1u << BUTTON_BIT_MINUS,
[BUTTON_PLUS] = 1u << BUTTON_BIT_PLUS,
[BUTTON_IRON] = 1u << BUTTON_BIT_IRON,
};
static uint8_t buttons_get_raw(void)
{
uint8_t state;
mb();
state = BUTTONS_PIN;
if (BUTTONS_ACTIVE_LOW)
state = (uint8_t)~state;
state = (uint8_t)(state & BUTTONS_MASK);
return state;
}
void buttons_register_handler(enum button_id button,
button_handler_t handler)
{
buttons.handlers[button] = handler;
}
uint8_t button_is_pressed(enum button_id button)
{
uint8_t state, mask;
mask = button_id_to_port_mask[button];
state = buttons_get_raw();
return state & mask;
}
void buttons_work(void)
{
uint8_t state, pos_edge, neg_edge, mask;
int8_t i;
button_handler_t handler;
if (!timer_expired(&buttons.debounce_timer))
return;
timer_add(&buttons.debounce_timer, BUTTONS_DEBOUNCE_MS);
state = buttons_get_raw();
pos_edge = (state & ~buttons.prev_state) & BUTTONS_MASK;
neg_edge = (~state & buttons.prev_state) & BUTTONS_MASK;
buttons.prev_state = state;
for (i = 0; i < NR_BUTTONS; i++) {
mask = button_id_to_port_mask[i];
handler = buttons.handlers[i];
if (handler) {
if (pos_edge & mask)
handler((enum button_id)i, BSTATE_POSEDGE);
else if (neg_edge & mask)
handler((enum button_id)i, BSTATE_NEGEDGE);
if (state & mask)
handler((enum button_id)i, BSTATE_PRESSED);
}
}
}
void buttons_init(void)
{
BUTTONS_DDR &= (uint8_t)~BUTTONS_MASK;
BUTTONS_PORT |= BUTTONS_MASK;
_delay_ms(50);
timer_set_now(&buttons.debounce_timer);
}
|