forked from s-marley/Uno_vu_line
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.cpp
75 lines (65 loc) · 1.3 KB
/
Button.cpp
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
#include "Button.h"
Button::Button(uint8_t pin, uint32_t dbTime)
{
_pin = pin;
_dbTime = dbTime;
pinMode(_pin, INPUT);
_state = digitalRead(_pin);
_time = millis();
_lastState = _state;
_changed = 0;
_lastChange = _time;
}
uint8_t Button::read(void)
{
static uint32_t ms;
static uint8_t pinVal;
ms = millis();
pinVal = digitalRead(_pin);
if (ms - _lastChange < _dbTime) {
_time = ms;
_changed = 0;
return _state;
}
else {
_lastState = _state;
_state = pinVal;
_time = ms;
if (_state != _lastState) {
_lastChange = ms;
_changed = 1;
}
else {
_changed = 0;
}
return _state;
}
}
uint8_t Button::isPressed(void)
{
return _state == 0 ? 0 : 1;
}
uint8_t Button::isReleased(void)
{
return _state == 0 ? 1 : 0;
}
uint8_t Button::wasPressed(void)
{
return _state && _changed;
}
uint8_t Button::wasReleased(void)
{
return !_state && _changed;
}
uint8_t Button::pressedFor(uint32_t ms)
{
return (_state == 1 && _time - _lastChange >= ms) ? 1 : 0;
}
uint8_t Button::releasedFor(uint32_t ms)
{
return (_state == 0 && _time - _lastChange >= ms) ? 1 : 0;
}
uint32_t Button::lastChange(void)
{
return _lastChange;
}