Skip to content

Commit ba03c1a

Browse files
committed
Add example of I2C trait default implementations
1 parent 5b97a32 commit ba03c1a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/blocking/i2c.rs

+31
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,37 @@ pub trait TransactionalIter<A: AddressMode = SevenBitAddress> {
298298

299299
/// Default implementation of `blocking::i2c::Write`, `blocking::i2c::Read` and
300300
/// `blocking::i2c::WriteRead` traits for `blocking::i2c::Transactional` implementers.
301+
///
302+
/// If you implement `blocking::i2c::Transactional` for your I2C peripheral,
303+
/// you can use this default implementation so that you do not need to implement
304+
/// the `blocking::i2c::Write`, `blocking::i2c::Read` and `blocking::i2c::WriteRead`
305+
/// traits as well.
306+
/// ```
307+
/// use embedded_hal::blocking::i2c;
308+
///
309+
/// struct I2c1;
310+
///
311+
/// impl i2c::Transactional<i2c::SevenBitAddress> for I2c1 {
312+
/// # type Error = ();
313+
/// fn try_exec<'a>(
314+
/// &mut self,
315+
/// address: i2c::SevenBitAddress,
316+
/// operations: &mut [i2c::Operation<'a>],
317+
/// ) -> Result<(), Self::Error> {
318+
/// // ...
319+
/// # Ok(())
320+
/// }
321+
/// }
322+
///
323+
/// // This is all you need to do:
324+
/// impl i2c::transactional::Default<i2c::SevenBitAddress> for I2c1 {};
325+
///
326+
/// // Then you can use `Write` and so on:
327+
/// use i2c::Write;
328+
///
329+
/// let mut i2c1 = I2c1{};
330+
/// i2c1.try_write(0x01, &[0xAB, 0xCD]).unwrap();
331+
/// ```
301332
pub mod transactional {
302333
use super::{AddressMode, Operation, Read, Transactional, Write, WriteRead};
303334

0 commit comments

Comments
 (0)