-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbtn.cpp
73 lines (64 loc) · 2.01 KB
/
modbtn.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
#include "modbtn.h"
Btn::Btn(byte pin,String bname,boolean btnpressed){
this->pin = pin;
this->btnname = bname;
this->stikyKey = btnpressed;
lastReading = LOW;
init();
}
void Btn::init(){
pinMode(pin,INPUT_PULLUP);
btnUpdate();
}
void Btn::btnUpdate()
{
byte newReading = digitalRead(this->pin);
if(newReading != lastReading){
lastDebounceTime =millis();
}
if((millis() - lastDebounceTime) > debounceDelay){
state = newReading;
}
lastReading = newReading;
}
byte Btn::getState(){
btnUpdate();
return state;
}
bool Btn::getStikyKey(){
return this->stikyKey;
}
void Btn::setStikyKey(bool key){
this->stikyKey = key;
}
bool Btn::isBtnPressed(){
return (getState() == HIGH);
}
String Btn::getBtnname(){
return this->btnname;
}
boolean Btn::isbuttonClicked(void){
if(this->isBtnPressed() && (this->getStikyKey() == false)){
this->setStikyKey(true);
this->btnClick = false;
return false;
}
if(!this->isBtnPressed() && (this->getStikyKey() == true)){
if(this->getStikyKey() == true){
this->btnClick = true;
this->btnClickCount += 1 ;
}
this->setStikyKey(false);
return true;
}
return false;
}
void Btn::debbuger(){
bool sbtn = getState();
String btnstate = String(sbtn ? "Pressed":"Released");
Serial<<"Button module\n"<<"Name:"<< btnname<<endl;
Serial<<"Status:"<<btnstate<<"\nValue:"<<sbtn<<endl;
Serial<<"Stiky key:"<<getStikyKey()<<endl;
Serial<<"Click Action:"<<this->btnClick<<endl;
Serial<<"Total Clicks:"<<this->btnClickCount<<"\n\n"<<endl;
}