pub struct I2cClient {
handle: KernelHandle<I2cService>,
reply: Reusable<Envelope<Result<Transaction, Infallible>>>,
cached_buf: Option<FixedVec<u8>>,
}
Expand description
A client for the I2cService
.
This type is used to perform I²C bus operations. It is obtained
using I2cClient::from_registry
(or
I2cClient::from_registry_no_retry
).
Once an I2cClient
has been acquired, it may be used to perform
I²C operations, either using its implementation of the
embedded_hal_async
I2c
trait, or using the lower-level
Transaction
interface returned by I2cClient::start_transaction
. See
the documentation for embedded_hal_async::i2c::I2c
for details on that
interface, or the Transaction
type for details on using the lower-level
transaction interface.
An I2cClient
does not represent a “lock” on the I²C bus.
Multiple I2cClients
can coexist without preventing each other from
performing bus operations. Instead, the bus is locked only while performing
a Transaction
, or while using the I2c::transaction
method on the
embedded_hal_async::i2c::I2c
implementation.
Fields§
§handle: KernelHandle<I2cService>
§reply: Reusable<Envelope<Result<Transaction, Infallible>>>
§cached_buf: Option<FixedVec<u8>>
Implementations§
source§impl I2cClient
impl I2cClient
sourcepub async fn from_registry(
kernel: &'static Kernel,
) -> Result<Self, ConnectError<I2cService>>
pub async fn from_registry( kernel: &'static Kernel, ) -> Result<Self, ConnectError<I2cService>>
Obtain an I2cClient
If the I2cService
hasn’t been registered yet, we will retry until it
has been registered.
sourcepub async fn from_registry_no_retry(
kernel: &'static Kernel,
) -> Result<Self, ConnectError<I2cService>>
pub async fn from_registry_no_retry( kernel: &'static Kernel, ) -> Result<Self, ConnectError<I2cService>>
Obtain an I2cClient
Does NOT attempt to get an I2cService
handle more than once.
Prefer I2cClient::from_registry
unless you will not be spawning one
around the same time as obtaining a client.
sourcepub fn with_cached_buf(self, buf: impl Into<Option<FixedVec<u8>>>) -> Self
pub fn with_cached_buf(self, buf: impl Into<Option<FixedVec<u8>>>) -> Self
Sets a cached buffer to use for embedded_hal_async::i2c::I2c
transactions.
If a cached buffer is present, and it is small enough to perform a read/write operation in a transaction, it will be used, rather than allocating a new buffer for that transaction.
sourcepub async fn start_transaction(&mut self, addr: Addr) -> Transaction
pub async fn start_transaction(&mut self, addr: Addr) -> Transaction
Starts an I²C Transaction
with the device at the provided
addr
.
This method begins a bus transaction with the target device. While the
returned Transaction
type is held, other I2cClients
cannot perform
bus operations; the bus is released when the Transaction
is dropped.
After starting a Transaction
, the Transaction::read
and
Transaction::write
methods are used to write to and read from the
target I²C device. See the Transaction
type’s
documentation for more information on how to use it.