3 releases

0.1.2 Apr 12, 2019
0.1.1 Apr 11, 2019
0.1.0 Apr 11, 2019

#1004 in Embedded development

MIT/Apache

8KB
64 lines

i2c-reg

Create registers for reading and writing with I2C interface.

Documentation

Features

Effortless creation of I2C read only/write only/read write registers with various sizes.

#[derive(Register, I2cReadRegister, I2cWriteRegister)]
#[address = 0b1110]
#[size = 2]
struct TemperatureRegister;

Typesafe conversion between raw bytes and actual values when reading from/writing to registers via interface.

type Raw = <TemperatureRegister as Register>::Raw;

struct Temperature(u16);

impl Into<Raw> for Temperature {
    fn into(self) -> Raw {
        self.0.to_be_bytes()
    }
}

impl From<Raw> for Temperature {
    fn from(raw: Raw) -> Self {
        Temperature(u16::from_be_bytes(raw))
    }
}

let mut interface = I2cInterface { i2c, address: 0b0110 };
interface.write_register(TemperatureRegister, Temperature(42)).unwrap();
let temperature: Temperature = interface.read_register(TemperatureRegister).unwrap();
assert_eq!(Temperature(42), temperature);

Examples

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~2MB
~47K SLoC