diff --git a/src/Arduboy2.cpp b/src/Arduboy2.cpp index df18e2c..a7c9780 100644 --- a/src/Arduboy2.cpp +++ b/src/Arduboy2.cpp @@ -273,23 +273,6 @@ int Arduboy2Base::cpuLoad() return lastFrameDurationMs*100 / eachFrameMillis; } -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 - - seed = ((unsigned long)ADC << 16) + micros(); - - power_adc_disable(); // ADC off - - return seed; -} - void Arduboy2Base::initRandomSeed() { randomSeed(generateRandomSeed()); diff --git a/src/Arduboy2.h b/src/Arduboy2.h index 775d0bb..79f2694 100644 --- a/src/Arduboy2.h +++ b/src/Arduboy2.h @@ -735,35 +735,17 @@ class Arduboy2Base : public Arduboy2Core */ uint8_t* getBuffer(); - /** \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 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(); - /** \brief * Seed the random number generator with a random value. * * \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 seed value is provided by calling the - * `generateRandomSeed()` function. + * The Arduino pseudorandom number generator is seeded with the random value + * returned from a call to `generateRandomSeed()`. * - * 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. + * \note + * This function will be more effective if called after a semi-random time, + * such as after waiting for the user to press a button to start a game, or + * another event that takes a variable amount of time after boot. * * \see generateRandomSeed() */ diff --git a/src/Arduboy2Core.cpp b/src/Arduboy2Core.cpp index e242793..953560a 100644 --- a/src/Arduboy2Core.cpp +++ b/src/Arduboy2Core.cpp @@ -87,7 +87,7 @@ void Arduboy2Core::boot() setCPUSpeed8MHz(); #endif - // Select the ADC input here so a delay isn't required in initRandomSeed() + // Select the ADC input here so a delay isn't required in generateRandomSeed() ADMUX = RAND_SEED_IN_ADMUX; bootPins(); @@ -563,6 +563,23 @@ uint8_t Arduboy2Core::buttonsState() return buttons; } +unsigned long Arduboy2Core::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 + + seed = ((unsigned long)ADC << 16) + micros(); + + power_adc_disable(); // ADC off + + return seed; +} + // delay in ms with 16 bit duration void Arduboy2Core::delayShort(uint16_t ms) { diff --git a/src/Arduboy2Core.h b/src/Arduboy2Core.h index 8df55ce..0170f73 100644 --- a/src/Arduboy2Core.h +++ b/src/Arduboy2Core.h @@ -202,7 +202,7 @@ // ----- Pins common on Arduboy and DevKit ----- -// Unconnected analog input used for noise by initRandomSeed() +// Unconnected analog input used for noise by generateRandomSeed() #define RAND_SEED_IN A4 #define RAND_SEED_IN_PORT PORTF #define RAND_SEED_IN_BIT PORTF1 @@ -829,6 +829,25 @@ class Arduboy2Core : public Arduboy2NoUSB */ static void safeMode(); + /** \brief + * Create a seed suitable for use with a pseudorandom number generator. + * + * \return A random value that can be used to seed a + * pseudorandom number generator. + * + * \details + * 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. + * + * \note + * This function will be more effective if called after a semi-random time, + * such as after waiting for the user to press a button to start a game, or + * another event that takes a variable amount of time after boot. + * + * \see Arduboy2Base::initRandomSeed() + */ + static unsigned long generateRandomSeed(); + /** \brief * Delay for the number of milliseconds, specified as a 16 bit value. *