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
|
#include <Arduino.h>
#include <button.h>
Button::Button( int pin )
:
_pin( pin ),
_last_state( digitalRead( pin )? true : false ),
_last_millis( millis() )
{
}
void Button::add_event_at( int duration, int event_id )
{
_press_events[ duration ] = event_id;
}
int Button::update()
{
// reset event
_event_id = 0;
// get time
int millis = ::millis();
// get state and check for change
bool state = digitalRead( _pin )? true : false;
if( state != _last_state )
{
// if the button has become unpressed
if( !state )
{
// check through events to see if it had been pressed long enough
// to trigger one
int duration = millis - _last_millis;
for( std::map< int, int >::iterator i = _press_events.begin();
i != _press_events.end(); i++ )
{
if( duration > i->first )
_event_id = i->second;
else
break;
}
}
// update last state
_last_state = state;
_last_millis = millis;
}
return _event_id;
}
int Button::get_triggered_event()
{
return _event_id;
}
|