-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.h
More file actions
41 lines (35 loc) · 1.46 KB
/
Copy pathbutton.h
File metadata and controls
41 lines (35 loc) · 1.46 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
#pragma once
#include <Arduino.h>
class Button {
public:
explicit Button(byte pin);
void begin();
// Return true once when a debounced press is detected.
bool wasPressed(unsigned long now, unsigned long debounceMs = 50);
// Return true once when the button has been held long enough.
bool wasLongPressed(unsigned long now, unsigned long holdMs = 800, unsigned long debounceMs = 50);
// Return whether the button is currently held after debouncing.
bool isPressed(unsigned long now, unsigned long debounceMs = 50);
// Return true once when a debounced release is detected.
bool wasReleased(unsigned long now, unsigned long debounceMs = 50);
private:
void update(unsigned long now, unsigned long debounceMs = 50);
byte pin_;
// Last stable button state after debouncing.
bool stableState_ = false;
// Most recent raw reading from the pin.
bool lastReading_ = false;
// Time when the raw reading last changed, used in debounce checks.
unsigned long lastChangedTime_ = 0;
unsigned long pressedAt_ = 0;
// Tracks whether the current press already counted as a long press.
bool longPressConsumed_ = false;
// True when a debounced press event is ready to be consumed.
bool isPressed_ = false;
// True when a debounced release event is ready to be consumed.
bool wasReleased_ = false;
// Time when the button state was last updated.
unsigned long lastUpdatedAt_ = 0;
// True after the first call to update().
bool hasUpdated_ = false;
};