Skip to content

Commit a3b0125

Browse files
committed
docs: fix example in readme and clean things up a bit
Signed-off-by: Paul Osborne <[email protected]>
1 parent c7bbaf3 commit a3b0125

File tree

4 files changed

+47
-56
lines changed

4 files changed

+47
-56
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ https://www.kernel.org/doc/Documentation/i2c/dev-interface
1818
libc = "^0.2.7"
1919
bitflags = "^0.5.0"
2020
byteorder = "^0.4.2"
21-
nix = "^0.5.0"
21+
nix = "^0.6.0"
2222

2323
[dev-dependencies]
2424
docopt = "^0.6.78"
25-
skeptic = "0.4"
25+
skeptic = "^0.5"
2626

2727
[build-dependencies]
28-
skeptic = "0.4"
28+
skeptic = "^0.5"

README.md

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,56 +20,44 @@ The source includes an example of using the library to talk to a Wii
2020
Nunchuck (which has an i2c interface).
2121
[Go View the Example](https://github.com/rust-embedded/rust-i2cdev/blob/master/examples/nunchuck.rs).
2222

23-
Here's a real quick example showing the guts of how you create a
23+
Here's a real quick example showing the guts of how you create
2424
device and start talking to it... This device only requires basic
2525
functions (read/write) which are done via the Read/Write traits (if
2626
you actually want to use the Wii Nunchuck you should use
2727
[`i2cdev::sensors::nunchuck::Nunchuck`][nunchuck]:
2828

29-
```rust
29+
```rust,no_run
3030
extern crate i2cdev;
31-
use i2cdev::*;
31+
3232
use std::thread;
33+
use std::time::Duration;
34+
35+
use i2cdev::core::*;
36+
use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError};
3337
3438
const NUNCHUCK_SLAVE_ADDR: u16 = 0x52;
3539
3640
// real code should probably not use unwrap()
37-
fn i2cfun() -> Result<(), I2CError> {
38-
let mut dev = try!(I2CDevice::new("/dev/i2c-1", NUNCHUCK_SLAVE_ADDR));
41+
fn i2cfun() -> Result<(), LinuxI2CError> {
42+
let mut dev = try!(LinuxI2CDevice::new("/dev/i2c-1", NUNCHUCK_SLAVE_ADDR));
3943
4044
// init sequence
41-
try!(self.i2cdev.smbus_write_byte_data(0xF0, 0x55));
42-
try!(self.i2cdev.smbus_write_byte_data(0xFB, 0x00));
43-
thread::sleep_ms(100);
45+
try!(dev.smbus_write_byte_data(0xF0, 0x55));
46+
try!(dev.smbus_write_byte_data(0xFB, 0x00));
47+
thread::sleep(Duration::from_millis(100));
4448
4549
loop {
46-
let mut buf: [u8: 6] = [0: 6];
47-
self.i2cdev.smbus_write_byte(0x00).unwrap();
48-
thread::sleep_ms(10);
49-
self.i2cdev.read(&mut buf).unwrap();
50+
let mut buf: [u8; 6] = [0; 6];
51+
dev.smbus_write_byte(0x00).unwrap();
52+
thread::sleep(Duration::from_millis(10));
53+
dev.read(&mut buf).unwrap();
5054
println!("Reading: {:?}", buf);
5155
}
5256
}
5357
```
5458

5559
In addition to the Read/Write traits, the following methods are
56-
available via the I2CSMBus trait:
57-
58-
```rust
59-
pub trait I2CSMBus {
60-
fn smbus_write_quick(&self, bit: bool) -> Result<(), Error>;
61-
fn smbus_read_byte(&self) -> Result<u8, Error>;
62-
fn smbus_write_byte(&self, value: u8) -> Result<(), Error>;
63-
fn smbus_read_byte_data(&self, register: u8) -> Result<u8, Error>;
64-
fn smbus_write_byte_data(&self, register: u8, value: u8) -> Result<(), Error>;
65-
fn smbus_read_word_data(&self, register: u8) -> Result<u16, Error>;
66-
fn smbus_write_word_data(&self, register: u8, value: u16) -> Result<(), Error>;
67-
fn smbus_process_word(&self, register: u8, value: u16) -> Result<u16, Error>;
68-
fn smbus_read_block_data(&self, register: u8) -> Result<Vec<u8>, Error>;
69-
fn smbus_write_block_data(&self, register: u8, values: &[u8]) -> Result<(), Error>;
70-
fn smbus_process_block(&self, register: u8, values: &[u8]) -> Result<(), Error>;
71-
}
72-
```
60+
available via the [I2CDevice trait](https://rust-embedded.github.io/rust-i2cdev/i2cdev/core/trait.I2CDevice.html).
7361

7462
[nunchuck]: http://rust-embedded.github.io/rust-i2cdev/i2cdev/sensors/nunchuck/struct.Nunchuck.html
7563

@@ -82,7 +70,7 @@ The following features are implemented and planned for the library:
8270
- [x] Implement the Write trait
8371
- [x] Implement SMBus Methods
8472
- [x] Add Tests/Example for SMBus Methods
85-
- [/] Add sensor library for handy sensors (and examples)
73+
- [x] Add sensor library for handy sensors (and examples)
8674
- [ ] Add higher-level APIs/Macros for simplifying access to devices
8775
with large register sets
8876
- [ ] Add Support for Non-SMBus ioctl methods
@@ -94,37 +82,17 @@ Cross Compiling
9482

9583
Most likely, the machine you are running on is not your development
9684
machine (although it could be). In those cases, you will need to
97-
cross-compile. The following basic instructions should work for the
98-
raspberry pi or beaglebone black:
99-
100-
1. Install rust and cargo
101-
2. Install an appropriate cross compiler. On an Ubuntu system, this
102-
can be done by doing `sudo apt-get install g++-arm-linux-gnueabihf`.
103-
3. Build or install rust for your target. This is necessary in order
104-
to have libstd available for your target. For arm-linux-gnueabihf,
105-
you can find binaries at https://github.com/japaric/ruststrap.
106-
With this approach or building it yourself, you will need to copy
107-
the ${rust}/lib/rustlib/arm-unknown-linux-gnueabihf to your system
108-
rust library folder (it is namespaced by triple, so it shouldn't
109-
break anything).
110-
4. Tell cargo how to link by adding the lines below to your
111-
~/.cargo/config file.
112-
5. Run your build `cargo build --target=arm-unknown-linux-gnueabi`.
113-
114-
The following snippet added to my ~/.cargo/config worked for me:
115-
116-
```
117-
[target.arm-unknown-linux-gnueabihf]
118-
linker = "arm-linux-gnueabihf-gcc"
119-
```
85+
cross-compile. See https://github.com/japaric/rust-cross for pointers.
12086

12187
License
12288
-------
12389

90+
```
12491
Copyright (c) 2015, Paul Osborne <[email protected]>
12592
12693
Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
12794
http://www.apache.org/license/LICENSE-2.0> or the MIT license
12895
<LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
12996
option. This file may not be copied, modified, or distributed
13097
except according to those terms.
98+
```

build.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2016, Paul Osborne <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
extern crate skeptic;
10+
11+
fn main() {
12+
// generate tests for README.md using skeptic
13+
skeptic::generate_doc_tests(&["README.md"]);
14+
}

tests/skeptic.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2016, Paul Osborne <[email protected]>
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
include!(concat!(env!("OUT_DIR"), "/skeptic-tests.rs"));

0 commit comments

Comments
 (0)