-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtripleclick.c
65 lines (54 loc) · 1.06 KB
/
tripleclick.c
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
#include <avr/io.h>
/* Due to the fact that the CPU stops to save power (see main.c), using
* a timer is not possible.
*
* The effective delay heres depends on the polling frequency.
*/
#define TICKS_BETWEEN_TRANSITIONS 20
#define TRIGGERED_TICKS 20
char _isTripleClick(int b)
{
static char transition_count = 0;
static char ticks = 0;
static char triggered = 0;
static int last_b;
if (triggered) {
triggered--;
return 1;
}
ticks++;
if (ticks > TICKS_BETWEEN_TRANSITIONS) {
transition_count = 0;
ticks = 0;
last_b = b;
return 0;
}
if (b != last_b) {
transition_count++;
ticks = 0;
}
if (transition_count >= 6) {
transition_count = 0;
ticks = 0;
triggered = TRIGGERED_TICKS;
return 1;
}
last_b = b;
return 0;
}
#define DEBOUNCE_CYCLES 4
char isTripleClick(int b)
{
static int last_b = 0;
static unsigned char debounce_cycles = 0;
if (b != last_b) {
debounce_cycles++;
if (debounce_cycles > DEBOUNCE_CYCLES) {
last_b = b;
debounce_cycles = 0;
}
} else {
debounce_cycles = 0;
}
return _isTripleClick(last_b);
}