From 41ad880ab3eba0b6c74ab88adca54079ec3b4fbf Mon Sep 17 00:00:00 2001 From: Edgar W Date: Sun, 3 Aug 2025 19:02:07 +0200 Subject: [PATCH] feat: add eeprom version code to be able to set default settings, Fixes #66 --- wordclock_esp8266.ino | 96 +++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 31 deletions(-) diff --git a/wordclock_esp8266.ino b/wordclock_esp8266.ino index b8d9046..9435069 100644 --- a/wordclock_esp8266.ino +++ b/wordclock_esp8266.ino @@ -48,20 +48,36 @@ // CONSTANTS // ---------------------------------------------------------------------------------- -#define EEPROM_SIZE 30 // size of EEPROM to save persistent variables -#define ADR_NM_START_H 0 -#define ADR_NM_END_H 4 -#define ADR_NM_START_M 8 -#define ADR_NM_END_M 12 -#define ADR_BRIGHTNESS 16 -#define ADR_MC_RED 20 -#define ADR_MC_GREEN 22 -#define ADR_MC_BLUE 24 -#define ADR_STATE 26 -#define ADR_NM_ACTIVATED 27 -#define ADR_COLSHIFTSPEED 28 -#define ADR_COLSHIFTACTIVE 29 - +#define EEPROM_VERSION_CODE 2 // Change this value when defaults settings change + +// EEPROM address map (all uint8_t, 1 byte each) +#define EEPROM_SIZE 13 // size of EEPROM to save persistent variables +#define ADR_EEPROM_VERSION 0 // uint8_t +#define ADR_NM_START_H 1 // uint8_t +#define ADR_NM_END_H 2 // uint8_t +#define ADR_NM_START_M 3 // uint8_t +#define ADR_NM_END_M 4 // uint8_t +#define ADR_BRIGHTNESS 5 // uint8_t +#define ADR_MC_RED 6 // uint8_t +#define ADR_MC_GREEN 7 // uint8_t +#define ADR_MC_BLUE 8 // uint8_t +#define ADR_STATE 9 // uint8_t +#define ADR_NM_ACTIVATED 10 // uint8_t +#define ADR_COLSHIFTSPEED 11 // uint8_t +#define ADR_COLSHIFTACTIVE 12 // uint8_t + +// DEFAULT SETTINGS (if one changes this, also increment the EEPROM_VERSION_CODE, to ensure that the EEPROM is updated with the new defaults) +#define DEFAULT_NM_START_HOUR 22 // default start hour of nightmode (0-23) +#define DEFAULT_NM_START_MIN 5 // default start minute of nightmode (0-59) +#define DEFAULT_NM_END_HOUR 7 // default end hour of nightmode (0-23) +#define DEFAULT_NM_END_MIN 0 // default end minute of nightmode (0-59) +#define DEFAULT_BRIGHTNESS 40 // default brightness of LEDs (0-255) +#define DEFAULT_MC_RED 200 // default main color red value +#define DEFAULT_MC_GREEN 200 // default main color green value +#define DEFAULT_MC_BLUE 0 // default main color blue value +#define DEFAULT_NM_ACTIVATED 1 // if function nightmode is activated (0 = deactivated, 1 = activated) +#define DEFAULT_COLSHIFT_SPEED 1 // needs to be between larger than 0 (1 = slowest, 255 = fastest) +#define DEFAULT_COLSHIFT_ACTIVE 0 // if dynamic color shift is active (0 = deactivated, 1 = activated) #define NEOPIXELPIN 5 // pin to which the NeoPixels are attached #define BUTTONPIN 14 // pin to which the button is attached @@ -159,7 +175,7 @@ const uint32_t colors24bit[NUM_COLORS] = { LEDMatrix::Color24bit(0, 128, 0), LEDMatrix::Color24bit(0, 0, 255) }; -uint8_t brightness = 40; // current brightness of leds +uint8_t brightness = DEFAULT_BRIGHTNESS; // current brightness of leds bool sprialDir = false; // timestamp variables @@ -182,24 +198,24 @@ Tetris mytetris = Tetris(&ledmatrix, &logger); Snake mysnake = Snake(&ledmatrix, &logger); Pong mypong = Pong(&ledmatrix, &logger); -float filterFactor = DEFAULT_SMOOTHING_FACTOR;// stores smoothing factor for led transition -uint8_t currentState = st_clock; // stores current state -bool stateAutoChange = false; // stores state of automatic state change -bool nightMode = false; // stores state of nightmode -bool nightModeActivated = true; // stores if the function nightmode is activated (its not the state of nightmode) -bool ledOff = false; // stores state of led off -uint32_t maincolor_clock = colors24bit[2]; // color of the clock and digital clock -uint32_t maincolor_snake = colors24bit[1]; // color of the random snake animation -bool apmode = false; // stores if WiFi AP mode is active -bool dynColorShiftActive = false; // stores if dynamic color shift is active -uint8_t dynColorShiftPhase = 0; // stores the phase of the dynamic color shift -uint8_t dynColorShiftSpeed = 1; // stores the speed of the dynamic color shift -> used to calc update period +float filterFactor = DEFAULT_SMOOTHING_FACTOR; // stores smoothing factor for led transition +uint8_t currentState = st_clock; // stores current state +bool stateAutoChange = false; // stores state of automatic state change +bool nightMode = false; // stores state of nightmode +bool nightModeActivated = DEFAULT_NM_ACTIVATED; // stores if the function nightmode is activated (its not the state of nightmode) +bool ledOff = false; // stores state of led off +uint32_t maincolor_clock = colors24bit[2]; // color of the clock and digital clock +uint32_t maincolor_snake = colors24bit[1]; // color of the random snake animation +bool apmode = false; // stores if WiFi AP mode is active +bool dynColorShiftActive = DEFAULT_COLSHIFT_ACTIVE; // stores if dynamic color shift is active +uint8_t dynColorShiftPhase = 0; // stores the phase of the dynamic color shift +uint8_t dynColorShiftSpeed = DEFAULT_COLSHIFT_SPEED; // stores the speed of the dynamic color shift -> used to calc update period // nightmode settings -uint8_t nightModeStartHour = 22; -uint8_t nightModeStartMin = 0; -uint8_t nightModeEndHour = 7; -uint8_t nightModeEndMin = 0; +uint8_t nightModeStartHour = DEFAULT_NM_START_HOUR; +uint8_t nightModeStartMin = DEFAULT_NM_START_MIN; +uint8_t nightModeEndHour = DEFAULT_NM_END_HOUR; +uint8_t nightModeEndMin = DEFAULT_NM_END_MIN; // Watchdog counter to trigger restart if NTP update was not possible 30 times in a row (5min) int watchdogCounter = 30; @@ -221,6 +237,24 @@ void setup() { //Init EEPROM EEPROM.begin(EEPROM_SIZE); + // Check EEPROM version code + uint8_t storedVersion = EEPROM.read(ADR_EEPROM_VERSION); + if (storedVersion != EEPROM_VERSION_CODE) { + // Set new defaults + EEPROM.write(ADR_EEPROM_VERSION, EEPROM_VERSION_CODE); + EEPROM.write(ADR_NM_START_H, DEFAULT_NM_START_HOUR); + EEPROM.write(ADR_NM_START_M, DEFAULT_NM_START_MIN); + EEPROM.write(ADR_NM_END_H, DEFAULT_NM_END_HOUR); + EEPROM.write(ADR_NM_END_M, DEFAULT_NM_END_MIN); + EEPROM.write(ADR_BRIGHTNESS, DEFAULT_BRIGHTNESS); + setMainColor(DEFAULT_MC_RED, DEFAULT_MC_GREEN, DEFAULT_MC_BLUE); + EEPROM.write(ADR_STATE, st_clock); + EEPROM.write(ADR_NM_ACTIVATED, DEFAULT_NM_ACTIVATED); + EEPROM.write(ADR_COLSHIFTSPEED, DEFAULT_COLSHIFT_SPEED); + EEPROM.write(ADR_COLSHIFTACTIVE, DEFAULT_COLSHIFT_ACTIVE); + EEPROM.commit(); + } + // configure button pin as input pinMode(BUTTONPIN, INPUT_PULLUP);