diff --git a/Cargo.lock b/Cargo.lock index 90e394396..559adf91c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2193,6 +2193,12 @@ dependencies = [ "syn 2.0.52", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "precomputed-hash" version = "0.1.1" @@ -2256,12 +2262,41 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd7a31eed1591dcbc95d92ad7161908e72f4677f8fabf2a32ca49b4237cbf211" +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + [[package]] name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +[[package]] +name = "random" +version = "0.1.0" +dependencies = [ + "rand", + "riot-rs", + "riot-rs-boards", + "riot-rs-random", +] + [[package]] name = "rbi" version = "0.1.1" @@ -2417,6 +2452,7 @@ dependencies = [ "linkme", "once_cell", "riot-rs-debug", + "riot-rs-random", "riot-rs-rt", "riot-rs-threads", "riot-rs-utils", @@ -2438,6 +2474,15 @@ dependencies = [ "trybuild", ] +[[package]] +name = "riot-rs-random" +version = "0.1.0" +dependencies = [ + "embassy-sync 0.5.0", + "rand_chacha", + "rand_core", +] + [[package]] name = "riot-rs-rt" version = "0.1.0" diff --git a/examples/laze.yml b/examples/laze.yml index 3fbb52486..b281446a1 100644 --- a/examples/laze.yml +++ b/examples/laze.yml @@ -13,4 +13,5 @@ subdirs: - embassy-usb-keyboard - hello-world - minimal + - random - threading diff --git a/examples/random/Cargo.toml b/examples/random/Cargo.toml new file mode 100644 index 000000000..7bc791234 --- /dev/null +++ b/examples/random/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "random" +version = "0.1.0" +authors.workspace = true +edition.workspace = true +publish = false + +[dependencies] +# not enabling riot-rs/hwrng even though it's currently needed -- laze takes +# care of that. +riot-rs = { path = "../../src/riot-rs", features = ["threading"] } +riot-rs-boards = { path = "../../src/riot-rs-boards" } + +riot-rs-random = { version = "0.1.0", path = "../../src/riot-rs-random" } +rand = { version = "0.8.5", default-features = false } diff --git a/examples/random/README.md b/examples/random/README.md new file mode 100644 index 000000000..46d79ee56 --- /dev/null +++ b/examples/random/README.md @@ -0,0 +1,17 @@ +# random + +## About + +This application performs a very important role in physical computing: +It produces randomness in the form of a dice roll (values 1 to 6, inclusive). +It prints out a single random value via the debug console. + +## How to run + +In this folder, run + + laze build -b nrf52840dk run + +This will print different values most of the time on all boards it can be built +on. If there is no way to get startup entropy on a board, the example can not +be built. diff --git a/examples/random/laze.yml b/examples/random/laze.yml new file mode 100644 index 000000000..667cd8551 --- /dev/null +++ b/examples/random/laze.yml @@ -0,0 +1,5 @@ +apps: + - name: random + selects: + - ?release + - rng diff --git a/examples/random/src/main.rs b/examples/random/src/main.rs new file mode 100644 index 000000000..207cb5bc3 --- /dev/null +++ b/examples/random/src/main.rs @@ -0,0 +1,19 @@ +#![no_main] +#![no_std] +#![feature(type_alias_impl_trait)] +#![feature(used_with_arg)] + +use riot_rs::debug::{println}; + +#[riot_rs::thread(autostart)] +fn main() { + use rand::Rng as _; + let mut rng = riot_rs_random::get_rng(); + + let value = rng.gen_range(1..=6); + + println!( + "The random value of this round is {}.", + value + ); +}