Skip to content

Commit

Permalink
fix seedRandom
Browse files Browse the repository at this point in the history
  • Loading branch information
Amerlander committed Nov 16, 2023
1 parent bb13417 commit d8c9c6f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
17 changes: 16 additions & 1 deletion model/MicroBit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,22 @@ int MicroBit::init()
}

// Seed our random number generator
seedRandom();
if(ble_running())
{
// Readings from different sensors
int16_t accelerometerX = accelerometer.getX();
int16_t accelerometerY = accelerometer.getY();
int16_t accelerometerZ = accelerometer.getZ();
int16_t temperatureValue = thermometer.getTemperature();
int16_t microphoneValue = io.MICROPHONE.getAnalogValue();
// Combine sensor values using XOR
uint32_t r = accelerometerX ^ accelerometerY ^ accelerometerZ ^ temperatureValue ^ microphoneValue;
seedRandom(r);
}
else
{
seedRandom();
}

// Create an event handler to trap any handlers being created for I2C services.
// We do this to enable initialisation of those services only when they're used,
Expand Down
14 changes: 1 addition & 13 deletions source/MicroBitDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,7 @@ void MicroBitDevice::seedRandom()
{
uint32_t r = 0xBBC5EED;

if(ble_running())
{
// Readings from different sensors
int16_t accelerometerX = uBit.accelerometer.getX();
int16_t accelerometerY = uBit.accelerometer.getY();
int16_t accelerometerZ = uBit.accelerometer.getZ();
int16_t temperatureValue = uBit.thermometer.getTemperature();
int16_t microphoneValue = uBit.io.MICROPHONE.getAnalogValue();

// Combine sensor values using XOR
r = accelerometerX ^ accelerometerY ^ accelerometerZ ^ temperatureValue ^ microphoneValue;
}
else
if(!ble_running())
{
// Start the Random number generator. No need to leave it running... I hope. :-)
NRF_RNG->TASKS_START = 1;
Expand Down

0 comments on commit d8c9c6f

Please sign in to comment.