/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( false ),
31
 
        _state_millis( 0 ),
32
 
        _state_duration_done( 0 ),
33
 
        _pending_event( 0 ),
34
 
        _send_interim( false ),
35
 
        _ignore_next_unpress( false )
36
 
{
37
 
}
38
 
 
39
 
 
40
 
void Button::set_event_times( int event_times[] )
41
 
{
42
 
        _event_times = event_times;
43
 
}
44
 
 
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
 
 
60
 
void Button::update()
61
 
{
 
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
 
62
48
        // get time
63
49
        unsigned long millis = ::millis();
64
50
 
66
52
        bool state = digitalRead( _pin )? false : true;
67
53
        if( state != _state )
68
54
        {
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
55
                _state = state;
94
56
                _state_millis = millis;
95
 
                _state_duration_done = 0;
 
57
                _state_duration = 0;
96
58
        }
97
59
 
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 )
 
60
        // if button has been pressed for any amount of time
 
61
        if( state && _state_millis < millis )
101
62
        {
102
63
                // calculate new duration for this state
103
64
                unsigned long duration = millis - _state_millis;
104
65
 
105
 
                // check through events to see if the button has been pressed long
106
 
                // enough to trigger one
107
 
                for( int a = 0; _event_times[ a ]; a++ )
 
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++ )
108
70
                {
109
71
                        // if this event is in the future, we can stop looking
110
 
                        if( duration < (unsigned long)_event_times[ a ] )
 
72
                        if( duration < (unsigned long)i->first )
111
73
                                break;
112
74
 
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;
 
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;
116
79
                }
117
80
 
118
81
                // update the duration we've accounted for
119
 
                _state_duration_done = duration;
 
82
                _state_duration = duration;
120
83
        }
 
84
 
 
85
        return _event_id;
121
86
}
122
87
 
123
88
 
124
 
int Button::get_event()
 
89
int Button::get_triggered_event()
125
90
{
126
 
        int event = _pending_event;
127
 
        _pending_event = 0;
128
 
        return event;
 
91
        return _event_id;
129
92
}