forked from arduino/BasicEducationShield
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButtonGroup.cpp
49 lines (42 loc) · 1015 Bytes
/
ButtonGroup.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
#include "BasicEducationShield.h"
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
ButtonGroup::ButtonGroup(){
for(int i=0;i<BUTTONGROUP_LENGTH;i++){
this->buttons[i]=-1;
}
}
void ButtonGroup::begin(int length, int buttons[],bool pressedValue){
for(int i=0;i<length;i++){
this->buttons[i]=buttons[i];
//this->iStarted[i]=false;
}
this->buttonsCount=length;
this->pressedValue=pressedValue;
}
int ButtonGroup::pressed(int timeout){
return this->checkPress(timeout, this->pressedValue);
}
int ButtonGroup::checkPress(int timeout, bool requiredValue){
bool iStarted[this->buttonsCount];
for(int i=0;i<this->buttonsCount;i++){
iStarted[i]=false;
}
long timer=millis();
while(!timeout || millis()-timer<=timeout){
for(int i=0;i<this->buttonsCount;i++){
bool stat=digitalRead(this->buttons[i]);
if(stat==!requiredValue && !iStarted[i]){
iStarted[i]=true;
}else{
if(iStarted[i] && stat==requiredValue){
return i;
}
}
}
}
return -1;
}