-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathButtons.cpp
More file actions
74 lines (61 loc) · 1.6 KB
/
Buttons.cpp
File metadata and controls
74 lines (61 loc) · 1.6 KB
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
74
#include "Buttons.h"
extern Logger logger;
void Buttons::enable() {
_enabled = true;
Serial.end();
for (int i = 0; i < 4; ++i) {
pinMode(_pinButtons[i], OUTPUT);
digitalWrite(_pinButtons[i], LOW);
}
logger.logln("Buttons enabled");
}
void Buttons::disable() {
_enabled = false;
Serial.begin(115200);
while(!Serial);
delay(100);
logger.logln("Buttons disabled");
}
void Buttons::pressButton(int pin) {
digitalWrite(pin, HIGH);
delay(_debouncePressMs);
digitalWrite(pin, LOW);
delay(_debounceReleaseMs);
}
void Buttons::press(const String& btn) {
logger.log("Pressing ");
logger.logln(btn);
int idx = -1;
if (btn == "set") idx = SET;
else if (btn == "left") idx = LEFT;
else if (btn == "down") idx = DOWN;
else if (btn == "up") idx = UP;
if (idx >= 0) pressButton(_pinButtons[idx]);
}
void Buttons::simulateSequence(int target, int current) {
logger.log("Entering setting mode (current: ");
logger.log(String(current));
logger.log(", target: ");
logger.log(String(target));
logger.logln(")");
press("set");
int curr[4], tgt[4];
for (int i = 0; i < 4; ++i) {
curr[i] = current % 10;
tgt[i] = target % 10;
current /= 10;
target /= 10;
}
for (int i = 0; i < 4; ++i) {
logger.log("Digit ");
logger.logln(String(i));
if (curr[i] < tgt[i]) {
for (int j = 0; j < tgt[i] - curr[i]; ++j) press("up");
} else if (curr[i] > tgt[i]) {
for (int j = 0; j < curr[i] - tgt[i]; ++j) press("down");
}
if (i < 3) press("left");
}
press("set");
logger.logln("Exiting setting mode");
}