Skip to content

Commit 87743c1

Browse files
Merge pull request #1 from techniccontroller/dev_new_background
Add background speed control and improve random background animation
2 parents 2cf9888 + 2d2b6cf commit 87743c1

File tree

6 files changed

+89
-15
lines changed

6 files changed

+89
-15
lines changed

constants.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define PERIOD_CLOCK_UPDATE 20
88
#define PERIOD_LED_UPDATE 100
99
#define PERIOD_NIGHTMODE_CHECK 20000
10-
#define PERIOD_BACKGROUND_RANDOM 1000
10+
#define PERIOD_BACKGROUND_RANDOM 20
1111

1212
#define EEPROM_SIZE 30 // size of EEPROM to save persistent variables
1313
#define ADR_NM_START_H 0
@@ -16,6 +16,7 @@
1616
#define ADR_NM_END_M 12
1717
#define ADR_BRIGHTNESS_BACKGROUND 16
1818
#define ADR_BRIGHTNESS_TIME 17
19+
#define ADR_SPEED_BACKGROUND 18
1920
#define ADR_BG_RED 21
2021
#define ADR_BG_GREEN 22
2122
#define ADR_BG_BLUE 23

data/index.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@
247247
}
248248

249249
.show{
250-
height: 240px;
250+
height: 270px;
251251
transition: height 1s;
252252
}
253253

@@ -284,6 +284,10 @@ <h1 id="headline">DIGITALCLOCK</h1>
284284
<label for="brightnessBackground">Brightness Background:</label>
285285
<input type="range" id="brightnessBackground" name="volume" min="10" max="255">
286286
</div>
287+
<div class="number-container">
288+
<label for="speedBackground">Background Speed:</label>
289+
<input type="range" id="speedBackground" name="volume" min="0.2" max="5" step="0.1">
290+
</div>
287291
<div class="number-container">
288292
<label for="nm_start" style="align-self: flex-start">Nightmode start time: </label>
289293
<input type="time" id="nm_start" name="nm_start" min="00:00" max="23:59">
@@ -449,6 +453,7 @@ <h1 id="headline">DIGITALCLOCK</h1>
449453
document.getElementById("nm_end").value = myVar.nightModeEnd.replace("-", ":");
450454
document.getElementById("brightnessTime").value = parseInt(myVar.brightnessTime);
451455
document.getElementById("brightnessBackground").value = parseInt(myVar.brightnessBackground);
456+
document.getElementById("speedBackground").value = parseFloat(myVar.speedBackground);
452457

453458
console.log(myVar);
454459
}
@@ -467,6 +472,7 @@ <h1 id="headline">DIGITALCLOCK</h1>
467472
var nmEnd = document.getElementById("nm_end");
468473
var sld_brightnessTime = document.getElementById("brightnessTime");
469474
var sld_brightnessBackground = document.getElementById("brightnessBackground");
475+
var sld_speedBackground = document.getElementById("speedBackground");
470476
var ckb_resetWifi = document.querySelector('input[id="reset_wifi"]');
471477
var cmdstr = "./cmd?setting=";
472478
cmdstr += nmStart.value.replace(":", "-");
@@ -476,6 +482,8 @@ <h1 id="headline">DIGITALCLOCK</h1>
476482
cmdstr += sld_brightnessTime.value;
477483
cmdstr += "-";
478484
cmdstr += sld_brightnessBackground.value;
485+
cmdstr += "-";
486+
cmdstr += sld_speedBackground.value;
479487
console.log(cmdstr);
480488
sendCommand(cmdstr);
481489
if(ckb_resetWifi.checked) {

data/index_de.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@
247247
}
248248

249249
.show{
250-
height: 240px;
250+
height: 270px;
251251
transition: height 1s;
252252
}
253253

@@ -285,7 +285,11 @@ <h1 id="headline">DIGITALCLOCK</h1>
285285
<input type="range" id="brightnessBackground" name="volume" min="10" max="255">
286286
</div>
287287
<div class="number-container">
288-
<label for="nm_start" style="align-self: flex-start">Nachtmodus Startzeit: </label>
288+
<label for="speedBackground">Hintergrund Geschw.:</label>
289+
<input type="range" id="speedBackground" name="volume" min="0.2" max="5" step="0.1">
290+
</div>
291+
<div class="number-container">
292+
<label for="nm_start" style="align-self: flex-start">Nightmode Startzeit: </label>
289293
<input type="time" id="nm_start" name="nm_start" min="00:00" max="23:59">
290294
</div>
291295
<div class="number-container">
@@ -447,6 +451,7 @@ <h1 id="headline">DIGITALCLOCK</h1>
447451
document.getElementById("nm_end").value = myVar.nightModeEnd.replace("-", ":");
448452
document.getElementById("brightnessTime").value = parseInt(myVar.brightnessTime);
449453
document.getElementById("brightnessBackground").value = parseInt(myVar.brightnessBackground);
454+
document.getElementById("speedBackground").value = parseFloat(myVar.speedBackground);
450455

451456
console.log(myVar);
452457
}
@@ -465,6 +470,7 @@ <h1 id="headline">DIGITALCLOCK</h1>
465470
var nmEnd = document.getElementById("nm_end");
466471
var sld_brightnessTime = document.getElementById("brightnessTime");
467472
var sld_brightnessBackground = document.getElementById("brightnessBackground");
473+
var sld_speedBackground = document.getElementById("speedBackground");
468474
var ckb_resetWifi = document.querySelector('input[id="reset_wifi"]');
469475
var cmdstr = "./cmd?setting=";
470476
cmdstr += nmStart.value.replace(":", "-");
@@ -474,6 +480,8 @@ <h1 id="headline">DIGITALCLOCK</h1>
474480
cmdstr += sld_brightnessTime.value;
475481
cmdstr += "-";
476482
cmdstr += sld_brightnessBackground.value;
483+
cmdstr += "-";
484+
cmdstr += sld_speedBackground.value;
477485
console.log(cmdstr);
478486
sendCommand(cmdstr);
479487
if(ckb_resetWifi.checked) {

digitalclock_esp8266.ino

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ void loop() {
241241
lastLedStep = millis();
242242
}
243243

244-
if(millis() - lastRandomBackground > PERIOD_BACKGROUND_RANDOM){
244+
if(!ledOff && !nightMode && millis() - lastRandomBackground > PERIOD_BACKGROUND_RANDOM){
245245
segmentClock.randomizeBackground();
246+
ledstrip.drawOnLEDsInstant();
246247
lastRandomBackground = millis();
247248
}
248249

@@ -403,13 +404,18 @@ void loadBrightnessSettingsFromEEPROM()
403404
{
404405
uint8_t brightnessTime = EEPROM.read(ADR_BRIGHTNESS_TIME);
405406
uint8_t brightnessBackground = EEPROM.read(ADR_BRIGHTNESS_BACKGROUND);
407+
float speedBackground = EEPROM.read(ADR_SPEED_BACKGROUND) / 100.0; // read speed as percentage
406408
if (brightnessTime < 10)
407409
brightnessTime = 10;
408410
if (brightnessBackground < 10)
409411
brightnessBackground = 10;
412+
if (speedBackground < 0.2)
413+
speedBackground = 0.2; // set minimum speed to 0.2
410414
segmentClock.setTimeBrightness(brightnessTime);
411415
segmentClock.setBackgroundBrightness(brightnessBackground);
412-
logger.logString("BrightnessTime: " + String(brightnessTime) + ", BrightnessBackround: " + String(brightnessBackground));
416+
segmentClock.setBackgroundAnimationSpeed(speedBackground);
417+
logger.logString("BrightnessTime: " + String(brightnessTime) + ", BrightnessBackround: " + String(brightnessBackground)
418+
+ ", SpeedBackground: " + String(speedBackground));
413419
}
414420

415421
/**
@@ -492,24 +498,29 @@ void handleCommand() {
492498
nightModeEndMin = split(timestr, '-', 3).toInt();
493499
uint8_t brightnessTime = split(timestr, '-', 4).toInt();
494500
uint8_t brightnessBackground = split(timestr, '-', 5).toInt();
501+
float speedBackground = split(timestr, '-', 6).toFloat();
495502
if(nightModeStartHour > 23) nightModeStartHour = 22; // set default
496503
if(nightModeStartMin > 59) nightModeStartMin = 0;
497504
if(nightModeEndHour > 23) nightModeEndHour = 7; // set default
498505
if(nightModeEndMin > 59) nightModeEndMin = 0;
499506
if(brightnessTime < 10) brightnessTime = 10;
500507
if(brightnessBackground < 10) brightnessBackground = 10;
508+
if(speedBackground < 0.2) speedBackground = 0.2; // set minimum speed
501509
EEPROM.write(ADR_NM_START_H, nightModeStartHour);
502510
EEPROM.write(ADR_NM_START_M, nightModeStartMin);
503511
EEPROM.write(ADR_NM_END_H, nightModeEndHour);
504512
EEPROM.write(ADR_NM_END_M, nightModeEndMin);
505513
EEPROM.write(ADR_BRIGHTNESS_TIME, brightnessTime);
506514
EEPROM.write(ADR_BRIGHTNESS_BACKGROUND, brightnessBackground);
515+
EEPROM.write(ADR_SPEED_BACKGROUND, (uint8_t)(speedBackground * 100)); // store speed as percentage
507516
EEPROM.commit();
508517
logger.logString("Nightmode starts at: " + String(nightModeStartHour) + ":" + String(nightModeStartMin));
509518
logger.logString("Nightmode ends at: " + String(nightModeEndHour) + ":" + String(nightModeEndMin));
510519
logger.logString("BrightnessTime: " + String(brightnessTime) + ", BrightnessBackground: " + String(brightnessBackground));
520+
logger.logString("SpeedBackground: " + String(speedBackground));
511521
segmentClock.setTimeBrightness(brightnessTime);
512522
segmentClock.setBackgroundBrightness(brightnessBackground);
523+
segmentClock.setBackgroundAnimationSpeed(speedBackground);
513524
lastNightmodeCheck = 0;
514525
}
515526
else if (server.argName(0) == "resetwifi"){
@@ -577,6 +588,8 @@ void handleDataRequest() {
577588
message += "\"brightnessTime\":\"" + String(segmentClock.getBrightnessTime()) + "\"";
578589
message += ",";
579590
message += "\"brightnessBackground\":\"" + String(segmentClock.getBrightnessBackground()) + "\"";
591+
message += ",";
592+
message += "\"speedBackground\":\"" + String(segmentClock.getBackgroundAnimationSpeed()) + "\"";
580593
}
581594
message += "}";
582595
logger.logString(message);

segment_clock.cpp

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ SegmentClock::SegmentClock(LEDStrip *ledstrip, UDPLogger *logger)
1212
this->ledstrip = ledstrip;
1313
this->logger = logger;
1414
calcBackgroundIds();
15+
16+
speeds = new float[LED_COUNT];
17+
phases = new float[LED_COUNT];
18+
19+
for (int i = 0; i < LED_COUNT; i++)
20+
{
21+
speeds[i] = 0.5 + random(100) / 100.0; // Speeds between 0.5 and 1.5
22+
phases[i] = random(0, 628) / 100.0; // Phase offset between 0 and 2π
23+
}
1524
}
1625

1726
/**
@@ -75,6 +84,16 @@ void SegmentClock::setBackgroundBrightness(uint8_t brightness)
7584
this->brightness_background = brightness;
7685
}
7786

87+
/**
88+
* @brief Set the speed of the background animation
89+
*
90+
* @param speed speed of the background animation
91+
*/
92+
void SegmentClock::setBackgroundAnimationSpeed(float speed)
93+
{
94+
this->animation_speed = speed;
95+
}
96+
7897
/**
7998
* @brief Get the brightness of the time
8099
*/
@@ -91,21 +110,36 @@ uint8_t SegmentClock::getBrightnessBackground()
91110
return brightness_background;
92111
}
93112

113+
/**
114+
* @brief Get the speed of the background animation
115+
*/
116+
float SegmentClock::getBackgroundAnimationSpeed()
117+
{
118+
return animation_speed;
119+
}
120+
94121
/**
95122
* @brief Randomize the background of the clock
96123
*/
97124
void SegmentClock::randomizeBackground()
98125
{
99-
for(int i = 0; i < LED_COUNT - 30; i++)
126+
background_time += 0.02 * animation_speed;
127+
128+
for (int i = 0; i < num_background_leds; i++)
100129
{
101-
uint8_t brightness = (random(255) / 255.0) * brightness_background;
130+
// Smooth brightness using sine wave animation
131+
float brightnessFactor = (sin(background_time * speeds[ids_of_background[i]] + phases[ids_of_background[i]]) + 1.0) / 2.0;
132+
uint8_t brightness = brightnessFactor * brightness_background;
133+
102134
uint32_t color = scaleColor(color_background, brightness);
103-
ledstrip->setPixel(ids_of_background[i], color);
135+
ledstrip->setPixel(ids_of_background[i], color);
104136
}
105-
106-
for(int i = 0; i < num_temp_background_leds; i++)
137+
for (int i = 0; i < num_temp_background_leds; i++)
107138
{
108-
uint8_t brightness = (random(255) / 255.0) * brightness_background;
139+
// Smooth brightness using sine wave animation
140+
float brightnessFactor = (sin(background_time * speeds[ids_of_background_temp[i]] + phases[ids_of_background_temp[i]]) + 1.0) / 2.0;
141+
uint8_t brightness = brightnessFactor * brightness_background;
142+
109143
uint32_t color = scaleColor(color_background, brightness);
110144
ledstrip->setPixel(ids_of_background_temp[i], color);
111145
}

segment_clock.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ class SegmentClock{
1414
void setTimeBrightness(uint8_t brightness);
1515
void setBackgroundColor(uint32_t color);
1616
void setBackgroundBrightness(uint8_t brightness);
17+
void setBackgroundAnimationSpeed(float speed);
1718
uint8_t getBrightnessTime();
1819
uint8_t getBrightnessBackground();
20+
float getBackgroundAnimationSpeed();
1921
void randomizeBackground();
2022

2123
private:
@@ -41,9 +43,17 @@ class SegmentClock{
4143
const uint8_t ids_of_third_segment[7] = {28, 22, 21, 32, 38, 39, 30};
4244
const uint8_t ids_of_fourth_segment[7] = {4, 2, 1, 8, 11, 12, 6};
4345
const uint8_t ids_of_points[2] = {45, 47};
44-
uint8_t ids_of_background[LED_COUNT- 30];
45-
uint8_t ids_of_background_temp[30];
46-
uint8_t num_temp_background_leds = 0;
46+
const static uint8_t num_segment_leds = 30;
47+
const static uint8_t num_background_leds = LED_COUNT - num_segment_leds;
48+
uint8_t num_temp_background_leds = 0; // number of segment leds that are temporarly are part of the background
49+
uint8_t ids_of_background[num_background_leds];
50+
uint8_t ids_of_background_temp[num_segment_leds];
51+
52+
// Animation state for smooth brightness changes
53+
float* speeds; // Array of speed factors for each LED
54+
float* phases; // Array of phase offsets for each LED
55+
float background_time = 0; // Global time for background animation in seconds (updated every 20ms)
56+
float animation_speed = 1.0; // Default speed multiplier (1.0 = normal speed)
4757

4858
uint32_t color_background = LEDStrip::Color24bit(0, 0, 0);
4959
uint32_t color_time = LEDStrip::Color24bit(255, 255, 255);

0 commit comments

Comments
 (0)