2021-06-29 09:26:16 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2021-06-30 04:55:30 +02:00
|
|
|
use embassy_nrf::rng::Rng;
|
2023-03-05 21:50:15 +01:00
|
|
|
use embassy_nrf::{bind_interrupts, peripherals, rng};
|
2021-06-29 09:26:16 +02:00
|
|
|
use rand::Rng as _;
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-04-02 04:35:06 +02:00
|
|
|
|
2023-03-05 21:50:15 +01:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
RNG => rng::InterruptHandler<peripherals::RNG>;
|
|
|
|
});
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 18:49:55 +02:00
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_nrf::init(Default::default());
|
2023-03-05 21:50:15 +01:00
|
|
|
let mut rng = Rng::new(p.RNG, Irqs);
|
2021-06-29 09:26:16 +02:00
|
|
|
|
|
|
|
// Async API
|
|
|
|
let mut bytes = [0; 4];
|
2022-01-13 23:10:24 +01:00
|
|
|
rng.fill_bytes(&mut bytes).await;
|
2021-06-29 09:26:16 +02:00
|
|
|
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];
|
2022-01-13 23:10:24 +01:00
|
|
|
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));
|
2021-06-29 09:26:16 +02:00
|
|
|
}
|