Skip to content

Commit

Permalink
lib: Add buffer_to_xyz
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
  • Loading branch information
patrickelectric committed Nov 1, 2023
1 parent 16ff885 commit b6c5e59
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ where
Ok(scaled_data)
}

fn buffer_to_xyz(buffer: &[u8; 6]) -> (i16, i16, i16) {
let x = i16::from_le_bytes([buffer[0], buffer[1]]);
let y = i16::from_le_bytes([buffer[2], buffer[3]]);
let z = i16::from_le_bytes([buffer[4], buffer[5]]);
(x, y, z)
}

// 9.4.3.2. Normal Read Sequence:
// 1. Check Data Ready or not by any of the following method:
// - Polling DRDY bit of ST1 register
Expand Down Expand Up @@ -261,21 +268,15 @@ where

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

let x = i16::from_le_bytes([buffer[0], buffer[1]]);
let y = i16::from_le_bytes([buffer[2], buffer[3]]);
let z = i16::from_le_bytes([buffer[4], buffer[5]]);
Ok((x, y, z))
Ok(Self::buffer_to_xyz(&buffer[..6].try_into().unwrap()))
}

pub fn read_raw_unchecked(&mut self) -> Result<(i16, i16, i16), Error<E>> {
let mut buffer: [u8; 6] = [0u8; 6];
self.i2c
.write_read(self.address, &[Register::HXL.into()], &mut buffer)
.map_err(Error::I2C)?;
let x = i16::from_le_bytes([buffer[0], buffer[1]]);
let y = i16::from_le_bytes([buffer[2], buffer[3]]);
let z = i16::from_le_bytes([buffer[4], buffer[5]]);
Ok((x, y, z))
Ok(Self::buffer_to_xyz(&buffer[..6].try_into().unwrap()))
}
}

Expand Down

0 comments on commit b6c5e59

Please sign in to comment.