1use embedded_hal::i2c::I2c;
2
3use glam::DVec3;
4
5use crate::Lsm6dsr;
6
7impl<I2C: I2c> Lsm6dsr<I2C> {
8
9 const CORRECT_WHO_AM_I: u8 = 0b01101011;
11 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 pub fn disable_i3c(&mut self) -> Result<(), I2C::Error> {
25
26 let mut settings = self.read_byte(0x18)?;
28
29 settings |= 0b00000010; self.write_byte(0x18, settings)
32 }
33
34 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 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 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 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 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 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}