Fix initRandomSeed(). Minor code and doc changes

The call to generateRandomSeed() in initRandomSeed() was missing
parentheses.

Added keyword for generateRandomSeed.

Minor code changes, and documentation changes and additions.
This commit is contained in:
Scott Allen 2018-04-23 15:44:15 -04:00
parent 4c2e412985
commit 0eae58c760
3 changed files with 15 additions and 5 deletions

View File

@ -64,6 +64,7 @@ flashlight KEYWORD2
flipVertical KEYWORD2
flipHorizontal KEYWORD2
freeRGBled KEYWORD2
generateRandomSeed KEYWORD2
getBuffer KEYWORD2
getCursorX KEYWORD2
getCursorY KEYWORD2

View File

@ -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<unsigned long>(ADC) << 16) + micros();
seed = ((unsigned long)ADC << 16) + micros();
power_adc_disable(); // ADC off
return seed;
}
void Arduboy2Base::initRandomSeed()
{
randomSeed(generateRandomSeed);
randomSeed(generateRandomSeed());
}
/* Graphics */

View File

@ -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();