RBE1001Lib
src
button.cpp
Go to the documentation of this file.
1
#include <
button.h
>
2
3
Button::Button
(uint8_t
pin
, uint32_t db)
4
{
5
buttonPin
=
pin
;
6
debouncePeriod
= db;
7
}
8
9
void
Button::Init
(
bool
usePullup)
10
{
11
if
(usePullup) pinMode(
buttonPin
, INPUT_PULLUP);
12
else
pinMode(
buttonPin
, INPUT);
13
14
stabButtonPos
=
tempButtonPos
= digitalRead(
buttonPin
);
15
}
16
17
bool
Button::CheckButtonPress
(
void
)
18
{
19
bool
retVal =
false
;
20
uint8_t currButtonPos = digitalRead(
buttonPin
);
21
22
if
(
tempButtonPos
!= currButtonPos)
//there's been a transistion, so start/continue debouncing
23
{
24
state
=
BUTTON_UNSTABLE
;
25
26
lastBounceTime
= millis();
//start/restart the debouncing timer
27
tempButtonPos
= currButtonPos;
//keep track of the bouncing
28
}
29
30
if
(
state
==
BUTTON_UNSTABLE
)
31
{
32
if
(millis() -
lastBounceTime
>=
debouncePeriod
)
//timer has expired
33
{
34
state
=
BUTTON_STABLE
;
35
}
36
}
37
38
if
(
state
==
BUTTON_STABLE
)
39
{
40
if
(
stabButtonPos
!=
tempButtonPos
)
//we have a transision
41
{
42
if
(
tempButtonPos
==
activeState
) retVal =
true
;
43
stabButtonPos
=
tempButtonPos
;
44
}
45
}
46
47
return
retVal;
48
}
Button::CheckButtonPress
bool CheckButtonPress(void)
Definition:
button.cpp:17
Button::debouncePeriod
uint32_t debouncePeriod
Definition:
button.h:31
button.h
Button::BUTTON_UNSTABLE
Definition:
button.h:21
Button::stabButtonPos
uint8_t stabButtonPos
Definition:
button.h:27
Button::Init
void Init(bool usePullup=true)
Definition:
button.cpp:9
Button::state
BUTTON_STATE state
Definition:
button.h:22
Button::tempButtonPos
uint8_t tempButtonPos
Definition:
button.h:28
Button::buttonPin
uint8_t buttonPin
Definition:
button.h:24
Button::BUTTON_STABLE
Definition:
button.h:21
Button::lastBounceTime
uint32_t lastBounceTime
Definition:
button.h:30
pin
int pin
Definition:
ToneExample.ino:7
Button::Button
Button(uint8_t pin, uint32_t db=10)
Definition:
button.cpp:3
Button::activeState
uint8_t activeState
Definition:
button.h:25
Generated by
1.8.13