/
Button.h
43 lines (38 loc) · 844 Bytes
/
Button.h
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
/*
* Button.h
*
* Created on: 23.12.2020
* Author: ag4716
*/
#ifndef BUTTON_H_
#define BUTTON_H_
#include "Arduino.h"
#define BUTTON_DEBOUNCE_TIME 200
#define BUTTON_LONG_PRESS_TIME 3000
#define BUTTON_SAMPLE_PERIOD 20
#define BUTTON_NUMBER_SAMPLES ((int)(BUTTON_DEBOUNCE_TIME/BUTTON_SAMPLE_PERIOD))
class Button {
public:
Button(uint8_t pin);
Button(bool (*readButtonFunction)());
virtual ~Button();
void update();
bool isPressed();
bool isLongPressed();
bool isShortPressed();
private:
void init();
uint8_t mPin;
bool state;
bool shortPressed;
bool longPressed;
bool lastState;
unsigned long lastChangeTime;
unsigned long longPressTime;
bool readButton();
bool (*externalReadButtonFunction)();
bool samples[BUTTON_NUMBER_SAMPLES];
int samplePointer;
unsigned long lastSampleTime;
};
#endif /* BUTTON_H_ */