-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSwitches.cpp
72 lines (64 loc) · 1.81 KB
/
Switches.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
#include "Switches.h"
#include "BitArrayOperations.h"
//#define DEBUG
#ifdef DEBUG
#include <cstdio>
#endif
Switches::Switches() :
buttonIndexes_(0),
buttonCount_(0),
lastStates_(0),
statuses_(0),
changeOnEvent_(IButtonHW::DOWN),
useLEDs_(false)
{
}
void Switches::init(unsigned char * buttonIndexes,
unsigned char count,
bool useLEDs,
IButtonHW::ButtonState changeOnEvent) {
buttonIndexes_ = buttonIndexes;
buttonCount_ = count;
changeOnEvent_ = changeOnEvent;
useLEDs_ = useLEDs;
ignoreButton_ = count;
}
void Switches::computeIgnoreButton() {
for (unsigned char i = 0; i < buttonCount_; i++) {
if (LEDsAndButtonsHWWrapper::isButtonDown(buttonIndexes_[i])) {
ignoreButton_ = i;
}
}
}
void Switches::update() {
for (unsigned char i = 0; i < buttonCount_; i++) {
bool buttonDown = LEDsAndButtonsHWWrapper::isButtonDown(buttonIndexes_[i]);
#ifdef DEBUG
printf("Button %d(%d) %s\n", i, buttonIndexes_[i], buttonDown ? "down" : "up");
#endif
if (((changeOnEvent_ == IButtonHW::DOWN) && !GETBIT(lastStates_, i) && buttonDown) ||
((changeOnEvent_ == IButtonHW::UP) && GETBIT(lastStates_, i) && !buttonDown) ) {
if (ignoreButton_ == i) {
ignoreButton_ = buttonCount_ + 1;
} else {
setStatus(i, !GETBIT(statuses_, i));
}
}
setBit(lastStates_, i, buttonDown);
}
}
bool Switches::getStatus(unsigned char buttonIndex)
{
return GETBIT(statuses_, buttonIndex) == 1;
}
void Switches::setStatus(unsigned char buttonIndex, bool value)
{
if (value) {
SETBITTRUE(statuses_, buttonIndex);
} else {
SETBITFALSE(statuses_, buttonIndex);
}
if (useLEDs_) {
LEDsAndButtonsHWWrapper::setLED(buttonIndexes_[buttonIndex], value ? ILEDHW::ON : ILEDHW::OFF);
}
}