Add basic device state handling for endpoints.

This commit is contained in:
alexmoon
2022-04-02 16:35:03 -04:00
committed by Dario Nieuwenhuis
parent 99f95a33c3
commit 2ce435dc34
5 changed files with 190 additions and 60 deletions

View File

@ -4,12 +4,13 @@
#![feature(type_alias_impl_trait)]
use core::mem;
use defmt::*;
use defmt::{info, panic};
use embassy::executor::Spawner;
use embassy_nrf::interrupt;
use embassy_nrf::pac;
use embassy_nrf::usb::Driver;
use embassy_nrf::usb::{Driver, Instance};
use embassy_nrf::Peripherals;
use embassy_usb::driver::{ReadError, WriteError};
use embassy_usb::{Config, UsbDeviceBuilder};
use embassy_usb_serial::{CdcAcmClass, State};
use futures::future::join;
@ -66,12 +67,11 @@ async fn main(_spawner: Spawner, p: Peripherals) {
// Do stuff with the class!
let echo_fut = async {
let mut buf = [0; 64];
loop {
let n = class.read_packet(&mut buf).await.unwrap();
let data = &buf[..n];
info!("data: {:x}", data);
class.write_packet(data).await.unwrap();
class.wait_connection().await;
info!("Connected");
let _ = echo(&mut class).await;
info!("Disconnected");
}
};
@ -79,3 +79,35 @@ async fn main(_spawner: Spawner, p: Peripherals) {
// If we had made everything `'static` above instead, we could do this using separate tasks instead.
join(usb_fut, echo_fut).await;
}
struct Disconnected {}
impl From<ReadError> for Disconnected {
fn from(val: ReadError) -> Self {
match val {
ReadError::BufferOverflow => panic!("Buffer overflow"),
ReadError::Disabled => Disconnected {},
}
}
}
impl From<WriteError> for Disconnected {
fn from(val: WriteError) -> Self {
match val {
WriteError::BufferOverflow => panic!("Buffer overflow"),
WriteError::Disabled => Disconnected {},
}
}
}
async fn echo<'d, T: Instance + 'd>(
class: &mut CdcAcmClass<'d, Driver<'d, T>>,
) -> Result<(), Disconnected> {
let mut buf = [0; 64];
loop {
let n = class.read_packet(&mut buf).await?;
let data = &buf[..n];
info!("data: {:x}", data);
class.write_packet(data).await?;
}
}