/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-18 18:29:50 UTC
  • Revision ID: tim@ed.am-20120518182950-t85bn9a21n72uzm8
text messages are now individually enabled and draw()n automatically

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_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
{
25
63
        // get time
26
 
        int millis = ::millis();
27
 
 
28
 
        // get state and check for change
29
 
        bool state = digitalRead( _pin )? true : false;
30
 
        if( state != _last_state )
 
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 )
31
71
        {
32
 
                // if the button has become unpressed
33
 
                if( !state )
 
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++ )
34
84
                {
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
 
                        }
 
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;
46
96
                }
47
97
 
48
 
                // update last state
49
 
                _last_state = state;
50
 
                _last_millis = millis;
51
 
        }
52
 
 
53
 
        return _event_id;
 
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;
54
118
}
55
119
 
56
120
 
57
 
int Button::get_triggered_event()
 
121
int Button::get_event()
58
122
{
59
 
        return _event_id;
 
123
        int event = _pending_event;
 
124
        _pending_event = 0;
 
125
        return event;
60
126
}