Skip to content

Commit bf42538

Browse files
committed
Fix EEPROM defaults on first load.
Fix leading zeros missing in MAC based name.
1 parent cb41371 commit bf42538

File tree

2 files changed

+52
-51
lines changed

2 files changed

+52
-51
lines changed

Fields.h

+2-8
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,14 @@ String getClockBackgroundFade() {
9292
void setShowClock(uint8_t value)
9393
{
9494
showClock = value == 0 ? 0 : 1;
95-
96-
EEPROM.write(9, showClock);
97-
EEPROM.commit();
98-
95+
writeAndCommitSettings();
9996
broadcastInt("showClock", showClock);
10097
}
10198

10299
void setClockBackgroundFade(uint8_t value)
103100
{
104101
clockBackgroundFade = value;
105-
106-
EEPROM.write(10, clockBackgroundFade);
107-
EEPROM.commit();
108-
102+
writeAndCommitSettings();
109103
broadcastInt("clockBackgroundFade", clockBackgroundFade);
110104
}
111105

esp8266-fastled-webserver.ino

+50-43
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
GNU General Public License for more details.
1414
1515
You should have received a copy of the GNU General Public License
16-
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

1919
//#define FASTLED_ALLOW_INTERRUPTS 1
@@ -80,7 +80,7 @@ CRGB leds[NUM_LEDS];
8080

8181
const uint8_t brightnessCount = 5;
8282
uint8_t brightnessMap[brightnessCount] = { 16, 32, 64, 128, 255 };
83-
uint8_t brightnessIndex = 0;
83+
uint8_t brightnessIndex = 3;
8484

8585
// ten seconds per color palette makes a good demo
8686
// 20-120 is better for deployment
@@ -112,7 +112,7 @@ CRGBPalette16 gTargetPalette( gGradientPalettes[0] );
112112

113113
CRGBPalette16 IceColors_p = CRGBPalette16(CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);
114114

115-
uint8_t currentPatternIndex = 0; // Index number of which pattern is current
115+
uint8_t currentPatternIndex = 3; // Index number of which pattern is current
116116
uint8_t autoplay = 0;
117117

118118
uint8_t autoplayDuration = 10;
@@ -305,7 +305,7 @@ void setup() {
305305
FastLED.show();
306306

307307
EEPROM.begin(512);
308-
loadSettings();
308+
readSettings();
309309

310310
FastLED.setBrightness(brightness);
311311

@@ -320,6 +320,7 @@ void setup() {
320320
Serial.print( F("Flash ID: ") ); Serial.println(spi_flash_get_id());
321321
Serial.print( F("Flash Size: ") ); Serial.println(ESP.getFlashChipRealSize());
322322
Serial.print( F("Vcc: ") ); Serial.println(ESP.getVcc());
323+
Serial.print( F("MAC Address: ") ); Serial.println(WiFi.macAddress());
323324
Serial.println();
324325

325326
SPIFFS.begin();
@@ -337,17 +338,23 @@ void setup() {
337338

338339
// Do a little work to get a unique-ish name. Get the
339340
// last two bytes of the MAC (HEX'd)":
341+
342+
// copy the mac address to a byte array
340343
uint8_t mac[WL_MAC_ADDR_LENGTH];
341344
WiFi.softAPmacAddress(mac);
342-
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
343-
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
344-
macID.toUpperCase();
345345

346-
nameString = "Fibonacci256-" + macID;
346+
// format the last two digits to hex character array, like 0A0B
347+
char macID[5];
348+
sprintf(macID, "%02X%02X", mac[WL_MAC_ADDR_LENGTH - 2], mac[WL_MAC_ADDR_LENGTH - 1]);
349+
350+
// convert the character arry to a string
351+
String macIdString = macID;
352+
macIdString.toUpperCase();
353+
354+
nameString = "Fibonacci256-" + macIdString;
347355

348356
char nameChar[nameString.length() + 1];
349357
memset(nameChar, 0, nameString.length() + 1);
350-
351358
for (int i = 0; i < nameString.length(); i++)
352359
nameChar[i] = nameString.charAt(i);
353360

@@ -886,8 +893,15 @@ void loop() {
886893
// }
887894
//}
888895

889-
void loadSettings()
896+
void readSettings()
890897
{
898+
// check for "magic number" so we know settings have been written to EEPROM
899+
// and it's not just full of random bytes
900+
901+
if (EEPROM.read(511) != 55) {
902+
return;
903+
}
904+
891905
brightness = EEPROM.read(0);
892906

893907
currentPatternIndex = EEPROM.read(1);
@@ -923,33 +937,42 @@ void loadSettings()
923937
clockBackgroundFade = EEPROM.read(10);
924938
}
925939

926-
void setPower(uint8_t value)
940+
void writeAndCommitSettings()
927941
{
928-
power = value == 0 ? 0 : 1;
929-
942+
EEPROM.write(0, brightness);
943+
EEPROM.write(1, currentPatternIndex);
944+
EEPROM.write(2, solidColor.r);
945+
EEPROM.write(3, solidColor.g);
946+
EEPROM.write(4, solidColor.b);
930947
EEPROM.write(5, power);
948+
EEPROM.write(6, autoplay);
949+
EEPROM.write(7, autoplayDuration);
950+
EEPROM.write(8, currentPaletteIndex);
951+
EEPROM.write(9, showClock);
952+
EEPROM.write(10, clockBackgroundFade);
953+
954+
EEPROM.write(511, 55);
931955
EEPROM.commit();
956+
}
932957

958+
void setPower(uint8_t value)
959+
{
960+
power = value == 0 ? 0 : 1;
961+
writeAndCommitSettings();
933962
broadcastInt("power", power);
934963
}
935964

936965
void setAutoplay(uint8_t value)
937966
{
938967
autoplay = value == 0 ? 0 : 1;
939-
940-
EEPROM.write(6, autoplay);
941-
EEPROM.commit();
942-
968+
writeAndCommitSettings();
943969
broadcastInt("autoplay", autoplay);
944970
}
945971

946972
void setAutoplayDuration(uint8_t value)
947973
{
948974
autoplayDuration = value;
949-
950-
EEPROM.write(7, autoplayDuration);
951-
EEPROM.commit();
952-
975+
writeAndCommitSettings();
953976
autoPlayTimeout = millis() + (autoplayDuration * 1000);
954977

955978
broadcastInt("autoplayDuration", autoplayDuration);
@@ -963,12 +986,7 @@ void setSolidColor(CRGB color)
963986
void setSolidColor(uint8_t r, uint8_t g, uint8_t b)
964987
{
965988
solidColor = CRGB(r, g, b);
966-
967-
EEPROM.write(2, r);
968-
EEPROM.write(3, g);
969-
EEPROM.write(4, b);
970-
EEPROM.commit();
971-
989+
writeAndCommitSettings();
972990
setPattern(patternCount - 1);
973991

974992
broadcastString("color", String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b));
@@ -989,8 +1007,7 @@ void adjustPattern(bool up)
9891007
currentPatternIndex = 0;
9901008

9911009
if (autoplay == 0) {
992-
EEPROM.write(1, currentPatternIndex);
993-
EEPROM.commit();
1010+
writeAndCommitSettings();
9941011
}
9951012

9961013
broadcastInt("pattern", currentPatternIndex);
@@ -1004,8 +1021,7 @@ void setPattern(uint8_t value)
10041021
currentPatternIndex = value;
10051022

10061023
if (autoplay == 0) {
1007-
EEPROM.write(1, currentPatternIndex);
1008-
EEPROM.commit();
1024+
writeAndCommitSettings();
10091025
}
10101026

10111027
broadcastInt("pattern", currentPatternIndex);
@@ -1027,10 +1043,7 @@ void setPalette(uint8_t value)
10271043
value = paletteCount - 1;
10281044

10291045
currentPaletteIndex = value;
1030-
1031-
EEPROM.write(8, currentPaletteIndex);
1032-
EEPROM.commit();
1033-
1046+
writeAndCommitSettings();
10341047
broadcastInt("palette", currentPaletteIndex);
10351048
}
10361049

@@ -1054,10 +1067,7 @@ void adjustBrightness(bool up)
10541067
brightness = brightnessMap[brightnessIndex];
10551068

10561069
FastLED.setBrightness(brightness);
1057-
1058-
EEPROM.write(0, brightness);
1059-
EEPROM.commit();
1060-
1070+
writeAndCommitSettings();
10611071
broadcastInt("brightness", brightness);
10621072
}
10631073

@@ -1070,10 +1080,7 @@ void setBrightness(uint8_t value)
10701080
brightness = value;
10711081

10721082
FastLED.setBrightness(brightness);
1073-
1074-
EEPROM.write(0, brightness);
1075-
EEPROM.commit();
1076-
1083+
writeAndCommitSettings();
10771084
broadcastInt("brightness", brightness);
10781085
}
10791086

0 commit comments

Comments
 (0)