/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: Tim Marston
  • Date: 2012-05-09 20:36:07 UTC
  • Revision ID: tim@ed.am-20120509203607-5sh14qikxjmm6p3y
updated arduino.mk

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
Button::Button( int pin )
28
28
        :
29
29
        _pin( pin ),
30
 
        _state_last( false ),
31
 
        _millis_state( 0 ),
32
 
        _millis_done( 0 ),
 
30
        _state( false ),
 
31
        _state_millis( 0 ),
 
32
        _state_duration_done( 0 ),
33
33
        _pending_event( 0 ),
34
 
        _interim_event( 0 ),
35
 
        _send_interim( true ),
 
34
        _send_interim( false ),
36
35
        _ignore_next_unpress( false )
37
36
{
38
37
}
50
49
        // to not sending interim presses, we need to ignore the next unpress, or
51
50
        // this button press may trigger events while being presses *and* events
52
51
        // when it is unpressed.
53
 
        if( _state_last && _send_interim && !send_interim )
 
52
        if( _state && _send_interim && !send_interim )
54
53
                _ignore_next_unpress = true;
55
54
 
56
55
        // set mode
63
62
        // get time
64
63
        unsigned long millis = ::millis();
65
64
 
66
 
        // get button state
 
65
        // get state and check for change
67
66
        bool state = digitalRead( _pin )? false : true;
68
 
 
69
 
        // if the button is pressed
70
 
        if( state )
 
67
        if( state != _state )
71
68
        {
72
 
                // if it has just been pressed, record the time now and reset how much
73
 
                // of the press we have already generated events for
74
 
                if( !_state_last ) {
75
 
                        _millis_state = millis;
76
 
                        _millis_done = 0;
 
69
                // if we're not sending interim presses, we care about when the button
 
70
                // is being unpressed
 
71
                if( !_send_interim && !state )
 
72
                {
 
73
                        // are we being told to ignore the next unpress?
 
74
                        if( _ignore_next_unpress )
 
75
                                _ignore_next_unpress = false;
 
76
                        else
 
77
                        {
 
78
                                // check through events to see if the button was pressed long
 
79
                                // enough to trigger one
 
80
                                for( int a = 0; _event_times[ a ]; a++ )
 
81
                                {
 
82
                                        // if this event is in the future, we can stop looking
 
83
                                        if( _state_millis < (unsigned long)_event_times[ a ] )
 
84
                                                break;
 
85
                                        
 
86
                                        // trigger it
 
87
                                        _pending_event = a + 1;
 
88
                                }
 
89
                        }
77
90
                }
78
 
 
79
 
                // calculate time that has elapsed in total this press
80
 
                unsigned long elapsed = millis - _millis_state;
81
 
 
82
 
                // look through the events to see if any need to be triggered
 
91
                
 
92
                // reset state
 
93
                _state = state;
 
94
                _state_millis = millis;
 
95
                _state_duration_done = 0;
 
96
        }
 
97
 
 
98
        // if we are sending interim presses, we care about all the time that the
 
99
        // button is being pressed
 
100
        if( _send_interim && state )
 
101
        {
 
102
                // calculate new duration for this state
 
103
                unsigned long duration = millis - _state_millis;
 
104
 
 
105
                // check through events to see if the button has been pressed long
 
106
                // enough to trigger one
83
107
                for( int a = 0; _event_times[ a ]; a++ )
84
108
                {
85
 
                        // if this event is in the future, stop
86
 
                        if( (unsigned)_event_times[ a ] > elapsed ) break;
87
 
 
88
 
                        // if this event has already been triggered, skip
89
 
                        if( (unsigned)_event_times[ a ] <= _millis_done ) continue;
90
 
 
91
 
                        // trigger event
92
 
                        if( _send_interim )
 
109
                        // if this event is in the future, we can stop looking
 
110
                        if( duration < (unsigned long)_event_times[ a ] )
 
111
                                break;
 
112
 
 
113
                        // if this event happened since the previous update, trigger it
 
114
                        if( _state_duration_done < (unsigned long)_event_times[ a ] )
93
115
                                _pending_event = a + 1;
94
 
                        else
95
 
                                _interim_event = a + 1;
96
116
                }
97
117
 
98
 
                // update the amount of press we have processed
99
 
                _millis_done = elapsed;
100
 
        }
101
 
 
102
 
        // if the button has just become unpressed and we're not sending interim
103
 
        // events, we'll need to trigger it now
104
 
        if( !state && _state_last && !_send_interim )
105
 
        {
106
 
                // unless we're being told not to, trigger any interim events
107
 
                if( _ignore_next_unpress )
108
 
                        _ignore_next_unpress = false;
109
 
                else
110
 
                        _pending_event = _interim_event;
111
 
 
112
 
                // clear any interim event
113
 
                _interim_event = 0;
114
 
        }
115
 
 
116
 
        // update our state
117
 
        _state_last = state;
 
118
                // update the duration we've accounted for
 
119
                _state_duration_done = duration;
 
120
        }
118
121
}
119
122
 
120
123