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;
34
_last_real_state( false ),
35
_millis_real_state( 0 )
40
void Button::set_event_times( int event_times[] )
42
_event_times = event_times;
26
int millis = ::millis();
28
// get state and check for change
29
bool state = digitalRead( _pin )? true : false;
30
if( state != _last_state )
49
unsigned long millis = ::millis();
53
bool real_state = digitalRead( _pin )? false : true;
55
// if real state has changed, reset the timer
56
if( real_state != _last_real_state ) {
57
_last_real_state = real_state;
58
_millis_real_state = millis;
61
// work out current state; if the real state timer has elapsed, set our
62
// state to real state
64
if( _last_real_state != state && millis - _millis_real_state > 15 )
65
state = _last_real_state;
67
// if the button is pressed
32
// if the button has become unpressed
70
// if it has just been pressed, record the time now and reset how much
71
// of the press we have already generated events for
73
_millis_state = millis;
77
// calculate time that has elapsed in total this press
78
unsigned long elapsed = millis - _millis_state;
80
// look through the events to see if any need to be triggered
81
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;
83
// if this event is in the future, stop
84
if( (unsigned)_event_times[ a ] > elapsed ) break;
86
// if this event has already been triggered, skip
87
if( (unsigned)_event_times[ a ] <= _millis_done ) continue;
90
_pending_event = a + 1;
50
_last_millis = millis;
93
// update the amount of press we have processed
94
_millis_done = elapsed;
57
int Button::get_triggered_event()
102
int Button::get_event()
104
int event = _pending_event;
110
bool Button::get_state()