-
Notifications
You must be signed in to change notification settings - Fork 9
Random Number Generator API #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Random Number Generator API #27
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do that two u32 values from the listener mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| pub fn register_listener<'share>( | |
| pub fn register_listener<'share>(&'share mut buffer, | |
| listener: &'share RandomDataListener, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.