-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushbuttoncommand.cpp
More file actions
134 lines (117 loc) · 3.21 KB
/
pushbuttoncommand.cpp
File metadata and controls
134 lines (117 loc) · 3.21 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdint.h>
#include <Arduino.h>
#include "pushbuttoncommand.h"
#define _STEP_START 0
#define _STEP_FIRST_RISING_EDGE 1
#define _STEP_WAIT_FOR_LONG_PUSH_END 2
#define _STEP_WAIT_FOR_SECOND_RISING_EDGE 3
#define _STEP_WAIT_FOR_SECOND_FALLING_EDGE 4
#define _STEP_END 5
PushButtonCommand::PushButtonCommand()
{
}
void PushButtonCommand::setup(uint8_t length, uint8_t debounce, uint8_t doublclick, uint16_t longpush)
{
if(_sequences != 0)
{
delete [] _sequences;
}
_sequences = new uint8_t [length];
if(_counters != 0)
{
delete [] _counters;
}
_counters = new uint16_t [length];
_debounce = debounce;
_doublclick = doublclick;
_longpush = longpush;
}
uint8_t PushButtonCommand::loop(uint8_t index, boolean input)
{
/* Reset counter if not equal last value */
if(getLastValue(index) != input)
{
setLastValue(index, input);
_counters[index] = 0;
}
switch(_sequences[index])
{
case _STEP_START:
{
if(input && _counters[index]++ == _debounce)
{
_sequences[index] = _STEP_FIRST_RISING_EDGE;
}
break;
}
case _STEP_FIRST_RISING_EDGE:
{
if(input && _counters[index]++ == _longpush)
{
_sequences[index] = _STEP_WAIT_FOR_LONG_PUSH_END;
return PUSH_CMD_LONG_PUSH;
}
else if(!input && _counters[index]++ == _debounce)
{
_sequences[index] = _STEP_WAIT_FOR_SECOND_RISING_EDGE;
}
break;
}
case _STEP_WAIT_FOR_LONG_PUSH_END:
{
if(input)
{
return PUSH_CMD_LONG_PUSH;
}
else if(_counters[index]++ == _debounce)
{
_sequences[index] = _STEP_END;
return PUSH_CMD_LONG_CLICK;
}
break;
}
case _STEP_WAIT_FOR_SECOND_RISING_EDGE:
{
if(input && _counters[index]++ == _debounce)
{
_sequences[index] = _STEP_WAIT_FOR_SECOND_FALLING_EDGE;
}
else if(!input && _counters[index]++ == _doublclick)
{
_sequences[index] = _STEP_END;
return PUSH_CMD_CLICK;
}
break;
}
case _STEP_WAIT_FOR_SECOND_FALLING_EDGE:
{
if(!input && _counters[index]++ == _debounce)
{
_sequences[index] = _STEP_END;
return PUSH_CMD_DOUBLE_CLICK;
}
break;
}
case _STEP_END:
default:
{
_sequences[index] = _STEP_START;
_counters[index] = 0;
break;
}
}
return PUSH_CMD_NONE;
}
boolean PushButtonCommand::getLastValue(uint8_t index) {
return (_lastValue & (1 << index));
}
void PushButtonCommand::setLastValue(uint8_t index, boolean value) {
if(value)
{
_lastValue |= (1 << index);
}
else
{
_lastValue &= ~(1 << index);
}
}