-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBounceSimplepcf.cpp
More file actions
91 lines (75 loc) · 2.31 KB
/
BounceSimplepcf.cpp
File metadata and controls
91 lines (75 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Please read BounceSimplepcf.h for information about the licence and authors
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "BounceSimplepcf.h"
extern "C" {
#include "utility\i2c.h"
}
#define DEBOUNCED_STATE 0
#define UNSTABLE_STATE 1
#define STATE_CHANGED 3
BounceSimplePcf::BounceSimplePcf()
: previous_millis(0)
, interval_millis(10)
, state(0)
, pin(0)
, pcfAddress(0)
{}
void BounceSimplePcf::attach(uint8_t pcfAddress, int pin, uint16_t interval_millis) {
this->pin = pin;
this->pcfAddress = pcfAddress;
this->interval_millis = interval_millis;
state = readPins(pin) ? _BV(DEBOUNCED_STATE) | _BV(UNSTABLE_STATE) : 0;
previous_millis = millis();
}
void BounceSimplePcf::interval(uint16_t interval_millis)
{
this->interval_millis = interval_millis;
}
bool BounceSimplePcf::update()
{
// Read the state of the switch in a temporary variable.
bool currentState = readPins(pin);
state &= ~_BV(STATE_CHANGED);
// If the reading is different from last reading, reset the debounce counter
if ( currentState != (bool)(state & _BV(UNSTABLE_STATE)) ) {
previous_millis = millis();
state ^= _BV(UNSTABLE_STATE);
} else
if ( millis() - previous_millis >= interval_millis ) {
// We have passed the threshold time, so the input is now stable
// If it is different from last state, set the STATE_CHANGED flag
if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) {
previous_millis = millis();
state ^= _BV(DEBOUNCED_STATE);
state |= _BV(STATE_CHANGED);
}
}
return state & _BV(STATE_CHANGED);
}
bool BounceSimplePcf::read()
{
return state & _BV(DEBOUNCED_STATE);
}
bool BounceSimplePcf::rose()
{
return ( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED));
}
bool BounceSimplePcf::fell()
{
return !( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED));
}
bool BounceSimplePcf::readPins(uint8_t pin)
{
register uint8_t iRetValue(0xff);
if(pcfAddress)
{
i2c_start(pcfAddress + 1);
iRetValue = i2c_readNAck();
i2c_stop();
}
return (((~iRetValue) >> pin) & 0x01 ? true : false); // since buttons are switch to GND, we invert the state
}