-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonHandler.h
121 lines (98 loc) · 4.09 KB
/
ButtonHandler.h
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//Registers a button press
void handlePress(int keyNumber) {
if (chosingFile) {
chosingFile = false;
chooseFile(keyNumber);
}
else if (altKeyPressed && !chosingFile) {
handleBoardChange(keyNumber);
altKeyPressed = false;
}
else if (!altKeyPressed && !chosingFile) {
if (buttons["pages"][page]["buttons"][keyNumber - 1]["type"].as<String>() == "socket") {
handleSocketCommand(buttons["pages"][page]["buttons"][keyNumber - 1]["command_id"].as<String>());
} else if (buttons["pages"][page]["buttons"][keyNumber - 1]["type"].as<String>() == "apiCall") {
if (buttons["pages"][page]["buttons"][keyNumber - 1].containsKey("fingerprint")) {
handleApiCall(buttons["pages"][page]["buttons"][keyNumber - 1]["url"].as<String>(), buttons["pages"][page]["buttons"][keyNumber - 1]["requestType"].as<String>(), buttons["pages"][page]["buttons"][keyNumber - 1]["headers"].to<JsonArray>(), buttons["pages"][page]["buttons"][keyNumber - 1]["body"].as<String>(), buttons["pages"][page]["buttons"][keyNumber - 1]["fingerprint"].as<String>());
} else {
handleApiCall(buttons["pages"][page]["buttons"][keyNumber - 1]["url"].as<String>(), buttons["pages"][page]["buttons"][keyNumber - 1]["requestType"].as<String>(), buttons["pages"][page]["buttons"][keyNumber - 1]["headers"].to<JsonArray>(), buttons["pages"][page]["buttons"][keyNumber - 1]["body"].as<String>());
}
}else if(buttons["pages"][page]["buttons"][keyNumber - 1]["type"].as<String>() == "mqtt"){
callMqttCommand(buttons["pages"][page]["buttons"][keyNumber - 1]["topic"].as<String>(), buttons["pages"][page]["buttons"][keyNumber - 1]["payload"].as<String>());
}
else {
tft.setTextColor(TFT_RED, TFT_WHITE);
tft.println("This file is not compatible: " + BUTTON_JSON[currentFile]);
tft.println();
delay(5000);
error = true;
}
}
}
bool hasInput() {
return analogRead(AD_PIN) > buttonConfigJson["defaultValue"];
}
bool checkButtonValue(int minValue, int maxValue, int buttonId) {
if (analogRead(AD_PIN) >= minValue && analogRead(AD_PIN) <= maxValue) {
if (buttonId == 16) {
if (!altKeyPressed) {
altKeyPressed = true;
interateOverPages();
} else if (altKeyPressed) {
interateOverFiles();
chosingFile = true;
}
}
else {
handlePress(buttonId);
}
is_button_pressed = true;
}
}
void iterateThroughButtons() {
for (int i = 0; i < buttonConfigJson["buttons"].size(); i++) {
checkButtonValue(buttonConfigJson["buttons"][i]["minValue"], buttonConfigJson["buttons"][i]["maxValue"], buttonConfigJson["buttons"][i]["buttonValue"]);
}
}
void handleButtonPress() {
if (!hasInput() && is_button_pressed) {
is_button_pressed = false;
}
else if (hasInput() && !is_button_pressed) {
iterateThroughButtons();
}
}
void calibrateButtons(int pinV) {
int offSet = (pinV * 2);
buttonConfigDoc["defaultValue"] = pinV;
int lastButtonVoltMin = pinV - offSet;
int lastButtonVoltMax = pinV + offSet;
for (int i = 0; i < buttonConfigJson["buttons"].size(); i++) {
clearScreen();
tft.print("hold button :");
tft.println(i + 1);
tft.println(analogRead(AD_PIN));
while ((analogRead(AD_PIN) > (pinV - offSet)) && (analogRead(AD_PIN) < (pinV + offSet))) {
delay(500);
tft.println(analogRead(AD_PIN));
}
while ((analogRead(AD_PIN) > lastButtonVoltMin) && (analogRead(AD_PIN) < lastButtonVoltMax)) {
delay(500);
tft.println(analogRead(AD_PIN));
}
lastButtonVoltMin = analogRead(AD_PIN) - offSet;
lastButtonVoltMax = analogRead(AD_PIN) + offSet;
tft.println(analogRead(AD_PIN));
tft.println(lastButtonVoltMin);
tft.println(lastButtonVoltMax);
buttonConfigDoc["buttons"][i]["minValue"] = lastButtonVoltMin;
buttonConfigDoc["buttons"][i]["maxValue"] = lastButtonVoltMax;
buttonConfigDoc["buttons"][i]["buttonValue"] = i + 1;
tft.println("DONE");
delay(2000);
}
writeToButtonConfigFile();
configDoc["calibrationMode"] = false;
writeToConfigFile();
ESP.restart();
}