-
Notifications
You must be signed in to change notification settings - Fork 935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds function and macro to create usb logger without device (also fixes a logger issue) #2414
Conversation
embassy-usb-logger/src/lib.rs
Outdated
@@ -105,6 +105,34 @@ impl<const N: usize> UsbLogger<N> { | |||
join(run_fut, join(log_fut, discard_fut)).await; | |||
} | |||
} | |||
|
|||
// Creates the futures needed for the logger from a given class | |||
pub async fn create_future_from_class<'d, D>(&'d self, class: CdcAcmClass<'d, D> ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to use create_future_from_class
inside run
to not duplicate the code, but couldn't make it work since neither the class nor sender and receiver can be copied. If somebody finds a good way to clean this up, feel free to let me know
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late review!
embassy-usb-logger/src/lib.rs
Outdated
loop { | ||
let log_fut = async { | ||
let mut rx: [u8; MAX_PACKET_SIZE as usize] = [0; MAX_PACKET_SIZE as usize]; | ||
sender.wait_connection().await; | ||
loop { | ||
let len = self.buffer.read(&mut rx[..]).await; | ||
let _ = sender.write_packet(&rx[..len]).await; | ||
if len as u8 == MAX_PACKET_SIZE { | ||
let _ = sender.write_packet(&[]).await; | ||
} | ||
} | ||
}; | ||
let discard_fut = async { | ||
let mut discard_buf: [u8; MAX_PACKET_SIZE as usize] = [0; MAX_PACKET_SIZE as usize]; | ||
receiver.wait_connection().await; | ||
loop { | ||
let _ = receiver.read_packet(&mut discard_buf).await; | ||
} | ||
}; | ||
join(log_fut, discard_fut).await; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if you make this an internal fn that takes a &mut sender and &mut receiver in order to reuse code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea! Done
Move const to the outside of the logger
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
This can be used in cases where the user wants to log something via usb but the usb connection is already in use.
It also serves as an example on how to create multiple usb classes with one device.
Finally this fixes an issue in the usb logger where messages that were exactly MAX_PACKET_SIZE (in this case 64) bytes long wouldn't show up in the log straight away, as explained in the CdcAcmClass (see the third point about messages with exactly the length of MAX_PACKET_SIZE)