forked from jkemble/radiopixel-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.h
53 lines (46 loc) · 1.04 KB
/
Button.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
struct Button
{
explicit Button( int _pin )
: pin( _pin ), down( false ), pressed( 0 ), released( 0 )
{
}
void update( )
{
bool _down = ( digitalRead( pin ) == LOW );
if ( _down != down )
{
down = _down;
if ( _down )
{
pressed = millis();
}
else
{
released = millis();
}
}
}
//! returns duration of most recent button press, if not yet handled
time_t duration( ) const
{
if ( down || ( pressed == 0 ) || ( released == 0 ) ||
( pressed >= released ) ||
( ( released - pressed ) < 50 ) ) // debounce
{
return 0;
}
return released - pressed;
}
//! indicates most recent press was now handled
void clear( )
{
pressed = released = 0;
}
void initializeButton( )
{
digitalWrite(pin, HIGH);
}
int pin;
bool down;
time_t pressed, released;
};