/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-04-29 15:27:19 UTC
  • Revision ID: tim@ed.am-20120429152719-4cu2t9lx7bxpbml1
added adjustable text scaling factor

Show diffs side-by-side

added added

removed removed

28
28
        :
29
29
        _pin( pin ),
30
30
        _state( false ),
31
 
        _millis_state( 0 ),
32
 
        _millis_done( 0 ),
 
31
        _state_millis( 0 ),
 
32
        _state_duration_done( 0 ),
33
33
        _pending_event( 0 ),
34
 
        _last_real_state( false ),
35
 
        _millis_real_state( 0 )
 
34
        _send_interim( false ),
 
35
        _ignore_next_unpress( false )
36
36
{
37
37
}
38
38
 
43
43
}
44
44
 
45
45
 
 
46
void Button::set_press_mode( bool send_interim )
 
47
{
 
48
        // if the button is down and we're switching from sending interim presses
 
49
        // to not sending interim presses, we need to ignore the next unpress, or
 
50
        // this button press may trigger events while being presses *and* events
 
51
        // when it is unpressed.
 
52
        if( _state && _send_interim && !send_interim )
 
53
                _ignore_next_unpress = true;
 
54
 
 
55
        // set mode
 
56
        _send_interim = send_interim;
 
57
}
 
58
 
 
59
 
46
60
void Button::update()
47
61
{
48
62
        // get time
49
63
        unsigned long millis = ::millis();
50
64
 
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;
 
65
        // get state and check for change
 
66
        bool state = digitalRead( _pin )? false : true;
 
67
        if( state != _state )
 
68
        {
 
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
                        }
 
90
                }
 
91
                
 
92
                // reset state
 
93
                _state = state;
 
94
                _state_millis = millis;
 
95
                _state_duration_done = 0;
59
96
        }
60
97
 
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 )
 
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 )
69
101
        {
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
 
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
81
107
                for( int a = 0; _event_times[ a ]; a++ )
82
108
                {
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;
 
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 ] )
 
115
                                _pending_event = a + 1;
91
116
                }
92
117
 
93
 
                // update the amount of press we have processed
94
 
                _millis_done = elapsed;
 
118
                // update the duration we've accounted for
 
119
                _state_duration_done = duration;
95
120
        }
96
 
 
97
 
        // update our state
98
 
        _state = state;
99
121
}
100
122
 
101
123
 
105
127
        _pending_event = 0;
106
128
        return event;
107
129
}
108
 
 
109
 
 
110
 
bool Button::get_state()
111
 
{
112
 
        return _state;
113
 
}