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

Unifying async and sync implementation, add async feature flag to i2c, spi #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Changed

- **Breaking** lib: `{SPI, I2C}Interface, PGPIO{8, 16}BitInterface` is renamed to `{Spi, I2c}Interface, PGpio{8, 16}BitInterface`
- **Breaking** i2c, spi: asynchronous implementations require the `async` feature
- simplified sync/async code by adding `maybe-async-cfg` crate to remove quasi-duplicate sync code

## [v0.5.0] - 2023-01-12

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ all-features = true

[dependencies]
defmt = { version = "0.3", optional = true }
maybe-async-cfg = "0.2.3"

[workspace]
members = [
Expand Down
7 changes: 6 additions & 1 deletion i2c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ all-features = true

[dependencies]
embedded-hal = "1.0.0"
embedded-hal-async = "1.0.0"
embedded-hal-async = { version = "1.0.0", optional = true }
display-interface = { version = "0.5.0", path = ".." }
maybe-async-cfg = "0.2.3"

[features]
default = []
async = ["embedded-hal-async"]
6 changes: 6 additions & 0 deletions i2c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ This Rust crate contains a generic I2C implementation of a data/command
interface for displays over any I2C driver implementing the `embedded-hal`/`embedded-hal-async`
`i2c::I2c` trait(s).

## Crate features

Additional features can be enabled by adding the following features to your Cargo.toml.

- `async`: adds `embedded-hal-async` `i2c::I2c` implementation.

## License

Licensed under either of
Expand Down
85 changes: 0 additions & 85 deletions i2c/src/asynch.rs

This file was deleted.

58 changes: 38 additions & 20 deletions i2c/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//! Generic I2C interface for display drivers

#![no_std]

mod asynch;

use display_interface::{DataFormat, DisplayError, WriteOnlyDataCommand};
#[cfg(feature = "async")]
use display_interface::AsyncWriteOnlyDataCommand;
#[cfg(not(feature = "async"))]
use display_interface::WriteOnlyDataCommand;
use display_interface::{DataFormat, DisplayError};
#[cfg(not(feature = "async"))]
use embedded_hal::i2c::I2c;
#[cfg(feature = "async")]
use embedded_hal_async::i2c::I2c as AsyncI2c;

/// I2C communication interface
pub struct I2cInterface<I2C> {
Expand All @@ -30,11 +32,22 @@ impl<I2C> I2cInterface<I2C> {
}
}

impl<I2C> WriteOnlyDataCommand for I2cInterface<I2C>
#[maybe_async_cfg::maybe(
sync(
cfg(not(feature = "async")),
keep_self,
idents(
AsyncWriteOnlyDataCommand(sync = "WriteOnlyDataCommand"),
AsyncI2c(sync = "I2c"),
)
),
async(feature = "async", keep_self)
)]
impl<I2C> AsyncWriteOnlyDataCommand for I2cInterface<I2C>
where
I2C: embedded_hal::i2c::I2c,
I2C: AsyncI2c,
{
fn send_commands(&mut self, cmds: DataFormat<'_>) -> Result<(), DisplayError> {
async fn send_commands(&mut self, cmds: DataFormat<'_>) -> Result<(), DisplayError> {
// Copy over given commands to new aray to prefix with command identifier
match cmds {
DataFormat::U8(slice) => {
Expand All @@ -43,13 +56,14 @@ where

self.i2c
.write(self.addr, &writebuf[..=slice.len()])
.await
.map_err(|_| DisplayError::BusWriteError)
}
_ => Err(DisplayError::DataFormatNotImplemented),
}
}

fn send_data(&mut self, buf: DataFormat<'_>) -> Result<(), DisplayError> {
async fn send_data(&mut self, buf: DataFormat<'_>) -> Result<(), DisplayError> {
match buf {
DataFormat::U8(slice) => {
// No-op if the data buffer is empty
Expand All @@ -62,17 +76,19 @@ where
// Data mode
writebuf[0] = self.data_byte;

slice
.chunks(16)
.try_for_each(|c| {
let chunk_len = c.len();
for chunk in slice.chunks(16) {
let chunk_len = chunk.len();

// Copy over all data from buffer, leaving the data command byte intact
writebuf[1..=chunk_len].copy_from_slice(c);
// Copy over all data from buffer, leaving the data command byte intact
writebuf[1..=chunk_len].copy_from_slice(chunk);

self.i2c.write(self.addr, &writebuf[0..=chunk_len])
})
.map_err(|_| DisplayError::BusWriteError)
self.i2c
.write(self.addr, &writebuf[0..=chunk_len])
.await
.map_err(|_| DisplayError::BusWriteError)?;
}

Ok(())
}
DataFormat::U8Iter(iter) => {
let mut writebuf = [0; 17];
Expand All @@ -89,6 +105,7 @@ where
if i == len {
self.i2c
.write(self.addr, &writebuf[0..=len])
.await
.map_err(|_| DisplayError::BusWriteError)?;
i = 1;
}
Expand All @@ -97,6 +114,7 @@ where
if i > 1 {
self.i2c
.write(self.addr, &writebuf[0..=i])
.await
.map_err(|_| DisplayError::BusWriteError)?;
}

Expand Down
7 changes: 6 additions & 1 deletion spi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ all-features = true

[dependencies]
embedded-hal = "1.0.0"
embedded-hal-async = "1.0.0"
embedded-hal-async = { version = "1.0.0", optional = true }
display-interface = { version = "0.5.0", path = ".." }
byte-slice-cast = { version = "1.2.2", default-features = false }
maybe-async-cfg = "0.2.3"

[features]
default = []
async = ["embedded-hal-async"]
6 changes: 6 additions & 0 deletions spi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ This Rust crate contains a generic SPI implementation of a data/command
interface for displays over any SPI driver implementing the `embedded-hal`/`embedded-hal-async`
`SpiDevice` trait(s).

## Crate features

Additional features can be enabled by adding the following features to your Cargo.toml.

- `async`: adds `embedded-hal-async` `SpiDevice` implementation.

## License

Licensed under either of
Expand Down
Loading