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

Add async API for CAN #585

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion embedded-can/CHANGELOG.md
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

...
- Add async API.

## [v0.4.1] - 2022-09-28

Expand Down
25 changes: 25 additions & 0 deletions embedded-can/src/asynch.rs
@@ -0,0 +1,25 @@
//! Async CAN API

/// An async CAN interface that is able to transmit and receive frames.
pub trait Can {
/// Associated frame type.
type Frame: crate::Frame;

/// Associated error type.
type Error: crate::Error;

/// Puts a frame in the transmit buffer.
/// Awaits until space is available in the transmit buffer.
async fn transmit(&mut self, frame: &Self::Frame) -> Result<(), Self::Error>;

/// Tries to put a frame in the transmit buffer.
/// If no space is available in the transmit buffer `None` is returned.
fn try_transmit(&mut self, frame: &Self::Frame) -> Option<Result<(), Self::Error>>;
Copy link
Member

Choose a reason for hiding this comment

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

Strange signature.

Copy link
Author

Choose a reason for hiding this comment

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

Inspired by a pattern I've seen around like here: https://docs.rs/rtic-sync/latest/rtic_sync/arbiter/struct.Arbiter.html

Can just get rid of it, but I think it's useful.


/// Awaits until a frame was received or an error occurred.
async fn receive(&mut self) -> Result<Self::Frame, Self::Error>;

/// Tries to receive a frame from the receive buffer.
/// If no frame is available `None` is returned.
fn try_receive(&mut self) -> Option<Result<Self::Frame, Self::Error>>;
}
2 changes: 2 additions & 0 deletions embedded-can/src/lib.rs
Expand Up @@ -2,7 +2,9 @@

#![warn(missing_docs)]
#![no_std]
#![allow(async_fn_in_trait)]

pub mod asynch;
pub mod blocking;
pub mod nb;

Expand Down