examples: Consistently use unwrap! in favor of .unwrap()

Unfortunately errors from `embedded_graphics` and `core` doesn't provide
the necessary instances currently.
This commit is contained in:
Ben Gamari 2021-07-31 11:51:40 -04:00 committed by Dario Nieuwenhuis
parent 36402b5487
commit f4950c4449
22 changed files with 67 additions and 65 deletions

View File

@ -6,6 +6,7 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use defmt::unwrap;
use embassy::executor::Spawner; use embassy::executor::Spawner;
use embassy::time::{Duration, Timer}; use embassy::time::{Duration, Timer};
use embassy_nrf::gpio::{Level, Output, OutputDrive}; use embassy_nrf::gpio::{Level, Output, OutputDrive};
@ -17,9 +18,9 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let mut led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard); let mut led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard);
loop { loop {
led.set_high().unwrap(); unwrap!(led.set_high());
Timer::after(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
led.set_low().unwrap(); unwrap!(led.set_low());
Timer::after(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
} }
} }

View File

@ -32,8 +32,8 @@ async fn main(spawner: Spawner, p: Peripherals) {
let btn3 = PortInput::new(Input::new(p.P0_24.degrade(), Pull::Up)); let btn3 = PortInput::new(Input::new(p.P0_24.degrade(), Pull::Up));
let btn4 = PortInput::new(Input::new(p.P0_25.degrade(), Pull::Up)); let btn4 = PortInput::new(Input::new(p.P0_25.degrade(), Pull::Up));
spawner.spawn(button_task(1, btn1)).unwrap(); unwrap!(spawner.spawn(button_task(1, btn1)));
spawner.spawn(button_task(2, btn2)).unwrap(); unwrap!(spawner.spawn(button_task(2, btn2)));
spawner.spawn(button_task(3, btn3)).unwrap(); unwrap!(spawner.spawn(button_task(3, btn3)));
spawner.spawn(button_task(4, btn4)).unwrap(); unwrap!(spawner.spawn(button_task(4, btn4)));
} }

View File

@ -6,6 +6,7 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use defmt::unwrap;
use embassy::executor::Spawner; use embassy::executor::Spawner;
use embassy::time::{Duration, Timer}; use embassy::time::{Duration, Timer};
use embassy::util::mpsc::TryRecvError; use embassy::util::mpsc::TryRecvError;
@ -39,7 +40,7 @@ async fn main(spawner: Spawner, p: Peripherals) {
let channel = CHANNEL.put(Channel::new()); let channel = CHANNEL.put(Channel::new());
let (sender, mut receiver) = mpsc::split(channel); let (sender, mut receiver) = mpsc::split(channel);
spawner.spawn(my_task(sender)).unwrap(); unwrap!(spawner.spawn(my_task(sender)));
// We could just loop on `receiver.recv()` for simplicity. The code below // We could just loop on `receiver.recv()` for simplicity. The code below
// is optimized to drain the queue as fast as possible in the spirit of // is optimized to drain the queue as fast as possible in the spirit of
@ -53,8 +54,8 @@ async fn main(spawner: Spawner, p: Peripherals) {
Err(TryRecvError::Closed) => break, Err(TryRecvError::Closed) => break,
}; };
match maybe_message { match maybe_message {
Some(LedState::On) => led.set_high().unwrap(), Some(LedState::On) => unwrap!(led.set_high()),
Some(LedState::Off) => led.set_low().unwrap(), Some(LedState::Off) => unwrap!(led.set_low()),
_ => (), _ => (),
} }
} }

View File

@ -35,19 +35,19 @@ async fn main(_spawner: Spawner, p: Peripherals) {
.await; .await;
let mut id = [1; 3]; let mut id = [1; 3];
q.custom_instruction(0x9F, &[], &mut id).await.unwrap(); unwrap!(q.custom_instruction(0x9F, &[], &mut id).await);
info!("id: {}", id); info!("id: {}", id);
// Read status register // Read status register
let mut status = [4; 1]; let mut status = [4; 1];
q.custom_instruction(0x05, &[], &mut status).await.unwrap(); unwrap!(q.custom_instruction(0x05, &[], &mut status).await);
info!("status: {:?}", status[0]); info!("status: {:?}", status[0]);
if status[0] & 0x40 == 0 { if status[0] & 0x40 == 0 {
status[0] |= 0x40; status[0] |= 0x40;
q.custom_instruction(0x01, &status, &mut []).await.unwrap(); unwrap!(q.custom_instruction(0x01, &status, &mut []).await);
info!("enabled quad in status"); info!("enabled quad in status");
} }
@ -58,19 +58,19 @@ async fn main(_spawner: Spawner, p: Peripherals) {
for i in 0..8 { for i in 0..8 {
info!("page {:?}: erasing... ", i); info!("page {:?}: erasing... ", i);
q.erase(i * PAGE_SIZE).await.unwrap(); unwrap!(q.erase(i * PAGE_SIZE).await);
for j in 0..PAGE_SIZE { for j in 0..PAGE_SIZE {
buf.0[j] = pattern((j + i * PAGE_SIZE) as u32); buf.0[j] = pattern((j + i * PAGE_SIZE) as u32);
} }
info!("programming..."); info!("programming...");
q.write(i * PAGE_SIZE, &buf.0).await.unwrap(); unwrap!(q.write(i * PAGE_SIZE, &buf.0).await);
} }
for i in 0..8 { for i in 0..8 {
info!("page {:?}: reading... ", i); info!("page {:?}: reading... ", i);
q.read(i * PAGE_SIZE, &mut buf.0).await.unwrap(); unwrap!(q.read(i * PAGE_SIZE, &mut buf.0).await);
info!("verifying..."); info!("verifying...");
for j in 0..PAGE_SIZE { for j in 0..PAGE_SIZE {

View File

@ -49,19 +49,19 @@ async fn main(_spawner: Spawner, mut p: Peripherals) {
.await; .await;
let mut id = [1; 3]; let mut id = [1; 3];
q.custom_instruction(0x9F, &[], &mut id).await.unwrap(); unwrap!(q.custom_instruction(0x9F, &[], &mut id).await);
info!("id: {}", id); info!("id: {}", id);
// Read status register // Read status register
let mut status = [4; 1]; let mut status = [4; 1];
q.custom_instruction(0x05, &[], &mut status).await.unwrap(); unwrap!(q.custom_instruction(0x05, &[], &mut status).await);
info!("status: {:?}", status[0]); info!("status: {:?}", status[0]);
if status[0] & 0x40 == 0 { if status[0] & 0x40 == 0 {
status[0] |= 0x40; status[0] |= 0x40;
q.custom_instruction(0x01, &status, &mut []).await.unwrap(); unwrap!(q.custom_instruction(0x01, &status, &mut []).await);
info!("enabled quad in status"); info!("enabled quad in status");
} }
@ -69,7 +69,7 @@ async fn main(_spawner: Spawner, mut p: Peripherals) {
let mut buf = AlignedBuf([0u8; 64]); let mut buf = AlignedBuf([0u8; 64]);
info!("reading..."); info!("reading...");
q.read(0, &mut buf.0).await.unwrap(); unwrap!(q.read(0, &mut buf.0).await);
info!("read: {=[u8]:x}", buf.0); info!("read: {=[u8]:x}", buf.0);
// Drop the QSPI instance. This disables the peripehral and deconfigures the pins. // Drop the QSPI instance. This disables the peripehral and deconfigures the pins.

View File

@ -6,7 +6,7 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use defmt::panic; use defmt::{panic, unwrap};
use embassy::executor::Spawner; use embassy::executor::Spawner;
use embassy::traits::rng::Rng as _; use embassy::traits::rng::Rng as _;
use embassy_nrf::interrupt; use embassy_nrf::interrupt;
@ -20,14 +20,14 @@ async fn main(_spawner: Spawner, p: Peripherals) {
// Async API // Async API
let mut bytes = [0; 4]; let mut bytes = [0; 4];
rng.fill_bytes(&mut bytes).await.unwrap(); // nRF RNG is infallible unwrap!(rng.fill_bytes(&mut bytes).await); // nRF RNG is infallible
defmt::info!("Some random bytes: {:?}", bytes); defmt::info!("Some random bytes: {:?}", bytes);
// Sync API with `rand` // Sync API with `rand`
defmt::info!("A random number from 1 to 10: {:?}", rng.gen_range(1..=10)); defmt::info!("A random number from 1 to 10: {:?}", rng.gen_range(1..=10));
let mut bytes = [0; 1024]; let mut bytes = [0; 1024];
rng.fill_bytes(&mut bytes).await.unwrap(); unwrap!(rng.fill_bytes(&mut bytes).await);
let zero_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_zeros()); 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()); let one_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_ones());
defmt::info!( defmt::info!(

View File

@ -31,12 +31,12 @@ async fn main(_spawner: Spawner, p: Peripherals) {
// softreset // softreset
cortex_m::asm::delay(10); cortex_m::asm::delay(10);
ncs.set_low().unwrap(); unwrap!(ncs.set_low());
cortex_m::asm::delay(5); cortex_m::asm::delay(5);
let tx = [0xFF]; let tx = [0xFF];
unwrap!(spim.read_write(&mut [], &tx).await); unwrap!(spim.read_write(&mut [], &tx).await);
cortex_m::asm::delay(10); cortex_m::asm::delay(10);
ncs.set_high().unwrap(); unwrap!(ncs.set_high());
cortex_m::asm::delay(100000); cortex_m::asm::delay(100000);
@ -44,31 +44,31 @@ async fn main(_spawner: Spawner, p: Peripherals) {
// read ESTAT // read ESTAT
cortex_m::asm::delay(5000); cortex_m::asm::delay(5000);
ncs.set_low().unwrap(); unwrap!(ncs.set_low());
cortex_m::asm::delay(5000); cortex_m::asm::delay(5000);
let tx = [0b000_11101, 0]; let tx = [0b000_11101, 0];
unwrap!(spim.read_write(&mut rx, &tx).await); unwrap!(spim.read_write(&mut rx, &tx).await);
cortex_m::asm::delay(5000); cortex_m::asm::delay(5000);
ncs.set_high().unwrap(); unwrap!(ncs.set_high());
info!("estat: {=[?]}", rx); info!("estat: {=[?]}", rx);
// Switch to bank 3 // Switch to bank 3
cortex_m::asm::delay(10); cortex_m::asm::delay(10);
ncs.set_low().unwrap(); unwrap!(ncs.set_low());
cortex_m::asm::delay(5); cortex_m::asm::delay(5);
let tx = [0b100_11111, 0b11]; let tx = [0b100_11111, 0b11];
unwrap!(spim.read_write(&mut rx, &tx).await); unwrap!(spim.read_write(&mut rx, &tx).await);
cortex_m::asm::delay(10); cortex_m::asm::delay(10);
ncs.set_high().unwrap(); unwrap!(ncs.set_high());
// read EREVID // read EREVID
cortex_m::asm::delay(10); cortex_m::asm::delay(10);
ncs.set_low().unwrap(); unwrap!(ncs.set_low());
cortex_m::asm::delay(5); cortex_m::asm::delay(5);
let tx = [0b000_10010, 0]; let tx = [0b000_10010, 0];
unwrap!(spim.read_write(&mut rx, &tx).await); unwrap!(spim.read_write(&mut rx, &tx).await);
cortex_m::asm::delay(10); cortex_m::asm::delay(10);
ncs.set_high().unwrap(); unwrap!(ncs.set_high());
info!("erevid: {=[?]}", rx); info!("erevid: {=[?]}", rx);
} }

View File

@ -27,7 +27,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
twi.write_then_read(ADDRESS, &mut [0x00], &mut buf).unwrap(); unwrap!(twi.write_then_read(ADDRESS, &mut [0x00], &mut buf));
info!("Read: {=[u8]:x}", buf); info!("Read: {=[u8]:x}", buf);
} }

View File

@ -37,7 +37,7 @@ async fn main(_spawner: Spawner, mut p: Peripherals) {
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 16]; let mut buf = [0u8; 16];
twi.write_then_read(ADDRESS, &mut [0x00], &mut buf).unwrap(); unwrap!(twi.write_then_read(ADDRESS, &mut [0x00], &mut buf));
info!("Read: {=[u8]:x}", buf); info!("Read: {=[u8]:x}", buf);

View File

@ -26,11 +26,11 @@ async fn main(_spawner: Spawner, p: Peripherals) {
loop { loop {
info!("high"); info!("high");
led.set_high().unwrap(); unwrap!(led.set_high());
Timer::after(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
info!("low"); info!("low");
led.set_low().unwrap(); unwrap!(led.set_low());
Timer::after(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
} }
} }

View File

@ -28,14 +28,14 @@ fn main() -> ! {
let mut led3 = Output::new(p.PB14, Level::High, Speed::Low); let mut led3 = Output::new(p.PB14, Level::High, Speed::Low);
loop { loop {
if button.is_high().unwrap() { if unwrap!(button.is_high()) {
info!("high"); info!("high");
led1.set_high().unwrap(); unwrap!(led1.set_high());
led3.set_low().unwrap(); unwrap!(led3.set_low());
} else { } else {
info!("low"); info!("low");
led1.set_low().unwrap(); unwrap!(led1.set_low());
led3.set_high().unwrap(); unwrap!(led3.set_high());
} }
} }
} }

View File

@ -26,12 +26,12 @@ fn main() -> ! {
let config = Config::default(); let config = Config::default();
let mut usart = Uart::new(p.USART3, p.PD9, p.PD8, NoDma, NoDma, config); let mut usart = Uart::new(p.USART3, p.PD9, p.PD8, NoDma, NoDma, config);
usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap(); unwrap!(usart.bwrite_all(b"Hello Embassy World!\r\n"));
info!("wrote Hello, starting echo"); info!("wrote Hello, starting echo");
let mut buf = [0u8; 1]; let mut buf = [0u8; 1];
loop { loop {
usart.read_blocking(&mut buf).unwrap(); unwrap!(usart.read_blocking(&mut buf).unwbrap());
usart.bwrite_all(&buf).unwrap(); unwrap!(usart.bwrite_all(&buf).unwrap());
} }
} }

View File

@ -31,7 +31,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let mut s: String<128> = String::new(); let mut s: String<128> = String::new();
core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap(); core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap();
usart.write(s.as_bytes()).await.unwrap(); unwrap!(usart.write(s.as_bytes()).await);
info!("wrote DMA"); info!("wrote DMA");
} }
} }

View File

@ -24,11 +24,11 @@ async fn main(_spawner: Spawner, p: Peripherals) {
loop { loop {
info!("high"); info!("high");
led.set_high().unwrap(); unwrap!(led.set_high());
Timer::after(Duration::from_millis(500)).await; Timer::after(Duration::from_millis(500)).await;
info!("low"); info!("low");
led.set_low().unwrap(); unwrap!(led.set_low());
Timer::after(Duration::from_millis(500)).await; Timer::after(Duration::from_millis(500)).await;
} }
} }

View File

@ -23,13 +23,13 @@ async fn main_task() {
let config = Config::default(); let config = Config::default();
let mut usart = Uart::new(p.UART7, p.PF6, p.PF7, NoDma, NoDma, config); let mut usart = Uart::new(p.UART7, p.PF6, p.PF7, NoDma, NoDma, config);
usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap(); unwrap!(usart.bwrite_all(b"Hello Embassy World!\r\n"));
info!("wrote Hello, starting echo"); info!("wrote Hello, starting echo");
let mut buf = [0u8; 1]; let mut buf = [0u8; 1];
loop { loop {
usart.read_blocking(&mut buf).unwrap(); unwrap!(usart.read_blocking(&mut buf));
usart.bwrite_all(&buf).unwrap(); unwrap!(usart.bwrite_all(&buf));
} }
} }

View File

@ -25,11 +25,11 @@ async fn main(_spawner: Spawner, mut p: Peripherals) {
loop { loop {
info!("high"); info!("high");
led.set_high().unwrap(); unwrap!(led.set_high());
Timer::after(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
info!("low"); info!("low");
led.set_low().unwrap(); unwrap!(led.set_low());
Timer::after(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
} }
} }

View File

@ -27,14 +27,14 @@ fn main() -> ! {
let mut led2 = Output::new(p.PB5, Level::High, Speed::Low); let mut led2 = Output::new(p.PB5, Level::High, Speed::Low);
loop { loop {
if button.is_high().unwrap() { if unwrap!(button.is_high()) {
info!("high"); info!("high");
led1.set_high().unwrap(); unwrap!(led1.set_high());
led2.set_low().unwrap(); unwrap!(led2.set_low());
} else { } else {
info!("low"); info!("low");
led1.set_low().unwrap(); unwrap!(led1.set_low());
led2.set_high().unwrap(); unwrap!(led2.set_high());
} }
} }
} }

View File

@ -25,9 +25,9 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let mut led = Output::new(p.PB14, Level::High, Speed::Low); let mut led = Output::new(p.PB14, Level::High, Speed::Low);
loop { loop {
led.set_high().unwrap(); unwrap!(led.set_high());
Timer::after(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
led.set_low().unwrap(); unwrap!(led.set_low());
Timer::after(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
} }
} }

View File

@ -24,7 +24,7 @@ fn main() -> ! {
let button = Input::new(p.PC13, Pull::Up); let button = Input::new(p.PC13, Pull::Up);
loop { loop {
if button.is_high().unwrap() { if unwrap!(button.is_high()) {
info!("high"); info!("high");
} else { } else {
info!("low"); info!("low");

View File

@ -45,10 +45,10 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let ready = Input::new(p.PE1, Pull::Up); let ready = Input::new(p.PE1, Pull::Up);
cortex_m::asm::delay(100_000); cortex_m::asm::delay(100_000);
reset.set_high().unwrap(); unwrap!(reset.set_high());
cortex_m::asm::delay(100_000); cortex_m::asm::delay(100_000);
while ready.is_low().unwrap() { while unwrap!(ready.is_low()) {
info!("waiting for ready"); info!("waiting for ready");
} }

View File

@ -26,12 +26,12 @@ fn main() -> ! {
let config = Config::default(); let config = Config::default();
let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, NoDma, NoDma, config); let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, NoDma, NoDma, config);
usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap(); unwrap!(usart.bwrite_all(b"Hello Embassy World!\r\n"));
info!("wrote Hello, starting echo"); info!("wrote Hello, starting echo");
let mut buf = [0u8; 1]; let mut buf = [0u8; 1];
loop { loop {
usart.read_blocking(&mut buf).unwrap(); unwrap!(usart.read_blocking(&mut buf));
usart.bwrite_all(&buf).unwrap(); unwrap!(usart.bwrite_all(&buf));
} }
} }

View File

@ -24,11 +24,11 @@ async fn main(_spawner: Spawner, p: Peripherals) {
loop { loop {
info!("high"); info!("high");
led.set_high().unwrap(); unwrap!(led.set_high());
Timer::after(Duration::from_millis(500)).await; Timer::after(Duration::from_millis(500)).await;
info!("low"); info!("low");
led.set_low().unwrap(); unwrap!(led.set_low());
Timer::after(Duration::from_millis(500)).await; Timer::after(Duration::from_millis(500)).await;
} }
} }