/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

 
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() )
 
30
        _state( digitalRead( pin )? false : true ),
 
31
        _state_millis( ::millis() ),
 
32
        _state_duration( 0 )
10
33
{
11
34
}
12
35
 
23
46
        _event_id = 0;
24
47
 
25
48
        // get time
26
 
        int millis = ::millis();
 
49
        unsigned long millis = ::millis();
27
50
 
28
51
        // 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 )
 
52
        bool state = digitalRead( _pin )? false : true;
 
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++ )
34
70
                {
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
 
                        }
 
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;
46
79
                }
47
80
 
48
 
                // update last state
49
 
                _last_state = state;
50
 
                _last_millis = millis;
 
81
                // update the duration we've accounted for
 
82
                _state_duration = duration;
51
83
        }
52
84
 
53
85
        return _event_id;