Skip to content

Conversation

@alexandruCarp
Copy link

@alexandruCarp alexandruCarp commented Apr 10, 2023

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.

@alexandruCarp alexandruCarp marked this pull request as ready for review April 17, 2023 10:03
@alexandruradovici alexandruradovici added the needs-rebase This pull request needs a rebase label Apr 24, 2023
@alexandruradovici
Copy link

Please rebase this.

@alexandruradovici
Copy link

@alexandruCarp any updates here?

@alexandruCarp
Copy link
Author

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?

@alexandruradovici alexandruradovici removed the needs-rebase This pull request needs a rebase label May 4, 2023

/// Register a listener to be called when the random generation is finished
pub fn register_listener<'share>(
listener: &'share Cell<Option<(u32, u32)>>,

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?

Copy link
Author

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to

https://github.com/DanutAldea/libtock-rs/blob/b70eb0cbe1766a37585472235e77f755a7c12786/apis/ambient_light/src/lib.rs#L67-L73

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.

@alexandruradovici
Copy link

I suggest writing a RandomListener similar to:

struct RandomListener<'a> {
    buffer: &'a mut [u8]
}

impl RandomListener {
   pub fn new (buffer: &'a mut [u8]) {...}
}

The generate_random function will get this listener as an argument an share it with the driver.


/// Register a listener to be called when the random generation is finished
pub fn register_listener<'share>(
listener: &'share Cell<Option<(u32, u32)>>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to

https://github.com/DanutAldea/libtock-rs/blob/b70eb0cbe1766a37585472235e77f755a7c12786/apis/ambient_light/src/lib.rs#L67-L73

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>(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn register_listener<'share>(
pub fn register_listener<'share>(&'share mut buffer,
listener: &'share RandomDataListener,

Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Implement the Random Number Generator API

2 participants