-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathButton.cpp
More file actions
126 lines (106 loc) · 2.3 KB
/
Button.cpp
File metadata and controls
126 lines (106 loc) · 2.3 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Button - a small library for Arduino to handle button debouncing
MIT licensed.
*/
#include "Button.h"
#include <Arduino.h>
Button::Button(uint8_t pin, uint16_t debounce_ms)
: _pin(pin)
, _delay(debounce_ms)
, _state(HIGH)
, _ignore_until(0)
, _has_changed(false)
, _reported_repeats(0)
, _repeat_delay_ms(-1)
, _repeat_ms(-1)
{
}
void Button::begin()
{
pinMode(_pin, INPUT_PULLUP);
}
//
// public methods
//
bool Button::read()
{
// ignore pin changes until after this delay time
if (_ignore_until > millis())
{
// ignore any changes during this period
}
// pin has changed
else if (digitalRead(_pin) != _state)
{
_state = !_state;
if (_state == RELEASED)
{
_reported_repeats = repeats_since_press();
}
else
{
_reported_repeats = 0;
}
_ignore_until = millis() + _delay;
_has_changed = true;
}
return _state;
}
// has the button been toggled from on -> off, or vice versa
bool Button::toggled()
{
read();
return has_changed();
}
// mostly internal, tells you if a button has changed after calling the read() function
bool Button::has_changed()
{
if (_has_changed)
{
_has_changed = false;
return true;
}
return false;
}
// how many repeated press events occured since the button was pressed
uint16_t Button::repeat_count()
{
return _state == PRESSED ? repeats_since_press() : _reported_repeats;
}
// has the button gone from off -> on or pressed repeatedly
bool Button::pressed()
{
if (read() == PRESSED)
{
uint16_t old_repeats = _reported_repeats;
_reported_repeats = repeats_since_press();
return (has_changed() || old_repeats != _reported_repeats);
}
else
{
return false;
}
}
// has the button gone from on -> off
bool Button::released()
{
return (read() == RELEASED && has_changed());
}
void Button::set_repeat(int16_t delay_ms, int16_t repeat_ms)
{
_repeat_delay_ms = delay_ms > _delay ? delay_ms - _delay : 0;
_repeat_ms = repeat_ms;
}
uint16_t Button::repeats_since_press()
{
if (_repeat_delay_ms == -1 || millis() < _ignore_until + _repeat_delay_ms)
{
return 0;
}
if (_repeat_ms <= 0)
{
return 1;
}
uint32_t press_time = millis() - _ignore_until;
return 1 + (press_time - _repeat_delay_ms) / _repeat_ms;
}