From be747b2cea89f7dc7fb9cb8915cbb9f75c0583b9 Mon Sep 17 00:00:00 2001 From: Joe Finney Date: Fri, 20 Jan 2017 23:56:08 +0000 Subject: [PATCH] Add support for Random Number Generator initialisation #9 - Update API to allow allow definition of per device RNG seed - Update to AnalogSensor to create a valid initial sample on creation. --- inc/core/CodalDevice.h | 11 +++++++---- source/drivers/AnalogSensor.cpp | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/inc/core/CodalDevice.h b/inc/core/CodalDevice.h index 5276f18..cfadff1 100644 --- a/inc/core/CodalDevice.h +++ b/inc/core/CodalDevice.h @@ -46,6 +46,8 @@ class CodalDevice */ CodalDevice() { + if (random_value == 0) + random_value = 0xC0DA1; } /** @@ -99,12 +101,13 @@ class CodalDevice /** * Seed the random number generator (RNG). * - * default: 0xCAFECAFE - * @return an unsigned 32 bit seed to seed codal's lightweight Galois LFSR. + * @param seed an unsigned 32 bit value used to seed codal's lightweight Galois LFSR. + * @return DEVICE_OK on success */ - virtual uint32_t seedRandom() + virtual int seedRandom(uint32_t seed) { - return 0xCAFECAFE; + random_value = seed; + return DEVICE_OK; } }; diff --git a/source/drivers/AnalogSensor.cpp b/source/drivers/AnalogSensor.cpp index 4fa1e4a..123ef7b 100644 --- a/source/drivers/AnalogSensor.cpp +++ b/source/drivers/AnalogSensor.cpp @@ -55,6 +55,7 @@ AnalogSensor::AnalogSensor(DevicePin &pin, uint16_t id) : _pin(pin) EventModel::defaultEventBus->listen(id, ANALOG_SENSOR_UPDATE_NEEDED, this, &AnalogSensor::onSampleEvent, MESSAGE_BUS_LISTENER_IMMEDIATE); setPeriod(500); + updateSample(); } /*