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

make async optional #189

Merged
merged 8 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ jobs:
matrix:
include:
- os: windows-latest
no-example: true
lint: true
- os: ubuntu-latest
lint: true
- os: windows-latest
arch: aarch64
no-example: true
no-test: true
- os: macos-latest
no-example: true
lint: true
- os: ubuntu-latest
lint: true
- os: ubuntu-latest
arch: arm32
Expand All @@ -34,12 +40,12 @@ jobs:
arch: aarch64
no-example: true
no-test: true
- os: windows-latest
arch: aarch64
no-test: true
- os: ubuntu-latest
features: "use_meter left_handed"
lint: true
- os: ubuntu-latest
features: "async"
lint: true
- os: ubuntu-latest
features: "lightweight async-trait"
lint: true
Expand Down
16 changes: 11 additions & 5 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ jobs:
matrix:
include:
- os: windows-latest
no-example: true
lint: true
- os: ubuntu-latest
lint: true
- os: windows-latest
arch: aarch64
no-example: true
no-test: true
- os: macos-latest
no-example: true
lint: true
- os: ubuntu-latest
lint: true
- os: ubuntu-latest
arch: arm32
Expand All @@ -69,12 +75,12 @@ jobs:
arch: aarch64
no-example: true
no-test: true
- os: windows-latest
arch: aarch64
no-test: true
- os: ubuntu-latest
features: "use_meter left_handed"
lint: true
- os: ubuntu-latest
features: "async"
lint: true
- os: ubuntu-latest
features: "lightweight async-trait"
lint: true
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Update firmware to v10.0.1
- phase correction bram and pulse width encoder table reset to default in clear op
- support for `dynamic_freq` version
- Make `async` optional
- Remove `Deref<Target = Link>` and `DerefMut` for `Controller`
- Impl `Deref<Target = Geometry>` and `DerefMut` for `Controller` instead
- Make `Transducer::new` public
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022-2024 Shun Suzuki
Copyright (c) 2022-2025 Shun Suzuki

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

# Author

Shun Suzuki, 2022-2024
Shun Suzuki, 2022-2025
3 changes: 2 additions & 1 deletion autd3-driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ criterion = { workspace = true }

[features]
default = ["derive"]
async = []
lightweight = []
async-trait = ["dep:async-trait"]
use_meter = []
Expand All @@ -59,5 +60,5 @@ path = "benches/gain.rs"
harness = false

[package.metadata.docs.rs]
features = ["lightweight", "async-trait", "serde", "derive"]
features = ["lightweight", "async", "serde", "derive"]
rustdoc-args = ["--cfg", "docsrs"]
2 changes: 1 addition & 1 deletion autd3-driver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ This crate provides driver of AUTD3 device.

# Author

Shun Suzuki, 2022-2024
Shun Suzuki, 2022-2025
1 change: 0 additions & 1 deletion autd3-driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub mod link;
#[doc(hidden)]
pub mod utils;

#[cfg_attr(docsrs, doc(cfg(feature = "async-trait")))]
#[cfg(feature = "async-trait")]
pub use async_trait::async_trait;

Expand Down
17 changes: 8 additions & 9 deletions autd3-driver/src/link.rs → autd3-driver/src/link/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ use crate::{
geometry::Geometry,
};

pub use internal::Link;
pub use internal::LinkBuilder;
pub use internal::{AsyncLink, AsyncLinkBuilder};

#[cfg(feature = "async-trait")]
mod internal {
use super::*;

/// A trait that provides the interface with the device.
#[async_trait::async_trait]
pub trait Link: Send {
pub trait AsyncLink: Send {
/// Closes the link.
async fn close(&mut self) -> Result<(), AUTDDriverError>;

Expand All @@ -40,16 +39,16 @@ mod internal {

/// A trait to build a link.
#[async_trait::async_trait]
pub trait LinkBuilder: Send + Sync {
pub trait AsyncLinkBuilder: Send + Sync {
/// The link type.
type L: Link;
type L: AsyncLink;

/// Opens a link.
async fn open(self, geometry: &Geometry) -> Result<Self::L, AUTDDriverError>;
}

#[async_trait::async_trait]
impl Link for Box<dyn Link> {
impl AsyncLink for Box<dyn AsyncLink> {
async fn close(&mut self) -> Result<(), AUTDDriverError> {
self.as_mut().close().await
}
Expand Down Expand Up @@ -81,7 +80,7 @@ mod internal {
use super::*;

/// A trait that provides the interface with the device.
pub trait Link: Send {
pub trait AsyncLink: Send {
/// Closes the link.
fn close(&mut self) -> impl std::future::Future<Output = Result<(), AUTDDriverError>>;

Expand Down Expand Up @@ -114,9 +113,9 @@ mod internal {
}

/// A trait to build a link.
pub trait LinkBuilder {
pub trait AsyncLinkBuilder {
/// The link type.
type L: Link;
type L: AsyncLink;

/// Opens a link.
fn open(
Expand Down
10 changes: 10 additions & 0 deletions autd3-driver/src/link/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
#[cfg(feature = "async")]
mod r#async;
mod sync;

#[cfg(feature = "async")]
#[doc(inline)]
pub use r#async::*;
#[doc(inline)]
pub use sync::*;
66 changes: 66 additions & 0 deletions autd3-driver/src/link/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::time::Duration;

use crate::{
derive::Geometry,
error::AUTDDriverError,
firmware::cpu::{RxMessage, TxMessage},
};

/// A trait that provides the interface with the device.
pub trait Link: Send {
/// Closes the link.
fn close(&mut self) -> Result<(), AUTDDriverError>;

#[doc(hidden)]
fn update(&mut self, _geometry: &Geometry) -> Result<(), AUTDDriverError> {
Ok(())
}

/// Sends a message to the device.
fn send(&mut self, tx: &[TxMessage]) -> Result<bool, AUTDDriverError>;

/// Receives a message from the device.
fn receive(&mut self, rx: &mut [RxMessage]) -> Result<bool, AUTDDriverError>;

/// Checks if the link is open.
#[must_use]
fn is_open(&self) -> bool;

#[doc(hidden)]
fn trace(&mut self, _: Option<Duration>, _: Option<usize>) {}
}

/// A trait to build a link.
pub trait LinkBuilder: Send + Sync {
/// The link type.
type L: Link;

/// Opens a link.
fn open(self, geometry: &Geometry) -> Result<Self::L, AUTDDriverError>;
}

impl Link for Box<dyn Link> {
fn close(&mut self) -> Result<(), AUTDDriverError> {
self.as_mut().close()
}

fn update(&mut self, geometry: &Geometry) -> Result<(), AUTDDriverError> {
self.as_mut().update(geometry)
}

fn send(&mut self, tx: &[TxMessage]) -> Result<bool, AUTDDriverError> {
self.as_mut().send(tx)
}

fn receive(&mut self, rx: &mut [RxMessage]) -> Result<bool, AUTDDriverError> {
self.as_mut().receive(rx)
}

fn is_open(&self) -> bool {
self.as_ref().is_open()
}

fn trace(&mut self, timeout: Option<Duration>, parallel_threshold: Option<usize>) {
self.as_mut().trace(timeout, parallel_threshold)
}
}
8 changes: 7 additions & 1 deletion autd3-link-simulator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ repository = { workspace = true }

[dependencies]
autd3-protobuf = { workspace = true }
autd3-driver = { workspace = true, features = ["derive"] }
autd3-driver = { workspace = true, features = ["derive", "async"] }
tonic = { workspace = true }
tracing = { workspace = true }
tokio = { workspace = true, features = ["rt", "rt-multi-thread"], optional = true }

[features]
default = []
blocking = ["tokio"]
async-trait = ["autd3-driver/async-trait", "autd3-protobuf/async-trait"]

[package.metadata.docs.rs]
features = ["blocking"]
rustdoc-args = ["--cfg", "docsrs"]
2 changes: 1 addition & 1 deletion autd3-link-simulator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ This crate provides a link for [AUTD3 Simulator](https://github.com/shinolab/aut

# Author

Shun Suzuki, 2022-2024
Shun Suzuki, 2022-2025
67 changes: 63 additions & 4 deletions autd3-link-simulator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]
#![warn(rustdoc::unescaped_backticks)]
Expand All @@ -13,10 +14,10 @@ use std::net::SocketAddr;
use autd3_driver::{
derive::*,
firmware::cpu::{RxMessage, TxMessage},
link::{Link, LinkBuilder},
link::{AsyncLink, AsyncLinkBuilder},
};

/// A [`Link`] for [`AUTD3 Simulator`].
/// A [`AsyncLink`] for [`AUTD3 Simulator`].
///
/// [`AUTD3 Simulator`]: https://github.com/shinolab/autd3-server
pub struct Simulator {
Expand All @@ -34,7 +35,7 @@ pub struct SimulatorBuilder {
}

#[cfg_attr(feature = "async-trait", autd3_driver::async_trait)]
impl LinkBuilder for SimulatorBuilder {
impl AsyncLinkBuilder for SimulatorBuilder {
type L = Simulator;

#[tracing::instrument(level = "debug", skip(geometry))]
Expand Down Expand Up @@ -74,7 +75,7 @@ impl Simulator {
}

#[cfg_attr(feature = "async-trait", autd3_driver::async_trait)]
impl Link for Simulator {
impl AsyncLink for Simulator {
async fn close(&mut self) -> Result<(), AUTDDriverError> {
if !self.is_open {
return Ok(());
Expand Down Expand Up @@ -137,3 +138,61 @@ impl Link for Simulator {
self.is_open
}
}

#[cfg(feature = "blocking")]
use autd3_driver::link::{Link, LinkBuilder};

/// A [`Link`] for [`AUTD3 Simulator`].
///
/// [`AUTD3 Simulator`]: https://github.com/shinolab/autd3-server
#[cfg_attr(docsrs, doc(cfg(feature = "blocking")))]
#[cfg(feature = "blocking")]
pub struct SimulatorBlocking {
runtime: tokio::runtime::Runtime,
inner: Simulator,
}

#[cfg(feature = "blocking")]
impl Link for SimulatorBlocking {
fn close(&mut self) -> Result<(), AUTDDriverError> {
self.runtime.block_on(self.inner.close())
}

fn update(
&mut self,
geometry: &autd3_driver::geometry::Geometry,
) -> Result<(), AUTDDriverError> {
self.runtime.block_on(self.inner.update(geometry))
}

fn send(&mut self, tx: &[TxMessage]) -> Result<bool, AUTDDriverError> {
self.runtime.block_on(self.inner.send(tx))
}

fn receive(&mut self, rx: &mut [RxMessage]) -> Result<bool, AUTDDriverError> {
self.runtime.block_on(self.inner.receive(rx))
}

fn is_open(&self) -> bool {
self.inner.is_open()
}

fn trace(&mut self, timeout: Option<std::time::Duration>, parallel_threshold: Option<usize>) {
self.inner.trace(timeout, parallel_threshold)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "blocking")))]
#[cfg(feature = "blocking")]
impl LinkBuilder for SimulatorBuilder {
type L = SimulatorBlocking;

fn open(self, geometry: &autd3_driver::geometry::Geometry) -> Result<Self::L, AUTDDriverError> {
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("Failed to create runtime");
let inner = runtime.block_on(<Self as AsyncLinkBuilder>::open(self, geometry))?;
Ok(Self::L { runtime, inner })
}
}
5 changes: 3 additions & 2 deletions autd3-link-twincat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ cc = { workspace = true, optional = true }
local = ["libloading"]
remote = ["itertools", "cc", "tracing"]
default = ["local"]
async = ["autd3-driver/async"]
async-trait = ["async", "autd3-driver/async-trait"]
all = ["local", "remote"]
async-trait = ["autd3-driver/async-trait"]

[package.metadata.docs.rs]
all-features = true
features = ["local", "remote", "async"]
rustdoc-args = ["--cfg", "docsrs"]
2 changes: 1 addition & 1 deletion autd3-link-twincat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ This crate provides a link to AUTD using TwinCAT.

# Author

Shun Suzuki, 2022-2024
Shun Suzuki, 2022-2025
Loading
Loading