/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 ),
85 by Tim Marston
rewrote/fixed button event triggering code
30
	_state_last( false ),
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 ),
85 by Tim Marston
rewrote/fixed button event triggering code
34
	_interim_event( 0 ),
35
	_send_interim( true ),
76 by edam
switch button to no interim presses during settings mode; added NVRAM support
36
	_ignore_next_unpress( false )
59 by edam
removed ulibc, fixed button, added text rendering
37
{
38
}
39
40
41
void Button::set_event_times( int event_times[] )
42
{
43
	_event_times = event_times;
44
}
45
46
76 by edam
switch button to no interim presses during settings mode; added NVRAM support
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.
85 by Tim Marston
rewrote/fixed button event triggering code
53
	if( _state_last && _send_interim && !send_interim )
76 by edam
switch button to no interim presses during settings mode; added NVRAM support
54
		_ignore_next_unpress = true;
55
56
	// set mode
57
	_send_interim = send_interim;
58
}
59
60
59 by edam
removed ulibc, fixed button, added text rendering
61
void Button::update()
62
{
56 by edam
updated software to include drawing abstraction infrastructure
63
	// get time
58 by edam
removed Bounce library and updated/fixed new code
64
	unsigned long millis = ::millis();
56 by edam
updated software to include drawing abstraction infrastructure
65
85 by Tim Marston
rewrote/fixed button event triggering code
66
	// get button state
58 by edam
removed Bounce library and updated/fixed new code
67
	bool state = digitalRead( _pin )? false : true;
85 by Tim Marston
rewrote/fixed button event triggering code
68
69
	// if the button is pressed
70
	if( state )
58 by edam
removed Bounce library and updated/fixed new code
71
	{
85 by Tim Marston
rewrote/fixed button event triggering code
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;
76 by edam
switch button to no interim presses during settings mode; added NVRAM support
77
		}
85 by Tim Marston
rewrote/fixed button event triggering code
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
59 by edam
removed ulibc, fixed button, added text rendering
83
		for( int a = 0; _event_times[ a ]; a++ )
56 by edam
updated software to include drawing abstraction infrastructure
84
		{
85 by Tim Marston
rewrote/fixed button event triggering code
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 )
59 by edam
removed ulibc, fixed button, added text rendering
93
				_pending_event = a + 1;
85 by Tim Marston
rewrote/fixed button event triggering code
94
			else
95
				_interim_event = a + 1;
56 by edam
updated software to include drawing abstraction infrastructure
96
		}
97
85 by Tim Marston
rewrote/fixed button event triggering code
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;
56 by edam
updated software to include drawing abstraction infrastructure
118
}
119
120
59 by edam
removed ulibc, fixed button, added text rendering
121
int Button::get_event()
56 by edam
updated software to include drawing abstraction infrastructure
122
{
59 by edam
removed ulibc, fixed button, added text rendering
123
	int event = _pending_event;
124
	_pending_event = 0;
125
	return event;
56 by edam
updated software to include drawing abstraction infrastructure
126
}