Skip to content
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

Merged
merged 6 commits into from
Feb 12, 2024

Conversation

JomerDev
Copy link
Contributor

@JomerDev JomerDev commented Jan 7, 2024

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)

@@ -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> )
Copy link
Contributor Author

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

@JomerDev JomerDev changed the title Adds function and macro to create usb logger without device Adds function and macro to create usb logger without device (also fixes a logger issue) Jan 15, 2024
Copy link
Member

@lulf lulf left a 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!

Comment on lines 121 to 141
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;
}
Copy link
Member

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?

Copy link
Contributor Author

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
@JomerDev JomerDev requested a review from lulf February 12, 2024 19:48
Copy link
Member

@lulf lulf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@lulf lulf added this pull request to the merge queue Feb 12, 2024
Merged via the queue into embassy-rs:main with commit a2b7b0c Feb 12, 2024
6 checks passed
@JomerDev JomerDev deleted the usb-logger-without-device branch February 12, 2024 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants