/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-05-17 22:49:11 UTC
  • Revision ID: tim@ed.am-20120517224911-dbd9dtcpl14xlhi0
rewrote/fixed button event triggering 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
 */
 
23
#include <Arduino.h>
 
24
#include <button.h>
 
25
 
 
26
 
 
27
Button::Button( int pin )
 
28
        :
 
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
{
 
63
        // get time
 
64
        unsigned long millis = ::millis();
 
65
 
 
66
        // get button state
 
67
        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++ )
 
84
                {
 
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;
 
96
                }
 
97
 
 
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;
 
118
}
 
119
 
 
120
 
 
121
int Button::get_event()
 
122
{
 
123
        int event = _pending_event;
 
124
        _pending_event = 0;
 
125
        return event;
 
126
}