-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
170 lines (153 loc) · 4.69 KB
/
Utils.cpp
File metadata and controls
170 lines (153 loc) · 4.69 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "Utils.h"
using namespace std;
String Utils::ipToString(IPAddress ip) {
return String(ip[0]) + "." + String(ip[1]) + "." + String(ip[2]) + "." +
String(ip[3]);
}
DynamicJsonDocument Utils::stringToJSON(String raw) {
DynamicJsonDocument doc(2048);
if (raw == "") {
return doc;
}
deserializeJson(doc, raw);
return doc;
}
RGB Utils::stringToRGB(String colorString) {
RGB color;
StringSplitter *splitter = new StringSplitter(colorString, '.', 3);
color.r = splitter->getItemAtIndex(0).toInt();
color.g = splitter->getItemAtIndex(1).toInt();
color.b = splitter->getItemAtIndex(2).toInt();
delete splitter;
return color;
}
std::vector<RGB> Utils::jsonArrayToVector(JsonArray array) {
std::vector<RGB> colors;
int i = 0;
for (String str_color : array) {
RGB color = Utils::stringToRGB(str_color);
colors.push_back(color);
}
return colors;
}
String Utils::patternIntToString(int patternNr) {
switch (patternNr) {
case 1:
return "plain";
case 2:
return "fading";
case 3:
return "gradient";
case 4:
return "runner";
case 5:
return "rainbow";
default:
return "error";
}
}
StripPattern Utils::dataToStripPattern(String patternString,
JsonArray colorArray, int timeout) {
StripPattern pattern;
if (patternString == "plain") {
pattern.pattern = 1;
}
if (patternString == "fading") {
pattern.pattern = 2;
}
if (patternString == "gradient") {
pattern.pattern = 3;
}
if (patternString == "runner") {
pattern.pattern = 4;
}
if (patternString == "rainbow") {
pattern.pattern = 5;
}
for (int i = 0; i < 10; i++) {
pattern.colors[i] = stringToRGB(colorArray[i]);
}
pattern.timeout = timeout;
return pattern;
}
int Utils::RGBToHue(RGB color) {
float r = color.r / 255.0f;
float g = color.g / 255.0f;
float b = color.b / 255.0f;
float min = std::min(r, std::min(g, b));
float max = std::max(r, std::max(g, b));
float hue = 0;
if (r == max) hue = (g - b) / (max - min);
if (g == max) hue = 2 + (b - r) / (max - min);
if (b == max) hue = 4 + (r - g) / (max - min);
return hue * 60;
}
std::vector<RGB> Utils::generatePixelsOff(int length) {
std::vector<RGB> arr;
arr.resize(length);
for (int i = 0; i < length; i++) {
arr[i] = RGB({0, 0, 0});
}
return arr;
}
std::vector<RGB> Utils::generatePixelsColor(int length, RGB color) {
std::vector<RGB> arr;
arr.resize(length);
std::fill(arr.begin(), arr.end(), color);
return arr;
}
std::vector<RGB> Utils::generatePixels(int length, StripPattern pattern,
int startIndex) {
int offset = Storage::getOffset();
std::vector<RGB> arr;
arr.resize(length);
std::fill(arr.begin(), arr.begin() + offset - 1, RGB({0, 0, 0}));
RGB firstColor = pattern.colors[0];
RGB secondColor = pattern.colors[1];
switch (pattern.pattern) {
default:
case 1:
case 2:
case 5:
std::fill(arr.begin() + offset, arr.end(), firstColor);
break;
case 3:
for (int i = offset; i < length; i++) {
arr[i] = interpolateColor(firstColor, secondColor, i - offset, (length - offset));
}
break;
case 4:
std::fill(arr.begin() + offset, arr.end(), RGB({0, 0, 0}));
std::fill(arr.begin() + startIndex + offset,
arr.begin() + startIndex + offset +
ceil((length - offset) / 15),
firstColor);
break;
case 6:
vector<RGB> n = Storage::getCustom();
arr.resize(offset);
for(int i = 0; i < n.size(); i++){
arr.push_back(n[i]);
}
arr.resize(length, RGB({0, 0, 0}));
break;
}
return arr;
}
RGB Utils::interpolateColor(RGB first, RGB second, int step, int steps) {
int r = interpolateValue(first.r, second.r, step, steps);
int g = interpolateValue(first.g, second.g, step, steps);
int b = interpolateValue(first.b, second.b, step, steps);
return RGB({r, g, b});
}
int Utils::interpolateValue(int first, int second, int step, int steps) {
return (first * (steps - step) + second * step) / steps;
}
void Utils::blink(int runs, int secs_on, int secs_off) {
for (int i = 0; i < runs; i++) {
digitalWrite(LED_BUILTIN, LOW);
delay(secs_on);
digitalWrite(LED_BUILTIN, HIGH);
delay(secs_off);
}
}