4848// CONSTANTS
4949// ----------------------------------------------------------------------------------
5050
51- #define EEPROM_SIZE 32 // size of EEPROM to save persistent variables
52- #define ADR_NM_START_H 0
53- #define ADR_NM_END_H 4
54- #define ADR_NM_START_M 8
55- #define ADR_NM_END_M 12
56- #define ADR_BRIGHTNESS 16
57- #define ADR_MC_RED 20
58- #define ADR_MC_GREEN 22
59- #define ADR_MC_BLUE 24
60- #define ADR_STATE 26
61- #define ADR_NM_ACTIVATED 27
62- #define ADR_COLSHIFTSPEED 28
63- #define ADR_COLSHIFTACTIVE 29
64- #define ADR_HOURANIMATION 30
65-
51+ #define EEPROM_VERSION_CODE 3 // Change this value when defaults settings change
52+
53+ // EEPROM address map (all uint8_t, 1 byte each)
54+ #define EEPROM_SIZE 15 // size of EEPROM to save persistent variables
55+ #define ADR_EEPROM_VERSION 0 // uint8_t
56+ #define ADR_NM_START_H 1 // uint8_t
57+ #define ADR_NM_END_H 2 // uint8_t
58+ #define ADR_NM_START_M 3 // uint8_t
59+ #define ADR_NM_END_M 4 // uint8_t
60+ #define ADR_BRIGHTNESS 5 // uint8_t
61+ #define ADR_MC_RED 6 // uint8_t
62+ #define ADR_MC_GREEN 7 // uint8_t
63+ #define ADR_MC_BLUE 8 // uint8_t
64+ #define ADR_STATE 9 // uint8_t
65+ #define ADR_NM_ACTIVATED 10 // uint8_t
66+ #define ADR_COLSHIFTSPEED 11 // uint8_t
67+ #define ADR_COLSHIFTACTIVE 12 // uint8_t
68+ #define ADR_NM_BRIGHTNESS 13 // uint8_t
69+ #define ADR_HOURANIMATION 14 // uint8_t
70+
71+ // DEFAULT SETTINGS (if one changes this, also increment the EEPROM_VERSION_CODE, to ensure that the EEPROM is updated with the new defaults)
72+ #define DEFAULT_NM_START_HOUR 22 // default start hour of nightmode (0-23)
73+ #define DEFAULT_NM_START_MIN 5 // default start minute of nightmode (0-59)
74+ #define DEFAULT_NM_END_HOUR 7 // default end hour of nightmode (0-23)
75+ #define DEFAULT_NM_END_MIN 0 // default end minute of nightmode (0-59)
76+ #define DEFAULT_BRIGHTNESS 40 // default brightness of LEDs (10-255)
77+ #define DEFAULT_MC_RED 200 // default main color red value
78+ #define DEFAULT_MC_GREEN 200 // default main color green value
79+ #define DEFAULT_MC_BLUE 0 // default main color blue value
80+ #define DEFAULT_NM_ACTIVATED 1 // if function nightmode is activated (0 = deactivated, 1 = activated)
81+ #define DEFAULT_NM_BRIGHTNESS 0 // default brightness during night mode (0-255)
82+ #define DEFAULT_COLSHIFT_SPEED 1 // needs to be between larger than 0 (1 = slowest, 255 = fastest)
83+ #define DEFAULT_COLSHIFT_ACTIVE 0 // if dynamic color shift is active (0 = deactivated, 1 = activated)
6684
6785#define NEOPIXELPIN 5 // pin to which the NeoPixels are attached
6886#define BUTTONPIN 14 // pin to which the button is attached
@@ -160,7 +178,7 @@ const uint32_t colors24bit[NUM_COLORS] = {
160178 LEDMatrix::Color24bit (0 , 128 , 0 ),
161179 LEDMatrix::Color24bit (0 , 0 , 255 ) };
162180
163- uint8_t brightness = 40 ; // current brightness of leds
181+ uint8_t brightness = DEFAULT_BRIGHTNESS ; // current brightness of leds
164182bool animationDir = false ;
165183
166184// timestamp variables
@@ -184,26 +202,27 @@ Tetris mytetris = Tetris(&ledmatrix, &logger);
184202Snake mysnake = Snake(&ledmatrix, &logger);
185203Pong mypong = Pong(&ledmatrix, &logger);
186204
187- float filterFactor = DEFAULT_SMOOTHING_FACTOR;// stores smoothing factor for led transition
188- uint8_t currentState = st_clock; // stores current state
189- bool stateAutoChange = false ; // stores state of automatic state change
190- bool nightMode = false ; // stores state of nightmode
191- bool nightModeActivated = true ; // stores if the function nightmode is activated (its not the state of nightmode)
192- bool ledOff = false ; // stores state of led off
193- uint32_t maincolor_clock = colors24bit[2 ]; // color of the clock and digital clock
194- uint32_t maincolor_snake = colors24bit[1 ]; // color of the random snake animation
195- bool apmode = false ; // stores if WiFi AP mode is active
196- bool dynColorShiftActive = false ; // stores if dynamic color shift is active
197- uint8_t dynColorShiftPhase = 0 ; // stores the phase of the dynamic color shift
198- uint8_t dynColorShiftSpeed = 1 ; // stores the speed of the dynamic color shift -> used to calc update period
205+ float filterFactor = DEFAULT_SMOOTHING_FACTOR; // stores smoothing factor for led transition
206+ uint8_t currentState = st_clock; // stores current state
207+ bool stateAutoChange = false ; // stores state of automatic state change
208+ bool nightMode = false ; // stores state of nightmode
209+ bool nightModeActivated = DEFAULT_NM_ACTIVATED; // stores if the function nightmode is activated (its not the state of nightmode)
210+ bool ledOff = false ; // stores state of led off
211+ uint32_t maincolor_clock = colors24bit[2 ]; // color of the clock and digital clock
212+ uint32_t maincolor_snake = colors24bit[1 ]; // color of the random snake animation
213+ bool apmode = false ; // stores if WiFi AP mode is active
214+ bool dynColorShiftActive = DEFAULT_COLSHIFT_ACTIVE; // stores if dynamic color shift is active
215+ uint8_t dynColorShiftPhase = 0 ; // stores the phase of the dynamic color shift
216+ uint8_t dynColorShiftSpeed = DEFAULT_COLSHIFT_SPEED; // stores the speed of the dynamic color shift -> used to calc update period
199217bool hourAnimation = false ; // stores if the hour animation is active
200218uint32_t hourAnimationDuration = 18000 ; // stores the duration of the hour animation in seconds
201219
202220// nightmode settings
203- uint8_t nightModeStartHour = 22 ;
204- uint8_t nightModeStartMin = 0 ;
205- uint8_t nightModeEndHour = 7 ;
206- uint8_t nightModeEndMin = 0 ;
221+ uint8_t nightModeStartHour = DEFAULT_NM_START_HOUR;
222+ uint8_t nightModeStartMin = DEFAULT_NM_START_MIN;
223+ uint8_t nightModeEndHour = DEFAULT_NM_END_HOUR;
224+ uint8_t nightModeEndMin = DEFAULT_NM_END_MIN;
225+ uint8_t nightModeBrightness = DEFAULT_NM_BRIGHTNESS;
207226
208227// Watchdog counter to trigger restart if NTP update was not possible 30 times in a row (5min)
209228int watchdogCounter = 30 ;
@@ -225,6 +244,25 @@ void setup() {
225244 // Init EEPROM
226245 EEPROM.begin (EEPROM_SIZE);
227246
247+ // Check EEPROM version code
248+ uint8_t storedVersion = EEPROM.read (ADR_EEPROM_VERSION);
249+ if (storedVersion != EEPROM_VERSION_CODE) {
250+ // Set new defaults
251+ EEPROM.write (ADR_EEPROM_VERSION, EEPROM_VERSION_CODE);
252+ EEPROM.write (ADR_NM_START_H, DEFAULT_NM_START_HOUR);
253+ EEPROM.write (ADR_NM_START_M, DEFAULT_NM_START_MIN);
254+ EEPROM.write (ADR_NM_END_H, DEFAULT_NM_END_HOUR);
255+ EEPROM.write (ADR_NM_END_M, DEFAULT_NM_END_MIN);
256+ EEPROM.write (ADR_BRIGHTNESS, DEFAULT_BRIGHTNESS);
257+ setMainColor (DEFAULT_MC_RED, DEFAULT_MC_GREEN, DEFAULT_MC_BLUE);
258+ EEPROM.write (ADR_STATE, st_clock);
259+ EEPROM.write (ADR_NM_ACTIVATED, DEFAULT_NM_ACTIVATED);
260+ EEPROM.write (ADR_COLSHIFTSPEED, DEFAULT_COLSHIFT_SPEED);
261+ EEPROM.write (ADR_COLSHIFTACTIVE, DEFAULT_COLSHIFT_ACTIVE);
262+ EEPROM.write (ADR_NM_BRIGHTNESS, DEFAULT_NM_BRIGHTNESS);
263+ EEPROM.commit ();
264+ }
265+
228266 // configure button pin as input
229267 pinMode (BUTTONPIN, INPUT_PULLUP);
230268
@@ -364,6 +402,7 @@ void setup() {
364402 loadNightmodeSettingsFromEEPROM ();
365403 loadBrightnessSettingsFromEEPROM ();
366404 loadColorShiftStateFromEEPROM ();
405+ loadNightmodeBrightnessFromEEPROM ();
367406 loadHourAnimationSettingsFromEEPROM ();
368407
369408 if (ESP.getResetReason ().equals (" Power On" ) || ESP.getResetReason ().equals (" External System" )){
@@ -450,15 +489,23 @@ void loop() {
450489 }
451490
452491 // handle state behaviours (trigger loopCycles of different states depending on current state)
453- if (!nightMode && ! ledOff && (millis () - lastStep > behaviorUpdatePeriod) && (millis () - lastLEDdirect > TIMEOUT_LEDDIRECT)){
492+ if (!ledOff && (millis () - lastStep > behaviorUpdatePeriod) && (millis () - lastLEDdirect > TIMEOUT_LEDDIRECT)){
454493 updateStateBehavior (currentState);
455494 lastStep = millis ();
456495 }
457496
458- // Turn off LEDs if ledOff is true or nightmode is active
459- if (( ledOff || nightMode) && !waitForTimeAfterReboot){
497+ // Turn off LEDs if ledOff is true
498+ if (ledOff && !waitForTimeAfterReboot){
460499 ledmatrix.gridFlush ();
461500 }
501+
502+ // Apply night mode brightness
503+ if (nightMode && !ledOff && !waitForTimeAfterReboot){
504+ ledmatrix.setBrightness (nightModeBrightness);
505+ }
506+ else if (!nightMode && !ledOff && !waitForTimeAfterReboot){
507+ ledmatrix.setBrightness (brightness);
508+ }
462509
463510 // periodically write colors to matrix
464511 if (millis () - lastAnimationStep > PERIOD_MATRIXUPDATE && !waitForTimeAfterReboot && (millis () - lastLEDdirect > TIMEOUT_LEDDIRECT)){
@@ -470,7 +517,7 @@ void loop() {
470517 handleButton ();
471518
472519 // handle state changes
473- if (stateAutoChange && (millis () - lastStateChange > PERIOD_STATECHANGE) && !nightMode && ! ledOff){
520+ if (stateAutoChange && (millis () - lastStateChange > PERIOD_STATECHANGE) && !ledOff){
474521 // increment state variable and trigger state change
475522 stateChange ((currentState + 1 ) % NUM_STATES, false );
476523
@@ -921,6 +968,16 @@ void loadColorShiftStateFromEEPROM()
921968 logger.logString (" ColorShiftActive: " + String (dynColorShiftActive));
922969}
923970
971+ /* *
972+ * @brief Load the night mode brightness from EEPROM
973+ *
974+ */
975+ void loadNightmodeBrightnessFromEEPROM ()
976+ {
977+ nightModeBrightness = EEPROM.read (ADR_NM_BRIGHTNESS);
978+ logger.logString (" Night mode brightness: " + String (nightModeBrightness));
979+ }
980+
924981/* *
925982 * @brief load the hour animation setting from EEPROM
926983 *
@@ -1004,6 +1061,7 @@ void handleCommand() {
10041061 nightModeEndMin = split (timestr, ' -' , 3 ).toInt ();
10051062 brightness = split (timestr, ' -' , 4 ).toInt ();
10061063 dynColorShiftSpeed = split (timestr, ' -' , 5 ).toInt ();
1064+ nightModeBrightness = split (timestr, ' -' , 6 ).toInt ();
10071065 if (nightModeStartHour < 0 || nightModeStartHour > 23 ) nightModeStartHour = 22 ;
10081066 if (nightModeStartMin < 0 || nightModeStartMin > 59 ) nightModeStartMin = 0 ;
10091067 if (nightModeEndHour < 0 || nightModeEndHour > 23 ) nightModeEndHour = 7 ;
@@ -1016,11 +1074,13 @@ void handleCommand() {
10161074 EEPROM.write (ADR_NM_END_M, nightModeEndMin);
10171075 EEPROM.write (ADR_BRIGHTNESS, brightness);
10181076 EEPROM.write (ADR_COLSHIFTSPEED, dynColorShiftSpeed);
1077+ EEPROM.write (ADR_NM_BRIGHTNESS, nightModeBrightness);
10191078 EEPROM.commit ();
10201079 logger.logString (" Nightmode starts at: " + String (nightModeStartHour) + " :" + String (nightModeStartMin));
10211080 logger.logString (" Nightmode ends at: " + String (nightModeEndHour) + " :" + String (nightModeEndMin));
10221081 logger.logString (" Brightness: " + String (brightness));
10231082 logger.logString (" ColorShiftSpeed: " + String (dynColorShiftSpeed));
1083+ logger.logString (" Night mode brightness: " + String (nightModeBrightness));
10241084 ledmatrix.setBrightness (brightness);
10251085 lastNightmodeCheck = millis () - PERIOD_NIGHTMODECHECK;
10261086 }
@@ -1182,6 +1242,8 @@ void handleDataRequest() {
11821242 message += " ," ;
11831243 message += " \" brightness\" :\" " + String (brightness) + " \" " ;
11841244 message += " ," ;
1245+ message += " \" nightModeBrightness\" :\" " + String (nightModeBrightness) + " \" " ;
1246+ message += " ," ;
11851247 message += " \" colorshift\" :\" " + String (dynColorShiftActive) + " \" " ;
11861248 message += " ," ;
11871249 message += " \" colorshiftspeed\" :\" " + String (dynColorShiftSpeed) + " \" " ;
0 commit comments