/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: 2013-03-31 17:07:36 UTC
  • Revision ID: tim@ed.am-20130331170736-hphm2hg0y6l7w6z1
made rtc-test's DS1307 library a symlink to the main one in src/util

Show diffs side-by-side

added added

removed removed

27
27
Button::Button( int pin )
28
28
        :
29
29
        _pin( pin ),
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
 
 
 
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 )
 
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::update()
 
47
{
48
48
        // get time
49
49
        unsigned long millis = ::millis();
50
50
 
51
 
        // get state and check for change
52
 
        bool state = digitalRead( _pin )? false : true;
53
 
        if( state != _state )
54
 
        {
55
 
                _state = state;
56
 
                _state_millis = millis;
57
 
                _state_duration = 0;
 
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;
58
59
        }
59
60
 
60
 
        // if button has been pressed for any amount of time
61
 
        if( state && _state_millis < millis )
 
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 )
62
69
        {
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++ )
 
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
 
81
                for( int a = 0; _event_times[ a ]; a++ )
70
82
                {
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;
 
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;
79
91
                }
80
92
 
81
 
                // update the duration we've accounted for
82
 
                _state_duration = duration;
 
93
                // update the amount of press we have processed
 
94
                _millis_done = elapsed;
83
95
        }
84
96
 
85
 
        return _event_id;
86
 
}
87
 
 
88
 
 
89
 
int Button::get_triggered_event()
90
 
{
91
 
        return _event_id;
 
97
        // update our state
 
98
        _state = state;
 
99
}
 
100
 
 
101
 
 
102
int Button::get_event()
 
103
{
 
104
        int event = _pending_event;
 
105
        _pending_event = 0;
 
106
        return event;
 
107
}
 
108
 
 
109
 
 
110
bool Button::get_state()
 
111
{
 
112
        return _state;
92
113
}