-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.cpp
50 lines (45 loc) · 924 Bytes
/
Button.cpp
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
#include "SPL/Atmega16A_SPL.hpp"
typedef void(*Action)();
class SingleButton // Button connected to gnd
{
private:
PinSBase * _ButtonPin;
volatile uint16_t _PressTime;
Action _ClickAction = nullptr;
Action _HoldAction = nullptr;
public:
const uint8_t MinClickTime = 15;
const uint8_t MinHoldTime = 220;
SingleButton(PinSBase &pin, Action click,Action hold)
{
_ButtonPin = &pin;
Initialize();
_ClickAction = click;
_HoldAction = hold;
}
void Initialize()
{
_ButtonPin->AsInput();
_ButtonPin->Set();
_PressTime = 0;
}
void CheckButtonState()
{
if (!_ButtonPin->Check()) //Atmega has only internal pull ups - so button must be setted (1), and button must ground it. If pin == 0 then is pressed
{
++_PressTime;
}
else
{
if (_PressTime > MinHoldTime)
{
_HoldAction();
}
else if (_PressTime > MinClickTime)
{
_ClickAction();
}
_PressTime = 0;
}
}
};