lsm6dsr/
commands.rs

1use embedded_hal::i2c::I2c;
2
3use glam::DVec3;
4
5use crate::Lsm6dsr;
6
7impl<I2C: I2c> Lsm6dsr<I2C> {
8
9	// page 50
10	const CORRECT_WHO_AM_I: u8 = 0b01101011;
11	/// checks to see if the chip reports the correct `WHO_AM_I` value
12	/// 
13	/// returns `Ok(true)` if the chip reports the correct value, `Ok(false)` if it
14	/// reports an incorrect value, and `Err()` if there was an error checking the value
15	pub fn is_valid(&mut self) -> Result<bool, I2C::Error> {
16		let id = self.read_byte(0x0f)?;
17
18		Ok(id == Self::CORRECT_WHO_AM_I)
19	}
20
21	/// disable i3c (*not* i2c)
22	/// 
23	/// It is recomended to do this before doing any other configuration stuff
24	pub fn disable_i3c(&mut self) -> Result<(), I2C::Error> {
25
26		// get the other settings so we don't accidently change them
27		let mut settings = self.read_byte(0x18)?;
28
29		settings |= 0b00000010; // set the i3c disable bit to 1
30
31		self.write_byte(0x18, settings)
32	}
33
34	/// get temperature in degrees Celsius
35	/// 
36	/// NOTE: Incorrect output!!!
37	pub fn get_temp(&mut self) -> Result<f32, I2C::Error> {
38		let raw_temp = self.read_i16(0x20)? as f32;
39
40		Ok((raw_temp / 256.0) + 25.0)
41	}
42
43	/// get acceleration in the x axis in Gs
44	pub fn x_accel(&mut self)  -> Result<f64, I2C::Error> {
45		let raw_val = self.read_i16(0x28)?;
46
47		Ok(self.accel_scale.convert(raw_val))
48	}
49
50	/// get acceleration in the y axis in Gs
51	pub fn y_accel(&mut self)  -> Result<f64, I2C::Error> {
52		let raw_val = self.read_i16(0x2A)?;
53
54		Ok(self.accel_scale.convert(raw_val))
55	}
56
57	/// get acceleration in the z axis in Gs
58	pub fn z_accel(&mut self)  -> Result<f64, I2C::Error> {
59		let raw_val = self.read_i16(0x2C)?;
60
61		Ok(self.accel_scale.convert(raw_val))
62	}
63
64	/// get acceleration in all 3 axis in Gs. this is more efficient than
65	/// getting each individually
66	pub fn acceleration(&mut self) -> Result<DVec3, I2C::Error> {
67		let raw_accel = self.read_trio_i16(0x28)?;
68		let mut accel = raw_accel.as_dvec3();
69		let coeff = self.accel_scale.coefficient();
70
71		accel *= coeff / 1000.0;
72
73		Ok(accel)
74	}
75
76	/// get angular rate in all 3 axis in degrees per second. this is more efficient than
77	/// getting each individually
78	pub fn rotation(&mut self) -> Result<DVec3, I2C::Error> {
79		let raw_rot = self.read_trio_i16(0x22)?;
80		let mut rot = raw_rot.as_dvec3();
81		let coeff = self.gyro_scale.coefficient();
82
83		rot *= coeff / 1000.0;
84
85		Ok(rot)
86	}
87}