-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartButton.h
38 lines (32 loc) · 862 Bytes
/
SmartButton.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
#pragma once
#include <Arduino.h>
#define DEBOUNCE_DELAY 10
#define CLICK_MAX_DURATION 500
#define MULTI_CLICK_MAX_DELAY 400
// Syntactic sugar for button callback lambdas
#define buttonCallback(x) [](x) -> void
class SmartButton {
private:
byte pin;
byte inputType;
bool dbKnownState = true;
bool readState = false;
unsigned long readStateMillis = 0;
bool value = false;
bool knownValue = false;
bool checkClick = false;
unsigned long checkClickMillis = 0;
bool checkMultiClick = false;
unsigned long checkMultiClickMillis = 0;
int clicks = 0;
void (*onPressCb)();
void (*onReleaseCb)();
void (*onClickCb)(byte);
public:
SmartButton(byte pin, byte type = INPUT_PULLUP);
void onPress(void (*callback)());
void onRelease(void (*callback)());
void onClick(void (*callback)(byte));
void init();
void handle();
};