i2c_i2cdev/
lib.rs

1#![deny(missing_docs)]
2#![doc(html_root_url = "http://arcnmx.github.io/i2c-rs/")]
3
4//! Implements the [`i2c::*`](https://crates.io/crates/i2c) traits for the
5//! [`i2cdev` crate](https://crates.io/crates/i2cdev).
6
7extern crate i2c;
8extern crate i2cdev;
9
10use std::{cmp, io};
11use i2cdev::core::I2CDevice;
12use i2cdev::linux::LinuxI2CDevice;
13
14/// A wrapper around an `I2CDevice` type that impls the i2c traits.
15///
16/// This is required due to Rust's orphan rules.
17pub struct I2cDev<I>(pub I);
18
19impl<I: I2CDevice> I2cDev<I> {
20    fn map_buffer(data: Vec<u8>, value: &mut [u8]) -> usize {
21        let len = cmp::min(value.len(), data.len());
22        value[..len].copy_from_slice(&data[..len]);
23        data.len()
24    }
25}
26
27impl<I: I2CDevice> i2c::Master for I2cDev<I> {
28    type Error = I::Error;
29}
30
31impl i2c::Address for I2cDev<LinuxI2CDevice> {
32    fn set_slave_address(&mut self, addr: u16, tenbit: bool) -> Result<(), Self::Error> {
33        if tenbit {
34            Err(io::Error::new(io::ErrorKind::Other, "10bit address not implemented in i2cdev").into())
35        } else {
36            self.0.set_slave_address(addr)
37        }
38    }
39}
40
41impl<I: I2CDevice> i2c::ReadWrite for I2cDev<I> {
42    fn i2c_read(&mut self, value: &mut [u8]) -> Result<usize, Self::Error> {
43        self.0.read(value)
44            .map(|_| value.len())
45    }
46
47    fn i2c_write(&mut self, value: &[u8]) -> Result<(), Self::Error> {
48        self.0.write(value)
49    }
50}
51
52impl<I: I2CDevice> i2c::Smbus for I2cDev<I> {
53    fn smbus_write_quick(&mut self, value: bool) -> Result<(), Self::Error> {
54        self.0.smbus_write_quick(value)
55    }
56
57    fn smbus_read_byte(&mut self) -> Result<u8, Self::Error> {
58        self.0.smbus_read_byte()
59    }
60
61    fn smbus_write_byte(&mut self, value: u8) -> Result<(), Self::Error> {
62        self.0.smbus_write_byte(value)
63    }
64
65    fn smbus_read_byte_data(&mut self, command: u8) -> Result<u8, Self::Error> {
66        self.0.smbus_read_byte_data(command)
67    }
68
69    fn smbus_write_byte_data(&mut self, command: u8, value: u8) -> Result<(), Self::Error> {
70        self.0.smbus_write_byte_data(command, value)
71    }
72
73    fn smbus_read_word_data(&mut self, command: u8) -> Result<u16, Self::Error> {
74        self.0.smbus_read_word_data(command)
75    }
76
77    fn smbus_write_word_data(&mut self, command: u8, value: u16) -> Result<(), Self::Error> {
78        self.0.smbus_write_word_data(command, value)
79    }
80
81    fn smbus_process_call(&mut self, command: u8, value: u16) -> Result<u16, Self::Error> {
82        self.0.smbus_process_word(command, value)
83    }
84
85    fn smbus_read_block_data(&mut self, command: u8, value: &mut [u8]) -> Result<usize, Self::Error> {
86        self.0.smbus_read_block_data(command)
87            .map(|data| Self::map_buffer(data, value))
88    }
89
90    fn smbus_write_block_data(&mut self, command: u8, value: &[u8]) -> Result<(), Self::Error> {
91        // TODO: error rather than let i2cdev silently truncate the data?
92        self.0.smbus_write_block_data(command, value)
93    }
94}
95
96impl<I: I2CDevice> i2c::BlockTransfer for I2cDev<I> {
97    fn i2c_read_block_data(&mut self, command: u8, value: &mut [u8]) -> Result<usize, Self::Error> {
98        self.0.smbus_read_i2c_block_data(command, value.len() as _)
99            .map(|data| Self::map_buffer(data, value))
100    }
101
102    fn i2c_write_block_data(&mut self, command: u8, value: &[u8]) -> Result<(), Self::Error> {
103        // TODO: error rather than let i2cdev silently truncate the data?
104
105        self.0.smbus_write_i2c_block_data(command, value)
106    }
107}
108
109impl<I: I2CDevice> i2c::Smbus20 for I2cDev<I> {
110    fn smbus_process_call_block(&mut self, command: u8, write: &[u8], read: &mut [u8]) -> Result<usize, Self::Error> {
111        // TODO: error rather than let i2cdev silently truncate the data?
112
113        self.0.smbus_process_block(command, write)
114            .map(|data| Self::map_buffer(data, read))
115    }
116}