Skip to content

Commit a2513ae

Browse files
committed
Make clippy happy and spell Sense HAT correctly.
Move Relative Humidity out to its own module.
1 parent cd8f60f commit a2513ae

File tree

9 files changed

+91
-114
lines changed

9 files changed

+91
-114
lines changed

Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
[package]
22
name = "sensehat"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
authors = ["Jonathan Pallant <[email protected]>"]
55
license-file = "LICENSE.md"
66
documentation = "https://docs.rs/crate/sensehat"
77
repository = "https://github.com/thejpster/sensehat-rs"
88
keywords = ["sensehat", "raspberry", "pi", "sensors"]
9-
description = "Interface with the Raspberry Pi Foundation's official Sense Hat sensor board."
9+
description = "Interface with the Raspberry Pi Foundation's official Sense HAT sensor board."
1010
readme = "README.md"
1111

1212
[dependencies]
13-
measurements = "0.7.0"
13+
measurements = "0.10.2"
1414
i2cdev = "0.3.1"
1515
byteorder = "1.0.0"
1616
libc = { version = "0.2.0", optional = true }
@@ -20,8 +20,7 @@ gcc = "0.3"
2020

2121
[features]
2222
# The default set of optional packages. Most people will want to use these
23-
# packages, but they are strictly optional. Note that `session` is not a package
24-
# but rather another feature listed in this manifest.
23+
# packages, but they are strictly optional.
2524
default = ["rtimu"]
2625
rtimu = [ "libc" ]
2726

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# sensehat-rs
2-
Rust support for the Raspberry Pi Sense Hat
32

4-
See https://www.raspberrypi.org/products/sense-hat/ for details on the Sense Hat.
3+
Rust support for the Raspberry Pi Sense HAT
54

6-
See https://github.com/RPi-Distro/python-sense-hat for the official Python driver. This one tries to follow the same API as the Python version.
5+
See <https://www.raspberrypi.org/products/sense-hat/> for details on the Sense HAT.
6+
7+
See <https://github.com/RPi-Distro/python-sense-hat> for the official Python driver. This one tries to follow the same API as the Python version.
8+
9+
See <https://github.com/thejpster/pi-workshop-rs/> for some workshop materials which use this driver.

examples/get_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate sensehat;
33
use sensehat::SenseHat;
44

55
fn main() {
6-
let mut sense_hat = SenseHat::new().expect("Couldn't create Sense Hat object");
6+
let mut sense_hat = SenseHat::new().expect("Couldn't create Sense HAT object");
77
let temp = sense_hat
88
.get_temperature_from_humidity()
99
.expect("Couldn't get temp");

src/hts221.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,44 @@ where
4545
let mut buf = [0u8; 2];
4646
buf[0] = i2cdev.smbus_read_byte_data(REG_T0_C_8)?;
4747
buf[1] = i2cdev.smbus_read_byte_data(REG_T1_T0)? & 0x03;
48-
let t0 = (LittleEndian::read_i16(&buf) as f64) / 8.0;
48+
let t0 = f64::from(LittleEndian::read_i16(&buf)) / 8.0;
4949
buf[0] = i2cdev.smbus_read_byte_data(REG_T1_C_8)?;
5050
buf[1] = (i2cdev.smbus_read_byte_data(REG_T1_T0)? & 0x0C) >> 2;
51-
let t1 = (LittleEndian::read_i16(&buf) as f64) / 8.0;
51+
let t1 = f64::from(LittleEndian::read_i16(&buf)) / 8.0;
5252

5353
buf[0] = i2cdev.smbus_read_byte_data(REG_T0_OUT)?;
5454
buf[1] = i2cdev.smbus_read_byte_data(REG_T0_OUT + 1)?;
55-
let t0_out = LittleEndian::read_i16(&buf) as f64;
55+
let t0_out = f64::from(LittleEndian::read_i16(&buf));
5656

5757
buf[0] = i2cdev.smbus_read_byte_data(REG_T1_OUT)?;
5858
buf[1] = i2cdev.smbus_read_byte_data(REG_T1_OUT + 1)?;
59-
let t1_out = LittleEndian::read_i16(&buf) as f64;
59+
let t1_out = f64::from(LittleEndian::read_i16(&buf));
6060

6161
buf[0] = i2cdev.smbus_read_byte_data(REG_H0_H_2)?;
62-
let h0 = (buf[0] as f64) / 2.0;
62+
let h0 = f64::from(buf[0]) / 2.0;
6363

6464
buf[0] = i2cdev.smbus_read_byte_data(REG_H1_H_2)?;
65-
let h1 = (buf[0] as f64) / 2.0;
65+
let h1 = f64::from(buf[0]) / 2.0;
6666

6767
buf[0] = i2cdev.smbus_read_byte_data(REG_H0_T0_OUT)?;
6868
buf[1] = i2cdev.smbus_read_byte_data(REG_H0_T0_OUT + 1)?;
69-
let h0_t0_out = LittleEndian::read_i16(&buf) as f64;
69+
let h0_t0_out = f64::from(LittleEndian::read_i16(&buf));
7070

7171
buf[0] = i2cdev.smbus_read_byte_data(REG_H1_T0_OUT)?;
7272
buf[1] = i2cdev.smbus_read_byte_data(REG_H1_T0_OUT + 1)?;
73-
let h1_t0_out = LittleEndian::read_i16(&buf) as f64;
73+
let h1_t0_out = f64::from(LittleEndian::read_i16(&buf));
7474

7575
let temp_m = (t1 - t0) / (t1_out - t0_out);
7676
let temp_c = t0 - (temp_m * t0_out);
7777
let hum_m = (h1 - h0) / (h1_t0_out - h0_t0_out);
7878
let hum_c = h0 - (hum_m * h0_t0_out);
7979

8080
Ok(Hts221 {
81-
i2cdev: i2cdev,
82-
temp_m: temp_m,
83-
temp_c: temp_c,
84-
hum_m: hum_m,
85-
hum_c: hum_c,
81+
i2cdev,
82+
temp_m,
83+
temp_c,
84+
hum_m,
85+
hum_c,
8686
})
8787
}
8888

@@ -100,7 +100,7 @@ where
100100

101101
pub fn get_relative_humidity_percent(&mut self) -> Result<f64, T::Error> {
102102
self.get_relative_humidity()
103-
.and_then(|c| Ok((c as f64 * self.hum_m) + self.hum_c))
103+
.and_then(|c| Ok((f64::from(c) * self.hum_m) + self.hum_c))
104104
}
105105

106106
pub fn get_temperature(&mut self) -> Result<i16, T::Error> {
@@ -112,6 +112,6 @@ where
112112

113113
pub fn get_temperature_celcius(&mut self) -> Result<f64, T::Error> {
114114
self.get_temperature()
115-
.and_then(|c| Ok((c as f64 * self.temp_m) + self.temp_c))
115+
.and_then(|c| Ok((f64::from(c) * self.temp_m) + self.temp_c))
116116
}
117117
}

src/lib.rs

Lines changed: 30 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
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+
118
extern crate byteorder;
219
extern crate i2cdev;
320
extern crate measurements;
421

522
#[cfg(feature = "rtimu")]
623
extern crate libc;
724

25+
mod rh;
26+
mod hts221;
27+
mod lps25h;
28+
829
pub use measurements::Temperature;
930
pub use measurements::Pressure;
31+
pub use measurements::Angle;
32+
pub use rh::RelativeHumidity;
1033

1134
use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError};
1235

13-
use std::fmt;
14-
15-
mod hts221;
16-
mod lps25h;
17-
1836
#[cfg(feature = "rtimu")]
1937
mod lsm9ds1;
2038

@@ -23,18 +41,6 @@ mod lsm9ds1_dummy;
2341
#[cfg(not(feature = "rtimu"))]
2442
use 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)]
4046
pub 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
4753
pub 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-
188188
impl 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

src/lps25h.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! * Driver for the LPS25H Pressure sensor
2-
//! See http://www.st.com/en/mems-and-sensors/lps25h.html
2+
//! See <http://www.st.com/en/mems-and-sensors/lps25h.html>
33
44
use i2cdev::core::I2CDevice;
55
use byteorder::{ByteOrder, LittleEndian};
@@ -31,7 +31,7 @@ where
3131
i2cdev.smbus_write_byte_data(REG_FIFO_CTRL, 0xc0)?;
3232
i2cdev.smbus_write_byte_data(REG_CTRL_REG_2, 0x40)?;
3333

34-
Ok(Lps25h { i2cdev: i2cdev })
34+
Ok(Lps25h { i2cdev })
3535
}
3636

3737
/// Obtain the status bitfield from the chip.
@@ -50,7 +50,7 @@ where
5050

5151
/// Obtain the temperature reading from the chip in deg C.
5252
pub fn get_temp_celcius(&mut self) -> Result<f64, T::Error> {
53-
self.get_temp().and_then(|c| Ok((c as f64 / 480.0) + 42.5))
53+
self.get_temp().and_then(|c| Ok((f64::from(c) / 480.0) + 42.5))
5454
}
5555

5656
/// Obtain the pressure reading from the chip.
@@ -65,6 +65,6 @@ where
6565

6666
/// Obtain the pressure reading from the chip in hPa.
6767
pub fn get_pressure_hpa(&mut self) -> Result<f64, T::Error> {
68-
self.get_pressure().and_then(|c| Ok(c as f64 / 4096.0))
68+
self.get_pressure().and_then(|c| Ok(f64::from(c) / 4096.0))
6969
}
7070
}

src/lsm9ds1.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//! in how it manages the settings file.
66
//!
77
//! It turns out handling IMUs is really complicated and involves a ton of
8-
//! vector maths. We just wrap RTIMULib. We use the `gcc` crate to compile
9-
//! a C wrapper of the RTIMULib C++ API. We then call that unsafe C wrapper
8+
//! vector maths. We just wrap `RTIMULib`. We use the `gcc` crate to compile
9+
//! a C wrapper of the `RTIMULib` C++ API. We then call that unsafe C wrapper
1010
//! here, ensuring that any memory allocations were undone on drop.
1111
1212
use super::{Angle, Orientation};
@@ -47,7 +47,7 @@ pub struct Lsm9ds1<'a> {
4747
}
4848

4949
impl<'a> Lsm9ds1<'a> {
50-
/// Uses the RTIMULib library.
50+
/// Uses the `RTIMULib` library.
5151
pub fn new() -> Result<Lsm9ds1<'a>, Error> {
5252
let ctx_ref = unsafe {
5353
let ctx_p = rtimulib_wrapper_create();

src/lsm9ds1_dummy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Lsm9ds1<'a> {
1717
}
1818

1919
impl<'a> Lsm9ds1<'a> {
20-
/// Uses the RTIMULib library.
20+
/// Uses the `RTIMULib` library.
2121
pub fn new() -> Result<Lsm9ds1<'a>, Error> {
2222
Ok(Lsm9ds1 {
2323
phantom: PhantomData,

src/rh.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! # Defines a unit for Relative Humidity (which isn't in the measurements crate)
2+
3+
use std::fmt;
4+
5+
/// Represents a relative humidity reading from the humidity sensor
6+
#[derive(Debug, Copy, Clone)]
7+
pub struct RelativeHumidity {
8+
value: f64,
9+
}
10+
11+
impl RelativeHumidity {
12+
pub fn from_percent(pc: f64) -> RelativeHumidity {
13+
RelativeHumidity { value: pc }
14+
}
15+
16+
pub fn as_percent(&self) -> f64 {
17+
self.value
18+
}
19+
}
20+
21+
impl fmt::Display for RelativeHumidity {
22+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23+
write!(f, "{:.1}%", self.as_percent())
24+
}
25+
}
26+
27+
// End of file

0 commit comments

Comments
 (0)