-
Notifications
You must be signed in to change notification settings - Fork 0
/
button_input.cpp
131 lines (116 loc) · 2.5 KB
/
button_input.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
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
#include <Arduino.h>
#include "button_input.h"
button_input::button_input()
{
this->pin = -1;
this->click_count = 0;
this->t_timeout = 0;
this->t_last_btn_down = 0;
this->duration = 0;
this->last_event = none;
this->last_event_click_count = 0;
this->last_event_duration = 0;
}
void button_input::setup(int pin)
{
this->pin = pin;
pinMode(this->pin, INPUT);
this->db_button.attach(this->pin);
this->db_button.interval(50);
}
void button_input::update(void)
{
unsigned long current_millis;
db_button.update();
current_millis = millis();
if (this->db_button.fell()) // Button down
{
this->t_timeout = current_millis + BUTTON_TIMEOUT;
this->t_last_btn_down = current_millis;
}
if (this->db_button.rose()) // Button up
{
this->click_count++;
this->duration = current_millis - this->t_last_btn_down;
if (this->t_timeout == 0)
{
this->last_event = press;
this->last_event_click_count = this->click_count;
this->last_event_duration = this->duration;
//Serial.println();
//Serial.print("long press duration: ");
//Serial.println(this->duration);
this->duration = 0;
this->click_count = 0;
}
}
if (this->t_timeout != 0 && current_millis > this->t_timeout)
{
this->t_timeout = 0;
if (this->db_button.read() == LOW && this->click_count == 0)
{
// Long Click
}
else
{
this->last_event = click;
this->last_event_click_count = this->click_count;
this->last_event_duration = this->duration;
//Serial.println();
//Serial.print("button press count: ");
//Serial.println(this->click_count);
this->click_count = 0;
//Serial.print("duration: ");
//Serial.println(this->duration);
this->duration = 0;
}
}
}
bool button_input::is_single_click()
{
if (this->last_event == click && this->last_event_click_count == 1)
{
this->last_event = none;
return true;
}
else
return false;
}
bool button_input::is_double_click()
{
if (this->last_event == click && this->last_event_click_count == 2)
{
this->last_event = none;
return true;
}
else
return false;
}
bool button_input::is_multi_click()
{
if (this->last_event == click && this->last_event_click_count > 2)
{
this->last_event = none;
return true;
}
else
return false;
}
bool button_input::is_long_press()
{
if (this->last_event == press)
{
this->last_event = none;
return true;
}
else
return false;
}
unsigned int button_input::get_duration()
{
return this->last_event_duration;
}
uint8 button_input::get_click_count()
{
return this->last_event_click_count;
}