Skip to content

Commit cd735b4

Browse files
committed
Fields are now read to and written from EEPROM
1 parent c963afa commit cd735b4

File tree

4 files changed

+123
-69
lines changed

4 files changed

+123
-69
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Control addressable LEDs with an ESP32 via a web browser over Wi-Fi.
88
* [x] DemoReel100 patterns
99
* [x] [Sam's multi-core support](https://github.com/samguyer/FastLED/blob/master/examples/DemoReelESP32/DemoReelESP32.ino)
1010
* [x] Static web app file serving from SPIFFS
11-
* [x] Ability to adjust these parameters via the HTTP REST API:
11+
* [x] Ability to adjust these settings via the HTTP REST API:
1212
* [x] power on/off
1313
* [x] brightness
1414
* [x] pattern
@@ -21,10 +21,10 @@ Control addressable LEDs with an ESP32 via a web browser over Wi-Fi.
2121
* [x] solid color
2222
* [x] twinkle speed/density
2323
* [x] fire cooling/sparking
24+
* [x] Setting storage in EEPROM
2425

2526
### Currently Lacking:
26-
* [ ] Setting storage in EEPROM
27-
* [ ] More patterns
27+
* [ ] WebSockets for automatically refreshing/syncing web clients
2828

2929
## Requirements
3030

esp32-fastled-webserver.ino

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <ESP8266WebServer.h>
2828
#include <FS.h>
2929
#include <SPIFFS.h>
30+
#include <EEPROM.h>
3031

3132
#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001008)
3233
#warning "Requires FastLED 3.1.8 or later; check github for latest code."
@@ -180,6 +181,8 @@ void setup() {
180181
SPIFFS.begin();
181182
listDir(SPIFFS, "/", 1);
182183

184+
loadFieldsFromEEPROM(fields, fieldCount);
185+
183186
setupWifi();
184187
setupWeb();
185188

field.h

+110-36
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ const String ColorFieldType = "Color";
3232
const String SectionFieldType = "Section";
3333

3434
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;
4344
};
4445

4546
typedef Field FieldList[];
@@ -62,12 +63,110 @@ String getFieldValue(String name, FieldList fields, uint8_t count) {
6263
return String();
6364
}
6465

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+
65107
String setFieldValue(String name, String value, FieldList fields, uint8_t count) {
108+
String result;
109+
66110
Field field = getField(name, fields, count);
67111
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+
}
69169
}
70-
return String();
71170
}
72171

73172
String getFieldsJson(FieldList fields, uint8_t count) {
@@ -78,7 +177,7 @@ String getFieldsJson(FieldList fields, uint8_t count) {
78177

79178
json += "{\"name\":\"" + field.name + "\",\"label\":\"" + field.label + "\",\"type\":\"" + field.type + "\"";
80179

81-
if(field.getValue) {
180+
if (field.getValue) {
82181
if (field.type == ColorFieldType || field.type == "String") {
83182
json += ",\"value\":\"" + field.getValue() + "\"";
84183
}
@@ -109,28 +208,3 @@ String getFieldsJson(FieldList fields, uint8_t count) {
109208
return json;
110209
}
111210

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-
*/

fields.h

+7-30
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ String getBrightness() {
3838

3939
String setBrightness(String value) {
4040
brightness = value.toInt();
41-
// if (brightness < 0) brightness = 0;
42-
// else if (brightness > 255) brightness = 255;
4341
FastLED.setBrightness(brightness);
4442
return String(brightness);
4543
}
@@ -54,13 +52,6 @@ void setPattern(uint8_t value)
5452
value = patternCount - 1;
5553

5654
currentPatternIndex = value;
57-
58-
// if (autoplay == 0) {
59-
// EEPROM.write(1, currentPatternIndex);
60-
// EEPROM.commit();
61-
// }
62-
63-
// broadcastInt("pattern", currentPatternIndex);
6455
}
6556

6657
String setPattern(String value) {
@@ -163,31 +154,21 @@ String getSolidColor() {
163154
return String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b);
164155
}
165156

166-
void setSolidColor(uint8_t r, uint8_t g, uint8_t b)
157+
String setSolidColor(uint8_t r, uint8_t g, uint8_t b)
167158
{
168159
solidColor = CRGB(r, g, b);
169160

170-
// EEPROM.write(2, r);
171-
// EEPROM.write(3, g);
172-
// EEPROM.write(4, b);
173-
// EEPROM.commit();
174-
175-
setPattern(patternCount - 1);
176-
177-
// broadcastString("color", String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b));
161+
return String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b);
178162
}
179163

180-
void setSolidColor(CRGB color)
181-
{
182-
setSolidColor(color.r, color.g, color.b);
164+
String setSolidColor(CRGB color) {
165+
return setSolidColor(color.r, color.g, color.b);
183166
}
184167

185168
String setSolidColor(String value) {
186-
String r = webServer.arg("r");
187-
String g = webServer.arg("g");
188-
String b = webServer.arg("b");
189-
setSolidColor(r.toInt(), g.toInt(), b.toInt());
190-
return String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b);
169+
CRGB color = parseColor(value);
170+
171+
return setSolidColor(color);
191172
}
192173

193174
String getCooling() {
@@ -216,8 +197,6 @@ String setTwinkleSpeed(String value) {
216197
twinkleSpeed = value.toInt();
217198
if (twinkleSpeed < 0) twinkleSpeed = 0;
218199
else if (twinkleSpeed > 8) twinkleSpeed = 8;
219-
// broadcastInt("twinkleSpeed", twinkleSpeed);
220-
// sendInt(twinkleSpeed);
221200
return String(twinkleSpeed);
222201
}
223202

@@ -229,8 +208,6 @@ String setTwinkleDensity(String value) {
229208
twinkleDensity = value.toInt();
230209
if (twinkleDensity < 0) twinkleDensity = 0;
231210
else if (twinkleDensity > 8) twinkleDensity = 8;
232-
// broadcastInt("twinkleDensity", twinkleDensity);
233-
// sendInt(twinkleDensity);
234211
return String(twinkleDensity);
235212
}
236213

0 commit comments

Comments
 (0)