/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/util/Bounce.cpp

  • Committer: edam
  • Date: 2012-02-25 14:54:33 UTC
  • Revision ID: edam@waxworlds.org-20120225145433-kih8qs45x05cum46
removed Bounce library and updated/fixed new code

Show diffs side-by-side

added added

removed removed

1
 
 
2
 
// Please read Bounce.h for information about the liscence and authors
3
 
 
4
 
#include <Arduino.h>
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
 
 
84
 
// The risingEdge method is true for one scan after the de-bounced
85
 
// input goes from off-to-on.
86
 
bool  Bounce::risingEdge() { return stateChanged && state; }
87
 
// The fallingEdge method it true for one scan after the de-bounced
88
 
// input goes from on-to-off.
89
 
bool  Bounce::fallingEdge() { return stateChanged && !state; }
90