/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/button.cc

  • Committer: edam
  • Date: 2012-02-25 01:29:52 UTC
  • Revision ID: tim@ed.am-20120225012952-32q8gg07aovk3qxh
updated arduino.mk

Show diffs side-by-side

added added

removed removed

1
 
#include <Arduino.h>
2
 
#include <button.h>
3
 
 
4
 
 
5
 
Button::Button( int pin )
6
 
        :
7
 
        _pin( pin ),
8
 
        _last_state( digitalRead( pin )? true : false ),
9
 
        _last_millis( millis() )
10
 
{
11
 
}
12
 
 
13
 
 
14
 
void Button::add_event_at( int duration, int event_id )
15
 
{
16
 
        _press_events[ duration ] = event_id;
17
 
}
18
 
 
19
 
 
20
 
int Button::update()
21
 
{
22
 
        // reset event
23
 
        _event_id = 0;
24
 
 
25
 
        // get time
26
 
        int millis = ::millis();
27
 
 
28
 
        // get state and check for change
29
 
        bool state = digitalRead( _pin )? true : false;
30
 
        if( state != _last_state )
31
 
        {
32
 
                // if the button has become unpressed
33
 
                if( !state )
34
 
                {
35
 
                        // check through events to see if it had been pressed long enough
36
 
                        // to trigger one
37
 
                        int duration = millis - _last_millis;
38
 
                        for( std::map< int, int >::iterator i = _press_events.begin();
39
 
                                 i != _press_events.end(); i++ )
40
 
                        {
41
 
                                if( duration > i->first )
42
 
                                        _event_id = i->second;
43
 
                                else
44
 
                                        break;
45
 
                        }
46
 
                }
47
 
 
48
 
                // update last state
49
 
                _last_state = state;
50
 
                _last_millis = millis;
51
 
        }
52
 
 
53
 
        return _event_id;
54
 
}
55
 
 
56
 
 
57
 
int Button::get_triggered_event()
58
 
{
59
 
        return _event_id;
60
 
}