/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-25 14:54:33 UTC
  • Revision ID: edam@waxworlds.org-20120225145433-kih8qs45x05cum46
removed Bounce library and updated/fixed new code

Show diffs side-by-side

added added

removed removed

27
27
Button::Button( int pin )
28
28
        :
29
29
        _pin( pin ),
30
 
        _state_last( false ),
31
 
        _millis_state( 0 ),
32
 
        _millis_done( 0 ),
33
 
        _pending_event( 0 ),
34
 
        _interim_event( 0 ),
35
 
        _send_interim( true ),
36
 
        _ignore_next_unpress( false )
37
 
{
38
 
}
39
 
 
40
 
 
41
 
void Button::set_event_times( int event_times[] )
42
 
{
43
 
        _event_times = event_times;
44
 
}
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
 
 
61
 
void Button::update()
62
 
{
 
30
        _state( digitalRead( pin )? false : true ),
 
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
 
63
48
        // get time
64
49
        unsigned long millis = ::millis();
65
50
 
66
 
        // get button state
 
51
        // get state and check for change
67
52
        bool state = digitalRead( _pin )? false : true;
68
 
 
69
 
        // if the button is pressed
70
 
        if( state )
71
 
        {
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;
77
 
                }
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
83
 
                for( int a = 0; _event_times[ a ]; a++ )
 
53
        if( state != _state )
 
54
        {
 
55
                _state = state;
 
56
                _state_millis = millis;
 
57
                _state_duration = 0;
 
58
        }
 
59
 
 
60
        // if button has been pressed for any amount of time
 
61
        if( state && _state_millis < millis )
 
62
        {
 
63
                // calculate new duration for this state
 
64
                unsigned long duration = millis - _state_millis;
 
65
 
 
66
                // check through events to see if the button has been
 
67
                // pressed long enough to trigger one
 
68
                for( std::map< int, int >::iterator i = _press_events.begin();
 
69
                         i != _press_events.end(); i++ )
84
70
                {
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 )
93
 
                                _pending_event = a + 1;
94
 
                        else
95
 
                                _interim_event = a + 1;
 
71
                        // if this event is in the future, we can stop looking
 
72
                        if( duration < (unsigned long)i->first )
 
73
                                break;
 
74
 
 
75
                        // if this event happened since the previous update, we
 
76
                        // can trigger it
 
77
                        if( _state_duration < (unsigned long)i->first )
 
78
                                _event_id = i->second;
96
79
                }
97
80
 
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;
 
81
                // update the duration we've accounted for
 
82
                _state_duration = duration;
 
83
        }
 
84
 
 
85
        return _event_id;
118
86
}
119
87
 
120
88
 
121
 
int Button::get_event()
 
89
int Button::get_triggered_event()
122
90
{
123
 
        int event = _pending_event;
124
 
        _pending_event = 0;
125
 
        return event;
 
91
        return _event_id;
126
92
}