Skip to content

Commit b6c5e59

Browse files
lib: Add buffer_to_xyz
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
1 parent 16ff885 commit b6c5e59

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ where
231231
Ok(scaled_data)
232232
}
233233

234+
fn buffer_to_xyz(buffer: &[u8; 6]) -> (i16, i16, i16) {
235+
let x = i16::from_le_bytes([buffer[0], buffer[1]]);
236+
let y = i16::from_le_bytes([buffer[2], buffer[3]]);
237+
let z = i16::from_le_bytes([buffer[4], buffer[5]]);
238+
(x, y, z)
239+
}
240+
234241
// 9.4.3.2. Normal Read Sequence:
235242
// 1. Check Data Ready or not by any of the following method:
236243
// - Polling DRDY bit of ST1 register
@@ -261,21 +268,15 @@ where
261268

262269
Self::check_st2_value(buffer[7])?;
263270

264-
let x = i16::from_le_bytes([buffer[0], buffer[1]]);
265-
let y = i16::from_le_bytes([buffer[2], buffer[3]]);
266-
let z = i16::from_le_bytes([buffer[4], buffer[5]]);
267-
Ok((x, y, z))
271+
Ok(Self::buffer_to_xyz(&buffer[..6].try_into().unwrap()))
268272
}
269273

270274
pub fn read_raw_unchecked(&mut self) -> Result<(i16, i16, i16), Error<E>> {
271275
let mut buffer: [u8; 6] = [0u8; 6];
272276
self.i2c
273277
.write_read(self.address, &[Register::HXL.into()], &mut buffer)
274278
.map_err(Error::I2C)?;
275-
let x = i16::from_le_bytes([buffer[0], buffer[1]]);
276-
let y = i16::from_le_bytes([buffer[2], buffer[3]]);
277-
let z = i16::from_le_bytes([buffer[4], buffer[5]]);
278-
Ok((x, y, z))
279+
Ok(Self::buffer_to_xyz(&buffer[..6].try_into().unwrap()))
279280
}
280281
}
281282

0 commit comments

Comments
 (0)