/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-17 22:49:36 UTC
  • Revision ID: tim@ed.am-20120517224936-0wgyem932dlq5bs4
various tweaks, a (failed) attempt to fix text reset bug and added TODO

Show diffs side-by-side

added added

removed removed

27
27
Button::Button( int pin )
28
28
        :
29
29
        _pin( pin ),
30
 
        _state( false ),
 
30
        _state_last( false ),
31
31
        _millis_state( 0 ),
32
32
        _millis_done( 0 ),
33
33
        _pending_event( 0 ),
34
 
        _last_real_state( false ),
35
 
        _millis_real_state( 0 )
 
34
        _interim_event( 0 ),
 
35
        _send_interim( true ),
 
36
        _ignore_next_unpress( false )
36
37
{
37
38
}
38
39
 
43
44
}
44
45
 
45
46
 
 
47
void Button::set_press_mode( bool send_interim )
 
48
{
 
49
        // if the button is down and we're switching from sending interim presses
 
50
        // to not sending interim presses, we need to ignore the next unpress, or
 
51
        // this button press may trigger events while being presses *and* events
 
52
        // when it is unpressed.
 
53
        if( _state_last && _send_interim && !send_interim )
 
54
                _ignore_next_unpress = true;
 
55
 
 
56
        // set mode
 
57
        _send_interim = send_interim;
 
58
}
 
59
 
 
60
 
46
61
void Button::update()
47
62
{
48
63
        // get time
49
64
        unsigned long millis = ::millis();
50
65
 
51
 
 
52
66
        // 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;
59
 
        }
60
 
 
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;
 
67
        bool state = digitalRead( _pin )? false : true;
66
68
 
67
69
        // if the button is pressed
68
70
        if( state )
69
71
        {
70
72
                // if it has just been pressed, record the time now and reset how much
71
73
                // of the press we have already generated events for
72
 
                if( !_state ) {
 
74
                if( !_state_last ) {
73
75
                        _millis_state = millis;
74
76
                        _millis_done = 0;
75
77
                }
87
89
                        if( (unsigned)_event_times[ a ] <= _millis_done ) continue;
88
90
 
89
91
                        // trigger event
90
 
                        _pending_event = a + 1;
 
92
                        if( _send_interim )
 
93
                                _pending_event = a + 1;
 
94
                        else
 
95
                                _interim_event = a + 1;
91
96
                }
92
97
 
93
98
                // update the amount of press we have processed
94
99
                _millis_done = elapsed;
95
100
        }
96
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
 
97
116
        // update our state
98
 
        _state = state;
 
117
        _state_last = state;
99
118
}
100
119
 
101
120
 
105
124
        _pending_event = 0;
106
125
        return event;
107
126
}
108
 
 
109
 
 
110
 
bool Button::get_state()
111
 
{
112
 
        return _state;
113
 
}