-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.cpp
More file actions
46 lines (33 loc) · 826 Bytes
/
Copy pathbutton.cpp
File metadata and controls
46 lines (33 loc) · 826 Bytes
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
//
// Created by kubab on 11/11/2023.
//
#include "button.h"
button::button(int pin) {
this->pin = pin;
gpio_init(pin);
gpio_set_dir(pin, GPIO_IN);
gpio_pull_up(pin);
}
button::~button() {
gpio_deinit(this->pin);
}
bool button::is_pressed() {
this->last_state = !gpio_get(this->pin);
return last_state;
}
bool button::is_released() {
this->last_state = gpio_get(this->pin);
return this->last_state;
}
bool button::is_pressed(uint32_t &delay) {
if (this->is_released()) return false;
uint32_t start_time = to_ms_since_boot(get_absolute_time());
while (this->is_pressed()) { ;
}
uint32_t current_time = to_ms_since_boot(get_absolute_time());
delay = start_time - current_time;
return true;
}
bool button::last_stage() {
return this->last_state;
}