@@ -32,14 +32,15 @@ const String ColorFieldType = "Color";
32
32
const String SectionFieldType = " Section" ;
33
33
34
34
typedef struct Field {
35
- String name ;
36
- String label ;
37
- String type ;
38
- uint8_t min ;
39
- uint8_t max ;
40
- FieldGetter getValue ;
41
- FieldGetter getOptions ;
42
- FieldSetter setValue ;
35
+ public:
36
+ String name;
37
+ String label;
38
+ String type;
39
+ uint8_t min;
40
+ uint8_t max;
41
+ FieldGetter getValue;
42
+ FieldGetter getOptions;
43
+ FieldSetter setValue;
43
44
};
44
45
45
46
typedef Field FieldList[];
@@ -62,12 +63,110 @@ String getFieldValue(String name, FieldList fields, uint8_t count) {
62
63
return String ();
63
64
}
64
65
66
+ CRGB parseColor (String value) {
67
+ uint8_t ri = value.indexOf (" ," );
68
+ uint8_t gi = value.indexOf (" ," , ri + 1 );
69
+
70
+ String rs = value.substring (0 , ri);
71
+ String gs = value.substring (ri + 1 , gi);
72
+ String bs = value.substring (gi + 1 );
73
+
74
+ uint8_t r = rs.toInt ();
75
+ uint8_t g = gs.toInt ();
76
+ uint8_t b = bs.toInt ();
77
+
78
+ return CRGB (r, g, b);
79
+ }
80
+
81
+ void writeFieldsToEEPROM (FieldList fields, uint8_t count) {
82
+ uint8_t index = 0 ;
83
+
84
+ EEPROM.write (index , 0 );
85
+
86
+ for (uint8_t i = 0 ; i < count; i++) {
87
+ Field field = fields[i];
88
+ if (!field.getValue && !field.setValue )
89
+ continue ;
90
+
91
+ String value = field.getValue ();
92
+
93
+ if (field.type == ColorFieldType) {
94
+ CRGB color = parseColor (value);
95
+ EEPROM.write (index ++, color.r );
96
+ EEPROM.write (index ++, color.g );
97
+ EEPROM.write (index ++, color.b );
98
+ } else {
99
+ byte v = value.toInt ();
100
+ EEPROM.write (index ++, v);
101
+ }
102
+ }
103
+
104
+ EEPROM.commit ();
105
+ }
106
+
65
107
String setFieldValue (String name, String value, FieldList fields, uint8_t count) {
108
+ String result;
109
+
66
110
Field field = getField (name, fields, count);
67
111
if (field.setValue ) {
68
- return field .setValue (value );
112
+ if (field.type == ColorFieldType) {
113
+ String r = webServer.arg (" r" );
114
+ String g = webServer.arg (" g" );
115
+ String b = webServer.arg (" b" );
116
+ String combinedValue = r + " ," + g + " ," + b;
117
+ result = field.setValue (combinedValue);
118
+ } else {
119
+ result = field.setValue (value);
120
+ }
121
+ }
122
+
123
+ writeFieldsToEEPROM (fields, count);
124
+
125
+ return result;
126
+ }
127
+
128
+ void loadFieldsFromEEPROM (FieldList fields, uint8_t count) {
129
+ uint8_t byteCount = 1 ;
130
+
131
+ for (uint8_t i = 0 ; i < count; i++) {
132
+ Field field = fields[i];
133
+ if (!field.setValue )
134
+ continue ;
135
+
136
+ if (field.type == ColorFieldType) {
137
+ byteCount += 3 ;
138
+ } else {
139
+ byteCount++;
140
+ }
141
+ }
142
+
143
+ if (!EEPROM.begin (count)) {
144
+ Serial.println (" Failed to initialize EEPROM!" );
145
+ return ;
146
+ }
147
+
148
+ if (EEPROM.read (0 ) == 255 ) {
149
+ Serial.println (" First run, or EEPROM erased, skipping settings load!" );
150
+ return ;
151
+ }
152
+
153
+ uint8_t index = 0 ;
154
+
155
+ for (uint8_t i = 0 ; i < count; i++) {
156
+ Field field = fields[i];
157
+ if (!field.setValue )
158
+ continue ;
159
+
160
+ if (field.type == ColorFieldType) {
161
+ String r = String (EEPROM.read (index ++));
162
+ String g = String (EEPROM.read (index ++));
163
+ String b = String (EEPROM.read (index ++));
164
+ field.setValue (r + " ," + g + " ," + b);
165
+ } else {
166
+ byte v = EEPROM.read (index ++);
167
+ field.setValue (String (v));
168
+ }
69
169
}
70
- return String ();
71
170
}
72
171
73
172
String getFieldsJson (FieldList fields, uint8_t count) {
@@ -78,7 +177,7 @@ String getFieldsJson(FieldList fields, uint8_t count) {
78
177
79
178
json += " {\" name\" :\" " + field.name + " \" ,\" label\" :\" " + field.label + " \" ,\" type\" :\" " + field.type + " \" " ;
80
179
81
- if (field .getValue ) {
180
+ if (field.getValue ) {
82
181
if (field.type == ColorFieldType || field.type == " String" ) {
83
182
json += " ,\" value\" :\" " + field.getValue () + " \" " ;
84
183
}
@@ -109,28 +208,3 @@ String getFieldsJson(FieldList fields, uint8_t count) {
109
208
return json;
110
209
}
111
210
112
- /*
113
- String json = "[";
114
-
115
- json += "{\"name\":\"power\",\"label\":\"Power\",\"type\":\"Boolean\",\"value\":" + String(power) + "},";
116
- json += "{\"name\":\"brightness\",\"label\":\"Brightness\",\"type\":\"Number\",\"value\":" + String(brightness) + "},";
117
-
118
- json += "{\"name\":\"pattern\",\"label\":\"Pattern\",\"type\":\"Select\",\"value\":" + String(currentPatternIndex) + ",\"options\":[";
119
- for (uint8_t i = 0; i < patternCount; i++)
120
- {
121
- json += "\"" + patterns[i].name + "\"";
122
- if (i < patternCount - 1)
123
- json += ",";
124
- }
125
- json += "]},";
126
-
127
- json += "{\"name\":\"autoplay\",\"label\":\"Autoplay\",\"type\":\"Boolean\",\"value\":" + String(autoplay) + "},";
128
- json += "{\"name\":\"autoplayDuration\",\"label\":\"Autoplay Duration\",\"type\":\"Number\",\"value\":" + String(autoplayDuration) + "},";
129
-
130
- json += "{\"name\":\"solidColor\",\"label\":\"Color\",\"type\":\"Color\",\"value\":\"" + String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b) +"\"},";
131
-
132
- json += "{\"name\":\"cooling\",\"label\":\"Cooling\",\"type\":\"Number\",\"value\":" + String(cooling) + "},";
133
- json += "{\"name\":\"sparking\",\"label\":\"Sparking\",\"type\":\"Number\",\"value\":" + String(sparking) + "}";
134
-
135
- json += "]";
136
- */
0 commit comments