Skip to content

Commit 7659aa4

Browse files
authored
Merge pull request #19 from Ragnaroek/master
added smbus_read_i2c_block_data
2 parents a3b0125 + 28ef678 commit 7659aa4

File tree

11 files changed

+39
-16
lines changed

11 files changed

+39
-16
lines changed

.travis.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
language: rust
22
sudo: false
3-
rust:
4-
- 1.3.0
5-
- 1.4.0
6-
- 1.5.0
7-
- 1.6.0
8-
- 1.7.0
9-
- stable
10-
- beta
11-
- nightly
123
env:
134
global:
145
- secure: gZdvTl4i1QfHQaCUPcVpnLtlrsE4WldSm99AD9C9jiMgjlzhhI4kQmIhssNFhA1BWDW6qDazrKnw4ZT1GA/rlmGSpLxs2lfBNTjPF0CJMVSTEQ1OaFcDGajziTTCSMh7jDml1/CFHuGSc+rS4bNv5wu0bj+pz3ZSRSuB7Vgoa0aRD8JQKc+phnIoXMCCfIh9vxlXWvO9mDMoR84/B35/gN/vWIi3+uuHgvvdKMK8qBbJ0ne25kOIA5CpXzJmOBFHCGblVQM4+AiTWvWtoXJjMcJXn9HZ2g3HWhrnixG07Ee1RrjUPo25tsxrBowHSGq9Bbo+jzmOmx03CRnCBHyXk6e12MXrfVFWRdmFaf7Sl/RBlz8Y2BRgktp8ihB6JQmjNuRptE4RdnWn6CU9liSQ5nzzGEl2ZtBrzN9GR3qyhq5W9e09v93MZfBr0svp/rtBoqDANfkpxvmWqtgO3fjN1fdf092sEcQfq3d7e6NaLSWKkI6V9wJnJ9UulPDAaqGVm8RrA9b0vJR6ouCf5ChZgYirz6LW5uPpMkf+cjESU9JxnX9xVB8tfPSoNrPKolRC/WRZ8gPbRmx0vF4wrDk8z9Fks5nHGwDsPdYe60Qe8m6ZclncCkpUzy2yAZWe/kiDaseOwNbSqEUZW80hLyHpuGi1IFpGBQfc8mYMPvqOHa8=

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
name = "i2cdev"
44
build = "build.rs"
5-
version = "0.3.0"
5+
version = "0.3.1"
66
authors = ["Paul Osborne <[email protected]>"]
77
license = "MIT/Apache-2.0"
88
repository = "https://github.com/rust-embedded/rust-i2cdev"

examples/nunchuck.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
extern crate i2cdev;
1212
extern crate docopt;
1313

14-
use i2cdev::core::*;
1514
use i2cdev::linux::*;
1615
use i2cdev::sensors::nunchuck::*;
1716
use std::env::args;

examples/sensors.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern crate i2cdev;
1515
extern crate docopt;
1616

1717
use std::thread;
18+
use std::time::Duration;
1819
use std::env::args;
1920
use docopt::Docopt;
2021
use i2cdev::sensors::{Thermometer, Barometer, Accelerometer};
@@ -56,6 +57,6 @@ fn main() {
5657
println!("Accel: {:?}", accel);
5758
println!("Accel Tot: {:?}",
5859
(accel.x.powi(2) + accel.y.powi(2) + accel.z.powi(2)).sqrt());
59-
thread::sleep_ms(1000);
60+
thread::sleep(Duration::from_millis(1000));
6061
}
6162
}

src/core.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ pub trait I2CDevice {
9090
/// count bytes read from the device.
9191
fn smbus_read_block_data(&mut self, register: u8) -> Result<Vec<u8>, Self::Error>;
9292

93+
/// Read a block of up to 32 bytes from a device
94+
///
95+
/// Uses read_i2c_block_data instead read_block_data.
96+
fn smbus_read_i2c_block_data(&mut self, register: u8, len: u8) -> Result<Vec<u8>, Self::Error>;
97+
9398
/// Write a block of up to 32 bytes to a device
9499
///
95100
/// The opposite of the Block Read command, this writes up to 32 bytes to

src/ffi.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,23 @@ pub fn i2c_smbus_read_block_data(fd: RawFd, register: u8) -> Result<Vec<u8>, I2C
338338
Ok((&data.block[1..(count + 1) as usize]).to_vec())
339339
}
340340

341+
pub fn i2c_smbus_read_i2c_block_data(fd: RawFd, register: u8, len: u8) -> Result<Vec<u8>, I2CError> {
342+
let mut data = i2c_smbus_data::empty();
343+
data.block[0] = len;
344+
try!(unsafe {
345+
i2c_smbus_access(fd,
346+
I2CSMBusReadWrite::I2C_SMBUS_READ,
347+
register,
348+
I2CSMBusSize::I2C_SMBUS_I2C_BLOCK_DATA,
349+
&mut data)
350+
});
351+
352+
// create a vector from the data in the block starting at byte
353+
// 1 and ending after count bytes after that
354+
let count = data.block[0];
355+
Ok((&data.block[1..(count + 1) as usize]).to_vec())
356+
}
357+
341358
#[inline]
342359
pub fn i2c_smbus_write_block_data(fd: RawFd, register: u8, values: &[u8]) -> Result<(), I2CError> {
343360
let mut data = i2c_smbus_data::empty();

src/linux.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ impl I2CDevice for LinuxI2CDevice {
196196
ffi::i2c_smbus_read_block_data(self.as_raw_fd(), register).map_err(From::from)
197197
}
198198

199+
/// Read a block of up to 32 bytes from a device via i2c_smbus_i2c_read_block_data
200+
fn smbus_read_i2c_block_data(&mut self, register: u8, len: u8) -> Result<Vec<u8>, LinuxI2CError> {
201+
ffi::i2c_smbus_read_i2c_block_data(self.as_raw_fd(), register, len).map_err(From::from)
202+
}
203+
199204
/// Write a block of up to 32 bytes to a device
200205
///
201206
/// The opposite of the Block Read command, this writes up to 32 bytes to

src/mock.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,8 @@ impl I2CDevice for MockI2CDevice {
9191
fn smbus_process_block(&mut self, _register: u8, _values: &[u8]) -> I2CResult<()> {
9292
unimplemented!()
9393
}
94+
95+
fn smbus_read_i2c_block_data(&mut self, _register: u8, _len: u8) -> I2CResult<Vec<u8>> {
96+
unimplemented!()
97+
}
9498
}

src/sensors/adxl345_accelerometer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use sensors::{Accelerometer, AccelerometerSample};
1212
use core::I2CDevice;
13-
use byteorder::{ByteOrder, LittleEndian, ReadBytesExt};
13+
use byteorder::{ByteOrder, LittleEndian};
1414

1515
// TODO: read/write data format (for now, assumed 0x00)
1616

src/sensors/mpl115a2_barometer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use sensors::{Thermometer, Barometer};
1212
use std::thread;
13+
use std::time::Duration;
1314
use std::error::Error;
1415
use core::I2CDevice;
1516
use byteorder::{ByteOrder, BigEndian};
@@ -102,7 +103,7 @@ impl MPL115A2RawReading {
102103
try!(i2cdev.smbus_write_byte_data(REGISTER_ADDR_START_CONVERSION, 0x00));
103104

104105
// maximum conversion time is 3ms
105-
thread::sleep_ms(3);
106+
thread::sleep(Duration::from_millis(3));
106107

107108
// The SMBus functions read word values as little endian but that is not
108109
// what we want

0 commit comments

Comments
 (0)