diff --git a/keywords.txt b/keywords.txt index 4e26a1f..b5caf94 100644 --- a/keywords.txt +++ b/keywords.txt @@ -64,6 +64,7 @@ flashlight KEYWORD2 flipVertical KEYWORD2 flipHorizontal KEYWORD2 freeRGBled KEYWORD2 +generateRandomSeed KEYWORD2 getBuffer KEYWORD2 getCursorX KEYWORD2 getCursorY KEYWORD2 diff --git a/src/Arduboy2.cpp b/src/Arduboy2.cpp index fc38443..5d59b55 100644 --- a/src/Arduboy2.cpp +++ b/src/Arduboy2.cpp @@ -277,22 +277,24 @@ int Arduboy2Base::cpuLoad() unsigned long Arduboy2Base::generateRandomSeed() { + unsigned long seed; + power_adc_enable(); // ADC on // do an ADC read from an unconnected input pin ADCSRA |= _BV(ADSC); // start conversion (ADMUX has been pre-set in boot()) while (bit_is_set(ADCSRA, ADSC)) { } // wait for conversion complete - unsigned long seed = (static_cast(ADC) << 16) + micros(); + seed = ((unsigned long)ADC << 16) + micros(); power_adc_disable(); // ADC off - + return seed; } void Arduboy2Base::initRandomSeed() { - randomSeed(generateRandomSeed); + randomSeed(generateRandomSeed()); } /* Graphics */ diff --git a/src/Arduboy2.h b/src/Arduboy2.h index 10e20c1..b7405a1 100644 --- a/src/Arduboy2.h +++ b/src/Arduboy2.h @@ -695,13 +695,17 @@ class Arduboy2Base : public Arduboy2Core /** \brief * Create a seed suitable for use with a random number generator. * + * \return A random value that can be used to seed a random number generator. + * * \details - * The seed is generated with a random value derived from entropy from an + * The returned value will be a random value derived from entropy from an * ADC reading of a floating pin combined with the microseconds since boot. * * This method is still most effective when called after a semi-random time, * such as after a user hits a button to start a game or other semi-random * event. + * + * \see initRandomSeed() */ unsigned long generateRandomSeed(); @@ -711,11 +715,14 @@ class Arduboy2Base : public Arduboy2Core * \details * The Arduino random number generator is seeded with a random value * derived from entropy from an ADC reading of a floating pin combined with - * the microseconds since boot. + * the microseconds since boot. The seed value is provided by calling the + * `generateRandomSeed()` function. * * This method is still most effective when called after a semi-random time, * such as after a user hits a button to start a game or other semi-random * event. + * + * \see generateRandomSeed() */ void initRandomSeed();