-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdebounce.hh
63 lines (56 loc) · 1.94 KB
/
debounce.hh
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
#ifndef KORYUU_DEBOUNCE_HH
#define KORYUU_DEBOUNCE_HH
#include <yaal/requirements.hh>
#ifdef __YAAL__
#include <yaal/io/ports.hh>
#include <avr/interrupt.h>
namespace koryuu {
template<typename ButtonPort>
class DebouncedButton {
ButtonPort button;
uint8_t counter;
const uint8_t debounce_factor;
bool state;
volatile bool is_down;
public:
DebouncedButton(uint8_t db_factor = 16)
: button(), counter(0), debounce_factor(db_factor),
state(false), is_down(false)
{}
YAAL_INLINE("DBButton::set_mode()")
void set_mode(yaal::Mode mode)
{
button.mode = mode;
}
YAAL_INLINE("DBButton::debounce()")
void debounce()
{
bool curr_state = !button;
if (curr_state != state) {
if (++counter == debounce_factor) {
state = curr_state;
if (state) {
// Will be reset to false when read
// in read().
is_down = true;
}
counter = 0;
}
}
else {
counter = 0;
}
}
YAAL_INLINE("DBButton::read()")
bool read()
{
cli();
const bool ret = is_down;
is_down = false;
sei();
return ret;
}
};
}
#endif // __YAAL__
#endif // KORYUU_DEBOUNCE_HH