hinge-angle 0.1.0

A crate for accessing hinge angle sensors on various platforms
Documentation
use std::{error::Error, thread, time::Duration};

use hinge_angle::HingeAngle;

cfg_if::cfg_if! {
    if #[cfg(target_os = "macos")] {
        fn create_hinge() -> hinge_angle::macos::Hinge {
            hinge_angle::macos::Hinge::new().expect("Failed to initialize MacBook hinge sensor")
        }
    } else if #[cfg(target_os = "android")] {
        fn create_hinge() -> hinge_angle::android::Hinge {
            hinge_angle::android::Hinge::new()
        }
    } else {
        fn create_hinge() -> hinge_angle::fake::Hinge {
            hinge_angle::fake::Hinge::new(42.0)
        }
    }
}

fn main() {
    let hinge = create_hinge();

    loop {
        let angle = hinge.angle();

        // A hack to unify the printing
        #[cfg(not(any(target_os = "android", target_os = "macos")))]
        let angle: Result<_, std::convert::Infallible> = Ok(angle);

        match angle {
            Ok(angle) => println!("Hinge angle: {angle:.2}°"),
            Err(e) => eprintln!("Error reading hinge angle: {e}: {:?}", e.source()),
        }
        thread::sleep(Duration::from_secs(1));
    }
}