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