/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock
58 by edam
removed Bounce library and updated/fixed new code
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
 */
56 by edam
updated software to include drawing abstraction infrastructure
23
#include <Arduino.h>
24
#include <button.h>
25
26
27
Button::Button( int pin )
28
	:
29
	_pin( pin ),
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
30
	_state( false ),
85 by Tim Marston
rewrote/fixed button event triggering code
31
	_millis_state( 0 ),
32
	_millis_done( 0 ),
76 by edam
switch button to no interim presses during settings mode; added NVRAM support
33
	_pending_event( 0 ),
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
34
	_last_real_state( false ),
35
	_millis_real_state( 0 )
59 by edam
removed ulibc, fixed button, added text rendering
36
{
37
}
38
39
40
void Button::set_event_times( int event_times[] )
41
{
42
	_event_times = event_times;
43
}
44
45
46
void Button::update()
47
{
56 by edam
updated software to include drawing abstraction infrastructure
48
	// get time
58 by edam
removed Bounce library and updated/fixed new code
49
	unsigned long millis = ::millis();
56 by edam
updated software to include drawing abstraction infrastructure
50
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
51
85 by Tim Marston
rewrote/fixed button event triggering code
52
	// get button state
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
53
	bool real_state = digitalRead( _pin )? false : true;
54
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;
59
	}
60
61
	// work out current state; if the real state timer has elapsed, set our
62
	// state to real state
63
	bool state = _state;
64
	if( _last_real_state != state && millis - _millis_real_state > 15 )
65
		state = _last_real_state;
85 by Tim Marston
rewrote/fixed button event triggering code
66
67
	// if the button is pressed
68
	if( state )
58 by edam
removed Bounce library and updated/fixed new code
69
	{
85 by Tim Marston
rewrote/fixed button event triggering code
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
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
72
		if( !_state ) {
85 by Tim Marston
rewrote/fixed button event triggering code
73
			_millis_state = millis;
74
			_millis_done = 0;
76 by edam
switch button to no interim presses during settings mode; added NVRAM support
75
		}
85 by Tim Marston
rewrote/fixed button event triggering code
76
77
		// calculate time that has elapsed in total this press
78
		unsigned long elapsed = millis - _millis_state;
79
80
		// look through the events to see if any need to be triggered
59 by edam
removed ulibc, fixed button, added text rendering
81
		for( int a = 0; _event_times[ a ]; a++ )
56 by edam
updated software to include drawing abstraction infrastructure
82
		{
85 by Tim Marston
rewrote/fixed button event triggering code
83
			// if this event is in the future, stop
84
			if( (unsigned)_event_times[ a ] > elapsed ) break;
85
86
			// if this event has already been triggered, skip
87
			if( (unsigned)_event_times[ a ] <= _millis_done ) continue;
88
89
			// trigger event
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
90
			_pending_event = a + 1;
56 by edam
updated software to include drawing abstraction infrastructure
91
		}
92
85 by Tim Marston
rewrote/fixed button event triggering code
93
		// update the amount of press we have processed
94
		_millis_done = elapsed;
95
	}
96
97
	// update our state
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
98
	_state = state;
56 by edam
updated software to include drawing abstraction infrastructure
99
}
100
101
59 by edam
removed ulibc, fixed button, added text rendering
102
int Button::get_event()
56 by edam
updated software to include drawing abstraction infrastructure
103
{
59 by edam
removed ulibc, fixed button, added text rendering
104
	int event = _pending_event;
105
	_pending_event = 0;
106
	return event;
56 by edam
updated software to include drawing abstraction infrastructure
107
}
91 by Tim Marston
fixed text glitch; extended all modes; added screen flip super-long press;
108
109
110
bool Button::get_state()
111
{
112
	return _state;
113
}