/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-28 16:50:26 UTC
  • Revision ID: edam@waxworlds.org-20120228165026-pwnwo300xx2e2kg6
removed ulibc, fixed button, added text rendering

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
 
{
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
 
 
 
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
{
48
46
        // get time
49
47
        unsigned long millis = ::millis();
50
48
 
58
56
        }
59
57
 
60
58
        // if button has been pressed for any amount of time
61
 
        if( state && _state_millis < millis )
 
59
        if( state && millis > _state_millis )
62
60
        {
63
61
                // calculate new duration for this state
64
62
                unsigned long duration = millis - _state_millis;
65
63
 
66
64
                // check through events to see if the button has been
67
65
                // pressed long enough to trigger one
68
 
                for( std::map< int, int >::iterator i = _press_events.begin();
69
 
                         i != _press_events.end(); i++ )
 
66
                for( int a = 0; _event_times[ a ]; a++ )
70
67
                {
71
68
                        // if this event is in the future, we can stop looking
72
 
                        if( duration < (unsigned long)i->first )
 
69
                        if( duration < (unsigned long)_event_times[ a ] )
73
70
                                break;
74
71
 
75
72
                        // if this event happened since the previous update, we
76
73
                        // can trigger it
77
 
                        if( _state_duration < (unsigned long)i->first )
78
 
                                _event_id = i->second;
 
74
                        if( _state_duration < (unsigned long)_event_times[ a ] )
 
75
                                _pending_event = a + 1;
79
76
                }
80
77
 
81
78
                // update the duration we've accounted for
82
79
                _state_duration = duration;
83
80
        }
84
 
 
85
 
        return _event_id;
86
81
}
87
82
 
88
83
 
89
 
int Button::get_triggered_event()
 
84
int Button::get_event()
90
85
{
91
 
        return _event_id;
 
86
        int event = _pending_event;
 
87
        _pending_event = 0;
 
88
        return event;
92
89
}