-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.cpp
59 lines (48 loc) · 1.2 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
/**
* @file button.cpp
*
* @author Pedro Henrique Martins de Santi <[email protected]>
*
* @brief Implementation of Button class
*
* @date 2022-03-29
*
* @copyright Copyright (c) 2022
*
*/
#include <Arduino.h>
#include "button.h"
/*****************************************
* Public Functions
*****************************************/
Button::Button(int GPIO_pin, const char* topic) {
this->GPIO_pin = GPIO_pin;
this->topic = (char*) topic;
// this->topic = topic; // with std::string (must be converted with c_str() afterwards)
}
Button::~Button() {
}
bool Button::is_pressed() {
return (!(this->previous_state) && this->current_state);
}
bool Button::is_released() {
return (this->previous_state && !(this->current_state));
}
bool Button::has_changed() {
this->previous_state = this->current_state;
this->current_state = digitalRead(this->GPIO_pin);
return (this->current_state != this->previous_state);
}
char* Button::get_topic() {
return this->topic;
}
char* Button::get_string_state() {
if (this->current_state) {
return (char*) ("1");
} else {
return (char*) ("0");
}
}
bool Button::get_state() {
return this->current_state;
}