forked from sfranzyshen/esp8266-fastled-webserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Field.h
130 lines (103 loc) · 3.89 KB
/
Field.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
122
123
124
125
126
127
128
129
130
/*
ESP8266 + FastLED + IR Remote: https://github.com/jasoncoon/esp8266-fastled-webserver
Copyright (C) 2016 Jason Coon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
typedef String (*FieldSetter)(String);
typedef String (*FieldGetter)();
const String NumberFieldType = "Number";
const String BooleanFieldType = "Boolean";
const String SelectFieldType = "Select";
const String ColorFieldType = "Color";
const String SectionFieldType = "Section";
typedef struct Field {
String name;
String label;
String type;
uint8_t min;
uint8_t max;
FieldGetter getValue;
FieldGetter getOptions;
FieldSetter setValue;
};
typedef Field FieldList[];
Field getField(String name, FieldList fields, uint8_t count) {
for (uint8_t i = 0; i < count; i++) {
Field field = fields[i];
if (field.name == name) {
return field;
}
}
return Field();
}
String getFieldValue(String name, FieldList fields, uint8_t count) {
Field field = getField(name, fields, count);
if (field.getValue) {
return field.getValue();
}
return String();
}
String setFieldValue(String name, String value, FieldList fields, uint8_t count) {
Field field = getField(name, fields, count);
if (field.setValue) {
return field.setValue(value);
}
return String();
}
String getFieldsJson(FieldList fields, uint8_t count) {
String json = "[";
for (uint8_t i = 0; i < count; i++) {
Field field = fields[i];
json += "{\"name\":\"" + field.name + "\",\"label\":\"" + field.label + "\",\"type\":\"" + field.type + "\"";
if(field.getValue) {
if (field.type == ColorFieldType || field.type == "String") {
json += ",\"value\":\"" + field.getValue() + "\"";
}
else {
json += ",\"value\":" + field.getValue();
}
}
if (field.type == NumberFieldType) {
json += ",\"min\":" + String(field.min);
json += ",\"max\":" + String(field.max);
}
if (field.getOptions) {
json += ",\"options\":[";
json += field.getOptions();
json += "]";
}
json += "}";
if (i < count - 1)
json += ",";
}
json += "]";
return json;
}
/*
String json = "[";
json += "{\"name\":\"power\",\"label\":\"Power\",\"type\":\"Boolean\",\"value\":" + String(power) + "},";
json += "{\"name\":\"brightness\",\"label\":\"Brightness\",\"type\":\"Number\",\"value\":" + String(brightness) + "},";
json += "{\"name\":\"pattern\",\"label\":\"Pattern\",\"type\":\"Select\",\"value\":" + String(currentPatternIndex) + ",\"options\":[";
for (uint8_t i = 0; i < patternCount; i++)
{
json += "\"" + patterns[i].name + "\"";
if (i < patternCount - 1)
json += ",";
}
json += "]},";
json += "{\"name\":\"autoplay\",\"label\":\"Autoplay\",\"type\":\"Boolean\",\"value\":" + String(autoplay) + "},";
json += "{\"name\":\"autoplayDuration\",\"label\":\"Autoplay Duration\",\"type\":\"Number\",\"value\":" + String(autoplayDuration) + "},";
json += "{\"name\":\"solidColor\",\"label\":\"Color\",\"type\":\"Color\",\"value\":\"" + String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b) +"\"},";
json += "{\"name\":\"cooling\",\"label\":\"Cooling\",\"type\":\"Number\",\"value\":" + String(cooling) + "},";
json += "{\"name\":\"sparking\",\"label\":\"Sparking\",\"type\":\"Number\",\"value\":" + String(sparking) + "}";
json += "]";
*/