1+ //! # A driver for the Raspberry Pi Sense HAT
2+ //!
3+ //! The [Sense HAT](https://www.raspberrypi.org/products/sense-hat/) is a
4+ //! sensor board for the Raspberry Pi. It features an LED matrix, a humidity
5+ //! and temperature sensor, a pressure and temperature sensor and a gyroscope.
6+ //!
7+ //! Supported components:
8+ //!
9+ //! * Humidity and Temperature Sensor (an HTS221)
10+ //! * Pressure and Temperature Sensor (a LPS25H)
11+ //! * Gyroscope (an LSM9DS1, requires the RTIMU library)
12+ //!
13+ //! Currently unsupported components:
14+ //!
15+ //! * LED matrix
16+ //! * Joystick
17+
118extern crate byteorder;
219extern crate i2cdev;
320extern crate measurements;
421
522#[ cfg( feature = "rtimu" ) ]
623extern crate libc;
724
25+ mod rh;
26+ mod hts221;
27+ mod lps25h;
28+
829pub use measurements:: Temperature ;
930pub use measurements:: Pressure ;
31+ pub use measurements:: Angle ;
32+ pub use rh:: RelativeHumidity ;
1033
1134use i2cdev:: linux:: { LinuxI2CDevice , LinuxI2CError } ;
1235
13- use std:: fmt;
14-
15- mod hts221;
16- mod lps25h;
17-
1836#[ cfg( feature = "rtimu" ) ]
1937mod lsm9ds1;
2038
@@ -23,18 +41,6 @@ mod lsm9ds1_dummy;
2341#[ cfg( not( feature = "rtimu" ) ) ]
2442use lsm9ds1_dummy as lsm9ds1;
2543
26- /// An angle between two lines
27- #[ derive( Debug , Copy , Clone ) ]
28- pub struct Angle {
29- value : f64 ,
30- }
31-
32- /// Represents a relative humidity reading from the humidity sensor
33- #[ derive( Debug , Copy , Clone ) ]
34- pub struct RelativeHumidity {
35- value : f64 ,
36- }
37-
3844/// Represents an orientation from the IMU
3945#[ derive( Debug , Copy , Clone ) ]
4046pub struct Orientation {
@@ -43,7 +49,7 @@ pub struct Orientation {
4349 pub yaw : Angle ,
4450}
4551
46- /// Represents the Sense Hat itself
52+ /// Represents the Sense HAT itself
4753pub struct SenseHat < ' a > {
4854 /// LPS25H pressure sensor
4955 pressure_chip : lps25h:: Lps25h < LinuxI2CDevice > ,
@@ -71,7 +77,7 @@ impl<'a> SenseHat<'a> {
7177 /// Try and create a new SenseHat object.
7278 ///
7379 /// Will open the relevant I2C devices and then attempt to initialise the
74- /// chips on the Sense Hat .
80+ /// chips on the Sense HAT .
7581 pub fn new ( ) -> SenseHatResult < SenseHat < ' a > > {
7682 Ok ( SenseHat {
7783 humidity_chip : hts221:: Hts221 :: new ( LinuxI2CDevice :: new ( "/dev/i2c-1" , 0x5f ) ?) ?,
@@ -90,7 +96,8 @@ impl<'a> SenseHat<'a> {
9096 pub fn get_temperature_from_pressure ( & mut self ) -> SenseHatResult < Temperature > {
9197 let status = self . pressure_chip . status ( ) ?;
9298 if ( status & 1 ) != 0 {
93- Ok ( Temperature :: from_celsius ( self . pressure_chip . get_temp_celcius ( ) ?) )
99+ Ok ( Temperature :: from_celsius ( self . pressure_chip
100+ . get_temp_celcius ( ) ?) )
94101 } else {
95102 Err ( SenseHatError :: NotReady )
96103 }
@@ -100,7 +107,8 @@ impl<'a> SenseHat<'a> {
100107 pub fn get_pressure ( & mut self ) -> SenseHatResult < Pressure > {
101108 let status = self . pressure_chip . status ( ) ?;
102109 if ( status & 2 ) != 0 {
103- Ok ( Pressure :: from_hectopascals ( self . pressure_chip . get_pressure_hpa ( ) ?) )
110+ Ok ( Pressure :: from_hectopascals ( self . pressure_chip
111+ . get_pressure_hpa ( ) ?) )
104112 } else {
105113 Err ( SenseHatError :: NotReady )
106114 }
@@ -177,14 +185,6 @@ impl<'a> SenseHat<'a> {
177185 }
178186}
179187
180- fn radians_to_degrees ( radians : f64 ) -> f64 {
181- 360.0 * ( radians / ( :: std:: f64:: consts:: PI * 2.0 ) )
182- }
183-
184- fn degrees_to_radians ( degrees : f64 ) -> f64 {
185- ( degrees * ( :: std:: f64:: consts:: PI * 2.0 ) ) / 360.0
186- }
187-
188188impl From < LinuxI2CError > for SenseHatError {
189189 fn from ( err : LinuxI2CError ) -> SenseHatError {
190190 SenseHatError :: I2CError ( err)
@@ -197,56 +197,4 @@ impl From<lsm9ds1::Error> for SenseHatError {
197197 }
198198}
199199
200- impl Angle {
201- pub fn from_radians ( rad : f64 ) -> Angle {
202- Angle {
203- value : radians_to_degrees ( rad) ,
204- }
205- }
206-
207- pub fn as_radians ( & self ) -> f64 {
208- degrees_to_radians ( self . value )
209- }
210-
211- pub fn from_degrees ( deg : f64 ) -> Angle {
212- Angle { value : deg }
213- }
214-
215- pub fn as_degrees ( & self ) -> f64 {
216- self . value
217- }
218- }
219-
220- impl RelativeHumidity {
221- pub fn from_percent ( pc : f64 ) -> RelativeHumidity {
222- RelativeHumidity { value : pc }
223- }
224-
225- pub fn as_percent ( & self ) -> f64 {
226- self . value
227- }
228- }
229-
230- impl fmt:: Display for RelativeHumidity {
231- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
232- write ! ( f, "{:.1}%" , self . as_percent( ) )
233- }
234- }
235-
236- impl fmt:: Display for Angle {
237- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
238- write ! ( f, "{:.1}°" , self . as_degrees( ) )
239- }
240- }
241-
242- #[ cfg( test) ]
243- mod tests {
244- use super :: * ;
245-
246- #[ test]
247- pub fn pressure_test ( ) {
248- let p = Pressure :: from_hectopascals ( 1000.0 ) ;
249- assert ! ( ( p. as_bars( ) - 1.0 ) . abs( ) < 1e-6 ) ;
250- assert ! ( ( p. as_psi( ) - 14.50376807894691 ) . abs( ) < 1e-6 ) ;
251- }
252- }
200+ // End of file
0 commit comments