File tree Expand file tree Collapse file tree 5 files changed +57
-9
lines changed Expand file tree Collapse file tree 5 files changed +57
-9
lines changed Original file line number Diff line number Diff line change 7
7
// except according to those terms.
8
8
9
9
use byteorder:: { ByteOrder , LittleEndian } ;
10
+ use std:: error:: Error ;
10
11
11
12
/// Interface to an I2C Slave Device from an I2C Master
12
13
///
13
14
/// Typical implementations will store state with references to the bus
14
15
/// in use and the address of the slave device. The trait is based on the
15
16
/// Linux i2cdev interface.
16
17
pub trait I2CDevice {
17
- type Error ;
18
+ type Error : Error ;
18
19
19
20
/// Read data from the device to fill the provided slice
20
21
fn read ( & mut self , data : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > ;
Original file line number Diff line number Diff line change 8
8
9
9
use ffi;
10
10
use core:: I2CDevice ;
11
+ use std:: error:: Error ;
11
12
use std:: path:: Path ;
12
13
use std:: fs:: File ;
13
14
use std:: fmt;
@@ -22,6 +23,7 @@ pub struct LinuxI2CDevice {
22
23
slave_address : u16 ,
23
24
}
24
25
26
+ #[ derive( Debug ) ]
25
27
pub enum LinuxI2CError {
26
28
Nix ( nix:: Error ) ,
27
29
Io ( io:: Error ) ,
@@ -55,11 +57,27 @@ impl From<LinuxI2CError> for io::Error {
55
57
}
56
58
}
57
59
58
- impl fmt:: Debug for LinuxI2CError {
60
+ impl fmt:: Display for LinuxI2CError {
59
61
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
60
62
match * self {
61
- LinuxI2CError :: Nix ( ref e) => fmt:: Debug :: fmt ( e, f) ,
62
- LinuxI2CError :: Io ( ref e) => fmt:: Debug :: fmt ( e, f) ,
63
+ LinuxI2CError :: Nix ( ref e) => fmt:: Display :: fmt ( e, f) ,
64
+ LinuxI2CError :: Io ( ref e) => fmt:: Display :: fmt ( e, f) ,
65
+ }
66
+ }
67
+ }
68
+
69
+ impl Error for LinuxI2CError {
70
+ fn description ( & self ) -> & str {
71
+ match * self {
72
+ LinuxI2CError :: Io ( ref e) => e. description ( ) ,
73
+ LinuxI2CError :: Nix ( ref e) => e. description ( ) ,
74
+ }
75
+ }
76
+
77
+ fn cause ( & self ) -> Option < & Error > {
78
+ match * self {
79
+ LinuxI2CError :: Io ( ref e) => Some ( e) ,
80
+ LinuxI2CError :: Nix ( ref e) => Some ( e) ,
63
81
}
64
82
}
65
83
}
Original file line number Diff line number Diff line change 6
6
// option. This file may not be copied, modified, or distributed
7
7
// except according to those terms.
8
8
9
+ use std:: error:: Error ;
10
+
9
11
pub mod adxl345_accelerometer;
10
12
pub mod mpl115a2_barometer;
11
13
pub mod nunchuck;
@@ -23,15 +25,15 @@ pub struct AccelerometerSample {
23
25
24
26
/// Trait for sensors that provide access to accelerometer readings (3-axis)
25
27
pub trait Accelerometer {
26
- type Error ;
28
+ type Error : Error ;
27
29
28
30
/// Grab an accelerometer sample from the device
29
31
fn accelerometer_sample ( & mut self ) -> Result < AccelerometerSample , Self :: Error > ;
30
32
}
31
33
32
34
/// Trait for sensors that provide access to temperature readings
33
35
pub trait Thermometer {
34
- type Error ;
36
+ type Error : Error ;
35
37
36
38
/// Get na temperature from the sensor in degrees celsisus
37
39
///
@@ -42,7 +44,7 @@ pub trait Thermometer {
42
44
43
45
/// Trait for sensors that provide access to pressure readings
44
46
pub trait Barometer {
45
- type Error ;
47
+ type Error : Error ;
46
48
47
49
/// Get a pressure reading from the sensor in kPa
48
50
///
Original file line number Diff line number Diff line change @@ -81,7 +81,7 @@ impl MPL115A2Coefficients {
81
81
/// This should be built from a read of registers 0x04-0x0B in
82
82
/// order. This gets the raw, unconverted value of each
83
83
/// coefficient.
84
- pub fn new < E > ( i2cdev : & mut I2CDevice < Error = E > ) -> Result < MPL115A2Coefficients , E > {
84
+ pub fn new < E : Error > ( i2cdev : & mut I2CDevice < Error = E > ) -> Result < MPL115A2Coefficients , E > {
85
85
let mut buf: [ u8 ; 8 ] = [ 0 ; 8 ] ;
86
86
try!( i2cdev. write ( & [ REGISTER_ADDR_A0 ] ) ) ;
87
87
try!( i2cdev. read ( & mut buf) ) ;
@@ -97,7 +97,7 @@ impl MPL115A2Coefficients {
97
97
98
98
impl MPL115A2RawReading {
99
99
/// Create a new reading from the provided I2C Device
100
- pub fn new < E > ( i2cdev : & mut I2CDevice < Error = E > ) -> Result < MPL115A2RawReading , E > {
100
+ pub fn new < E : Error > ( i2cdev : & mut I2CDevice < Error = E > ) -> Result < MPL115A2RawReading , E > {
101
101
// tell the chip to do an ADC read so we can get updated values
102
102
try!( i2cdev. smbus_write_byte_data ( REGISTER_ADDR_START_CONVERSION , 0x00 ) ) ;
103
103
Original file line number Diff line number Diff line change 9
9
// Reads data from Wii Nunchuck
10
10
11
11
use std:: io:: prelude:: * ;
12
+ use std:: error:: Error ;
12
13
use std:: thread;
14
+ use std:: fmt;
13
15
14
16
use core:: I2CDevice ;
15
17
@@ -21,6 +23,31 @@ pub enum NunchuckError<E> {
21
23
ParseError ,
22
24
}
23
25
26
+ impl < E : Error > fmt:: Display for NunchuckError < E > {
27
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
28
+ match * self {
29
+ NunchuckError :: Error ( ref e) => fmt:: Display :: fmt ( e, f) ,
30
+ NunchuckError :: ParseError => fmt:: Display :: fmt ( self . description ( ) , f) ,
31
+ }
32
+ }
33
+ }
34
+
35
+ impl < E : Error > Error for NunchuckError < E > {
36
+ fn description ( & self ) -> & str {
37
+ match * self {
38
+ NunchuckError :: Error ( ref e) => e. description ( ) ,
39
+ NunchuckError :: ParseError => "Unable to Parse Data" ,
40
+ }
41
+ }
42
+
43
+ fn cause ( & self ) -> Option < & Error > {
44
+ match * self {
45
+ NunchuckError :: Error ( ref e) => Some ( e) ,
46
+ NunchuckError :: ParseError => None ,
47
+ }
48
+ }
49
+ }
50
+
24
51
// TODO: Move Nunchuck code out to be an actual sensor and add tests
25
52
26
53
#[ derive( Debug ) ]
You can’t perform that action at this time.
0 commit comments