Skip to content

Commit

Permalink
Add support for Random Number Generator initialisation #9
Browse files Browse the repository at this point in the history
- Update API to allow allow definition of per device RNG seed
- Update to AnalogSensor to create a valid initial sample on creation.
  • Loading branch information
finneyj committed Jan 20, 2017
1 parent 93d9b2b commit be747b2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions inc/core/CodalDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class CodalDevice
*/
CodalDevice()
{
if (random_value == 0)
random_value = 0xC0DA1;
}

/**
Expand Down Expand Up @@ -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;
}
};

Expand Down
1 change: 1 addition & 0 deletions source/drivers/AnalogSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/*
Expand Down

0 comments on commit be747b2

Please sign in to comment.