forked from jasoncoon/esp8266-fastled-webserver
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Field.h
104 lines (84 loc) · 2.67 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
/*
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 {
String name;
String label;
String type;
uint8_t min;
uint8_t max;
FieldGetter getValue;
FieldGetter getOptions;
FieldSetter setValue;
} Field;
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 += ",";
}
return json;
}
// EOF