@@ -37,8 +37,7 @@ pub trait Thermometer {
37
37
38
38
/// Get a temperature from the sensor in degrees celsius
39
39
///
40
- /// Returns Some(temperature) if available, otherwise returns
41
- /// None
40
+ /// Returns `Ok(temperature)` if available, otherwise returns `Err(Self::Error)`
42
41
fn temperature_celsius ( & mut self ) -> Result < f32 , Self :: Error > ;
43
42
}
44
43
@@ -48,7 +47,31 @@ pub trait Barometer {
48
47
49
48
/// Get a pressure reading from the sensor in kPa
50
49
///
51
- /// Returns Some(temperature) if avialable, otherwise returns
52
- /// None
50
+ /// Returns `Ok(temperature)` if avialable, otherwise returns `Err(Self::Error)`
53
51
fn pressure_kpa ( & mut self ) -> Result < f32 , Self :: Error > ;
54
52
}
53
+
54
+ /// Trait for sensors that provide access to altitude readings
55
+ pub trait Altimeter {
56
+ type Error : Error ;
57
+
58
+ /// Get an altitude reading from the sensor in meters, relative to the pressure in kPa at
59
+ /// sea level
60
+ ///
61
+ /// Returns `Ok(altitude)` if available, otherwise returns `Err(Self::Error)`
62
+ fn altitude_meters ( & mut self , sea_level_kpa : f32 ) -> Result < f32 , Self :: Error > ;
63
+ }
64
+
65
+ impl < T > Altimeter for T
66
+ where T : Barometer
67
+ {
68
+ type Error = <Self as Barometer >:: Error ;
69
+
70
+ fn altitude_meters ( & mut self , sea_level_kpa : f32 ) -> Result < f32 , Self :: Error > {
71
+ let pressure = try!( self . pressure_kpa ( ) ) * 1000. ;
72
+ let sea_level_pa = sea_level_kpa * 1000. ;
73
+
74
+ let altitude = 44330. * ( 1. - ( pressure / sea_level_pa) . powf ( 0.1903 ) ) ;
75
+ Ok ( altitude)
76
+ }
77
+ }
0 commit comments