-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclickButton.cpp
109 lines (91 loc) · 3.1 KB
/
clickButton.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
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
#define CLICKBTN_PULLUP HIGH
#include <stdint.h>
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include "clickButton.h"
ClickButton::ClickButton(uint8_t buttonPin)
{
_pin = buttonPin;
_activeHigh = LOW; // Assume active-low button
_btnState = !_activeHigh; // initial button state in active-high logic
_lastState = _btnState;
_clickCount = 0;
clicks = 0;
depressed = false;
_lastBounceTime= 0;
debounceTime = 20; // Debounce timer in ms
multiclickTime = 250; // Time limit for multi clicks
longClickTime = 1000; // time until long clicks register
pinMode(_pin, INPUT);
}
ClickButton::ClickButton(uint8_t buttonPin, bool activeType)
{
_pin = buttonPin;
_activeHigh = activeType;
_btnState = !_activeHigh; // initial button state in active-high logic
_lastState = _btnState;
_clickCount = 0;
clicks = 0;
depressed = 0;
_lastBounceTime= 0;
debounceTime = 20; // Debounce timer in ms
multiclickTime = 250; // Time limit for multi clicks
longClickTime = 1000; // time until long clicks register
pinMode(_pin, INPUT);
}
ClickButton::ClickButton(uint8_t buttonPin, bool activeType, bool internalPullup)
{
_pin = buttonPin;
_activeHigh = activeType;
_btnState = !_activeHigh; // initial button state in active-high logic
_lastState = _btnState;
_clickCount = 0;
clicks = 0;
depressed = 0;
_lastBounceTime= 0;
debounceTime = 20; // Debounce timer in ms
multiclickTime = 250; // Time limit for multi clicks
longClickTime = 1000; // time until "long" click register
// Turn on internal pullup resistor if applicable
if (_activeHigh == LOW && internalPullup == CLICKBTN_PULLUP)
{
pinMode(_pin, INPUT);
pullUpDnControl(_pin, PUD_UP);
}
else
{
printf("setting _pin to INPUT\n");
pinMode(_pin, INPUT);
}
}
void ClickButton::Update()
{
long now = (long)millis(); // get current time
_btnState = digitalRead(_pin); // current appearant button state
// Make the button logic active-high in code
if (!_activeHigh) _btnState = !_btnState;
// If the switch changed, due to noise or a button press, reset the debounce timer
if (_btnState != _lastState) _lastBounceTime = now;
// debounce the button (Check if a stable, changed state has occured)
if (now - _lastBounceTime > debounceTime && _btnState != depressed)
{
depressed = _btnState;
if (depressed) _clickCount++;
}
// If the button released state is stable, report nr of clicks and start new cycle
if (!depressed && (now - _lastBounceTime) > multiclickTime)
{
// positive count for released buttons
clicks = _clickCount;
_clickCount = 0;
}
// Check for "long click"
if (depressed && (now - _lastBounceTime > longClickTime))
{
// negative count for long clicks
clicks = 0 - _clickCount;
_clickCount = 0;
}
_lastState = _btnState;
}