-
Notifications
You must be signed in to change notification settings - Fork 5
/
OneFuncButtonLib.cpp
88 lines (80 loc) · 2.21 KB
/
OneFuncButtonLib.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
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
88
#include "OneFuncButtonLib.h"
Button::Button(int _pin, bool _activeState = HIGH, unsigned int _fht = 800, unsigned int _sht = 200)
{
pin = _pin;
activeState = _activeState;
firstHoldTime = _fht;
subsqHoldTime = _sht;
pinMode(_pin, INPUT);
btnState = UP;
timeForNextChange = millis();
}
byte Button::state()
{
switch (btnState)
{
case UP:
if (digitalRead(pin) == activeState)
{
/* TIME */ timeForNextChange = millis() + DEBOUNCING_TIME;
/* STATE */ btnState = DEBOUNCING_DOWN;
/* RETURN */ return PRESS_SHORT;
}
return UP;
case DOWN_SHORT:
if (digitalRead(pin) != activeState)
{
/* TIME */ timeForNextChange = millis() + DEBOUNCING_TIME;
/* STATE */ btnState = DEBOUNCING_UP;
/* RETURN */ return RELEASE_SHORT;
}
else if (millis() >= timeForNextChange)
{
/* TIME */ timeForNextChange = millis() + subsqHoldTime;
/* STATE */ btnState = DOWN_LONG_FIRST;
/* RETURN */ return PRESS_LONG_FIRST;
}
return DOWN_SHORT;
case DOWN_LONG_FIRST:
if (digitalRead(pin) != activeState)
{
/* TIME */ timeForNextChange = millis() + DEBOUNCING_TIME;
/* STATE */ btnState = DEBOUNCING_UP;
/* RETURN */ return RELEASE_LONG_FIRST;
}
else if (millis() >= timeForNextChange)
{
/* TIME */ timeForNextChange = millis() + subsqHoldTime;
/* STATE */ btnState = DOWN_LONG_SUBSQ;
/* RETURN */ return PRESS_LONG_SUBSQ;
}
return DOWN_LONG_FIRST;
case DOWN_LONG_SUBSQ:
if (digitalRead(pin) != activeState)
{
/* TIME */ timeForNextChange = millis() + DEBOUNCING_TIME;
/* STATE */ btnState = DEBOUNCING_UP;
/* RETURN */ return RELEASE_LONG_SUBSQ;
}
else if (millis() >= timeForNextChange)
{
/* TIME */ timeForNextChange = millis() + subsqHoldTime;
/* STATE */ btnState = DOWN_LONG_SUBSQ;
/* RETURN */ return PRESS_LONG_SUBSQ;
}
return DOWN_LONG_SUBSQ;
case DEBOUNCING_UP:
if (millis() >= timeForNextChange)
{
/* STATE */ btnState = UP;
}
return UP;
case DEBOUNCING_DOWN:
if (millis() >= timeForNextChange)
{
/* TIME */ timeForNextChange = millis() + firstHoldTime;
/* STATE */ btnState = DOWN_SHORT;
}
return DOWN_SHORT;
}
}