forked from benlaurie/arduino--
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushbutton.h
78 lines (62 loc) · 1.92 KB
/
pushbutton.h
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
// -*- mode: c++; indent-tabs-mode: nil; -*-
#ifndef PUSHBUTTON_H_
#define PUSHBUTTON_H_
#include "arduino--.h"
template<class Timer_, class Pin_, int debounce_>
class PushButton
{
public:
enum event_type
{
none,
keyup,
keydown
};
static void init()
{
Pin_::modeInput();
// aktivate pullup
Pin_::set();
previous_ = !Pin_::read();
changed_ = Timer_::millis();
duration_ = 0;
}
static event_type read()
{
const typename Timer_::time_res_t now = Timer_::millis();
// The button is active low
const bool pressed = !Pin_::read();
// If the switch changed, due to noise or pressing...
if (pressed != previous_)
{
// reset the debouncing timer
previous_ = pressed;
const typename Timer_::time_res_t delta = now - changed_;
changed_ = now;
if (delta > debounce_)
{
if (pressed)
{
duration_ = now;
return keydown;
}
duration_ = now - duration_;
return keyup;
}
}
return none;
}
// duration is only valid after a keyup event
static typename Timer_::time_res_t duration() { return duration_; }
private:
static bool previous_;
static typename Timer_::time_res_t changed_;
static typename Timer_::time_res_t duration_;
};
template<class Timer_, class Pin_, int debounce_>
bool PushButton<Timer_, Pin_, debounce_>::previous_ = false;
template<class Timer_, class Pin_, int debounce_>
typename Timer_::time_res_t PushButton<Timer_, Pin_, debounce_>::changed_ = 0;
template<class Timer_, class Pin_, int debounce_>
typename Timer_::time_res_t PushButton<Timer_, Pin_, debounce_>::duration_ = 0;
#endif