mirror of https://github.com/MLXXXp/Arduboy2.git
Move generateRandomSeed() to Arduboy2Core
Suggested by @yyyc514 (Josh Goebel) The generateRandomSeed() function was moved from the Arduboy2Base class to the Arduboy2Core class, since it deals directly with hardware. It was also made static. Some related documentation was changed.
This commit is contained in:
parent
2491e639aa
commit
13130f368a
|
@ -273,23 +273,6 @@ int Arduboy2Base::cpuLoad()
|
||||||
return lastFrameDurationMs*100 / eachFrameMillis;
|
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()
|
void Arduboy2Base::initRandomSeed()
|
||||||
{
|
{
|
||||||
randomSeed(generateRandomSeed());
|
randomSeed(generateRandomSeed());
|
||||||
|
|
|
@ -735,35 +735,17 @@ class Arduboy2Base : public Arduboy2Core
|
||||||
*/
|
*/
|
||||||
uint8_t* getBuffer();
|
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
|
/** \brief
|
||||||
* Seed the random number generator with a random value.
|
* Seed the random number generator with a random value.
|
||||||
*
|
*
|
||||||
* \details
|
* \details
|
||||||
* The Arduino random number generator is seeded with a random value
|
* The Arduino pseudorandom number generator is seeded with the random value
|
||||||
* derived from entropy from an ADC reading of a floating pin combined with
|
* returned from a call to `generateRandomSeed()`.
|
||||||
* 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,
|
* \note
|
||||||
* such as after a user hits a button to start a game or other semi-random
|
* This function will be more effective if called after a semi-random time,
|
||||||
* event.
|
* 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()
|
* \see generateRandomSeed()
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -87,7 +87,7 @@ void Arduboy2Core::boot()
|
||||||
setCPUSpeed8MHz();
|
setCPUSpeed8MHz();
|
||||||
#endif
|
#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;
|
ADMUX = RAND_SEED_IN_ADMUX;
|
||||||
|
|
||||||
bootPins();
|
bootPins();
|
||||||
|
@ -563,6 +563,23 @@ uint8_t Arduboy2Core::buttonsState()
|
||||||
return buttons;
|
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
|
// delay in ms with 16 bit duration
|
||||||
void Arduboy2Core::delayShort(uint16_t ms)
|
void Arduboy2Core::delayShort(uint16_t ms)
|
||||||
{
|
{
|
||||||
|
|
|
@ -202,7 +202,7 @@
|
||||||
|
|
||||||
// ----- Pins common on Arduboy and DevKit -----
|
// ----- 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 A4
|
||||||
#define RAND_SEED_IN_PORT PORTF
|
#define RAND_SEED_IN_PORT PORTF
|
||||||
#define RAND_SEED_IN_BIT PORTF1
|
#define RAND_SEED_IN_BIT PORTF1
|
||||||
|
@ -829,6 +829,25 @@ class Arduboy2Core : public Arduboy2NoUSB
|
||||||
*/
|
*/
|
||||||
static void safeMode();
|
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
|
/** \brief
|
||||||
* Delay for the number of milliseconds, specified as a 16 bit value.
|
* Delay for the number of milliseconds, specified as a 16 bit value.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue