/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-03-21 19:37:33 UTC
  • Revision ID: tim@ed.am-20120321193733-29euxt0t0h9dwsj3
added .dep directories to bzrignore

Show diffs side-by-side

added added

removed removed

 
1
/*
 
2
 * button.cc
 
3
 *
 
4
 * Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
 
5
 *
 
6
 * This file is part of propeller-clock (hereafter referred to as "this
 
7
 * program"). See http://ed.am/dev/software/arduino/propeller-clock for more
 
8
 * information.
 
9
 *
 
10
 * This program is free software: you can redistribute it and/or modify
 
11
 * it under the terms of the GNU Lesser General Public License as published
 
12
 * by the Free Software Foundation, either version 3 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU Lesser General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
 */
1
23
#include <Arduino.h>
2
24
#include <button.h>
3
25
 
5
27
Button::Button( int pin )
6
28
        :
7
29
        _pin( pin ),
8
 
        _last_state( digitalRead( pin )? true : false ),
9
 
        _last_millis( millis() )
10
 
{
11
 
}
12
 
 
13
 
 
14
 
void Button::add_event_at( int duration, int event_id )
15
 
{
16
 
        _press_events[ duration ] = event_id;
17
 
}
18
 
 
19
 
 
20
 
int Button::update()
21
 
{
22
 
        // reset event
23
 
        _event_id = 0;
24
 
 
 
30
        _state( digitalRead( pin )? false : true ),
 
31
        _state_millis( ::millis() ),
 
32
        _state_duration( 0 ),
 
33
        _pending_event( 0 )
 
34
{
 
35
}
 
36
 
 
37
 
 
38
void Button::set_event_times( int event_times[] )
 
39
{
 
40
        _event_times = event_times;
 
41
}
 
42
 
 
43
 
 
44
void Button::update()
 
45
{
25
46
        // get time
26
 
        int millis = ::millis();
 
47
        unsigned long millis = ::millis();
27
48
 
28
49
        // get state and check for change
29
 
        bool state = digitalRead( _pin )? true : false;
30
 
        if( state != _last_state )
31
 
        {
32
 
                // if the button has become unpressed
33
 
                if( !state )
 
50
        bool state = digitalRead( _pin )? false : true;
 
51
        if( state != _state )
 
52
        {
 
53
                _state = state;
 
54
                _state_millis = millis;
 
55
                _state_duration = 0;
 
56
        }
 
57
 
 
58
        // if button has been pressed for any amount of time
 
59
        if( state && millis > _state_millis )
 
60
        {
 
61
                // calculate new duration for this state
 
62
                unsigned long duration = millis - _state_millis;
 
63
 
 
64
                // check through events to see if the button has been
 
65
                // pressed long enough to trigger one
 
66
                for( int a = 0; _event_times[ a ]; a++ )
34
67
                {
35
 
                        // check through events to see if it had been pressed long enough
36
 
                        // to trigger one
37
 
                        int duration = millis - _last_millis;
38
 
                        for( std::map< int, int >::iterator i = _press_events.begin();
39
 
                                 i != _press_events.end(); i++ )
40
 
                        {
41
 
                                if( duration > i->first )
42
 
                                        _event_id = i->second;
43
 
                                else
44
 
                                        break;
45
 
                        }
 
68
                        // if this event is in the future, we can stop looking
 
69
                        if( duration < (unsigned long)_event_times[ a ] )
 
70
                                break;
 
71
 
 
72
                        // if this event happened since the previous update, we
 
73
                        // can trigger it
 
74
                        if( _state_duration < (unsigned long)_event_times[ a ] )
 
75
                                _pending_event = a + 1;
46
76
                }
47
77
 
48
 
                // update last state
49
 
                _last_state = state;
50
 
                _last_millis = millis;
 
78
                // update the duration we've accounted for
 
79
                _state_duration = duration;
51
80
        }
52
 
 
53
 
        return _event_id;
54
81
}
55
82
 
56
83
 
57
 
int Button::get_triggered_event()
 
84
int Button::get_event()
58
85
{
59
 
        return _event_id;
 
86
        int event = _pending_event;
 
87
        _pending_event = 0;
 
88
        return event;
60
89
}