forked from alonf/WebControlledSwitch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushButtonManager.cpp
More file actions
61 lines (56 loc) · 1.73 KB
/
Copy pathPushButtonManager.cpp
File metadata and controls
61 lines (56 loc) · 1.73 KB
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
//
//
//
#include "PushButtonManager.h"
#include "Configuration.h"
PushButtonManager::PushButtonManager(int pin, IPushButtonActionsPtr_t pushButtonActions)
: _pin(pin), _pushButtonActions(pushButtonActions)
{
pinMode(pin, INPUT);
}
void PushButtonManager::Loop()
{
auto currentState = digitalRead(_pin);
if (currentState == ButtonPressed && _previousButtonState == ButtonReleased) //Trigger
{
_pressStartTime = millis();
}
//handle detection callbacks
if (currentState == ButtonPressed && _previousButtonState == ButtonPressed) //continue press
{
auto length = millis() - _pressStartTime;
if (!_bLongDetection && _pushButtonActions->GetLongPressPeriod() < length && length < _pushButtonActions->GetVeryLongPressPeriod())
{
_bLongDetection = true;
_pushButtonActions->OnLongPressDetected();
}
else if (!_bVeryLongDetection && length > _pushButtonActions->GetVeryLongPressPeriod())
{
_bVeryLongDetection = true;
_pushButtonActions->OnVeryLongPressDetected();
}
}
//Handle button press
if (currentState == ButtonReleased && _previousButtonState == ButtonPressed && _pressStartTime != 0 && millis() - _pressStartTime > 100) //long enough
{
auto length = millis() - _pressStartTime;
if (length < _pushButtonActions->GetLongPressPeriod()) //less then 5 seconds, regular press
{
_state = StateOnTrigger();
_pushButtonActions->OnStateChanged(_state); //Notify change
}
else if (length < _pushButtonActions->GetVeryLongPressPeriod()) //less the 20 seconds
{
_pushButtonActions->OnLongPress();
}
else //over 20 seconds
{
_pushButtonActions->OnVeryLongPress();
}
//reset all
_pressStartTime = 0;
_bLongDetection = false;
_bVeryLongDetection = false;
}
_previousButtonState = currentState;
}