-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathButtonPress.h
More file actions
54 lines (48 loc) · 1.85 KB
/
ButtonPress.h
File metadata and controls
54 lines (48 loc) · 1.85 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
#ifndef BUTTON_PRESS_H
#define BUTTON_PRESS_H
#include <Arduino.h>
/**
* @class ButtonPress
* @author felix
* @date 14/05/19
* @file ButtonPress.h
* @brief De-bounces button pushes, to create "once-only" events at button pushes, and definable auto-repeat when button is held down.
*/
class ButtonPress {
private:
uint8_t pin;
bool lastState;
uint8_t activeState;
uint32_t lastChange;
uint16_t filterTime;
uint16_t repeatPeriod;
public:
/**
* @brief Constructor for button press.
* @param pin Input pin to use for the button
* @param repeatPeriod Time in milliseconds after which the button press event is repeated when button is held down.
* @param config Pin state flag (pull-up, pull-down, etc.) for pin configuration.
* @param activeState Polarity of button. Default: low when pushed.
* @param filterTime De-bouncing rejection time span in millisecond (jitter will be ignored within this time period after the first activation).
*/
ButtonPress(uint8_t pin, uint16_t repeatPeriod=0, uint16_t filterTime=100, uint8_t config=INPUT_PULLUP, uint8_t activeState=LOW):
pin(pin), activeState(activeState), filterTime(filterTime), repeatPeriod(repeatPeriod), lastState(0), lastChange(0) {
pinMode(pin, config);
};
/**
* @brief True only once for each button push. Repeats at specified period.
* @return True once after button is pushed, and true repeatedly after a defined repeat period.
*/
bool pushed();
/**
* @brief True only once for each button push after release.
* @return True once after button release.
*/
bool released();
/**
* @brief Current state of button.
* @return True when pushed, false otherwise.
*/
bool currentlyPushed(); // current state (remains set). True when pushed, false otherwise.
};
#endif