diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-09-01 22:53:10 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-09-01 22:53:10 +0200 |
| commit | bc68657c2397008087bdd6afcd2088ee649a0058 (patch) | |
| tree | 9681cd2613ea69736c2ffd20a6ecd74336ecfe2a /examples | |
| parent | ea688afe9b32cfdf10e91805d8c1e5668e0e85cd (diff) | |
| parent | aaa4a477d56bdcaeff36723ce7fdaba0dce054d1 (diff) | |
Merge pull request #379 from bobmcwhirter/random_range
Random range
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32h7/src/bin/eth.rs | 6 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/rng.rs | 35 |
2 files changed, 38 insertions, 3 deletions
diff --git a/examples/stm32h7/src/bin/eth.rs b/examples/stm32h7/src/bin/eth.rs index 4a9f261c2..df4931455 100644 --- a/examples/stm32h7/src/bin/eth.rs +++ b/examples/stm32h7/src/bin/eth.rs | |||
| @@ -21,7 +21,7 @@ use embassy_net::{ | |||
| 21 | }; | 21 | }; |
| 22 | use embassy_stm32::eth::lan8742a::LAN8742A; | 22 | use embassy_stm32::eth::lan8742a::LAN8742A; |
| 23 | use embassy_stm32::eth::{Ethernet, State}; | 23 | use embassy_stm32::eth::{Ethernet, State}; |
| 24 | use embassy_stm32::rng::Random; | 24 | use embassy_stm32::rng::Rng; |
| 25 | use embassy_stm32::{interrupt, peripherals}; | 25 | use embassy_stm32::{interrupt, peripherals}; |
| 26 | use heapless::Vec; | 26 | use heapless::Vec; |
| 27 | use panic_probe as _; | 27 | use panic_probe as _; |
| @@ -81,7 +81,7 @@ fn _embassy_rand(buf: &mut [u8]) { | |||
| 81 | }); | 81 | }); |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | static mut RNG_INST: Option<Random<RNG>> = None; | 84 | static mut RNG_INST: Option<Rng<RNG>> = None; |
| 85 | 85 | ||
| 86 | static EXECUTOR: Forever<Executor> = Forever::new(); | 86 | static EXECUTOR: Forever<Executor> = Forever::new(); |
| 87 | static STATE: Forever<State<'static, 4, 4>> = Forever::new(); | 87 | static STATE: Forever<State<'static, 4, 4>> = Forever::new(); |
| @@ -97,7 +97,7 @@ fn main() -> ! { | |||
| 97 | 97 | ||
| 98 | let p = embassy_stm32::init(config()); | 98 | let p = embassy_stm32::init(config()); |
| 99 | 99 | ||
| 100 | let rng = Random::new(p.RNG); | 100 | let rng = Rng::new(p.RNG); |
| 101 | unsafe { | 101 | unsafe { |
| 102 | RNG_INST.replace(rng); | 102 | RNG_INST.replace(rng); |
| 103 | } | 103 | } |
diff --git a/examples/stm32h7/src/bin/rng.rs b/examples/stm32h7/src/bin/rng.rs new file mode 100644 index 000000000..2dc8268b5 --- /dev/null +++ b/examples/stm32h7/src/bin/rng.rs | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(type_alias_impl_trait)] | ||
| 5 | #![allow(incomplete_features)] | ||
| 6 | |||
| 7 | #[path = "../example_common.rs"] | ||
| 8 | mod example_common; | ||
| 9 | use embassy::executor::Spawner; | ||
| 10 | use embassy::time::{Duration, Timer}; | ||
| 11 | use embassy::traits::rng::Random; | ||
| 12 | use embassy_stm32::gpio::{Level, Output, Speed}; | ||
| 13 | use embassy_stm32::rng::Rng; | ||
| 14 | use embassy_stm32::Peripherals; | ||
| 15 | use embedded_hal::digital::v2::OutputPin; | ||
| 16 | use example_common::*; | ||
| 17 | |||
| 18 | #[embassy::main] | ||
| 19 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 20 | info!("Hello World!"); | ||
| 21 | |||
| 22 | let mut led = Output::new(p.PB14, Level::High, Speed::Low); | ||
| 23 | |||
| 24 | let mut rng = Random::new(Rng::new(p.RNG)); | ||
| 25 | |||
| 26 | loop { | ||
| 27 | info!("high {}", unwrap!(rng.next_u8(16).await)); | ||
| 28 | unwrap!(led.set_high()); | ||
| 29 | Timer::after(Duration::from_millis(500)).await; | ||
| 30 | |||
| 31 | info!("low {}", unwrap!(rng.next_u8(16).await)); | ||
| 32 | unwrap!(led.set_low()); | ||
| 33 | Timer::after(Duration::from_millis(500)).await; | ||
| 34 | } | ||
| 35 | } | ||
