diff --git a/model/MicroBit.cpp b/model/MicroBit.cpp index 79f63bb2..ef018255 100644 --- a/model/MicroBit.cpp +++ b/model/MicroBit.cpp @@ -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, diff --git a/source/MicroBitDevice.cpp b/source/MicroBitDevice.cpp index 7e7fa37f..2bd4ccae 100644 --- a/source/MicroBitDevice.cpp +++ b/source/MicroBitDevice.cpp @@ -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;