cleanup example

This commit is contained in:
Jacob Rosenthal 2021-12-15 12:30:48 -07:00
parent cfb5e8df57
commit 5d19f87acb

View File

@ -40,53 +40,55 @@ async fn main(_spawner: Spawner, p: Peripherals) {
irq.set_priority(interrupt::Priority::P3); irq.set_priority(interrupt::Priority::P3);
let mut state = State::new(); let mut state = State::new();
let usb = unsafe { Usb::new(&mut state, device, serial, irq) }; let usb = unsafe { Usb::new(&mut state, device, serial, irq) };
pin_mut!(usb); pin_mut!(usb);
let (mut read_interface, mut write_interface) = usb.as_ref().take_serial_0(); let (mut reader, mut writer) = usb.as_ref().take_serial_0();
unwrap!(write_interface.write_all(b"\r\nSend something\r\n").await); unwrap!(writer.write_all(b"\r\nSend something\r\n").await);
info!("usb initialized!"); info!("usb initialized!");
let mut buf = [0u8; 64]; let mut buf = [0u8; 64];
loop { loop {
let mut n = 0; let mut n = 0;
let left = { let timed_out = {
let recv_fut = async { let newline_fut = async {
loop { loop {
let byte = unwrap!(read_interface.read_byte().await); let char = unwrap!(reader.read_byte().await);
unwrap!(write_interface.write_byte(byte).await); // echo input back to screen
buf[n] = byte; unwrap!(writer.write_byte(char).await);
buf[n] = char;
n += 1; n += 1;
if byte == b'\n' || byte == b'\r' || n == buf.len() { if char == b'\n' || char == b'\r' || n == buf.len() {
break; break;
} }
} }
}; };
pin_mut!(recv_fut); pin_mut!(newline_fut);
let timeout = Timer::after(Duration::from_ticks(32768 * 10)); let timeout_fut = Timer::after(Duration::from_ticks(32768 * 10));
match select(recv_fut, timeout).await { // select chooses whichever returns first
Either::Left(_) => true, match select(newline_fut, timeout_fut).await {
Either::Right(_) => false, Either::Left(_) => false,
Either::Right(_) => true,
} }
}; };
if left { if timed_out {
for c in buf[..n].iter_mut() { unwrap!(writer.write_all(b"\r\nTimed out\r\n").await);
if 0x61 <= *c && *c <= 0x7a {
*c &= !0x20;
}
}
unwrap!(write_interface.write_byte(b'\n').await);
unwrap!(write_interface.write_all(&buf[..n]).await);
unwrap!(write_interface.write_byte(b'\n').await);
} else { } else {
unwrap!(write_interface.write_all(b"\r\nSend something\r\n").await); for char in buf[..n].iter_mut() {
// upper case
if 0x61 <= *char && *char <= 0x7a {
*char &= !0x20;
}
}
unwrap!(writer.write_byte(b'\n').await);
unwrap!(writer.write_all(&buf[..n]).await);
unwrap!(writer.write_byte(b'\n').await);
} }
} }
} }