bzr branch
http://bzr.ed.am/elec/propeller-clock
16
by edam
finished first revision of propeller-clock code (can display clock and test); added Bounce library |
1 |
|
2 |
/* |
|
3 |
* This program is free software; you can redistribute it and/or modify |
|
4 |
* it under the terms of the GNU General Public License as published by |
|
5 |
* the Free Software Foundation; either version 2 of the License, or |
|
6 |
* (at your option) any later version. |
|
7 |
* |
|
8 |
* This program is distributed in the hope that it will be useful, |
|
9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 |
* GNU General Public License for more details. |
|
12 |
* |
|
13 |
* You should have received a copy of the GNU General Public License |
|
14 |
* along with this program; if not, write to the Free Software |
|
15 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
16 |
* MA 02110-1301, USA. |
|
17 |
*/ |
|
18 |
||
19 |
||
20 |
||
21 |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
22 |
Main code by Thomas O Fredericks |
|
23 |
Rebounce and duration functions contributed by Eric Lowry |
|
24 |
Write function contributed by Jim Schimpf |
|
25 |
risingEdge and fallingEdge contributed by Tom Harkaway |
|
26 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
|
27 |
||
28 |
#ifndef Bounce_h |
|
29 |
#define Bounce_h |
|
30 |
||
31 |
#include <inttypes.h> |
|
32 |
||
33 |
class Bounce |
|
34 |
{ |
|
35 |
||
36 |
public: |
|
37 |
// Initialize |
|
38 |
Bounce(uint8_t pin, unsigned long interval_millis ); |
|
39 |
// Sets the debounce interval |
|
40 |
void interval(unsigned long interval_millis); |
|
41 |
// Updates the pin |
|
42 |
// Returns 1 if the state changed |
|
43 |
// Returns 0 if the state did not change |
|
44 |
int update(); |
|
45 |
// Forces the pin to signal a change (through update()) in X milliseconds |
|
46 |
// even if the state does not actually change |
|
47 |
// Example: press and hold a button and have it repeat every X milliseconds |
|
48 |
void rebounce(unsigned long interval); |
|
49 |
// Returns the updated pin state |
|
50 |
int read(); |
|
51 |
// Sets the stored pin state |
|
52 |
void write(int new_state); |
|
53 |
// Returns the number of milliseconds the pin has been in the current state |
|
54 |
unsigned long duration(); |
|
55 |
// The risingEdge method is true for one scan after the de-bounced input goes from off-to-on. |
|
56 |
bool risingEdge(); |
|
57 |
// The fallingEdge method it true for one scan after the de-bounced input goes from on-to-off. |
|
58 |
bool fallingEdge(); |
|
59 |
||
60 |
protected: |
|
61 |
int debounce(); |
|
62 |
unsigned long previous_millis, interval_millis, rebounce_millis; |
|
63 |
uint8_t state; |
|
64 |
uint8_t pin; |
|
65 |
uint8_t stateChanged; |
|
66 |
}; |
|
67 |
||
68 |
#endif |
|
69 |
||
70 |