Skip to content

Commit

Permalink
Fix EEPROM defaults on first load.
Browse files Browse the repository at this point in the history
Fix leading zeros missing in MAC based name.
  • Loading branch information
jasoncoon committed Apr 5, 2021
1 parent cb41371 commit bf42538
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
10 changes: 2 additions & 8 deletions Fields.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,14 @@ String getClockBackgroundFade() {
void setShowClock(uint8_t value)
{
showClock = value == 0 ? 0 : 1;

EEPROM.write(9, showClock);
EEPROM.commit();

writeAndCommitSettings();
broadcastInt("showClock", showClock);
}

void setClockBackgroundFade(uint8_t value)
{
clockBackgroundFade = value;

EEPROM.write(10, clockBackgroundFade);
EEPROM.commit();

writeAndCommitSettings();
broadcastInt("clockBackgroundFade", clockBackgroundFade);
}

Expand Down
93 changes: 50 additions & 43 deletions esp8266-fastled-webserver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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/>.
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

//#define FASTLED_ALLOW_INTERRUPTS 1
Expand Down Expand Up @@ -80,7 +80,7 @@ CRGB leds[NUM_LEDS];

const uint8_t brightnessCount = 5;
uint8_t brightnessMap[brightnessCount] = { 16, 32, 64, 128, 255 };
uint8_t brightnessIndex = 0;
uint8_t brightnessIndex = 3;

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

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

uint8_t currentPatternIndex = 0; // Index number of which pattern is current
uint8_t currentPatternIndex = 3; // Index number of which pattern is current
uint8_t autoplay = 0;

uint8_t autoplayDuration = 10;
Expand Down Expand Up @@ -305,7 +305,7 @@ void setup() {
FastLED.show();

EEPROM.begin(512);
loadSettings();
readSettings();

FastLED.setBrightness(brightness);

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

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

// Do a little work to get a unique-ish name. Get the
// last two bytes of the MAC (HEX'd)":

// copy the mac address to a byte array
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();

nameString = "Fibonacci256-" + macID;
// format the last two digits to hex character array, like 0A0B
char macID[5];
sprintf(macID, "%02X%02X", mac[WL_MAC_ADDR_LENGTH - 2], mac[WL_MAC_ADDR_LENGTH - 1]);

// convert the character arry to a string
String macIdString = macID;
macIdString.toUpperCase();

nameString = "Fibonacci256-" + macIdString;

char nameChar[nameString.length() + 1];
memset(nameChar, 0, nameString.length() + 1);

for (int i = 0; i < nameString.length(); i++)
nameChar[i] = nameString.charAt(i);

Expand Down Expand Up @@ -886,8 +893,15 @@ void loop() {
// }
//}

void loadSettings()
void readSettings()
{
// check for "magic number" so we know settings have been written to EEPROM
// and it's not just full of random bytes

if (EEPROM.read(511) != 55) {
return;
}

brightness = EEPROM.read(0);

currentPatternIndex = EEPROM.read(1);
Expand Down Expand Up @@ -923,33 +937,42 @@ void loadSettings()
clockBackgroundFade = EEPROM.read(10);
}

void setPower(uint8_t value)
void writeAndCommitSettings()
{
power = value == 0 ? 0 : 1;

EEPROM.write(0, brightness);
EEPROM.write(1, currentPatternIndex);
EEPROM.write(2, solidColor.r);
EEPROM.write(3, solidColor.g);
EEPROM.write(4, solidColor.b);
EEPROM.write(5, power);
EEPROM.write(6, autoplay);
EEPROM.write(7, autoplayDuration);
EEPROM.write(8, currentPaletteIndex);
EEPROM.write(9, showClock);
EEPROM.write(10, clockBackgroundFade);

EEPROM.write(511, 55);
EEPROM.commit();
}

void setPower(uint8_t value)
{
power = value == 0 ? 0 : 1;
writeAndCommitSettings();
broadcastInt("power", power);
}

void setAutoplay(uint8_t value)
{
autoplay = value == 0 ? 0 : 1;

EEPROM.write(6, autoplay);
EEPROM.commit();

writeAndCommitSettings();
broadcastInt("autoplay", autoplay);
}

void setAutoplayDuration(uint8_t value)
{
autoplayDuration = value;

EEPROM.write(7, autoplayDuration);
EEPROM.commit();

writeAndCommitSettings();
autoPlayTimeout = millis() + (autoplayDuration * 1000);

broadcastInt("autoplayDuration", autoplayDuration);
Expand All @@ -963,12 +986,7 @@ void setSolidColor(CRGB color)
void setSolidColor(uint8_t r, uint8_t g, uint8_t b)
{
solidColor = CRGB(r, g, b);

EEPROM.write(2, r);
EEPROM.write(3, g);
EEPROM.write(4, b);
EEPROM.commit();

writeAndCommitSettings();
setPattern(patternCount - 1);

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

if (autoplay == 0) {
EEPROM.write(1, currentPatternIndex);
EEPROM.commit();
writeAndCommitSettings();
}

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

if (autoplay == 0) {
EEPROM.write(1, currentPatternIndex);
EEPROM.commit();
writeAndCommitSettings();
}

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

currentPaletteIndex = value;

EEPROM.write(8, currentPaletteIndex);
EEPROM.commit();

writeAndCommitSettings();
broadcastInt("palette", currentPaletteIndex);
}

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

FastLED.setBrightness(brightness);

EEPROM.write(0, brightness);
EEPROM.commit();

writeAndCommitSettings();
broadcastInt("brightness", brightness);
}

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

FastLED.setBrightness(brightness);

EEPROM.write(0, brightness);
EEPROM.commit();

writeAndCommitSettings();
broadcastInt("brightness", brightness);
}

Expand Down

0 comments on commit bf42538

Please sign in to comment.