diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-01-13 23:10:24 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-01-13 23:56:39 +0100 |
| commit | 6eec3d8acca1a4c6a853d0b65e43ec0a0f5c5c27 (patch) | |
| tree | d49ecce9b03d6f7c5e2dc2c4419e798a69453ec9 /examples | |
| parent | df00c83984b99c5c87a1e90d042147eae7c75631 (diff) | |
nrf/rng: expose all functionality as inherent methods.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf/src/bin/rng.rs | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/examples/nrf/src/bin/rng.rs b/examples/nrf/src/bin/rng.rs index 2f1a26993..a35a9fa85 100644 --- a/examples/nrf/src/bin/rng.rs +++ b/examples/nrf/src/bin/rng.rs | |||
| @@ -5,9 +5,7 @@ | |||
| 5 | #[path = "../example_common.rs"] | 5 | #[path = "../example_common.rs"] |
| 6 | mod example_common; | 6 | mod example_common; |
| 7 | 7 | ||
| 8 | use defmt::unwrap; | ||
| 9 | use embassy::executor::Spawner; | 8 | use embassy::executor::Spawner; |
| 10 | use embassy::traits::rng::Rng as _; | ||
| 11 | use embassy_nrf::interrupt; | 9 | use embassy_nrf::interrupt; |
| 12 | use embassy_nrf::rng::Rng; | 10 | use embassy_nrf::rng::Rng; |
| 13 | use embassy_nrf::Peripherals; | 11 | use embassy_nrf::Peripherals; |
| @@ -19,14 +17,14 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 19 | 17 | ||
| 20 | // Async API | 18 | // Async API |
| 21 | let mut bytes = [0; 4]; | 19 | let mut bytes = [0; 4]; |
| 22 | unwrap!(rng.fill_bytes(&mut bytes).await); // nRF RNG is infallible | 20 | rng.fill_bytes(&mut bytes).await; |
| 23 | defmt::info!("Some random bytes: {:?}", bytes); | 21 | defmt::info!("Some random bytes: {:?}", bytes); |
| 24 | 22 | ||
| 25 | // Sync API with `rand` | 23 | // Sync API with `rand` |
| 26 | defmt::info!("A random number from 1 to 10: {:?}", rng.gen_range(1..=10)); | 24 | defmt::info!("A random number from 1 to 10: {:?}", rng.gen_range(1..=10)); |
| 27 | 25 | ||
| 28 | let mut bytes = [0; 1024]; | 26 | let mut bytes = [0; 1024]; |
| 29 | unwrap!(rng.fill_bytes(&mut bytes).await); | 27 | rng.fill_bytes(&mut bytes).await; |
| 30 | let zero_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_zeros()); | 28 | let zero_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_zeros()); |
| 31 | let one_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_ones()); | 29 | let one_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_ones()); |
| 32 | defmt::info!( | 30 | defmt::info!( |
