-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRadioButtons.cpp
60 lines (53 loc) · 1.58 KB
/
RadioButtons.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
#include "RadioButtons.h"
//#define DEBUG
#ifdef DEBUG
#include <iostream>
#endif
void RadioButtons::init(unsigned char * buttonIndexes, unsigned char count) {
buttonIndexes_ = buttonIndexes;
buttonCount_ = count;
selectedButton_ = 0;
lastDownButton_ = 255;
for (unsigned char i = 0; i < buttonCount_; i++) {
if (LEDsAndButtonsHWWrapper::isButtonDown(buttonIndexes_[i])) {
lastDownButton_ = i;
}
}
buttonSelected_ = false;
}
void RadioButtons::update() {
for (unsigned char i = 0; i < buttonCount_; i++) {
bool buttonDown = LEDsAndButtonsHWWrapper::isButtonDown(buttonIndexes_[i]);
#ifdef DEBUG
printf("Button: %i %s Last button down: %i\n", buttonIndexes_[i], buttonDown ? "DOWN" : "UP", lastDownButton_);
#endif
if (buttonDown) {
//Button that is down is the same as previous one so just ignore everything
if (lastDownButton_ == i) {
return;
}
if (!buttonSelected_ || selectedButton_ != i) {
setSelectedButton(i);
} else {
resetSelection();
}
lastDownButton_ = i;
return;
}
//Disable lastButtonDown by value that hopefully will never occur
}
lastDownButton_ = 255;
}
void RadioButtons::setSelectedButton(unsigned char selectedButton) {
resetSelection();
selectedButton_ = selectedButton;
buttonSelected_ = true;
}
bool RadioButtons::getSelectedButton(unsigned char &selectedButton)
{
if (buttonSelected_) {
selectedButton = selectedButton_;
return true;
}
return false;
}