-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathuArmButton.cpp
More file actions
105 lines (86 loc) · 1.3 KB
/
uArmButton.cpp
File metadata and controls
105 lines (86 loc) · 1.3 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
******************************************************************************
* @file uArmButton.cpp
* @author David.Long
* @email xiaokun.long@ufactory.cc
* @date 2016-10-17
******************************************************************************
*/
#include "uArmButton.h"
uArmButton::uArmButton()
{
mPin = 0xff;
mState = INIT;
mEvent = EVENT_NONE;
}
void uArmButton::setPin(unsigned char pin)
{
mPin = pin;
mState = IDLE;
}
bool uArmButton::clicked()
{
return (mEvent == EVENT_CLICK);
}
bool uArmButton::longPressed()
{
return (mEvent == EVENT_LONG_PRESS);
}
bool uArmButton::isPressed()
{
if (!digitalRead(mPin))
return true;
return false;
}
void uArmButton::clearEvent()
{
mEvent = EVENT_NONE;
}
void uArmButton::tick()
{
switch (mState)
{
case IDLE:
if (isPressed())
{
mTicks = 0;
mState = HALF_PRESSED;
}
break;
case HALF_PRESSED:
if (isPressed())
{
buzzer.buzz(4000, 100);
mState = PRESSED;
}
else
{
mState = IDLE;
}
break;
case PRESSED:
if (isPressed())
{
mTicks++;
}
else
{
mState = RELEASE;
}
break;
case RELEASE:
if (mTicks >= (1000/TICK_INTERVAL))
{
mEvent = EVENT_LONG_PRESS;
}
else
{
mEvent = EVENT_CLICK;
}
mTicks = 0;
mState = IDLE;
break;
default:
break;
}
}