-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button2.h
87 lines (72 loc) · 2.45 KB
/
Button2.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
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
/////////////////////////////////////////////////////////////////
/*
Button2.cpp - Arduino Library to simplify working with buttons.
Created by Lennart Hennigs, October 28, 2017.
*/
/////////////////////////////////////////////////////////////////
#pragma once
#ifndef Button2_h
#define Button2_h
/////////////////////////////////////////////////////////////////
#include "Arduino.h"
/////////////////////////////////////////////////////////////////
#define DEBOUNCE_MS 50
#define LONGCLICK_MS 250
#define DOUBLECLICK_MS 400
#define SINGLE_CLICK 1
#define DOUBLE_CLICK 2
#define TRIPLE_CLICK 3
#define LONG_CLICK 4
/////////////////////////////////////////////////////////////////
class Button2
{
private:
uint8_t pin;
int prev_state;
int state = HIGH;
uint8_t click_count = 0;
unsigned int last_click_type = 0;
unsigned long click_ms;
unsigned long down_ms;
unsigned int debounce_time_ms;
unsigned int down_time_ms = 0;
bool pressed_triggered = false;
bool longclick_detected = false;
typedef void (*CallbackFunction) (Button2 &);
CallbackFunction pressed_cb = NULL;
CallbackFunction released_cb = NULL;
CallbackFunction change_cb = NULL;
CallbackFunction tap_cb = NULL;
CallbackFunction click_cb = NULL;
CallbackFunction long_cb = NULL;
CallbackFunction double_cb = NULL;
CallbackFunction triple_cb = NULL;
public:
Button2()
{
pin = 0xFF;
}
Button2(uint8_t attachTo, uint8_t buttonMode = INPUT_PULLUP, unsigned int debounceTimeout = DEBOUNCE_MS);
void setDebounceTime(unsigned int ms);
void setChangedHandler(CallbackFunction f);
void setPressedHandler(CallbackFunction f);
void setReleasedHandler(CallbackFunction f);
void setClickHandler(CallbackFunction f);
void setTapHandler(CallbackFunction f);
void setLongClickHandler(CallbackFunction f);
void setDoubleClickHandler(CallbackFunction f);
void setTripleClickHandler(CallbackFunction f);
unsigned int wasPressedFor();
boolean isPressed();
unsigned int getNumberOfClicks();
unsigned int getClickType();
uint8_t getAttachPin()
{
return pin;
}
bool operator==(Button2 &rhs);
void loop();
};
/////////////////////////////////////////////////////////////////
#endif
/////////////////////////////////////////////////////////////////