-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControl.cpp
More file actions
112 lines (101 loc) · 3.03 KB
/
Control.cpp
File metadata and controls
112 lines (101 loc) · 3.03 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
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
#include "Control.h"
void Control::setup() {
client.connect("192.168.0.236", 2389);
Storage::setOffset(15);
strip.setLength(Storage::getCount());
strip.setBrightness(Storage::getBrightness(), true);
if (Storage::getIsOn()) {
StripPattern pattern = Storage::getStripPattern();
strip.showPattern(pattern);
}
Serial.println("control setup done");
}
void Control::loop() {
ArduinoOTA.handle();
strip.update();
DynamicJsonDocument data = Utils::stringToJSON(readData());
String command = data["command"];
if (!data.isNull()) {
if (command == "leds") {
StripPattern pattern = Utils::dataToStripPattern(
data["data"]["pattern"], data["data"]["colors"],
data["data"]["timeout"]);
bool noFade = (bool)data["data"]["noFade"];
strip.showPattern(pattern, noFade);
Storage::setStripPattern(pattern);
}
if (command == "on") {
Storage::setIsOn(true);
strip.showCurrentPattern();
}
if (command == "off") {
Storage::setIsOn(false);
strip.stopRunning();
strip.showOff();
}
if (command == "brightness") {
strip.setBrightness((int)data["data"]);
Storage::setBrightness((int)data["data"]);
}
if (command == "blink") {
RGB color = Utils::stringToRGB(data["data"]["color"]);
strip.showColor(color, true);
delay(int(data["data"]["time"]));
strip.showCurrentPattern();
}
if (command == "custom") {
strip.stopRunning();
JsonArray array = data["data"];
Storage::setCustom(Utils::jsonArrayToVector(array));
StripPattern pattern;
pattern.pattern = 6;
Storage::setStripPattern(pattern);
strip.showPattern(pattern);
}
if (command == "count") {
int count = (int)data["data"];
strip.setLength(count);
}
if (command == "serverRestart") {
delay(5000);
client.write("shutdown\n");
client.stop();
ESP.restart();
}
if (command == "restart") {
client.write("shutdown\n");
client.stop();
strip.showOff(true);
ESP.restart();
}
if (command == "reset") {
Storage::clear();
strip.showOff(true);
client.write("shutdown\n");
client.stop();
ESP.restart();
}
if (command == "logStorage") {
Storage::print();
}
}
}
String Control::readData() {
// read data from server if available
if (!client) {
return "";
}
if (!client.connected()) {
return "";
}
String readString = "";
while (client.available()) {
char c = client.read();
if (c == '\n') {
break;
}
readString += c;
yield();
}
return readString;
}