@@ -20,56 +20,44 @@ The source includes an example of using the library to talk to a Wii
20
20
Nunchuck (which has an i2c interface).
21
21
[ Go View the Example] ( https://github.com/rust-embedded/rust-i2cdev/blob/master/examples/nunchuck.rs ) .
22
22
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
24
24
device and start talking to it... This device only requires basic
25
25
functions (read/write) which are done via the Read/Write traits (if
26
26
you actually want to use the Wii Nunchuck you should use
27
27
[ ` i2cdev::sensors::nunchuck::Nunchuck ` ] [ nunchuck ] :
28
28
29
- ``` rust
29
+ ``` rust,no_run
30
30
extern crate i2cdev;
31
- use i2cdev :: * ;
31
+
32
32
use std::thread;
33
+ use std::time::Duration;
34
+
35
+ use i2cdev::core::*;
36
+ use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError};
33
37
34
38
const NUNCHUCK_SLAVE_ADDR: u16 = 0x52;
35
39
36
40
// 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));
39
43
40
44
// 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) );
44
48
45
49
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();
50
54
println!("Reading: {:?}", buf);
51
55
}
52
56
}
53
57
```
54
58
55
59
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 ) .
73
61
74
62
[ nunchuck ] : http://rust-embedded.github.io/rust-i2cdev/i2cdev/sensors/nunchuck/struct.Nunchuck.html
75
63
@@ -82,7 +70,7 @@ The following features are implemented and planned for the library:
82
70
- [x] Implement the Write trait
83
71
- [x] Implement SMBus Methods
84
72
- [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)
86
74
- [ ] Add higher-level APIs/Macros for simplifying access to devices
87
75
with large register sets
88
76
- [ ] Add Support for Non-SMBus ioctl methods
@@ -94,37 +82,17 @@ Cross Compiling
94
82
95
83
Most likely, the machine you are running on is not your development
96
84
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.
120
86
121
87
License
122
88
-------
123
89
90
+ ```
124
91
Copyright (c) 2015, Paul Osborne <[email protected] >
125
92
126
93
Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
127
94
http://www.apache.org/license/LICENSE-2.0> or the MIT license
128
95
<LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
129
96
option. This file may not be copied, modified, or distributed
130
97
except according to those terms.
98
+ ```
0 commit comments