Skip to content

Commit

Permalink
Very simple WriteIterRead and WriteIter impls in I2C
Browse files Browse the repository at this point in the history
  • Loading branch information
rubdos committed Dec 8, 2020
1 parent c72a6f5 commit 4cb687b
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::gpio::gpioa::{PA10, PA7, PA9};
use crate::gpio::gpiob::{PB10, PB11, PB13, PB14, PB4, PB6, PB7, PB8, PB9};
use crate::gpio::gpioc::{PC0, PC1};
use crate::gpio::{Alternate, OpenDrain, Output, AF4};
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
use crate::hal::blocking::i2c::{Read, Write, WriteIter, WriteIterRead, WriteRead};
use crate::rcc::Rcc;
use crate::time::Hertz;

Expand Down Expand Up @@ -223,6 +223,23 @@ macro_rules! hal {
}
}

impl<PINS> WriteIter for I2c<$I2CX, PINS> {
type Error = Error;

fn write<B: IntoIterator<Item=u8>>(&mut self, addr: u8, bytes: B) -> Result<(), Error> {
let bytes = bytes.into_iter();

let mut bytes_allocated = [0u8; 255];
let mut len = 0;
for byte in bytes.into_iter() {
assert!(len < 256);
bytes_allocated[len] = byte;
len += 1;
}
Write::write(self, addr, &bytes_allocated[..len])
}
}

impl<PINS> Read for I2c<$I2CX, PINS> {
type Error = Error;

Expand Down Expand Up @@ -321,6 +338,28 @@ macro_rules! hal {
Ok(())
}
}

impl<PINS> WriteIterRead for I2c<$I2CX, PINS> {
type Error = Error;

fn write_iter_read<B: IntoIterator<Item=u8>>(
&mut self,
addr: u8,
bytes: B,
buffer: &mut [u8]
) -> Result<(), Error> {
let bytes = bytes.into_iter();

let mut bytes_allocated = [0u8; 255];
let mut len = 0;
for byte in bytes.into_iter() {
assert!(len < 256);
bytes_allocated[len] = byte;
len += 1;
}
WriteRead::write_read(self, addr, &bytes_allocated, buffer)
}
}
)+
}
}
Expand Down

0 comments on commit 4cb687b

Please sign in to comment.