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 |
// Please read Bounce.h for information about the liscence and authors |
|
3 |
||
26
by edam
update for arduino-1.0 release |
4 |
#include <Arduino.h> |
16
by edam
finished first revision of propeller-clock code (can display clock and test); added Bounce library |
5 |
#include "Bounce.h" |
6 |
||
7 |
||
8 |
Bounce::Bounce(uint8_t pin,unsigned long interval_millis) |
|
9 |
{ |
|
10 |
interval(interval_millis); |
|
11 |
previous_millis = millis(); |
|
12 |
state = digitalRead(pin); |
|
13 |
this->pin = pin; |
|
14 |
} |
|
15 |
||
16 |
||
17 |
void Bounce::write(int new_state) |
|
18 |
{ |
|
19 |
this->state = new_state; |
|
20 |
digitalWrite(pin,state); |
|
21 |
} |
|
22 |
||
23 |
||
24 |
void Bounce::interval(unsigned long interval_millis) |
|
25 |
{ |
|
26 |
this->interval_millis = interval_millis; |
|
27 |
this->rebounce_millis = 0; |
|
28 |
} |
|
29 |
||
30 |
void Bounce::rebounce(unsigned long interval) |
|
31 |
{ |
|
32 |
this->rebounce_millis = interval; |
|
33 |
} |
|
34 |
||
35 |
||
36 |
||
37 |
int Bounce::update() |
|
38 |
{ |
|
39 |
if ( debounce() ) { |
|
40 |
rebounce(0); |
|
41 |
return stateChanged = 1; |
|
42 |
} |
|
43 |
||
44 |
// We need to rebounce, so simulate a state change |
|
45 |
||
46 |
if ( rebounce_millis && (millis() - previous_millis >= rebounce_millis) ) { |
|
47 |
previous_millis = millis(); |
|
48 |
rebounce(0); |
|
49 |
return stateChanged = 1; |
|
50 |
} |
|
51 |
||
52 |
return stateChanged = 0; |
|
53 |
} |
|
54 |
||
55 |
||
56 |
unsigned long Bounce::duration() |
|
57 |
{ |
|
58 |
return millis() - previous_millis; |
|
59 |
} |
|
60 |
||
61 |
||
62 |
int Bounce::read() |
|
63 |
{ |
|
64 |
return (int)state; |
|
65 |
} |
|
66 |
||
67 |
||
68 |
// Protected: debounces the pin |
|
69 |
int Bounce::debounce() { |
|
70 |
||
71 |
uint8_t newState = digitalRead(pin); |
|
72 |
if (state != newState ) { |
|
73 |
if (millis() - previous_millis >= interval_millis) { |
|
74 |
previous_millis = millis(); |
|
75 |
state = newState; |
|
76 |
return 1; |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
return 0; |
|
81 |
||
82 |
} |
|
83 |
||
42
by edam
lengthened button debounce time and turned on the pull-up resistor (oops!) |
84 |
// The risingEdge method is true for one scan after the de-bounced |
85 |
// input goes from off-to-on. |
|
16
by edam
finished first revision of propeller-clock code (can display clock and test); added Bounce library |
86 |
bool Bounce::risingEdge() { return stateChanged && state; } |
42
by edam
lengthened button debounce time and turned on the pull-up resistor (oops!) |
87 |
// The fallingEdge method it true for one scan after the de-bounced |
88 |
// input goes from on-to-off. |
|
16
by edam
finished first revision of propeller-clock code (can display clock and test); added Bounce library |
89 |
bool Bounce::fallingEdge() { return stateChanged && !state; } |
90 |