Skip to content

Commit

Permalink
Add function to convert bytes to meters
Browse files Browse the repository at this point in the history
  • Loading branch information
aklajnert committed Sep 26, 2020
1 parent c4fece6 commit 2fae8e5
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,20 @@ const UP: [u8; 2] = [0x47, 0x00];
const DOWN: [u8; 2] = [0x46, 0x00];
const STOP: [u8; 2] = [0xFF, 0x00];

const MIN_HEIGHT: f32 = 0.62;
const MAX_HEIGHT: f32 = 1.27;

fn bytes_to_meters(bytes: [u8; 4]) -> f32 {
0.0
pub const MIN_HEIGHT: f32 = 0.62;
pub const MAX_HEIGHT: f32 = 1.27;

/// convert desk response from bytes to meters
///
/// ```
/// assert_eq!(idasen::bytes_to_meters(&[0x64, 0x19, 0x00, 0x00]), idasen::MAX_HEIGHT);
/// assert_eq!(idasen::bytes_to_meters(&[0x00, 0x00, 0x00, 0x00]), idasen::MIN_HEIGHT);
/// assert_eq!(idasen::bytes_to_meters(&[0x51, 0x04, 0x00, 0x00]), 0.7305);
/// assert_eq!(idasen::bytes_to_meters(&[0x08, 0x08, 0x00, 0x00]), 0.8256);
/// ```
pub fn bytes_to_meters(bytes: &[u8]) -> f32 {
let as_int = ((bytes[1] as u32) << 8) + bytes[0] as u32;
(as_int as f32 / 10000.0) + MIN_HEIGHT
}

pub fn main() -> Vec<Characteristic> {
Expand Down Expand Up @@ -96,9 +105,18 @@ pub fn main() -> Vec<Characteristic> {
println!("{:?}", desk.command(&control_characteristic, &DOWN));
println!("{:?}", desk.command(&control_characteristic, &STOP));

for (bytes, value) in test_values {
println!(
"{:?} = {}, expected: {}",
bytes,
bytes_to_meters(&bytes),
value
);
}

loop {
let response = desk.read_by_type(&status_characteristic, status_characteristic.uuid);
println!("H: {:?}", response);
println!("H: {:?}", bytes_to_meters(&response.unwrap()));
thread::sleep(Duration::from_secs(1));
}

Expand Down

0 comments on commit 2fae8e5

Please sign in to comment.