/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 14:54:33 UTC
  • Revision ID: edam@waxworlds.org-20120225145433-kih8qs45x05cum46
removed Bounce library and updated/fixed new code

Show diffs side-by-side

added added

removed removed

29
29
        _pin( pin ),
30
30
        _state( digitalRead( pin )? false : true ),
31
31
        _state_millis( ::millis() ),
32
 
        _state_duration( 0 ),
33
 
        _pending_event( 0 )
34
 
{
35
 
}
36
 
 
37
 
 
38
 
void Button::set_event_times( int event_times[] )
39
 
{
40
 
        _event_times = event_times;
41
 
}
42
 
 
43
 
 
44
 
void Button::update()
45
 
{
 
32
        _state_duration( 0 )
 
33
{
 
34
}
 
35
 
 
36
 
 
37
void Button::add_event_at( int duration, int event_id )
 
38
{
 
39
        _press_events[ duration ] = event_id;
 
40
}
 
41
 
 
42
 
 
43
int Button::update()
 
44
{
 
45
        // reset event
 
46
        _event_id = 0;
 
47
 
46
48
        // get time
47
49
        unsigned long millis = ::millis();
48
50
 
56
58
        }
57
59
 
58
60
        // if button has been pressed for any amount of time
59
 
        if( state && millis > _state_millis )
 
61
        if( state && _state_millis < millis )
60
62
        {
61
63
                // calculate new duration for this state
62
64
                unsigned long duration = millis - _state_millis;
63
65
 
64
66
                // check through events to see if the button has been
65
67
                // pressed long enough to trigger one
66
 
                for( int a = 0; _event_times[ a ]; a++ )
 
68
                for( std::map< int, int >::iterator i = _press_events.begin();
 
69
                         i != _press_events.end(); i++ )
67
70
                {
68
71
                        // if this event is in the future, we can stop looking
69
 
                        if( duration < (unsigned long)_event_times[ a ] )
 
72
                        if( duration < (unsigned long)i->first )
70
73
                                break;
71
74
 
72
75
                        // if this event happened since the previous update, we
73
76
                        // can trigger it
74
 
                        if( _state_duration < (unsigned long)_event_times[ a ] )
75
 
                                _pending_event = a + 1;
 
77
                        if( _state_duration < (unsigned long)i->first )
 
78
                                _event_id = i->second;
76
79
                }
77
80
 
78
81
                // update the duration we've accounted for
79
82
                _state_duration = duration;
80
83
        }
 
84
 
 
85
        return _event_id;
81
86
}
82
87
 
83
88
 
84
 
int Button::get_event()
 
89
int Button::get_triggered_event()
85
90
{
86
 
        int event = _pending_event;
87
 
        _pending_event = 0;
88
 
        return event;
 
91
        return _event_id;
89
92
}