-
Notifications
You must be signed in to change notification settings - Fork 41
/
button.cpp
66 lines (61 loc) · 1.08 KB
/
button.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
#include "button.h"
#include "pico/stdlib.h"
#include <cstdio>
button :: button(uint8_t gpio_num):gpio_num(gpio_num)
{
button_state = up;
time_pressed = 0;
gpio_init(gpio_num);
gpio_set_dir(gpio_num, GPIO_IN);
gpio_pull_up(gpio_num);
}
void button :: update_state()
{
if(button_state == up)
{
if(!gpio_get(gpio_num)) //on
{
time_pressed = time_us_32();
button_state = down;
}
}
else if(button_state == down)
{
if((time_us_32() - time_pressed) > (500 * 1000))
{
button_state = held;
}
else if(gpio_get(gpio_num)) //off
{
if((time_us_32() - time_pressed) > (50 * 1000))
{
button_state = pressed;
}
else
{
button_state = up;
}
}
}
else if(button_state == pressed)
{
button_state = up;
}
else if(button_state == held)
{
if(gpio_get(gpio_num)) //off
{
button_state = up;
}
}
}
bool button :: is_pressed()
{
update_state();
return button_state == pressed;
}
bool button :: is_held()
{
update_state();
return button_state == held;
}