-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSupportFunctions.cpp
More file actions
86 lines (68 loc) · 2 KB
/
Copy pathSupportFunctions.cpp
File metadata and controls
86 lines (68 loc) · 2 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
#include <Arduino.h>
#include "SupportFunctions.h"
// constructor
SupportFunctions::SupportFunctions()
{
}
String SupportFunctions::getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = {
0, -1 };
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
double SupportFunctions::rgbToHue(byte r, byte g, byte b) {
double rd = (double) r/255;
double gd = (double) g/255;
double bd = (double) b/255;
double max = threeway_max(rd, gd, bd);
double min = threeway_min(rd, gd, bd);
double h, s, l = (max + min) / 2;
if (max == min) {
h = s = 0; // achromatic
} else {
double d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
if (max == rd) {
h = (gd - bd) / d + (gd < bd ? 6 : 0);
} else if (max == gd) {
h = (bd - rd) / d + 2;
} else if (max == bd) {
h = (rd - gd) / d + 4;
}
h /= 6;
}
return h;
}
double SupportFunctions::threeway_max(double a, double b, double c) {
return max(a, max(b, c));
}
double SupportFunctions::threeway_min(double a, double b, double c) {
return min(a, min(b, c));
}
String SupportFunctions::macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
String SupportFunctions::randomStr(int length, String *randomstring)
{
char letters[] = {'a', 'b','c', 'd', 'e', 'f', 'g', 'h', 'i', 'j','k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z'};
const byte lettersLength = sizeof(letters) / sizeof(letters[0]);
for (int n = 0; n < length ; n++)
{
*randomstring = String(*randomstring + letters[random(0, lettersLength)]);
}
}