/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-03-21 20:35:28 UTC
  • Revision ID: tim@ed.am-20120321203528-wfhpych1tub75rgj
fixed bug initialising text services on mode activation

Show diffs side-by-side

added added

removed removed

27
27
Button::Button( int pin )
28
28
        :
29
29
        _pin( pin ),
30
 
        _state( false ),
31
 
        _millis_state( 0 ),
32
 
        _millis_done( 0 ),
33
 
        _pending_event( 0 ),
34
 
        _last_real_state( false ),
35
 
        _millis_real_state( 0 )
 
30
        _state( digitalRead( pin )? false : true ),
 
31
        _state_millis( ::millis() ),
 
32
        _state_duration( 0 ),
 
33
        _pending_event( 0 )
36
34
{
37
35
}
38
36
 
48
46
        // get time
49
47
        unsigned long millis = ::millis();
50
48
 
51
 
 
52
 
        // get button state
53
 
        bool real_state = digitalRead( _pin )? false : true;
54
 
 
55
 
        // if real state has changed, reset the timer
56
 
        if( real_state != _last_real_state ) {
57
 
                _last_real_state = real_state;
58
 
                _millis_real_state = millis;
 
49
        // get state and check for change
 
50
        bool state = digitalRead( _pin )? false : true;
 
51
        if( state != _state )
 
52
        {
 
53
                _state = state;
 
54
                _state_millis = millis;
 
55
                _state_duration = 0;
59
56
        }
60
57
 
61
 
        // work out current state; if the real state timer has elapsed, set our
62
 
        // state to real state
63
 
        bool state = _state;
64
 
        if( _last_real_state != state && millis - _millis_real_state > 15 )
65
 
                state = _last_real_state;
66
 
 
67
 
        // if the button is pressed
68
 
        if( state )
 
58
        // if button has been pressed for any amount of time
 
59
        if( state && millis > _state_millis )
69
60
        {
70
 
                // if it has just been pressed, record the time now and reset how much
71
 
                // of the press we have already generated events for
72
 
                if( !_state ) {
73
 
                        _millis_state = millis;
74
 
                        _millis_done = 0;
75
 
                }
76
 
 
77
 
                // calculate time that has elapsed in total this press
78
 
                unsigned long elapsed = millis - _millis_state;
79
 
 
80
 
                // look through the events to see if any need to be triggered
 
61
                // calculate new duration for this state
 
62
                unsigned long duration = millis - _state_millis;
 
63
 
 
64
                // check through events to see if the button has been
 
65
                // pressed long enough to trigger one
81
66
                for( int a = 0; _event_times[ a ]; a++ )
82
67
                {
83
 
                        // if this event is in the future, stop
84
 
                        if( (unsigned)_event_times[ a ] > elapsed ) break;
85
 
 
86
 
                        // if this event has already been triggered, skip
87
 
                        if( (unsigned)_event_times[ a ] <= _millis_done ) continue;
88
 
 
89
 
                        // trigger event
90
 
                        _pending_event = a + 1;
 
68
                        // if this event is in the future, we can stop looking
 
69
                        if( duration < (unsigned long)_event_times[ a ] )
 
70
                                break;
 
71
 
 
72
                        // if this event happened since the previous update, we
 
73
                        // can trigger it
 
74
                        if( _state_duration < (unsigned long)_event_times[ a ] )
 
75
                                _pending_event = a + 1;
91
76
                }
92
77
 
93
 
                // update the amount of press we have processed
94
 
                _millis_done = elapsed;
 
78
                // update the duration we've accounted for
 
79
                _state_duration = duration;
95
80
        }
96
 
 
97
 
        // update our state
98
 
        _state = state;
99
81
}
100
82
 
101
83
 
105
87
        _pending_event = 0;
106
88
        return event;
107
89
}
108
 
 
109
 
 
110
 
bool Button::get_state()
111
 
{
112
 
        return _state;
113
 
}