Random Number Generator API#27
Random Number Generator API#27alexandruCarp wants to merge 8 commits intoUPB-CS-OpenSourceUpstream:masterfrom
Conversation
|
Please rebase this. |
|
@alexandruCarp any updates here? |
|
I don't think it is possible to also check the buffer size for the async generation, so I guess I will leave that with the behavior of the driver (which is to only write buf.len() bytes if more are requested). The sync function now returns SIZE error in this case. Is there anything else I should do? |
|
|
||
| /// Register a listener to be called when the random generation is finished | ||
| pub fn register_listener<'share>( | ||
| listener: &'share Cell<Option<(u32, u32)>>, |
There was a problem hiding this comment.
What do that two u32 values from the listener mean?
There was a problem hiding this comment.
The first one is always 0, and the second one represents the number of random bytes successfuly written in the buffer.
There was a problem hiding this comment.
Similar to
I suggest adding a RandomDataListener, as the user wants to get a function called when data is available.
pub struct RandomDataListener<share etc, F: Fn(&'share [u8])>(pub F);
impl<F: Fn(u32)> Upcall<OneId<DRIVER_NUM, 0>> for IntensityListener<F> {
fn upcall(&self, _arg0: u32, len: u32, _arg2: u32) {
// unshare buffer
f(buffer[0..len]);
// share buffer back
}
}Add an unregister_listener function that returns Result<&'share [u8], ErrorCode> to unshare the buffer earlier.
|
I suggest writing a struct RandomListener<'a> {
buffer: &'a mut [u8]
}
impl RandomListener {
pub fn new (buffer: &'a mut [u8]) {...}
}The |
|
|
||
| /// Register a listener to be called when the random generation is finished | ||
| pub fn register_listener<'share>( | ||
| listener: &'share Cell<Option<(u32, u32)>>, |
There was a problem hiding this comment.
Similar to
I suggest adding a RandomDataListener, as the user wants to get a function called when data is available.
pub struct RandomDataListener<share etc, F: Fn(&'share [u8])>(pub F);
impl<F: Fn(u32)> Upcall<OneId<DRIVER_NUM, 0>> for IntensityListener<F> {
fn upcall(&self, _arg0: u32, len: u32, _arg2: u32) {
// unshare buffer
f(buffer[0..len]);
// share buffer back
}
}Add an unregister_listener function that returns Result<&'share [u8], ErrorCode> to unshare the buffer earlier.
| } | ||
|
|
||
| /// Register a listener to be called when the random generation is finished | ||
| pub fn register_listener<'share>( |
There was a problem hiding this comment.
| pub fn register_listener<'share>( | |
| pub fn register_listener<'share>(&'share mut buffer, | |
| listener: &'share RandomDataListener, |
There was a problem hiding this comment.
I tried to implement these changes but I don't know how could that unregister_listener function return the buffer (where would it get it from). I also don't understand how could I share the buffer back from the listener.
Fixes #25.
Implemented the API and an example app which asks for some random numbers and prints them.
The app works fine on the microbit.
Added fake driver and tests.