Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seed FastLED's RNG #3552

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions wled00/FX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ uint16_t mode_multi_comet(void) {
}
comets[i]++;
} else {
if(!random(SEGLEN)) {
if(!random16(SEGLEN)) {
comets[i] = 0;
}
}
Expand Down Expand Up @@ -1990,7 +1990,7 @@ uint16_t mode_fire_2012() {

// Step 1. Cool down every cell a little
for (int i = 0; i < SEGLEN; i++) {
uint8_t cool = (it != SEGENV.step) ? random8((((20 + SEGMENT.speed/3) * 16) / SEGLEN)+2) : random(4);
uint8_t cool = (it != SEGENV.step) ? random8((((20 + SEGMENT.speed/3) * 16) / SEGLEN)+2) : random8(4);
uint8_t minTemp = (i<ignition) ? (ignition-i)/4 + 16 : 0; // should not become black in ignition area
uint8_t temp = qsub8(heat[i], cool);
heat[i] = temp<minTemp ? minTemp : temp;
Expand Down Expand Up @@ -4566,15 +4566,15 @@ class AuroraWave {

public:
void init(uint32_t segment_length, CRGB color) {
ttl = random(500, 1501);
ttl = random16(500, 1501);
basecolor = color;
basealpha = random(60, 101) / (float)100;
basealpha = random8(60, 101) / (float)100;
age = 0;
width = random(segment_length / 20, segment_length / W_WIDTH_FACTOR); //half of width to make math easier
width = random16(segment_length / 20, segment_length / W_WIDTH_FACTOR); //half of width to make math easier
if (!width) width = 1;
center = random(101) / (float)100 * segment_length;
goingleft = random(0, 2) == 0;
speed_factor = (random(10, 31) / (float)100 * W_MAX_SPEED / 255);
center = random8(101) / (float)100 * segment_length;
goingleft = random8(0, 2) == 0;
speed_factor = (random8(10, 31) / (float)100 * W_MAX_SPEED / 255);
alive = true;
}

Expand Down Expand Up @@ -4659,7 +4659,7 @@ uint16_t mode_aurora(void) {
waves = reinterpret_cast<AuroraWave*>(SEGENV.data);

for (int i = 0; i < SEGENV.aux1; i++) {
waves[i].init(SEGLEN, CRGB(SEGMENT.color_from_palette(random8(), false, false, random(0, 3))));
waves[i].init(SEGLEN, CRGB(SEGMENT.color_from_palette(random8(), false, false, random8(0, 3))));
}
} else {
waves = reinterpret_cast<AuroraWave*>(SEGENV.data);
Expand All @@ -4671,7 +4671,7 @@ uint16_t mode_aurora(void) {

if(!(waves[i].stillAlive())) {
//If a wave dies, reinitialize it starts over.
waves[i].init(SEGLEN, CRGB(SEGMENT.color_from_palette(random8(), false, false, random(0, 3))));
waves[i].init(SEGLEN, CRGB(SEGMENT.color_from_palette(random8(), false, false, random8(0, 3))));
}
}

Expand Down Expand Up @@ -5883,7 +5883,7 @@ uint16_t mode_2Dghostrider(void) {
if (lighter->reg[i]) {
lighter->lightersPosY[i] = lighter->gPosY;
lighter->lightersPosX[i] = lighter->gPosX;
lighter->Angle[i] = lighter->gAngle + random(-10, 10);
lighter->Angle[i] = lighter->gAngle + ((int)random8(20) - 10);
lighter->time[i] = 0;
lighter->reg[i] = false;
} else {
Expand Down Expand Up @@ -6744,7 +6744,7 @@ uint16_t mode_puddlepeak(void) { // Puddlepeak. By Andrew Tuline.

uint16_t size = 0;
uint8_t fadeVal = map(SEGMENT.speed,0,255, 224, 254);
uint16_t pos = random(SEGLEN); // Set a random starting position.
uint16_t pos = random16(SEGLEN); // Set a random starting position.

um_data_t *um_data;
if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) {
Expand Down
10 changes: 10 additions & 0 deletions wled00/wled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,16 @@ void WLED::setup()
initServer();
DEBUG_PRINT(F("heap ")); DEBUG_PRINTLN(ESP.getFreeHeap());

// Seed FastLED random functions with an esp random value, which already works properly at this point.
#if defined(ARDUINO_ARCH_ESP32)
const uint32_t seed32 = esp_random();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: please check if the header file for esp_random is already included.

#if defined(ARDUINO_ARCH_ESP32)
#include "esp_random.h"
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I also include something for the esp8266?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure ... is there a header on 8266 that defines RANDOM_REG32 ?

#elif defined(ARDUINO_ARCH_ESP8266)
const uint32_t seed32 = RANDOM_REG32;
#else
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this "else" branch needed? We only support esp32 variants (ARDUINO_ARCH_ESP32) and 8266 (ARDUINO_ARCH_ESP8266).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but it doesn't hurt, does it? If someone were to come along and try to port WLED to a different architecture, this would be one less thing to worry about. I don't have strong feelings about this though, and can remove it if you

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if you remove it - its unused code and we don't have any plans for supporting non-esp architectures.

const uint32_t seed32 = random(std::numeric_limits<long>::max());
#endif
random16_set_seed((uint16_t)((seed32 & 0xFFFF) ^ (seed32 >> 16)));

#if WLED_WATCHDOG_TIMEOUT > 0
enableWatchdog();
#endif
Expand Down
Loading