-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsmc_button.h
80 lines (67 loc) · 2.05 KB
/
smc_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
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
#pragma once
#include "Arduino.h"
#include "optimized_gpio.h"
#define BUTTON_STATE_RELEASED 0
#define BUTTON_STATE_CLICKED 1
#define BUTTON_STATE_LONG_PRESSED 2
#define TICK_TIME_US 10000
#define BUTTON_LONG_DELAY_MS 1000
#define BUTTON_DEBOUNCE_DELAY_MS 50
#define TICK_TIME_MS (TICK_TIME_US / 1000)
#define BUTTON_LONG_DELAY (BUTTON_LONG_DELAY_MS / TICK_TIME_MS)
#define BUTTON_DEBOUNCE_DELAY (BUTTON_DEBOUNCE_DELAY_MS / TICK_TIME_MS)
class SmcButton{
private:
uint8_t pin;
uint16_t counter=0;
uint8_t state = BUTTON_STATE_RELEASED;
void (*onClick)() = NULL;
void (*onLongPress)() = NULL;
public:
SmcButton(uint8_t pinNumber){
pin = pinNumber;
pinMode_opt(pin, INPUT_PULLUP);
}
void tick(){
uint8_t pinValue = digitalRead_opt(pin);
if (counter > 0){
counter--;
}
if (counter > BUTTON_LONG_DELAY-BUTTON_DEBOUNCE_DELAY){
return;
}
else if (counter == 0 && pinValue==0 && state == BUTTON_STATE_CLICKED){
//Long press
state = BUTTON_STATE_LONG_PRESSED;
counter = BUTTON_LONG_DELAY;
if (onLongPress!=NULL){
onLongPress();
}
}
else{
//Click
if (pinValue==0 && state == BUTTON_STATE_RELEASED){
state = BUTTON_STATE_CLICKED;
counter = BUTTON_LONG_DELAY;
}
//Release
else if (pinValue == 1 && state == BUTTON_STATE_CLICKED){
state = BUTTON_STATE_RELEASED;
counter = BUTTON_LONG_DELAY;
if (onClick!=NULL){
onClick();
}
}
else if (pinValue == 1 && state == BUTTON_STATE_LONG_PRESSED){
state = BUTTON_STATE_RELEASED;
counter = BUTTON_LONG_DELAY;
}
}
}
void attachClick(void (*callback)()){
onClick = callback;
}
void attachDuringLongPress(void (*callback)()){
onLongPress = callback;
}
};