embassy/examples/nrf52840/src/bin/rng.rs

35 lines
1.1 KiB
Rust
Raw Normal View History

#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use embassy_executor::Spawner;
2021-06-30 04:55:30 +02:00
use embassy_nrf::rng::Rng;
use embassy_nrf::{bind_interrupts, peripherals, rng};
use rand::Rng as _;
2022-06-12 22:15:44 +02:00
use {defmt_rtt as _, panic_probe as _};
bind_interrupts!(struct Irqs {
RNG => rng::InterruptHandler<peripherals::RNG>;
});
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_nrf::init(Default::default());
let mut rng = Rng::new(p.RNG, Irqs);
// Async API
let mut bytes = [0; 4];
rng.fill_bytes(&mut bytes).await;
defmt::info!("Some random bytes: {:?}", bytes);
// Sync API with `rand`
defmt::info!("A random number from 1 to 10: {:?}", rng.gen_range(1..=10));
2021-06-30 04:55:30 +02:00
let mut bytes = [0; 1024];
rng.fill_bytes(&mut bytes).await;
2021-06-30 04:55:30 +02:00
let zero_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_zeros());
let one_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_ones());
2022-06-12 22:15:44 +02:00
defmt::info!("Chance of zero: {}%", zero_count * 100 / (bytes.len() as u32 * 8));
defmt::info!("Chance of one: {}%", one_count * 100 / (bytes.len() as u32 * 8));
}