4
* Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
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
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.
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.
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/>.
1
23
#include <Arduino.h>
5
27
Button::Button( int pin )
8
_last_state( digitalRead( pin )? true : false ),
9
_last_millis( millis() )
14
void Button::add_event_at( int duration, int event_id )
16
_press_events[ duration ] = event_id;
35
_send_interim( true ),
36
_ignore_next_unpress( false )
41
void Button::set_event_times( int event_times[] )
43
_event_times = event_times;
47
void Button::set_press_mode( bool send_interim )
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;
57
_send_interim = send_interim;
26
int millis = ::millis();
28
// get state and check for change
29
bool state = digitalRead( _pin )? true : false;
30
if( state != _last_state )
64
unsigned long millis = ::millis();
67
bool state = digitalRead( _pin )? false : true;
69
// if the button is pressed
32
// if the button has become unpressed
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
75
_millis_state = millis;
79
// calculate time that has elapsed in total this press
80
unsigned long elapsed = millis - _millis_state;
82
// look through the events to see if any need to be triggered
83
for( int a = 0; _event_times[ a ]; a++ )
35
// check through events to see if it had been pressed long enough
37
int duration = millis - _last_millis;
38
for( std::map< int, int >::iterator i = _press_events.begin();
39
i != _press_events.end(); i++ )
41
if( duration > i->first )
42
_event_id = i->second;
85
// if this event is in the future, stop
86
if( (unsigned)_event_times[ a ] > elapsed ) break;
88
// if this event has already been triggered, skip
89
if( (unsigned)_event_times[ a ] <= _millis_done ) continue;
93
_pending_event = a + 1;
95
_interim_event = a + 1;
50
_last_millis = millis;
98
// update the amount of press we have processed
99
_millis_done = elapsed;
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 )
106
// unless we're being told not to, trigger any interim events
107
if( _ignore_next_unpress )
108
_ignore_next_unpress = false;
110
_pending_event = _interim_event;
112
// clear any interim event
57
int Button::get_triggered_event()
121
int Button::get_event()
123
int event = _pending_event;